Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6dd2123b authored by Henri Chataing's avatar Henri Chataing Committed by Gerrit Code Review
Browse files

Merge "RootCanal: Improvements in rootcanal_test_host"

parents 2627355a 158545c8
Loading
Loading
Loading
Loading
+13 −12
Original line number Diff line number Diff line
@@ -112,12 +112,13 @@ class AsyncManagerSocketTest : public ::testing::Test {
    do {
      n = read(fd, server_buffer_, kBufferSize - 1);
    } while (n == -1 && errno == EAGAIN);
    ASSERT_GE(n, 0) << strerror(errno);

    if (n == 0) {  // got EOF
    if (n == 0 || errno == EBADF) {
      // Got EOF, or file descriptor disconnected.
      async_manager_.StopWatchingFileDescriptor(fd);
      close(fd);
    } else {
      ASSERT_GE(n, 0) << strerror(errno);
      n = write(fd, "1", 1);
    }
  }
@@ -235,33 +236,33 @@ TEST_F(AsyncManagerSocketTest, CanUnsubscribeTaskFromWithinTask) {
    running.set(true);
  });

  EXPECT_TRUE(running.wait_for(10ms));
  EXPECT_TRUE(running.wait_for(100ms));
}

TEST_F(AsyncManagerSocketTest, UnsubScribeWaitsUntilCompletion) {
  using namespace std::chrono_literals;
  Event running;
  bool cancel_done = false;
  bool task_complete = false;
  async_manager_.ExecAsyncPeriodically(
  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)
        EXPECT_FALSE(cancel_done.load())
            << "Task cancellation did not wait for us to complete!";
        task_complete = true;
        task_complete.store(true);
      });

  EXPECT_TRUE(running.wait_for(10ms));
  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(1))
  EXPECT_TRUE(async_manager_.CancelAsyncTask(task_id))
      << "We were scheduled, so cancel should return true";
  cancel_done = true;
  EXPECT_TRUE(task_complete)
  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 =