Skip to content

Commit e3afc16

Browse files
committed
functional-tests: fix code review comments
Signed-off-by: Patryk Wrobel <183546751+pwrobeldev@users.noreply.github.com>
1 parent 1f4fb3b commit e3afc16

2 files changed

Lines changed: 43 additions & 33 deletions

File tree

functional-tests/functional/android-kotlin/src/test/kotlin/com/here/android/test/AsyncDecoratorTest.kt

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import org.junit.Assert.assertEquals
2323
import org.junit.Assert.assertFalse
2424
import org.junit.Assert.assertTrue
2525
import org.junit.runner.RunWith
26+
import org.junit.After
27+
import org.junit.Before
2628
import org.junit.Test
2729
import org.robolectric.RobolectricTestRunner
2830
import 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)
3638
class 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 >= 50ms", t2ElapsedTimeMs >= 50)
156+
assertTrue("Time of the second job: $t2ElapsedTimeMs < 100ms", t2ElapsedTimeMs < 100)
143157
}
144-
145-
// Ensure that underlying C++ threads join.
146-
engine.waitForCompletion()
147158
}
148159
}

functional-tests/functional/input/src/cpp/AsyncDecorator.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,23 @@ download_cool_labels(const std::string& url)
4545

4646
if (url == "my-super-labels.com")
4747
{
48-
std::this_thread::sleep_for(1000ms);
48+
std::this_thread::sleep_for(100ms);
4949
return std::vector<std::string>{"SUPER_LABEL", "ANOTHER_SUPER_LABEL"};
5050
}
5151

5252
if (url == "cool-labels.com")
5353
{
54-
std::this_thread::sleep_for(1000ms);
54+
std::this_thread::sleep_for(100ms);
5555
return std::vector<std::string>{"COOL_LABEL"};
5656
}
5757

5858
if (url == "dummy-labels.com")
5959
{
60-
std::this_thread::sleep_for(3000ms);
60+
std::this_thread::sleep_for(300ms);
6161
return std::vector<std::string>{"DUMMY_LABEL"};
6262
}
6363

64-
std::this_thread::sleep_for(500ms);
64+
std::this_thread::sleep_for(50ms);
6565
return std::nullopt;
6666
}
6767

@@ -74,12 +74,11 @@ class NonCancellableAsyncTask : public AsyncTaskHandle
7474
{
7575
m_worker = std::thread{
7676
[this, w = std::move(do_work)] {
77+
m_is_running.store(true);
7778
w();
7879
m_is_running.store(false);
7980
}
8081
};
81-
82-
m_is_running.store(true);
8382
}
8483
}
8584

@@ -128,15 +127,15 @@ class CoolEngineImpl : public CoolEngine
128127
std::shared_ptr<test::AsyncTaskHandle>
129128
download_cool_labels_async(const ::std::string& url, const ::test::EngineWorkCompletedCallback& callback) override
130129
{
131-
std::function<void()> do_work = [url, c = std::move(callback)] {
130+
std::function<void()> do_work = [url, callback] {
132131
auto labels = download_cool_labels(url);
133132
if (labels.has_value())
134133
{
135-
c({}, *labels);
134+
callback({}, *labels);
136135
}
137136
else
138137
{
139-
c(EngineError::ENGINE_ON_FIRE, std::nullopt);
138+
callback(EngineError::ENGINE_ON_FIRE, std::nullopt);
140139
}
141140
};
142141

0 commit comments

Comments
 (0)