Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Utilities/cmcppdap/src/socket_test.cpp
3153 views
1
// Copyright 2019 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include "socket.h"
16
17
#include "gmock/gmock.h"
18
#include "gtest/gtest.h"
19
20
#include <chrono>
21
#include <thread>
22
#include <vector>
23
24
// Basic socket send & receive test
25
TEST(Socket, SendRecv) {
26
const char* port = "19021";
27
28
auto server = dap::Socket("localhost", port);
29
30
auto client = dap::Socket::connect("localhost", port, 0);
31
ASSERT_TRUE(client != nullptr);
32
33
const std::string expect = "Hello World!";
34
std::string read;
35
36
auto thread = std::thread([&] {
37
auto conn = server.accept();
38
ASSERT_TRUE(conn != nullptr);
39
char c;
40
while (conn->read(&c, 1) != 0) {
41
read += c;
42
}
43
});
44
45
ASSERT_TRUE(client->write(expect.data(), expect.size()));
46
47
client->close();
48
thread.join();
49
50
ASSERT_EQ(read, expect);
51
}
52
53
// See https://github.com/google/cppdap/issues/37
54
TEST(Socket, CloseOnDifferentThread) {
55
const char* port = "19021";
56
57
auto server = dap::Socket("localhost", port);
58
59
auto client = dap::Socket::connect("localhost", port, 0);
60
ASSERT_TRUE(client != nullptr);
61
62
auto conn = server.accept();
63
64
auto thread = std::thread([&] {
65
// Closing client on different thread should unblock client->read().
66
client->close();
67
});
68
69
char c;
70
while (client->read(&c, 1) != 0) {
71
}
72
73
thread.join();
74
}
75
76
TEST(Socket, ConnectTimeout) {
77
const char* port = "19021";
78
const int timeoutMillis = 200;
79
const int maxAttempts = 1024;
80
81
using namespace std::chrono;
82
83
auto server = dap::Socket("localhost", port);
84
85
std::vector<std::shared_ptr<dap::ReaderWriter>> connections;
86
87
for (int i = 0; i < maxAttempts; i++) {
88
auto start = system_clock::now();
89
auto connection = dap::Socket::connect("localhost", port, timeoutMillis);
90
auto end = system_clock::now();
91
92
if (!connection) {
93
auto timeTakenMillis = duration_cast<milliseconds>(end - start).count();
94
ASSERT_GE(timeTakenMillis + 20, // +20ms for a bit of timing wiggle room
95
timeoutMillis);
96
return;
97
}
98
99
// Keep hold of the connections to saturate any incoming socket buffers.
100
connections.emplace_back(std::move(connection));
101
}
102
103
FAIL() << "Failed to test timeout after " << maxAttempts << " attempts";
104
}
105
106