blob: 961ab6af3557f4e788c28679aaadf7ed2115973c [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "rtc_base/null_socket_server.h"
Yves Gerey3e707812018-11-28 16:47:49 +010012
13#include <stdint.h>
14#include <memory>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/gunit.h"
Yves Gerey3e707812018-11-28 16:47:49 +010017#include "rtc_base/location.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/message_handler.h"
19#include "rtc_base/message_queue.h"
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/time_utils.h"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "test/gtest.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023
24namespace rtc {
25
Peter Boström0c4e06b2015-10-07 12:23:21 +020026static const uint32_t kTimeout = 5000U;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000027
Mirko Bonadei6a489f22019-04-09 15:11:12 +020028class NullSocketServerTest : public ::testing::Test, public MessageHandler {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000029 protected:
Steve Anton9de3aac2017-10-24 10:08:26 -070030 void OnMessage(Message* message) override { ss_.WakeUp(); }
31
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000032 NullSocketServer ss_;
33};
34
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +000035TEST_F(NullSocketServerTest, WaitAndSet) {
tommie7251592017-07-14 14:44:46 -070036 auto thread = Thread::Create();
37 EXPECT_TRUE(thread->Start());
38 thread->Post(RTC_FROM_HERE, this, 0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000039 // The process_io will be ignored.
40 const bool process_io = true;
andresp@webrtc.org53d90122015-02-09 14:19:09 +000041 EXPECT_TRUE_WAIT(ss_.Wait(SocketServer::kForever, process_io), kTimeout);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000042}
43
44TEST_F(NullSocketServerTest, TestWait) {
Honghai Zhang82d78622016-05-06 11:29:15 -070045 int64_t start = TimeMillis();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046 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