aboutsummaryrefslogtreecommitdiff
path: root/util/operation_loop_unittest.cc
blob: 4b4ea5df6719bd53863c43da4d57b25c1c664911 (plain)
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "util/operation_loop.h"

#include <atomic>
#include <chrono>
#include <thread>

#include "gtest/gtest.h"

namespace openscreen {

TEST(OperationsLoopTest, PerformAllOperationsWaits) {
  constexpr Clock::duration kTimeout{0};
  constexpr Clock::duration kMinRuntime{500};

  const auto start_time = Clock::now();
  std::atomic<Clock::time_point> last_run{start_time};
  std::atomic<Clock::time_point> current_run{start_time};
  std::function<void(Clock::duration)> test_function =
      [last = &last_run, current = &current_run](Clock::duration timeout) {
        last->store(current->load());
        current->store(Clock::now());
      };
  OperationLoop loop({test_function}, kTimeout, kMinRuntime);

  std::thread run_loop([&loop]() { loop.RunUntilStopped(); });

  while (last_run.load() == start_time) {
  }

  loop.RequestStopSoon();
  run_loop.join();

  EXPECT_GE(
      std::chrono::nanoseconds(current_run.load() - last_run.load()).count(),
      std::chrono::nanoseconds(kMinRuntime).count());
}

}  // namespace openscreen