forked from rsocket/rsocket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSocketClientServerTest.cpp
More file actions
139 lines (117 loc) · 4.94 KB
/
RSocketClientServerTest.cpp
File metadata and controls
139 lines (117 loc) · 4.94 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) Facebook, Inc. and its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "RSocketTests.h"
#include <folly/Random.h>
#include <folly/io/async/ScopedEventBaseThread.h>
#include <gtest/gtest.h>
#include "rsocket/test/handlers/HelloStreamRequestHandler.h"
using namespace rsocket;
using namespace rsocket::tests;
using namespace rsocket::tests::client_server;
TEST(RSocketClientServer, StartAndShutdown) {
folly::ScopedEventBaseThread worker;
auto server = makeServer(std::make_shared<HelloStreamRequestHandler>());
auto client = makeClient(worker.getEventBase(), *server->listeningPort());
}
TEST(RSocketClientServer, ConnectOne) {
folly::ScopedEventBaseThread worker;
auto server = makeServer(std::make_shared<HelloStreamRequestHandler>());
auto client = makeClient(worker.getEventBase(), *server->listeningPort());
}
TEST(RSocketClientServer, ConnectManySync) {
folly::ScopedEventBaseThread worker;
auto server = makeServer(std::make_shared<HelloStreamRequestHandler>());
for (size_t i = 0; i < 100; ++i) {
auto client = makeClient(worker.getEventBase(), *server->listeningPort());
}
}
TEST(RSocketClientServer, ConnectManyAsync) {
auto server = makeServer(std::make_shared<HelloStreamRequestHandler>());
constexpr size_t connectionCount = 100;
constexpr size_t workerCount = 10;
std::vector<folly::ScopedEventBaseThread> workers(workerCount);
std::vector<folly::Future<std::shared_ptr<RSocketClient>>> clients;
std::atomic<int> executed{0};
for (size_t i = 0; i < connectionCount; ++i) {
int workerId = folly::Random::rand32(workerCount);
auto clientFuture =
makeClientAsync(
workers[workerId].getEventBase(), *server->listeningPort())
.thenValue(
[&executed](std::shared_ptr<rsocket::RSocketClient> client) {
++executed;
return client;
})
.thenError([&](folly::exception_wrapper ex) {
LOG(ERROR) << "error: " << ex.what();
++executed;
return std::shared_ptr<RSocketClient>(nullptr);
});
clients.emplace_back(std::move(clientFuture));
}
CHECK_EQ(clients.size(), connectionCount);
auto results =
folly::collectAllSemiFuture(clients).get(std::chrono::minutes{1});
CHECK_EQ(results.size(), connectionCount);
results.clear();
clients.clear();
CHECK_EQ(executed, connectionCount);
workers.clear();
}
TEST(RSocketClientServer, ConnectOnDifferentEvb) {
folly::ScopedEventBaseThread transportWorker{"transportWorker"};
folly::ScopedEventBaseThread stateMachineWorker{"stateMachineWorker"};
auto server = makeServer(std::make_shared<HelloStreamRequestHandler>());
auto client = makeClient(
transportWorker.getEventBase(),
*server->listeningPort(),
stateMachineWorker.getEventBase());
}
/// Test destroying a client with an open connection on the same worker thread
/// as that connection.
TEST(RSocketClientServer, ClientClosesOnWorker) {
folly::ScopedEventBaseThread worker;
auto server = makeServer(std::make_shared<HelloStreamRequestHandler>());
auto client = makeClient(worker.getEventBase(), *server->listeningPort());
// Move the client to the worker thread.
worker.getEventBase()->runInEventBaseThread([c = std::move(client)] {});
}
/// Test that sending garbage to the server doesn't crash it.
TEST(RSocketClientServer, ServerGetsGarbage) {
auto server = makeServer(std::make_shared<HelloStreamRequestHandler>());
folly::SocketAddress address{"127.0.0.1", *server->listeningPort()};
folly::ScopedEventBaseThread worker;
auto factory =
std::make_shared<TcpConnectionFactory>(*worker.getEventBase(), address);
auto result =
factory->connect(ProtocolVersion::Latest, ResumeStatus::NEW_SESSION)
.get();
auto connection = std::move(result.connection);
auto evb = &result.eventBase;
evb->runInEventBaseThreadAndWait([conn = std::move(connection)]() mutable {
conn->send(folly::IOBuf::copyBuffer("ABCDEFGHIJKLMNOP"));
conn.reset();
});
}
/// Test closing a server with a bunch of open connections.
TEST(RSocketClientServer, CloseServerWithConnections) {
folly::ScopedEventBaseThread worker;
auto server = makeServer(std::make_shared<HelloStreamRequestHandler>());
std::vector<std::shared_ptr<RSocketClient>> clients;
for (size_t i = 0; i < 100; ++i) {
clients.push_back(
makeClient(worker.getEventBase(), *server->listeningPort()));
}
server.reset();
}