2 * Copyright 2015-present Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #include "folly/experimental/ThreadedRepeatingFunctionRunner.h"
18 #include <folly/portability/GTest.h>
24 explicit Foo(std::atomic<int>& d) : data(d) {}
30 runner_.add([this]() {
32 return std::chrono::seconds(0);
36 std::atomic<int>& data;
37 folly::ThreadedRepeatingFunctionRunner runner_; // Must be declared last
41 explicit FooLongSleep(std::atomic<int>& d) : data(d) {}
48 runner_.add([this]() {
50 return 1000h; // Test would time out if we waited
54 std::atomic<int>& data;
55 folly::ThreadedRepeatingFunctionRunner runner_; // Must be declared last
58 TEST(TestThreadedRepeatingFunctionRunner, HandleBackgroundLoop) {
59 std::atomic<int> data(0);
62 EXPECT_EQ(0, data.load());
63 f.start(); // Runs increment thread in background
64 while (data.load() == 0) {
65 /* sleep override */ this_thread::sleep_for(chrono::milliseconds(10));
68 // The increment thread should have been destroyed
69 auto prev_val = data.load();
70 /* sleep override */ this_thread::sleep_for(chrono::milliseconds(100));
71 EXPECT_EQ(data.load(), prev_val);
74 TEST(TestThreadedRepeatingFunctionRunner, HandleLongSleepingThread) {
75 std::atomic<int> data(0);
78 EXPECT_EQ(0, data.load());
80 while (data.load() == 0) {
81 /* sleep override */ this_thread::sleep_for(chrono::milliseconds(10));
83 EXPECT_EQ(1, data.load());
85 // Foo should have been destroyed, which stopped the thread!
86 EXPECT_EQ(-1, data.load());