1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "IOEventLoop.h"
#include <gtest/gtest.h>
#include <atomic>
#include <chrono>
#include <thread>
TEST(IOEventLoop, read) {
int fd[2];
ASSERT_EQ(0, pipe(fd));
IOEventLoop loop;
int count = 0;
int retry_count = 0;
ASSERT_NE(nullptr, loop.AddReadEvent(fd[0], [&]() {
while (true) {
char c;
int ret = read(fd[0], &c, 1);
if (ret == 1) {
if (++count == 100) {
return loop.ExitLoop();
}
} else if (ret == -1 && errno == EAGAIN) {
retry_count++;
break;
} else {
return false;
}
}
return true;
}));
std::thread thread([&]() {
for (int i = 0; i < 100; ++i) {
usleep(1000);
char c;
write(fd[1], &c, 1);
}
});
ASSERT_TRUE(loop.RunLoop());
thread.join();
ASSERT_EQ(100, count);
// Test retry_count to make sure we are not doing blocking read.
ASSERT_GT(retry_count, 0);
close(fd[0]);
close(fd[1]);
}
TEST(IOEventLoop, write) {
int fd[2];
ASSERT_EQ(0, pipe(fd));
IOEventLoop loop;
int count = 0;
ASSERT_NE(nullptr, loop.AddWriteEvent(fd[1], [&]() {
int ret = 0;
char buf[4096];
while ((ret = write(fd[1], buf, sizeof(buf))) > 0) {
}
if (ret == -1 && errno == EAGAIN) {
if (++count == 100) {
loop.ExitLoop();
}
return true;
}
return false;
}));
std::thread thread([&]() {
usleep(500000);
while (true) {
usleep(1000);
char buf[4096];
if (read(fd[0], buf, sizeof(buf)) <= 0) {
break;
}
}
});
ASSERT_TRUE(loop.RunLoop());
// close fd[1] to make read thread stop.
close(fd[1]);
thread.join();
close(fd[0]);
ASSERT_EQ(100, count);
}
TEST(IOEventLoop, signal) {
IOEventLoop loop;
int count = 0;
ASSERT_TRUE(loop.AddSignalEvent(SIGINT, [&]() {
if (++count == 100) {
loop.ExitLoop();
}
return true;
}));
std::atomic<bool> stop_thread(false);
std::thread thread([&]() {
while (!stop_thread) {
usleep(1000);
kill(getpid(), SIGINT);
}
});
ASSERT_TRUE(loop.RunLoop());
stop_thread = true;
thread.join();
ASSERT_EQ(100, count);
}
TEST(IOEventLoop, periodic) {
timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 1000;
int count = 0;
IOEventLoop loop;
ASSERT_TRUE(loop.AddPeriodicEvent(tv, [&]() {
if (++count == 100) {
loop.ExitLoop();
}
return true;
}));
auto start_time = std::chrono::steady_clock::now();
ASSERT_TRUE(loop.RunLoop());
auto end_time = std::chrono::steady_clock::now();
ASSERT_EQ(100, count);
double time_used = std::chrono::duration_cast<std::chrono::duration<double>>(
end_time - start_time)
.count();
// time_used is 0.1 if running precisely, and we accept small errors by using
// a range [0.1, 0.15).
ASSERT_GE(time_used, 0.1);
ASSERT_LT(time_used, 0.15);
}
TEST(IOEventLoop, read_and_del_event) {
int fd[2];
ASSERT_EQ(0, pipe(fd));
IOEventLoop loop;
int count = 0;
IOEventRef ref = loop.AddReadEvent(fd[0], [&]() {
count++;
return IOEventLoop::DelEvent(ref);
});
ASSERT_NE(nullptr, ref);
std::thread thread([&]() {
for (int i = 0; i < 100; ++i) {
usleep(1000);
char c;
write(fd[1], &c, 1);
}
});
ASSERT_TRUE(loop.RunLoop());
thread.join();
ASSERT_EQ(1, count);
close(fd[0]);
close(fd[1]);
}
TEST(IOEventLoop, disable_enable_event) {
int fd[2];
ASSERT_EQ(0, pipe(fd));
IOEventLoop loop;
int count = 0;
IOEventRef ref = loop.AddWriteEvent(fd[1], [&]() {
count++;
return IOEventLoop::DisableEvent(ref);
});
ASSERT_NE(nullptr, ref);
timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 500000;
int periodic_count = 0;
ASSERT_TRUE(loop.AddPeriodicEvent(tv, [&]() {
periodic_count++;
if (periodic_count == 1) {
if (count != 1) {
return false;
}
return IOEventLoop::EnableEvent(ref);
} else {
if (count != 2) {
return false;
}
return loop.ExitLoop();
}
}));
ASSERT_TRUE(loop.RunLoop());
ASSERT_EQ(2, count);
ASSERT_EQ(2, periodic_count);
close(fd[0]);
close(fd[1]);
}
|