blob: 70f7cf8ca383619646c79847f88deb233b829066 [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Yves Gerey3e707812018-11-28 16:47:49 +010015#include <memory>
16
Markus Handell9a21c492022-08-25 11:40:13 +000017#include "api/units/time_delta.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/gunit.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "rtc_base/location.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/message_handler.h"
Yves Gerey3e707812018-11-28 16:47:49 +010021#include "rtc_base/thread.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/time_utils.h"
Yves Gerey3e707812018-11-28 16:47:49 +010023#include "test/gtest.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024
25namespace rtc {
26
Peter Boström0c4e06b2015-10-07 12:23:21 +020027static const uint32_t kTimeout = 5000U;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000028
Tomas Gunnarssonabdb4702020-09-05 18:43:36 +020029class NullSocketServerTest : public ::testing::Test,
30 public MessageHandlerAutoCleanup {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031 protected:
Steve Anton9de3aac2017-10-24 10:08:26 -070032 void OnMessage(Message* message) override { ss_.WakeUp(); }
33
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000034 NullSocketServer ss_;
35};
36
henrike@webrtc.orgc732a3e2014-10-09 22:08:15 +000037TEST_F(NullSocketServerTest, WaitAndSet) {
tommie7251592017-07-14 14:44:46 -070038 auto thread = Thread::Create();
39 EXPECT_TRUE(thread->Start());
40 thread->Post(RTC_FROM_HERE, this, 0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000041 // The process_io will be ignored.
42 const bool process_io = true;
andresp@webrtc.org53d90122015-02-09 14:19:09 +000043 EXPECT_TRUE_WAIT(ss_.Wait(SocketServer::kForever, process_io), kTimeout);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000044}
45
46TEST_F(NullSocketServerTest, TestWait) {
Honghai Zhang82d78622016-05-06 11:29:15 -070047 int64_t start = TimeMillis();
Markus Handell9a21c492022-08-25 11:40:13 +000048 ss_.Wait(webrtc::TimeDelta::Millis(200), true);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000049 // The actual wait time is dependent on the resolution of the timer used by
50 // the Event class. Allow for the event to signal ~20ms early.
51 EXPECT_GE(TimeSince(start), 180);
52}
53
54} // namespace rtc