1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker *
4*288bf522SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker *
8*288bf522SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker *
10*288bf522SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker */
16*288bf522SAndroid Build Coastguard Worker
17*288bf522SAndroid Build Coastguard Worker #include "IOEventLoop.h"
18*288bf522SAndroid Build Coastguard Worker
19*288bf522SAndroid Build Coastguard Worker #include <gtest/gtest.h>
20*288bf522SAndroid Build Coastguard Worker
21*288bf522SAndroid Build Coastguard Worker #include <atomic>
22*288bf522SAndroid Build Coastguard Worker #include <chrono>
23*288bf522SAndroid Build Coastguard Worker #include <thread>
24*288bf522SAndroid Build Coastguard Worker
25*288bf522SAndroid Build Coastguard Worker #include <android-base/logging.h>
26*288bf522SAndroid Build Coastguard Worker
27*288bf522SAndroid Build Coastguard Worker using namespace simpleperf;
28*288bf522SAndroid Build Coastguard Worker
29*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(IOEventLoop,read)30*288bf522SAndroid Build Coastguard Worker TEST(IOEventLoop, read) {
31*288bf522SAndroid Build Coastguard Worker int fd[2];
32*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(0, pipe(fd));
33*288bf522SAndroid Build Coastguard Worker IOEventLoop loop;
34*288bf522SAndroid Build Coastguard Worker int count = 0;
35*288bf522SAndroid Build Coastguard Worker int retry_count = 0;
36*288bf522SAndroid Build Coastguard Worker ASSERT_NE(nullptr, loop.AddReadEvent(fd[0], [&]() {
37*288bf522SAndroid Build Coastguard Worker while (true) {
38*288bf522SAndroid Build Coastguard Worker char c;
39*288bf522SAndroid Build Coastguard Worker int ret = read(fd[0], &c, 1);
40*288bf522SAndroid Build Coastguard Worker if (ret == 1) {
41*288bf522SAndroid Build Coastguard Worker if (++count == 100) {
42*288bf522SAndroid Build Coastguard Worker return loop.ExitLoop();
43*288bf522SAndroid Build Coastguard Worker }
44*288bf522SAndroid Build Coastguard Worker } else if (ret == -1 && errno == EAGAIN) {
45*288bf522SAndroid Build Coastguard Worker retry_count++;
46*288bf522SAndroid Build Coastguard Worker break;
47*288bf522SAndroid Build Coastguard Worker } else {
48*288bf522SAndroid Build Coastguard Worker return false;
49*288bf522SAndroid Build Coastguard Worker }
50*288bf522SAndroid Build Coastguard Worker }
51*288bf522SAndroid Build Coastguard Worker return true;
52*288bf522SAndroid Build Coastguard Worker }));
53*288bf522SAndroid Build Coastguard Worker std::thread thread([&]() {
54*288bf522SAndroid Build Coastguard Worker for (int i = 0; i < 100; ++i) {
55*288bf522SAndroid Build Coastguard Worker usleep(1000);
56*288bf522SAndroid Build Coastguard Worker char c;
57*288bf522SAndroid Build Coastguard Worker CHECK_EQ(write(fd[1], &c, 1), 1);
58*288bf522SAndroid Build Coastguard Worker }
59*288bf522SAndroid Build Coastguard Worker });
60*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(loop.RunLoop());
61*288bf522SAndroid Build Coastguard Worker thread.join();
62*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(100, count);
63*288bf522SAndroid Build Coastguard Worker // Test retry_count to make sure we are not doing blocking read.
64*288bf522SAndroid Build Coastguard Worker ASSERT_GT(retry_count, 0);
65*288bf522SAndroid Build Coastguard Worker close(fd[0]);
66*288bf522SAndroid Build Coastguard Worker close(fd[1]);
67*288bf522SAndroid Build Coastguard Worker }
68*288bf522SAndroid Build Coastguard Worker
69*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(IOEventLoop,write)70*288bf522SAndroid Build Coastguard Worker TEST(IOEventLoop, write) {
71*288bf522SAndroid Build Coastguard Worker int fd[2];
72*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(0, pipe(fd));
73*288bf522SAndroid Build Coastguard Worker IOEventLoop loop;
74*288bf522SAndroid Build Coastguard Worker int count = 0;
75*288bf522SAndroid Build Coastguard Worker ASSERT_NE(nullptr, loop.AddWriteEvent(fd[1], [&]() {
76*288bf522SAndroid Build Coastguard Worker int ret = 0;
77*288bf522SAndroid Build Coastguard Worker char buf[4096];
78*288bf522SAndroid Build Coastguard Worker while ((ret = write(fd[1], buf, sizeof(buf))) > 0) {
79*288bf522SAndroid Build Coastguard Worker }
80*288bf522SAndroid Build Coastguard Worker if (ret == -1 && errno == EAGAIN) {
81*288bf522SAndroid Build Coastguard Worker if (++count == 100) {
82*288bf522SAndroid Build Coastguard Worker loop.ExitLoop();
83*288bf522SAndroid Build Coastguard Worker }
84*288bf522SAndroid Build Coastguard Worker return true;
85*288bf522SAndroid Build Coastguard Worker }
86*288bf522SAndroid Build Coastguard Worker return false;
87*288bf522SAndroid Build Coastguard Worker }));
88*288bf522SAndroid Build Coastguard Worker std::thread thread([&]() {
89*288bf522SAndroid Build Coastguard Worker usleep(500000);
90*288bf522SAndroid Build Coastguard Worker while (true) {
91*288bf522SAndroid Build Coastguard Worker usleep(1000);
92*288bf522SAndroid Build Coastguard Worker char buf[4096];
93*288bf522SAndroid Build Coastguard Worker if (read(fd[0], buf, sizeof(buf)) <= 0) {
94*288bf522SAndroid Build Coastguard Worker break;
95*288bf522SAndroid Build Coastguard Worker }
96*288bf522SAndroid Build Coastguard Worker }
97*288bf522SAndroid Build Coastguard Worker });
98*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(loop.RunLoop());
99*288bf522SAndroid Build Coastguard Worker // close fd[1] to make read thread stop.
100*288bf522SAndroid Build Coastguard Worker close(fd[1]);
101*288bf522SAndroid Build Coastguard Worker thread.join();
102*288bf522SAndroid Build Coastguard Worker close(fd[0]);
103*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(100, count);
104*288bf522SAndroid Build Coastguard Worker }
105*288bf522SAndroid Build Coastguard Worker
106*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(IOEventLoop,signal)107*288bf522SAndroid Build Coastguard Worker TEST(IOEventLoop, signal) {
108*288bf522SAndroid Build Coastguard Worker IOEventLoop loop;
109*288bf522SAndroid Build Coastguard Worker int count = 0;
110*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(loop.AddSignalEvent(SIGINT, [&]() {
111*288bf522SAndroid Build Coastguard Worker if (++count == 100) {
112*288bf522SAndroid Build Coastguard Worker loop.ExitLoop();
113*288bf522SAndroid Build Coastguard Worker }
114*288bf522SAndroid Build Coastguard Worker return true;
115*288bf522SAndroid Build Coastguard Worker }));
116*288bf522SAndroid Build Coastguard Worker std::atomic<bool> stop_thread(false);
117*288bf522SAndroid Build Coastguard Worker std::thread thread([&]() {
118*288bf522SAndroid Build Coastguard Worker while (!stop_thread) {
119*288bf522SAndroid Build Coastguard Worker usleep(1000);
120*288bf522SAndroid Build Coastguard Worker kill(getpid(), SIGINT);
121*288bf522SAndroid Build Coastguard Worker }
122*288bf522SAndroid Build Coastguard Worker });
123*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(loop.RunLoop());
124*288bf522SAndroid Build Coastguard Worker stop_thread = true;
125*288bf522SAndroid Build Coastguard Worker thread.join();
126*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(100, count);
127*288bf522SAndroid Build Coastguard Worker }
128*288bf522SAndroid Build Coastguard Worker
TestPeriodicEvents(int period_in_us,int iterations)129*288bf522SAndroid Build Coastguard Worker void TestPeriodicEvents(int period_in_us, int iterations) {
130*288bf522SAndroid Build Coastguard Worker timeval tv;
131*288bf522SAndroid Build Coastguard Worker tv.tv_sec = period_in_us / 1000000;
132*288bf522SAndroid Build Coastguard Worker tv.tv_usec = period_in_us % 1000000;
133*288bf522SAndroid Build Coastguard Worker int count = 0;
134*288bf522SAndroid Build Coastguard Worker IOEventLoop loop;
135*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(loop.AddPeriodicEvent(tv, [&]() {
136*288bf522SAndroid Build Coastguard Worker if (++count == iterations) {
137*288bf522SAndroid Build Coastguard Worker loop.ExitLoop();
138*288bf522SAndroid Build Coastguard Worker }
139*288bf522SAndroid Build Coastguard Worker return true;
140*288bf522SAndroid Build Coastguard Worker }));
141*288bf522SAndroid Build Coastguard Worker auto start_time = std::chrono::steady_clock::now();
142*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(loop.RunLoop());
143*288bf522SAndroid Build Coastguard Worker auto end_time = std::chrono::steady_clock::now();
144*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(iterations, count);
145*288bf522SAndroid Build Coastguard Worker double time_used =
146*288bf522SAndroid Build Coastguard Worker std::chrono::duration_cast<std::chrono::duration<double>>(end_time - start_time).count();
147*288bf522SAndroid Build Coastguard Worker double min_time_in_sec = period_in_us / 1e6 * iterations;
148*288bf522SAndroid Build Coastguard Worker double max_time_in_sec = min_time_in_sec + 0.3;
149*288bf522SAndroid Build Coastguard Worker ASSERT_GE(time_used, min_time_in_sec);
150*288bf522SAndroid Build Coastguard Worker ASSERT_LT(time_used, max_time_in_sec);
151*288bf522SAndroid Build Coastguard Worker }
152*288bf522SAndroid Build Coastguard Worker
153*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(IOEventLoop,periodic)154*288bf522SAndroid Build Coastguard Worker TEST(IOEventLoop, periodic) {
155*288bf522SAndroid Build Coastguard Worker TestPeriodicEvents(1000, 100);
156*288bf522SAndroid Build Coastguard Worker }
157*288bf522SAndroid Build Coastguard Worker
158*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(IOEventLoop,one_time_event)159*288bf522SAndroid Build Coastguard Worker TEST(IOEventLoop, one_time_event) {
160*288bf522SAndroid Build Coastguard Worker int duration_in_us = 1000;
161*288bf522SAndroid Build Coastguard Worker timeval tv = {};
162*288bf522SAndroid Build Coastguard Worker tv.tv_usec = duration_in_us;
163*288bf522SAndroid Build Coastguard Worker int count = 0;
164*288bf522SAndroid Build Coastguard Worker auto callback_time = std::chrono::steady_clock::now();
165*288bf522SAndroid Build Coastguard Worker IOEventLoop loop;
166*288bf522SAndroid Build Coastguard Worker // Add a one time event to test callback count and time.
167*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(loop.AddOneTimeEvent(tv, [&]() {
168*288bf522SAndroid Build Coastguard Worker ++count;
169*288bf522SAndroid Build Coastguard Worker callback_time = std::chrono::steady_clock::now();
170*288bf522SAndroid Build Coastguard Worker return true;
171*288bf522SAndroid Build Coastguard Worker }));
172*288bf522SAndroid Build Coastguard Worker // Add another one time event to exit loop.
173*288bf522SAndroid Build Coastguard Worker tv.tv_usec = duration_in_us * 3;
174*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(loop.AddOneTimeEvent(tv, [&]() { return loop.ExitLoop(); }));
175*288bf522SAndroid Build Coastguard Worker
176*288bf522SAndroid Build Coastguard Worker auto start_time = std::chrono::steady_clock::now();
177*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(loop.RunLoop());
178*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(1, count);
179*288bf522SAndroid Build Coastguard Worker double time_used =
180*288bf522SAndroid Build Coastguard Worker std::chrono::duration_cast<std::chrono::duration<double>>(callback_time - start_time).count();
181*288bf522SAndroid Build Coastguard Worker double min_time_in_sec = duration_in_us / 1e6;
182*288bf522SAndroid Build Coastguard Worker double max_time_in_sec = min_time_in_sec + 0.3;
183*288bf522SAndroid Build Coastguard Worker ASSERT_GE(time_used, min_time_in_sec);
184*288bf522SAndroid Build Coastguard Worker ASSERT_LT(time_used, max_time_in_sec);
185*288bf522SAndroid Build Coastguard Worker }
186*288bf522SAndroid Build Coastguard Worker
187*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(IOEventLoop,read_and_del_event)188*288bf522SAndroid Build Coastguard Worker TEST(IOEventLoop, read_and_del_event) {
189*288bf522SAndroid Build Coastguard Worker int fd[2];
190*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(0, pipe(fd));
191*288bf522SAndroid Build Coastguard Worker IOEventLoop loop;
192*288bf522SAndroid Build Coastguard Worker int count = 0;
193*288bf522SAndroid Build Coastguard Worker IOEventRef ref = loop.AddReadEvent(fd[0], [&]() {
194*288bf522SAndroid Build Coastguard Worker count++;
195*288bf522SAndroid Build Coastguard Worker return IOEventLoop::DelEvent(ref);
196*288bf522SAndroid Build Coastguard Worker });
197*288bf522SAndroid Build Coastguard Worker ASSERT_NE(nullptr, ref);
198*288bf522SAndroid Build Coastguard Worker
199*288bf522SAndroid Build Coastguard Worker std::thread thread([&]() {
200*288bf522SAndroid Build Coastguard Worker for (int i = 0; i < 100; ++i) {
201*288bf522SAndroid Build Coastguard Worker usleep(1000);
202*288bf522SAndroid Build Coastguard Worker char c;
203*288bf522SAndroid Build Coastguard Worker CHECK_EQ(write(fd[1], &c, 1), 1);
204*288bf522SAndroid Build Coastguard Worker }
205*288bf522SAndroid Build Coastguard Worker });
206*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(loop.RunLoop());
207*288bf522SAndroid Build Coastguard Worker thread.join();
208*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(1, count);
209*288bf522SAndroid Build Coastguard Worker close(fd[0]);
210*288bf522SAndroid Build Coastguard Worker close(fd[1]);
211*288bf522SAndroid Build Coastguard Worker }
212*288bf522SAndroid Build Coastguard Worker
213*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(IOEventLoop,disable_enable_event)214*288bf522SAndroid Build Coastguard Worker TEST(IOEventLoop, disable_enable_event) {
215*288bf522SAndroid Build Coastguard Worker int fd[2];
216*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(0, pipe(fd));
217*288bf522SAndroid Build Coastguard Worker IOEventLoop loop;
218*288bf522SAndroid Build Coastguard Worker int count = 0;
219*288bf522SAndroid Build Coastguard Worker IOEventRef ref = loop.AddWriteEvent(fd[1], [&]() {
220*288bf522SAndroid Build Coastguard Worker count++;
221*288bf522SAndroid Build Coastguard Worker return IOEventLoop::DisableEvent(ref);
222*288bf522SAndroid Build Coastguard Worker });
223*288bf522SAndroid Build Coastguard Worker ASSERT_NE(nullptr, ref);
224*288bf522SAndroid Build Coastguard Worker
225*288bf522SAndroid Build Coastguard Worker timeval tv;
226*288bf522SAndroid Build Coastguard Worker tv.tv_sec = 0;
227*288bf522SAndroid Build Coastguard Worker tv.tv_usec = 500000;
228*288bf522SAndroid Build Coastguard Worker int periodic_count = 0;
229*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(loop.AddPeriodicEvent(tv, [&]() {
230*288bf522SAndroid Build Coastguard Worker periodic_count++;
231*288bf522SAndroid Build Coastguard Worker if (periodic_count == 1) {
232*288bf522SAndroid Build Coastguard Worker if (count != 1) {
233*288bf522SAndroid Build Coastguard Worker return false;
234*288bf522SAndroid Build Coastguard Worker }
235*288bf522SAndroid Build Coastguard Worker return IOEventLoop::EnableEvent(ref);
236*288bf522SAndroid Build Coastguard Worker } else {
237*288bf522SAndroid Build Coastguard Worker if (count != 2) {
238*288bf522SAndroid Build Coastguard Worker return false;
239*288bf522SAndroid Build Coastguard Worker }
240*288bf522SAndroid Build Coastguard Worker return loop.ExitLoop();
241*288bf522SAndroid Build Coastguard Worker }
242*288bf522SAndroid Build Coastguard Worker }));
243*288bf522SAndroid Build Coastguard Worker
244*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(loop.RunLoop());
245*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(2, count);
246*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(2, periodic_count);
247*288bf522SAndroid Build Coastguard Worker close(fd[0]);
248*288bf522SAndroid Build Coastguard Worker close(fd[1]);
249*288bf522SAndroid Build Coastguard Worker }
250*288bf522SAndroid Build Coastguard Worker
251*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(IOEventLoop,disable_enable_periodic_event)252*288bf522SAndroid Build Coastguard Worker TEST(IOEventLoop, disable_enable_periodic_event) {
253*288bf522SAndroid Build Coastguard Worker timeval tv;
254*288bf522SAndroid Build Coastguard Worker tv.tv_sec = 0;
255*288bf522SAndroid Build Coastguard Worker tv.tv_usec = 200000;
256*288bf522SAndroid Build Coastguard Worker IOEventLoop loop;
257*288bf522SAndroid Build Coastguard Worker IOEventRef wait_ref = loop.AddPeriodicEvent(tv, [&]() { return loop.ExitLoop(); });
258*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(wait_ref != nullptr);
259*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(loop.DisableEvent(wait_ref));
260*288bf522SAndroid Build Coastguard Worker
261*288bf522SAndroid Build Coastguard Worker tv.tv_sec = 0;
262*288bf522SAndroid Build Coastguard Worker tv.tv_usec = 100000;
263*288bf522SAndroid Build Coastguard Worker size_t periodic_count = 0;
264*288bf522SAndroid Build Coastguard Worker IOEventRef ref = loop.AddPeriodicEvent(tv, [&]() {
265*288bf522SAndroid Build Coastguard Worker if (!loop.DisableEvent(ref)) {
266*288bf522SAndroid Build Coastguard Worker return false;
267*288bf522SAndroid Build Coastguard Worker }
268*288bf522SAndroid Build Coastguard Worker periodic_count++;
269*288bf522SAndroid Build Coastguard Worker if (periodic_count < 2u) {
270*288bf522SAndroid Build Coastguard Worker return loop.EnableEvent(ref);
271*288bf522SAndroid Build Coastguard Worker }
272*288bf522SAndroid Build Coastguard Worker return loop.EnableEvent(wait_ref);
273*288bf522SAndroid Build Coastguard Worker });
274*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(loop.RunLoop());
275*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(2u, periodic_count);
276*288bf522SAndroid Build Coastguard Worker }
277*288bf522SAndroid Build Coastguard Worker
278*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(IOEventLoop,exit_before_loop)279*288bf522SAndroid Build Coastguard Worker TEST(IOEventLoop, exit_before_loop) {
280*288bf522SAndroid Build Coastguard Worker IOEventLoop loop;
281*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(loop.ExitLoop());
282*288bf522SAndroid Build Coastguard Worker }
283*288bf522SAndroid Build Coastguard Worker
284*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(IOEventLoop,priority)285*288bf522SAndroid Build Coastguard Worker TEST(IOEventLoop, priority) {
286*288bf522SAndroid Build Coastguard Worker int low_priority_fd[2];
287*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(0, pipe(low_priority_fd));
288*288bf522SAndroid Build Coastguard Worker int high_priority_fd[2];
289*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(0, pipe(high_priority_fd));
290*288bf522SAndroid Build Coastguard Worker
291*288bf522SAndroid Build Coastguard Worker IOEventLoop loop;
292*288bf522SAndroid Build Coastguard Worker int count = 0;
293*288bf522SAndroid Build Coastguard Worker
294*288bf522SAndroid Build Coastguard Worker ASSERT_NE(nullptr, loop.AddReadEvent(
295*288bf522SAndroid Build Coastguard Worker low_priority_fd[0],
296*288bf522SAndroid Build Coastguard Worker [&]() {
297*288bf522SAndroid Build Coastguard Worker char c;
298*288bf522SAndroid Build Coastguard Worker read(low_priority_fd[0], &c, 1);
299*288bf522SAndroid Build Coastguard Worker CHECK_EQ(count, 1);
300*288bf522SAndroid Build Coastguard Worker count++;
301*288bf522SAndroid Build Coastguard Worker return loop.ExitLoop();
302*288bf522SAndroid Build Coastguard Worker },
303*288bf522SAndroid Build Coastguard Worker IOEventLowPriority));
304*288bf522SAndroid Build Coastguard Worker
305*288bf522SAndroid Build Coastguard Worker ASSERT_NE(nullptr, loop.AddReadEvent(
306*288bf522SAndroid Build Coastguard Worker high_priority_fd[0],
307*288bf522SAndroid Build Coastguard Worker [&]() {
308*288bf522SAndroid Build Coastguard Worker char c;
309*288bf522SAndroid Build Coastguard Worker read(high_priority_fd[0], &c, 1);
310*288bf522SAndroid Build Coastguard Worker CHECK_EQ(count, 0);
311*288bf522SAndroid Build Coastguard Worker count++;
312*288bf522SAndroid Build Coastguard Worker return true;
313*288bf522SAndroid Build Coastguard Worker },
314*288bf522SAndroid Build Coastguard Worker IOEventHighPriority));
315*288bf522SAndroid Build Coastguard Worker
316*288bf522SAndroid Build Coastguard Worker char c;
317*288bf522SAndroid Build Coastguard Worker CHECK_EQ(write(low_priority_fd[1], &c, 1), 1);
318*288bf522SAndroid Build Coastguard Worker CHECK_EQ(write(high_priority_fd[1], &c, 1), 1);
319*288bf522SAndroid Build Coastguard Worker ASSERT_TRUE(loop.RunLoop());
320*288bf522SAndroid Build Coastguard Worker ASSERT_EQ(2, count);
321*288bf522SAndroid Build Coastguard Worker for (int i = 0; i < 2; i++) {
322*288bf522SAndroid Build Coastguard Worker close(low_priority_fd[i]);
323*288bf522SAndroid Build Coastguard Worker close(high_priority_fd[i]);
324*288bf522SAndroid Build Coastguard Worker }
325*288bf522SAndroid Build Coastguard Worker }
326