michael@0: michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // Original author: ekr@rtfm.com michael@0: #include michael@0: michael@0: #include "nsThreadUtils.h" michael@0: #include "nsXPCOM.h" michael@0: michael@0: // nrappkit includes michael@0: extern "C" { michael@0: #include "nr_api.h" michael@0: #include "async_timer.h" michael@0: } michael@0: michael@0: #include "mtransport_test_utils.h" michael@0: #include "runnable_utils.h" michael@0: michael@0: #define GTEST_HAS_RTTI 0 michael@0: #include "gtest/gtest.h" michael@0: #include "gtest_utils.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: MtransportTestUtils *test_utils; michael@0: michael@0: namespace { michael@0: michael@0: class TimerTest : public ::testing::Test { michael@0: public: michael@0: TimerTest() : handle_(nullptr), fired_(false) {} michael@0: michael@0: int ArmTimer(int timeout) { michael@0: int ret; michael@0: michael@0: test_utils->sts_target()->Dispatch( michael@0: WrapRunnableRet(this, &TimerTest::ArmTimer_w, timeout, &ret), michael@0: NS_DISPATCH_SYNC); michael@0: michael@0: return ret; michael@0: } michael@0: michael@0: int ArmTimer_w(int timeout) { michael@0: return NR_ASYNC_TIMER_SET(timeout, cb, this, &handle_); michael@0: } michael@0: michael@0: int CancelTimer() { michael@0: int ret; michael@0: michael@0: test_utils->sts_target()->Dispatch( michael@0: WrapRunnableRet(this, &TimerTest::CancelTimer_w, &ret), michael@0: NS_DISPATCH_SYNC); michael@0: michael@0: return ret; michael@0: } michael@0: michael@0: int CancelTimer_w() { michael@0: return NR_async_timer_cancel(handle_); michael@0: } michael@0: michael@0: int Schedule() { michael@0: int ret; michael@0: michael@0: test_utils->sts_target()->Dispatch( michael@0: WrapRunnableRet(this, &TimerTest::Schedule_w, &ret), michael@0: NS_DISPATCH_SYNC); michael@0: michael@0: return ret; michael@0: } michael@0: michael@0: int Schedule_w() { michael@0: NR_ASYNC_SCHEDULE(cb, this); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: michael@0: static void cb(NR_SOCKET r, int how, void *arg) { michael@0: std::cerr << "Timer fired " << std::endl; michael@0: michael@0: TimerTest *t = static_cast(arg); michael@0: michael@0: t->fired_ = true; michael@0: } michael@0: michael@0: protected: michael@0: void *handle_; michael@0: bool fired_; michael@0: }; michael@0: } michael@0: michael@0: TEST_F(TimerTest, SimpleTimer) { michael@0: ArmTimer(100); michael@0: ASSERT_TRUE_WAIT(fired_, 1000); michael@0: } michael@0: michael@0: TEST_F(TimerTest, CancelTimer) { michael@0: ArmTimer(1000); michael@0: CancelTimer(); michael@0: PR_Sleep(2000); michael@0: ASSERT_FALSE(fired_); michael@0: } michael@0: michael@0: TEST_F(TimerTest, ScheduleTest) { michael@0: Schedule(); michael@0: ASSERT_TRUE_WAIT(fired_, 1000); michael@0: } michael@0: michael@0: int main(int argc, char **argv) { michael@0: test_utils = new MtransportTestUtils(); michael@0: michael@0: // Start the tests michael@0: ::testing::InitGoogleTest(&argc, argv); michael@0: michael@0: int rv = RUN_ALL_TESTS(); michael@0: delete test_utils; michael@0: return rv; michael@0: }