tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nrappkit_unittest.cpp (2442B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 // Original author: ekr@rtfm.com
      8 #include <iostream>
      9 
     10 // nrappkit includes
     11 extern "C" {
     12 // clang-format off
     13 #include "nr_api.h"
     14 #include "async_timer.h"
     15 // clang-format on
     16 }
     17 
     18 #include "runnable_utils.h"
     19 
     20 #define GTEST_HAS_RTTI 0
     21 #include "gtest/gtest.h"
     22 #include "gtest_utils.h"
     23 
     24 using namespace mozilla;
     25 
     26 namespace {
     27 
     28 class TimerTest : public MtransportTest {
     29 public:
     30  TimerTest() : handle_(nullptr), fired_(false) {}
     31  virtual ~TimerTest() = default;
     32 
     33  int ArmTimer(int timeout) {
     34    int ret;
     35 
     36    test_utils_->SyncDispatchToSTS(
     37        WrapRunnableRet(&ret, this, &TimerTest::ArmTimer_w, timeout));
     38 
     39    return ret;
     40  }
     41 
     42  int ArmCancelTimer(int timeout) {
     43    int ret;
     44 
     45    test_utils_->SyncDispatchToSTS(
     46        WrapRunnableRet(&ret, this, &TimerTest::ArmCancelTimer_w, timeout));
     47 
     48    return ret;
     49  }
     50 
     51  int ArmTimer_w(int timeout) {
     52    return NR_ASYNC_TIMER_SET(timeout, cb, this, &handle_);
     53  }
     54 
     55  int ArmCancelTimer_w(int timeout) {
     56    int r;
     57    r = ArmTimer_w(timeout);
     58    if (r) return r;
     59 
     60    return CancelTimer_w();
     61  }
     62 
     63  int CancelTimer() {
     64    int ret;
     65 
     66    test_utils_->SyncDispatchToSTS(
     67        WrapRunnableRet(&ret, this, &TimerTest::CancelTimer_w));
     68 
     69    return ret;
     70  }
     71 
     72  int CancelTimer_w() { return NR_async_timer_cancel(handle_); }
     73 
     74  int Schedule() {
     75    int ret;
     76 
     77    test_utils_->SyncDispatchToSTS(
     78        WrapRunnableRet(&ret, this, &TimerTest::Schedule_w));
     79 
     80    return ret;
     81  }
     82 
     83  int Schedule_w() {
     84    NR_ASYNC_SCHEDULE(cb, this);
     85 
     86    return 0;
     87  }
     88 
     89  static void cb(NR_SOCKET r, int how, void* arg) {
     90    std::cerr << "Timer fired\n";
     91 
     92    TimerTest* t = static_cast<TimerTest*>(arg);
     93 
     94    t->fired_ = true;
     95  }
     96 
     97 protected:
     98  void* handle_;
     99  std::atomic<bool> fired_;
    100 };
    101 }  // namespace
    102 
    103 TEST_F(TimerTest, SimpleTimer) {
    104  ArmTimer(100);
    105  ASSERT_TRUE_WAIT(fired_, 1000);
    106 }
    107 
    108 TEST_F(TimerTest, CancelTimer) {
    109  ArmTimer(1000);
    110  CancelTimer();
    111  PR_Sleep(2000);
    112  ASSERT_FALSE(fired_);
    113 }
    114 
    115 TEST_F(TimerTest, CancelTimer0) {
    116  ArmCancelTimer(0);
    117  PR_Sleep(100);
    118  ASSERT_FALSE(fired_);
    119 }
    120 
    121 TEST_F(TimerTest, ScheduleTest) {
    122  Schedule();
    123  ASSERT_TRUE_WAIT(fired_, 1000);
    124 }