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 #ifndef gtest_utils_h__ 8 #define gtest_utils_h__ 9 10 #define GTEST_HAS_RTTI 0 11 #include "gtest/gtest.h" 12 #include "test_io.h" 13 14 namespace nss_test { 15 16 // Gtest utilities 17 class Timeout : public PollTarget { 18 public: Timeout(int32_t timer_ms)19 Timeout(int32_t timer_ms) : handle_(nullptr) { 20 Poller::Instance()->SetTimer(timer_ms, this, &Timeout::ExpiredCallback, 21 &handle_); 22 } ~Timeout()23 ~Timeout() { 24 if (handle_) { 25 handle_->Cancel(); 26 } 27 } 28 ExpiredCallback(PollTarget * target,Event event)29 static void ExpiredCallback(PollTarget* target, Event event) { 30 Timeout* timeout = static_cast<Timeout*>(target); 31 timeout->handle_ = nullptr; 32 } 33 timed_out()34 bool timed_out() const { return !handle_; } 35 36 private: 37 Poller::Timer* handle_; 38 }; 39 40 } // namespace nss_test 41 42 #define WAIT_(expression, timeout) \ 43 do { \ 44 Timeout tm(timeout); \ 45 while (!(expression)) { \ 46 Poller::Instance()->Poll(); \ 47 if (tm.timed_out()) break; \ 48 } \ 49 } while (0) 50 51 #define ASSERT_TRUE_WAIT(expression, timeout) \ 52 do { \ 53 WAIT_(expression, timeout); \ 54 ASSERT_TRUE(expression); \ 55 } while (0) 56 57 #endif 58