#include <fcntl.h> // for fcntl, F_SETFL, O_NONBLOCK #include <gtest/gtest.h> // for Message, TestPartResult, SuiteApi... #include <netdb.h> // for gethostbyname, h_addr, hostent #include <netinet/in.h> // for sockaddr_in, in_addr, INADDR_ANY #include <stdio.h> // for printf #include <sys/socket.h> // for socket, AF_INET, accept, bind #include <sys/types.h> // for in_addr_t #include <time.h> // for NULL, size_t #include <unistd.h> // for close, write, read
#include <condition_variable> // for condition_variable #include <cstdint> // for uint16_t #include <cstring> // for memset, strcmp, strcpy, strlen #include <mutex> // for mutex #include <ratio> // for ratio #include <string> // for string #include <thread> #include <tuple> // for tuple
namespace rootcanal {
class Event { public: void set(bool set = true) {
std::unique_lock<std::mutex> lk(m_);
set_ = set;
cv_.notify_all();
}
while (!stopped) {
write(socket_cli_fd, data.data(), data.size());
std::this_thread::sleep_for(5ms);
}
SUCCEED();
close(socket_cli_fd);
}
TEST_F(AsyncManagerSocketTest, CanUnsubscribeTaskFromWithinTask) {
Event running; usingnamespace std::chrono_literals;
async_manager_.ExecAsyncPeriodically(1, 1ms, 2ms, [&running, this]() {
EXPECT_TRUE(async_manager_.CancelAsyncTask(1))
<< "We were scheduled, so cancel should return true";
EXPECT_FALSE(async_manager_.CancelAsyncTask(1))
<< "We were not scheduled, so cancel should return false";
running.set(true);
});
EXPECT_TRUE(running.wait_for(100ms));
}
TEST_F(AsyncManagerSocketTest, UnsubScribeWaitsUntilCompletion) { usingnamespace std::chrono_literals;
Event running;
std::atomic<bool> cancel_done = false;
std::atomic<bool> task_complete = false;
AsyncTaskId task_id = async_manager_.ExecAsyncPeriodically( 1, 1ms, 2ms, [&running, &cancel_done, &task_complete]() { // Let the other thread now we are in the callback..
running.set(true); // Wee bit of a hack that relies on timing..
std::this_thread::sleep_for(20ms);
EXPECT_FALSE(cancel_done.load())
<< "Task cancellation did not wait for us to complete!";
task_complete.store(true);
});
EXPECT_TRUE(running.wait_for(100ms)); auto start = std::chrono::system_clock::now();
// There is a 20ms wait.. so we know that this should take some time.
EXPECT_TRUE(async_manager_.CancelAsyncTask(task_id))
<< "We were scheduled, so cancel should return true";
cancel_done.store(true);
EXPECT_TRUE(task_complete.load()) << "We managed to cancel a task while it was not yet finished."; auto end = std::chrono::system_clock::now(); auto passed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
EXPECT_GT(passed_ms.count(), 10);
}
TEST_F(AsyncManagerSocketTest, NoEventsAfterUnsubscribe) { // This tests makes sure the AsyncManager never fires an event // after calling StopWatchingFileDescriptor. using clock = std::chrono::system_clock; usingnamespace std::chrono_literals;
int round = 0; auto [slow_cli_fd, slow_s_fd] = ConnectSocketPair();
fcntl(slow_s_fd, F_SETFL, O_NONBLOCK);
auto [fast_cli_fd, fast_s_fd] = ConnectSocketPair();
fcntl(fast_s_fd, F_SETFL, O_NONBLOCK);
std::string data(1, 'x');
// The idea here is as follows: // We want to make sure that an unsubscribed callback never gets called. // This is to make sure we can safely do things like this: // // class Foo { // Foo(int fd, AsyncManager* am) : fd_(fd), am_(am) { // am_->WatchFdForNonBlockingReads( // fd, [&](int fd) { printf("This shouldn't crash! %p\n", this); }); // } // ~Foo() { am_->StopWatchingFileDescriptor(fd_); } // // AsyncManager* am_; // int fd_; // }; // // We are going to force a failure as follows: // // The slow callback needs to be called first, if it does not we cannot // force failure, so we have to try multiple times. // // t1, is the thread doing the loop. // t2, is the async manager handler thread. // // t1 will block until the slowcallback. // t2 will now block (for at most 250 ms). // t1 will unsubscribe the fast callback. // 2 cases: // with bug: // - t1 takes a timestamp, unblocks t2, // - t2 invokes the fast callback, and gets a timestamp. // - Now the unsubscribe time is before the callback time. // without bug.: // - t1 locks un unsusbcribe in asyn manager // - t2 unlocks due to timeout, // - t2 invokes the fast callback, and gets a timestamp. // - t1 is unlocked and gets a timestamp. // - Now the unsubscribe time is after the callback time..
// Block in the right places. if (inslow.wait_for(25ms)) {
async_manager_.StopWatchingFileDescriptor(fast_s_fd);
time_stopped_listening = clock::now();
printf("stop: %lld\n", time_stopped_listening.time_since_epoch().count() % 10000);
unblock_slow.set();
}
infast.wait_for(25ms);
// Unregister.
async_manager_.StopWatchingFileDescriptor(fast_s_fd);
async_manager_.StopWatchingFileDescriptor(slow_s_fd);
} while (time_fast_called < time_slow_called);
// fast before stop listening.
ASSERT_LT(time_fast_called.time_since_epoch().count(),
time_stopped_listening.time_since_epoch().count());
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.