@@ -23,6 +23,8 @@ import org.junit.Assert.assertEquals
2323import org.junit.Assert.assertFalse
2424import org.junit.Assert.assertTrue
2525import org.junit.runner.RunWith
26+ import org.junit.After
27+ import org.junit.Before
2628import org.junit.Test
2729import org.robolectric.RobolectricTestRunner
2830import org.robolectric.annotation.Config
@@ -34,11 +36,23 @@ data class SuspendingJobResult(val labels: List<String>, val timestamp: Long, va
3436@RunWith(RobolectricTestRunner ::class )
3537@Config(application = RobolectricApplication ::class )
3638class AsyncDecoratorTest {
37- data class SuspendingJobResult (val labels : List <String >, val timestamp : Long )
39+ var testedEngine: CoolEngine ? = null
40+
41+ @Before
42+ fun setUp () {
43+ // Create the engine used in tests.
44+ testedEngine = CoolEngine ()
45+ }
46+
47+ @After
48+ fun tearDown () {
49+ // Ensure that underlying C++ threads join.
50+ testedEngine?.waitForCompletion()
51+ }
3852
3953 @Test
4054 fun multipleSuspendingFunctionsExecutedConcurrently () {
41- val engine = CoolEngine ()
55+ val engine = testedEngine !!
4256 val startTimestamp = System .currentTimeMillis()
4357
4458 // Test scenario is as follows:
@@ -56,12 +70,12 @@ class AsyncDecoratorTest {
5670 // - this way we can tell that they were executed concurrently and there was no blocking -- if there was blocking
5771 // then elapsed time would be total of given task and the ones before it
5872 runBlocking {
59- // This task should work for a bit longer than 2000 ms.
73+ // This task should work for a bit longer than 200 ms.
6074 val t0 = async {
61- // Worker thread on C++ level will sleep for: 1000 ms.
75+ // Worker thread on C++ level will sleep for: 100 ms.
6276 val coolLabels: List <String > = engine.downloadCoolLabelsAsync(" cool-labels.com" )
6377
64- // Worker thread on C++ level will sleep for: 1000 ms.
78+ // Worker thread on C++ level will sleep for: 100 ms.
6579 val superLabels: List <String > = engine.downloadCoolLabelsAsync(" my-super-labels.com" )
6680
6781 // Return all labels from the task + timestamp.
@@ -72,15 +86,15 @@ class AsyncDecoratorTest {
7286 )
7387 }
7488
75- // This task should work for a bit longer than 6000ms .
89+ // This task should work for a bit longer than 600ms .
7690 val t1 = async {
77- // Worker thread on C++ level will sleep for: 1000 ms.
91+ // Worker thread on C++ level will sleep for: 100 ms.
7892 val coolLabels: List <String > = engine.downloadCoolLabelsAsync(" cool-labels.com" )
7993
80- // Introduce artificial delay of 2000ms .
81- delay(2000 )
94+ // Introduce artificial delay of 200ms .
95+ delay(200 )
8296
83- // Worker thread on C++ level will sleep for: 3000 ms.
97+ // Worker thread on C++ level will sleep for: 300 ms.
8498 val dummyLabels: List <String > = engine.downloadCoolLabelsAsync(" dummy-labels.com" )
8599
86100 // Return all labels from the task + timestamp.
@@ -91,13 +105,13 @@ class AsyncDecoratorTest {
91105 )
92106 }
93107
94- // This task should finish with exception after a bit longer than 500ms .
108+ // This task should finish with exception after a bit longer than 50ms .
95109 val t2 = async {
96110 var exceptionCaught: Boolean = false
97111 var labels = emptyList<String >()
98112
99113 try {
100- // This url is not handled. It will throw after 500ms .
114+ // This url is not handled. It will throw after 50ms .
101115 labels = engine.downloadCoolLabelsAsync(" this-url-will-throw.com" )
102116 } catch (e: Exception ) {
103117 exceptionCaught = true
@@ -116,33 +130,30 @@ class AsyncDecoratorTest {
116130 // Check that expected labels were obtained without exception.
117131 assertEquals(listOf (" COOL_LABEL" , " SUPER_LABEL" , " ANOTHER_SUPER_LABEL" ), t0Result.labels)
118132 assertFalse(t0Result.exceptionCaught)
119- // Check that job took at least 2000ms , and less than 2500ms (margin).
133+ // Check that job took at least 200ms , and less than 250ms (margin).
120134 val t0ElapsedTimeMs = t0Result.timestamp - startTimestamp
121- assertTrue(" Time of the first job: $t0ElapsedTimeMs >= 2000ms " , t0ElapsedTimeMs >= 2000 )
122- assertTrue(" Time of the first job: $t0ElapsedTimeMs < 2500ms " , t0ElapsedTimeMs < 2500 )
135+ assertTrue(" Time of the first job: $t0ElapsedTimeMs >= 200ms " , t0ElapsedTimeMs >= 200 )
136+ assertTrue(" Time of the first job: $t0ElapsedTimeMs < 250ms " , t0ElapsedTimeMs < 250 )
123137
124138 // Wait for the second task's result.
125139 val t1Result = t1.await()
126140 // Check that expected labels were obtained without exception.
127141 assertEquals(listOf (" COOL_LABEL" , " DUMMY_LABEL" ), t1Result.labels)
128142 assertFalse(t1Result.exceptionCaught)
129- // Check that job took at least 6000ms , and less than 6500ms (margin).
143+ // Check that job took at least 600ms , and less than 650ms (margin).
130144 val t1ElapsedTimeMs = t1Result.timestamp - startTimestamp
131- assertTrue(" Time of the second job: $t1ElapsedTimeMs >= 6000ms " , t1ElapsedTimeMs >= 6000 )
132- assertTrue(" Time of the second job: $t1ElapsedTimeMs < 6500ms " , t1ElapsedTimeMs < 6500 )
145+ assertTrue(" Time of the second job: $t1ElapsedTimeMs >= 600ms " , t1ElapsedTimeMs >= 600 )
146+ assertTrue(" Time of the second job: $t1ElapsedTimeMs < 650ms " , t1ElapsedTimeMs < 650 )
133147
134148 // Wait for the third task's result.
135149 val t2Result = t2.await()
136150 // Check that expected labels were obtained without exception.
137151 assertTrue(t2Result.labels.isEmpty())
138152 assertTrue(t2Result.exceptionCaught)
139- // Check that job took at least 500ms , and less than 1000ms (margin).
153+ // Check that job took at least 50ms , and less than 100ms (margin).
140154 val t2ElapsedTimeMs = t2Result.timestamp - startTimestamp
141- assertTrue(" Time of the second job: $t2ElapsedTimeMs >= 500ms" , t2ElapsedTimeMs >= 500 )
142- assertTrue(" Time of the second job: $t2ElapsedTimeMs < 1000ms" , t2ElapsedTimeMs < 1000 )
155+ assertTrue(" Time of the second job: $t2ElapsedTimeMs >= 500ms" , t2ElapsedTimeMs >= 50 )
156+ assertTrue(" Time of the second job: $t2ElapsedTimeMs < 1000ms" , t2ElapsedTimeMs < 100 )
143157 }
144-
145- // Ensure that underlying C++ threads join.
146- engine.waitForCompletion()
147158 }
148159}
0 commit comments