1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "StreamWorker.h"
18
19 #include <sched.h>
20 #include <unistd.h>
21 #include <atomic>
22
23 #include <gtest/gtest.h>
24 #define LOG_TAG "StreamWorker_Test"
25 #include <log/log.h>
26
27 struct TestStream {
28 std::atomic<bool> error = false;
29 };
30
31 class TestWorker : public StreamWorker<TestWorker> {
32 public:
33 // Use nullptr to test error reporting from the worker thread.
TestWorker(TestStream * stream)34 explicit TestWorker(TestStream* stream) : mStream(stream) {}
35
getWorkerCycles() const36 size_t getWorkerCycles() const { return mWorkerCycles; }
hasWorkerCycleCalled() const37 bool hasWorkerCycleCalled() const { return mWorkerCycles != 0; }
hasNoWorkerCycleCalled(useconds_t usec)38 bool hasNoWorkerCycleCalled(useconds_t usec) {
39 const size_t cyclesBefore = mWorkerCycles;
40 usleep(usec);
41 return mWorkerCycles == cyclesBefore;
42 }
43
workerInit()44 bool workerInit() { return mStream; }
workerCycle()45 bool workerCycle() {
46 do {
47 mWorkerCycles++;
48 } while (mWorkerCycles == 0);
49 return !mStream->error;
50 }
51
52 private:
53 TestStream* const mStream;
54 std::atomic<size_t> mWorkerCycles = 0;
55 };
56
57 // The parameter specifies whether an extra call to 'stop' is made at the end.
58 class StreamWorkerInvalidTest : public testing::TestWithParam<bool> {
59 public:
StreamWorkerInvalidTest()60 StreamWorkerInvalidTest() : StreamWorkerInvalidTest(nullptr) {}
TearDown()61 void TearDown() override {
62 if (GetParam()) {
63 worker.stop();
64 }
65 }
66
67 protected:
StreamWorkerInvalidTest(TestStream * stream)68 StreamWorkerInvalidTest(TestStream* stream) : testing::TestWithParam<bool>(), worker(stream) {}
69 TestWorker worker;
70 };
71
TEST_P(StreamWorkerInvalidTest,Uninitialized)72 TEST_P(StreamWorkerInvalidTest, Uninitialized) {
73 EXPECT_FALSE(worker.hasWorkerCycleCalled());
74 EXPECT_FALSE(worker.hasError());
75 }
76
TEST_P(StreamWorkerInvalidTest,UninitializedPauseIgnored)77 TEST_P(StreamWorkerInvalidTest, UninitializedPauseIgnored) {
78 EXPECT_FALSE(worker.hasError());
79 worker.pause();
80 EXPECT_FALSE(worker.hasError());
81 }
82
TEST_P(StreamWorkerInvalidTest,UninitializedResumeIgnored)83 TEST_P(StreamWorkerInvalidTest, UninitializedResumeIgnored) {
84 EXPECT_FALSE(worker.hasError());
85 worker.resume();
86 EXPECT_FALSE(worker.hasError());
87 }
88
TEST_P(StreamWorkerInvalidTest,Start)89 TEST_P(StreamWorkerInvalidTest, Start) {
90 EXPECT_FALSE(worker.start());
91 EXPECT_FALSE(worker.hasWorkerCycleCalled());
92 EXPECT_TRUE(worker.hasError());
93 }
94
TEST_P(StreamWorkerInvalidTest,PauseIgnored)95 TEST_P(StreamWorkerInvalidTest, PauseIgnored) {
96 EXPECT_FALSE(worker.start());
97 EXPECT_TRUE(worker.hasError());
98 worker.pause();
99 EXPECT_TRUE(worker.hasError());
100 }
101
TEST_P(StreamWorkerInvalidTest,ResumeIgnored)102 TEST_P(StreamWorkerInvalidTest, ResumeIgnored) {
103 EXPECT_FALSE(worker.start());
104 EXPECT_TRUE(worker.hasError());
105 worker.resume();
106 EXPECT_TRUE(worker.hasError());
107 }
108
109 INSTANTIATE_TEST_SUITE_P(StreamWorkerInvalid, StreamWorkerInvalidTest, testing::Bool());
110
111 class StreamWorkerTest : public StreamWorkerInvalidTest {
112 public:
StreamWorkerTest()113 StreamWorkerTest() : StreamWorkerInvalidTest(&stream) {}
114
115 protected:
116 TestStream stream;
117 };
118
119 static constexpr unsigned kWorkerIdleCheckTime = 50 * 1000;
120
TEST_P(StreamWorkerTest,Uninitialized)121 TEST_P(StreamWorkerTest, Uninitialized) {
122 EXPECT_FALSE(worker.hasWorkerCycleCalled());
123 EXPECT_FALSE(worker.hasError());
124 }
125
TEST_P(StreamWorkerTest,Start)126 TEST_P(StreamWorkerTest, Start) {
127 ASSERT_TRUE(worker.start());
128 worker.waitForAtLeastOneCycle();
129 EXPECT_FALSE(worker.hasError());
130 }
131
TEST_P(StreamWorkerTest,WorkerError)132 TEST_P(StreamWorkerTest, WorkerError) {
133 ASSERT_TRUE(worker.start());
134 stream.error = true;
135 worker.waitForAtLeastOneCycle();
136 EXPECT_TRUE(worker.hasError());
137 EXPECT_TRUE(worker.hasNoWorkerCycleCalled(kWorkerIdleCheckTime));
138 }
139
TEST_P(StreamWorkerTest,PauseResume)140 TEST_P(StreamWorkerTest, PauseResume) {
141 ASSERT_TRUE(worker.start());
142 worker.waitForAtLeastOneCycle();
143 EXPECT_FALSE(worker.hasError());
144 worker.pause();
145 EXPECT_TRUE(worker.hasNoWorkerCycleCalled(kWorkerIdleCheckTime));
146 EXPECT_FALSE(worker.hasError());
147 const size_t workerCyclesBefore = worker.getWorkerCycles();
148 worker.resume();
149 // 'resume' is synchronous and returns after the worker has looped at least once.
150 EXPECT_GT(worker.getWorkerCycles(), workerCyclesBefore);
151 EXPECT_FALSE(worker.hasError());
152 }
153
TEST_P(StreamWorkerTest,StopPaused)154 TEST_P(StreamWorkerTest, StopPaused) {
155 ASSERT_TRUE(worker.start());
156 worker.waitForAtLeastOneCycle();
157 EXPECT_FALSE(worker.hasError());
158 worker.pause();
159 worker.stop();
160 EXPECT_FALSE(worker.hasError());
161 }
162
TEST_P(StreamWorkerTest,PauseAfterErrorIgnored)163 TEST_P(StreamWorkerTest, PauseAfterErrorIgnored) {
164 ASSERT_TRUE(worker.start());
165 stream.error = true;
166 worker.waitForAtLeastOneCycle();
167 EXPECT_TRUE(worker.hasError());
168 worker.pause();
169 EXPECT_TRUE(worker.hasNoWorkerCycleCalled(kWorkerIdleCheckTime));
170 EXPECT_TRUE(worker.hasError());
171 }
172
TEST_P(StreamWorkerTest,ResumeAfterErrorIgnored)173 TEST_P(StreamWorkerTest, ResumeAfterErrorIgnored) {
174 ASSERT_TRUE(worker.start());
175 stream.error = true;
176 worker.waitForAtLeastOneCycle();
177 EXPECT_TRUE(worker.hasError());
178 worker.resume();
179 EXPECT_TRUE(worker.hasNoWorkerCycleCalled(kWorkerIdleCheckTime));
180 EXPECT_TRUE(worker.hasError());
181 }
182
TEST_P(StreamWorkerTest,WorkerErrorOnResume)183 TEST_P(StreamWorkerTest, WorkerErrorOnResume) {
184 ASSERT_TRUE(worker.start());
185 worker.waitForAtLeastOneCycle();
186 EXPECT_FALSE(worker.hasError());
187 worker.pause();
188 EXPECT_FALSE(worker.hasError());
189 stream.error = true;
190 EXPECT_FALSE(worker.hasError());
191 worker.resume();
192 worker.waitForAtLeastOneCycle();
193 EXPECT_TRUE(worker.hasError());
194 EXPECT_TRUE(worker.hasNoWorkerCycleCalled(kWorkerIdleCheckTime));
195 }
196
TEST_P(StreamWorkerTest,WaitForAtLeastOneCycle)197 TEST_P(StreamWorkerTest, WaitForAtLeastOneCycle) {
198 ASSERT_TRUE(worker.start());
199 const size_t workerCyclesBefore = worker.getWorkerCycles();
200 EXPECT_TRUE(worker.waitForAtLeastOneCycle());
201 EXPECT_GT(worker.getWorkerCycles(), workerCyclesBefore);
202 }
203
TEST_P(StreamWorkerTest,WaitForAtLeastOneCycleError)204 TEST_P(StreamWorkerTest, WaitForAtLeastOneCycleError) {
205 ASSERT_TRUE(worker.start());
206 stream.error = true;
207 EXPECT_FALSE(worker.waitForAtLeastOneCycle());
208 }
209
210 INSTANTIATE_TEST_SUITE_P(StreamWorker, StreamWorkerTest, testing::Bool());
211