tensorflow/core/kernels/batching_util/periodic_function_test.cc
// Test for race in calculating the first time the callback should fire.
Test for race in calculating the first time the callback should fire.
Test for race in calculating the first time the callback should fire .
TEST(PeriodicFunctionTest, StartupDelayRace) {
  const int64_t kDelayMicros = 10;
  const int64_t kPeriodMicros = kDelayMicros / 10;
  mutex mu;
  int counter = 0;
  std::unique_ptr<Notification> listener(new Notification);
  FakeClockEnv fake_clock_env(Env::Default());
  PeriodicFunction::Options options;
  options.env = &fake_clock_env;
  options.startup_delay_micros = kDelayMicros;
  PeriodicFunction periodic_function(
      [&mu, &counter, &listener]() {
        mutex_lock l(mu);
        counter++;
        listener->Notify();
      },
      kPeriodMicros, options);
  fake_clock_env.BlockUntilThreadsAsleep(1);
  fake_clock_env.AdvanceByMicroseconds(kDelayMicros);
  listener->WaitForNotification();
  {
    mutex_lock l(mu);
    EXPECT_EQ(1, counter);
    listener.reset(new Notification);
  }
  fake_clock_env.BlockUntilThreadsAsleep(1);
  fake_clock_env.AdvanceByMicroseconds(kPeriodMicros);
  listener->WaitForNotification();
  {
    mutex_lock l(mu);
    EXPECT_EQ(2, counter);
  }
  StopPeriodicFunction(&periodic_function, &fake_clock_env, kPeriodMicros);
}
TEST ( PeriodicFunctionTest , StartupDelayRace ) { const int64_t kDelayMicros = 10 ; const int64_t kPeriodMicros = kDelayMicros / 10 ; mutex mu ; int counter = 0 ; std :: unique_ptr < Notification > listener ( new Notification ) ; FakeClockEnv fake_clock_env ( Env :: Default ( ) ) ; PeriodicFunction :: Options options ; options . env = & fake_clock_env ; options . startup_delay_micros = kDelayMicros ; PeriodicFunction periodic_function ( [ & mu , & counter , & listener ] ( ) { mutex_lock l ( mu ) ; counter ++ ; listener -> Notify ( ) ; } , kPeriodMicros , options ) ; fake_clock_env . BlockUntilThreadsAsleep ( 1 ) ; fake_clock_env . AdvanceByMicroseconds ( kDelayMicros ) ; listener -> WaitForNotification ( ) ; { mutex_lock l ( mu ) ; EXPECT_EQ ( 1 , counter ) ; listener . reset ( new Notification ) ; } fake_clock_env . BlockUntilThreadsAsleep ( 1 ) ; fake_clock_env . AdvanceByMicroseconds ( kPeriodMicros ) ; listener -> WaitForNotification ( ) ; { mutex_lock l ( mu ) ; EXPECT_EQ ( 2 , counter ) ; } StopPeriodicFunction ( & periodic_function , & fake_clock_env , kPeriodMicros ) ; }
Test for race in calculating the first time the callback should fire.
Test for race in calculating the first time the callback should fire .
// A notification can only be notified once.