@@ -25,8 +25,15 @@ import org.junit.Assert.assertEquals
2525import org.junit.Assert.assertTrue
2626import org.junit.Assert.fail
2727import org.junit.Test
28-
29- class AwaitSignalTest {
28+ import org.junit.runner.RunWith
29+ import org.junit.runners.Parameterized
30+ import org.junit.runners.Parameterized.Parameters
31+
32+ @RunWith(Parameterized ::class )
33+ class AwaitSignalTest (
34+ factory : TimeoutFactory ,
35+ ) {
36+ private val timeout = factory.newTimeout()
3037 val executorService = TestingExecutors .newScheduledExecutorService(0 )
3138
3239 val lock: ReentrantLock = ReentrantLock ()
@@ -39,7 +46,6 @@ class AwaitSignalTest {
3946
4047 @Test
4148 fun signaled () = lock.withLock {
42- val timeout = Timeout ()
4349 timeout.timeout(5000 , TimeUnit .MILLISECONDS )
4450 val start = now()
4551 executorService.schedule(
@@ -54,7 +60,6 @@ class AwaitSignalTest {
5460 @Test
5561 fun timeout () = lock.withLock {
5662 assumeNotWindows()
57- val timeout = Timeout ()
5863 timeout.timeout(1000 , TimeUnit .MILLISECONDS )
5964 val start = now()
6065 try {
@@ -69,7 +74,6 @@ class AwaitSignalTest {
6974 @Test
7075 fun deadline () = lock.withLock {
7176 assumeNotWindows()
72- val timeout = Timeout ()
7377 timeout.deadline(1000 , TimeUnit .MILLISECONDS )
7478 val start = now()
7579 try {
@@ -84,7 +88,6 @@ class AwaitSignalTest {
8488 @Test
8589 fun deadlineBeforeTimeout () = lock.withLock {
8690 assumeNotWindows()
87- val timeout = Timeout ()
8891 timeout.timeout(5000 , TimeUnit .MILLISECONDS )
8992 timeout.deadline(1000 , TimeUnit .MILLISECONDS )
9093 val start = now()
@@ -100,7 +103,6 @@ class AwaitSignalTest {
100103 @Test
101104 fun timeoutBeforeDeadline () = lock.withLock {
102105 assumeNotWindows()
103- val timeout = Timeout ()
104106 timeout.timeout(1000 , TimeUnit .MILLISECONDS )
105107 timeout.deadline(5000 , TimeUnit .MILLISECONDS )
106108 val start = now()
@@ -116,7 +118,6 @@ class AwaitSignalTest {
116118 @Test
117119 fun deadlineAlreadyReached () = lock.withLock {
118120 assumeNotWindows()
119- val timeout = Timeout ()
120121 timeout.deadlineNanoTime(System .nanoTime())
121122 val start = now()
122123 try {
@@ -131,7 +132,6 @@ class AwaitSignalTest {
131132 @Test
132133 fun threadInterrupted () = lock.withLock {
133134 assumeNotWindows()
134- val timeout = Timeout ()
135135 val start = now()
136136 Thread .currentThread().interrupt()
137137 try {
@@ -147,7 +147,6 @@ class AwaitSignalTest {
147147 @Test
148148 fun threadInterruptedOnThrowIfReached () = lock.withLock {
149149 assumeNotWindows()
150- val timeout = Timeout ()
151150 Thread .currentThread().interrupt()
152151 try {
153152 timeout.throwIfReached()
@@ -159,15 +158,12 @@ class AwaitSignalTest {
159158 }
160159
161160 @Test
162- fun cancelBeforeWaitDoesNothing () {
163- val timeout = Timeout ()
161+ fun cancelBeforeWaitDoesNothing () = lock.withLock {
164162 timeout.timeout(1000 , TimeUnit .MILLISECONDS )
165163 timeout.cancel()
166164 val start = now()
167165 try {
168- lock.withLock {
169- timeout.awaitSignal(condition)
170- }
166+ timeout.awaitSignal(condition)
171167 fail()
172168 } catch (expected: InterruptedIOException ) {
173169 assertEquals(" timeout" , expected.message)
@@ -176,32 +172,26 @@ class AwaitSignalTest {
176172 }
177173
178174 @Test
179- fun canceledTimeoutDoesNotThrowWhenNotNotifiedOnTime () {
175+ fun canceledTimeoutDoesNotThrowWhenNotNotifiedOnTime () = lock.withLock {
180176 assumeNotWindows()
181- val timeout = Timeout ()
182177 timeout.timeout(1000 , TimeUnit .MILLISECONDS )
183178 timeout.cancelLater(500 )
184179
185180 val start = now()
186- lock.withLock {
187- timeout.awaitSignal(condition) // Returns early but doesn't throw.
188- }
181+ timeout.awaitSignal(condition) // Returns early but doesn't throw.
189182 assertElapsed(1000.0 , start)
190183 }
191184
192185 @Test
193186 @Synchronized
194- fun multipleCancelsAreIdempotent () {
195- val timeout = Timeout ()
187+ fun multipleCancelsAreIdempotent () = lock.withLock {
196188 timeout.timeout(1000 , TimeUnit .MILLISECONDS )
197189 timeout.cancelLater(250 )
198190 timeout.cancelLater(500 )
199191 timeout.cancelLater(750 )
200192
201193 val start = now()
202- lock.withLock {
203- timeout.awaitSignal(condition) // Returns early but doesn't throw.
204- }
194+ timeout.awaitSignal(condition) // Returns early but doesn't throw.
205195 assertElapsed(1000.0 , start)
206196 }
207197
@@ -225,4 +215,10 @@ class AwaitSignalTest {
225215 TimeUnit .MILLISECONDS ,
226216 )
227217 }
218+
219+ companion object {
220+ @Parameters(name = " {0}" )
221+ @JvmStatic
222+ fun parameters (): List <Array <out Any ?>> = TimeoutFactory .entries.map { arrayOf(it) }
223+ }
228224}
0 commit comments