1 /*
2 * Copyright 2012 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "rtc_base/null_socket_server.h"
12
13 #include <stdint.h>
14
15 #include <memory>
16
17 #include "rtc_base/gunit.h"
18 #include "rtc_base/location.h"
19 #include "rtc_base/message_handler.h"
20 #include "rtc_base/thread.h"
21 #include "rtc_base/time_utils.h"
22 #include "test/gtest.h"
23
24 namespace rtc {
25
26 static const uint32_t kTimeout = 5000U;
27
28 class NullSocketServerTest : public ::testing::Test, public MessageHandler {
29 protected:
OnMessage(Message * message)30 void OnMessage(Message* message) override { ss_.WakeUp(); }
31
32 NullSocketServer ss_;
33 };
34
TEST_F(NullSocketServerTest,WaitAndSet)35 TEST_F(NullSocketServerTest, WaitAndSet) {
36 auto thread = Thread::Create();
37 EXPECT_TRUE(thread->Start());
38 thread->Post(RTC_FROM_HERE, this, 0);
39 // The process_io will be ignored.
40 const bool process_io = true;
41 EXPECT_TRUE_WAIT(ss_.Wait(SocketServer::kForever, process_io), kTimeout);
42 }
43
TEST_F(NullSocketServerTest,TestWait)44 TEST_F(NullSocketServerTest, TestWait) {
45 int64_t start = TimeMillis();
46 ss_.Wait(200, true);
47 // The actual wait time is dependent on the resolution of the timer used by
48 // the Event class. Allow for the event to signal ~20ms early.
49 EXPECT_GE(TimeSince(start), 180);
50 }
51
52 } // namespace rtc
53