aboutsummaryrefslogtreecommitdiff
path: root/okio/src/jvmTest/kotlin/okio/WaitUntilNotifiedTest.kt
blob: 44c6bbfd8f2f8c28e8ec467f17197690909f172e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * Copyright (C) 2016 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package okio

import java.io.InterruptedIOException
import java.util.concurrent.TimeUnit
import okio.TestUtil.assumeNotWindows
import okio.TestingExecutors.newScheduledExecutorService
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Assert.fail
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.junit.runners.Parameterized.Parameters

@RunWith(Parameterized::class)
class WaitUntilNotifiedTest(
  factory: TimeoutFactory,
) {
  private val timeout = factory.newTimeout()
  private val executorService = newScheduledExecutorService(0)

  @After
  fun tearDown() {
    executorService.shutdown()
  }

  @Test
  @Synchronized
  fun notified() {
    timeout.timeout(5000, TimeUnit.MILLISECONDS)
    val start = now()
    executorService.schedule(
      {
        synchronized(this@WaitUntilNotifiedTest) {
          (this as Object).notify()
        }
      },
      1000,
      TimeUnit.MILLISECONDS,
    )
    timeout.waitUntilNotified(this)
    assertElapsed(1000.0, start)
  }

  @Test
  @Synchronized
  fun timeout() {
    assumeNotWindows()
    timeout.timeout(1000, TimeUnit.MILLISECONDS)
    val start = now()
    try {
      timeout.waitUntilNotified(this)
      fail()
    } catch (expected: InterruptedIOException) {
      assertEquals("timeout", expected.message)
    }
    assertElapsed(1000.0, start)
  }

  @Test
  @Synchronized
  fun deadline() {
    assumeNotWindows()
    timeout.deadline(1000, TimeUnit.MILLISECONDS)
    val start = now()
    try {
      timeout.waitUntilNotified(this)
      fail()
    } catch (expected: InterruptedIOException) {
      assertEquals("timeout", expected.message)
    }
    assertElapsed(1000.0, start)
  }

  @Test
  @Synchronized
  fun deadlineBeforeTimeout() {
    assumeNotWindows()
    timeout.timeout(5000, TimeUnit.MILLISECONDS)
    timeout.deadline(1000, TimeUnit.MILLISECONDS)
    val start = now()
    try {
      timeout.waitUntilNotified(this)
      fail()
    } catch (expected: InterruptedIOException) {
      assertEquals("timeout", expected.message)
    }
    assertElapsed(1000.0, start)
  }

  @Test
  @Synchronized
  fun timeoutBeforeDeadline() {
    assumeNotWindows()
    timeout.timeout(1000, TimeUnit.MILLISECONDS)
    timeout.deadline(5000, TimeUnit.MILLISECONDS)
    val start = now()
    try {
      timeout.waitUntilNotified(this)
      fail()
    } catch (expected: InterruptedIOException) {
      assertEquals("timeout", expected.message)
    }
    assertElapsed(1000.0, start)
  }

  @Test
  @Synchronized
  fun deadlineAlreadyReached() {
    assumeNotWindows()
    timeout.deadlineNanoTime(System.nanoTime())
    val start = now()
    try {
      timeout.waitUntilNotified(this)
      fail()
    } catch (expected: InterruptedIOException) {
      assertEquals("timeout", expected.message)
    }
    assertElapsed(0.0, start)
  }

  @Test
  @Synchronized
  fun threadInterrupted() {
    assumeNotWindows()
    val start = now()
    Thread.currentThread().interrupt()
    try {
      timeout.waitUntilNotified(this)
      fail()
    } catch (expected: InterruptedIOException) {
      assertEquals("interrupted", expected.message)
      assertTrue(Thread.interrupted())
    }
    assertElapsed(0.0, start)
  }

  @Test
  @Synchronized
  fun threadInterruptedOnThrowIfReached() {
    assumeNotWindows()
    Thread.currentThread().interrupt()
    try {
      timeout.throwIfReached()
      fail()
    } catch (expected: InterruptedIOException) {
      assertEquals("interrupted", expected.message)
      assertTrue(Thread.interrupted())
    }
  }

  @Test
  @Synchronized
  fun cancelBeforeWaitDoesNothing() {
    assumeNotWindows()
    timeout.timeout(1000, TimeUnit.MILLISECONDS)
    timeout.cancel()
    val start = now()
    try {
      timeout.waitUntilNotified(this)
      fail()
    } catch (expected: InterruptedIOException) {
      assertEquals("timeout", expected.message)
    }
    assertElapsed(1000.0, start)
  }

  @Test
  @Synchronized
  fun canceledTimeoutDoesNotThrowWhenNotNotifiedOnTime() {
    timeout.timeout(1000, TimeUnit.MILLISECONDS)
    timeout.cancelLater(500)

    val start = now()
    timeout.waitUntilNotified(this) // Returns early but doesn't throw.
    assertElapsed(1000.0, start)
  }

  @Test
  @Synchronized
  fun multipleCancelsAreIdempotent() {
    timeout.timeout(1000, TimeUnit.MILLISECONDS)
    timeout.cancelLater(250)
    timeout.cancelLater(500)
    timeout.cancelLater(750)

    val start = now()
    timeout.waitUntilNotified(this) // Returns early but doesn't throw.
    assertElapsed(1000.0, start)
  }

  /** Returns the nanotime in milliseconds as a double for measuring timeouts.  */
  private fun now(): Double {
    return System.nanoTime() / 1000000.0
  }

  /**
   * Fails the test unless the time from start until now is duration, accepting differences in
   * -50..+450 milliseconds.
   */
  private fun assertElapsed(duration: Double, start: Double) {
    assertEquals(duration, now() - start - 200.0, 250.0)
  }

  private fun Timeout.cancelLater(delay: Long) {
    executorService.schedule(
      {
        cancel()
      },
      delay,
      TimeUnit.MILLISECONDS,
    )
  }

  companion object {
    @Parameters(name = "{0}")
    @JvmStatic
    fun parameters(): List<Array<out Any?>> = TimeoutFactory.entries.map { arrayOf(it) }
  }
}