File: socket_handle_waiter_posix_unittest.cc

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (107 lines) | stat: -rw-r--r-- 3,859 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "platform/impl/socket_handle_waiter_posix.h"

#include <sys/socket.h>

#include <chrono>
#include <iostream>
#include <thread>

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "platform/impl/socket_handle_posix.h"
#include "platform/impl/timeval_posix.h"
#include "platform/test/fake_clock.h"

namespace openscreen {
namespace {

using namespace ::testing;
using ::testing::_;

class MockSubscriber : public SocketHandleWaiter::Subscriber {
 public:
  using SocketHandleRef = SocketHandleWaiter::SocketHandleRef;
  MOCK_METHOD2(ProcessReadyHandle, void(SocketHandleRef, uint32_t));
};

class TestingSocketHandleWaiter : public SocketHandleWaiter {
 public:
  using SocketHandleRef = SocketHandleWaiter::SocketHandleRef;
  using ReadyHandle = SocketHandleWaiter::ReadyHandle;

  TestingSocketHandleWaiter() : SocketHandleWaiter(&FakeClock::now) {}

  MOCK_METHOD2(
      AwaitSocketsReadable,
      ErrorOr<std::vector<ReadyHandle>>(const std::vector<SocketHandleRef>&,
                                        const Clock::duration&));

  FakeClock fake_clock{Clock::time_point{Clock::duration{1234567}}};
};

}  // namespace

TEST(SocketHandleWaiterTest, BubblesUpAwaitSocketsReadableErrors) {
  MockSubscriber subscriber;
  TestingSocketHandleWaiter waiter;
  SocketHandle handle0{0};
  SocketHandle handle1{1};
  SocketHandle handle2{2};
  const SocketHandle& handle0_ref = handle0;
  const SocketHandle& handle1_ref = handle1;
  const SocketHandle& handle2_ref = handle2;

  waiter.Subscribe(&subscriber, std::cref(handle0_ref));
  waiter.Subscribe(&subscriber, std::cref(handle1_ref));
  waiter.Subscribe(&subscriber, std::cref(handle2_ref));
  Error::Code response = Error::Code::kAgain;
  EXPECT_CALL(subscriber, ProcessReadyHandle(_, _)).Times(0);
  EXPECT_CALL(waiter, AwaitSocketsReadable(_, _))
      .WillOnce(Return(ByMove(response)));
  waiter.ProcessHandles(Clock::duration{0});
}

TEST(SocketHandleWaiterTest, WatchedSocketsReturnedToCorrectSubscribers) {
  MockSubscriber subscriber;
  MockSubscriber subscriber2;
  TestingSocketHandleWaiter waiter;
  SocketHandle handle0{0};
  SocketHandle handle1{1};
  SocketHandle handle2{2};
  SocketHandle handle3{3};
  const SocketHandle& handle0_ref = handle0;
  const SocketHandle& handle1_ref = handle1;
  const SocketHandle& handle2_ref = handle2;
  const SocketHandle& handle3_ref = handle3;

  waiter.Subscribe(&subscriber, std::cref(handle0_ref));
  waiter.Subscribe(&subscriber, std::cref(handle2_ref));
  waiter.Subscribe(&subscriber2, std::cref(handle1_ref));
  waiter.Subscribe(&subscriber2, std::cref(handle3_ref));
  constexpr uint32_t r_flags = SocketHandleWaiter::Flags::kReadable;
  constexpr uint32_t w_flags = SocketHandleWaiter::Flags::kWriteable;
  constexpr uint32_t rw_flags = SocketHandleWaiter::Flags::kReadable |
                                SocketHandleWaiter::Flags::kWriteable;
  EXPECT_CALL(subscriber, ProcessReadyHandle(std::cref(handle0_ref), r_flags))
      .Times(1);
  EXPECT_CALL(subscriber, ProcessReadyHandle(std::cref(handle2_ref), w_flags))
      .Times(1);
  EXPECT_CALL(subscriber2, ProcessReadyHandle(std::cref(handle1_ref), r_flags))
      .Times(1);
  EXPECT_CALL(subscriber2, ProcessReadyHandle(std::cref(handle3_ref), rw_flags))
      .Times(1);
  EXPECT_CALL(waiter, AwaitSocketsReadable(_, _))
      .WillOnce(
          Return(ByMove(std::vector<TestingSocketHandleWaiter::ReadyHandle>{
              {std::cref(handle0_ref), r_flags},
              {std::cref(handle1_ref), r_flags},
              {std::cref(handle2_ref), w_flags},
              {std::cref(handle3_ref), rw_flags}})));
  waiter.ProcessHandles(Clock::duration{0});
}

}  // namespace openscreen