Skip to content

Commit 48cc47b

Browse files
committed
Fix timing issues of several unit tests.
1 parent 075c381 commit 48cc47b

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/test/scala/com/github/yruslan/channel/ChannelSuite.scala

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ class ChannelSuite extends AnyWordSpec with BeforeAndAfterAll {
241241
)
242242
}
243243

244+
setUncaughtExceptionHandler(t1) ((_, _) => {})
245+
244246
t1.start()
245247
t2.start()
246248

@@ -275,6 +277,8 @@ class ChannelSuite extends AnyWordSpec with BeforeAndAfterAll {
275277
ch.send(300)
276278
}
277279

280+
setUncaughtExceptionHandler(t1) ((_, _) => {})
281+
278282
val t2 = createThread {
279283
ch.send(400)
280284
Thread.sleep(30)
@@ -502,12 +506,18 @@ class ChannelSuite extends AnyWordSpec with BeforeAndAfterAll {
502506
"handle non-blocking way" when {
503507
"data is available" in {
504508
val ch = Channel.make[String]
509+
val wg = WaitGroup()
505510

506-
Future {
511+
wg.add(1)
512+
val t = createThread {
513+
wg.done()
507514
ch.send("test")
508515
}
509-
Thread.sleep(30)
516+
t.start()
517+
wg.await()
518+
Thread.sleep(100)
510519
val v = ch.tryRecv(Duration.Zero)
520+
t.join()
511521

512522
assert(v.nonEmpty)
513523
assert(v.contains("test"))
@@ -525,11 +535,14 @@ class ChannelSuite extends AnyWordSpec with BeforeAndAfterAll {
525535
"handle finite timeouts" when {
526536
"timeout is not expired" in {
527537
val ch = Channel.make[String]
538+
val wg = WaitGroup()
539+
wg.add()
528540

529541
val f = Future {
530-
Thread.sleep(10)
531-
ch.trySend("test", Duration.Zero)
542+
wg.done()
543+
ch.send("test")
532544
}
545+
wg.await()
533546

534547
val v = ch.tryRecv(Duration.create(200, TimeUnit.MILLISECONDS))
535548
Await.result(f, Duration.apply(2, SECONDS))

0 commit comments

Comments
 (0)