forked from rsocket/rsocket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestResponseTest.cpp
More file actions
200 lines (176 loc) · 6.82 KB
/
RequestResponseTest.cpp
File metadata and controls
200 lines (176 loc) · 6.82 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2004-present Facebook. All Rights Reserved.
#include <folly/io/async/ScopedEventBaseThread.h>
#include <folly/synchronization/Baton.h>
#include <gtest/gtest.h>
#include <thread>
#include "RSocketTests.h"
#include "rsocket/test/test_utils/GenericRequestResponseHandler.h"
#include "yarpl/Single.h"
#include "yarpl/single/SingleTestObserver.h"
using namespace yarpl;
using namespace yarpl::single;
using namespace rsocket;
using namespace rsocket::tests;
using namespace rsocket::tests::client_server;
namespace {
class TestHandlerCancel : public rsocket::RSocketResponder {
public:
TestHandlerCancel(
std::shared_ptr<folly::Baton<>> onCancel,
std::shared_ptr<folly::Baton<>> onSubscribe)
: onCancel_(std::move(onCancel)), onSubscribe_(std::move(onSubscribe)) {}
Reference<Single<Payload>> handleRequestResponse(Payload request, StreamId)
override {
// used to signal to the client when the subscribe is received
onSubscribe_->post();
// used to block this responder thread until a cancel is sent from client
// over network
auto cancelFromClient = std::make_shared<folly::Baton<>>();
// used to signal to the client once we receive a cancel
auto onCancel = onCancel_;
auto requestString = request.moveDataToString();
return Single<Payload>::create(
[ name = std::move(requestString), cancelFromClient, onCancel ](
auto subscriber) mutable {
std::thread([
subscriber = std::move(subscriber),
name = std::move(name),
cancelFromClient,
onCancel
]() {
auto subscription = SingleSubscriptions::create(
[cancelFromClient] { cancelFromClient->post(); });
subscriber->onSubscribe(subscription);
// simulate slow processing or IO being done
// and block this current background thread
// until we are cancelled
cancelFromClient->wait();
if (subscription->isCancelled()) {
// this is used by the unit test to assert the cancel was
// received
onCancel->post();
} else {
// if not cancelled would do work and emit here
}
})
.detach();
});
}
private:
std::shared_ptr<folly::Baton<>> onCancel_;
std::shared_ptr<folly::Baton<>> onSubscribe_;
};
} // namespace
TEST(RequestResponseTest, Cancel) {
folly::ScopedEventBaseThread worker;
auto onCancel = std::make_shared<folly::Baton<>>();
auto onSubscribe = std::make_shared<folly::Baton<>>();
auto server =
makeServer(std::make_shared<TestHandlerCancel>(onCancel, onSubscribe));
auto client = makeClient(worker.getEventBase(), *server->listeningPort());
auto requester = client->getRequester();
auto to = SingleTestObserver<std::string>::create();
requester->requestResponse(Payload("Jane"))
->map([](auto p) { return p.moveDataToString(); })
->subscribe(to);
// NOTE: wait for server to receive request/subscribe
// otherwise the cancellation will all happen locally
onSubscribe->wait();
// now cancel the local subscription
to->cancel();
// wait for cancel to propagate to server
onCancel->wait();
// assert no signals received on client
to->assertNoTerminalEvent();
}
// response creation usage
TEST(RequestResponseTest, CanCtorTypes) {
Response r1 = payload_response("foo", "bar");
Response r2 = error_response(std::runtime_error("whew!"));
}
TEST(RequestResponseTest, Hello) {
folly::ScopedEventBaseThread worker;
auto server = makeServer(std::make_shared<GenericRequestResponseHandler>(
[](StringPair const& request) {
return payload_response(
"Hello, " + request.first + " " + request.second + "!", ":)");
}));
auto client = makeClient(worker.getEventBase(), *server->listeningPort());
auto requester = client->getRequester();
auto to = SingleTestObserver<StringPair>::create();
requester->requestResponse(Payload("Jane", "Doe"))
->map(payload_to_stringpair)
->subscribe(to);
to->awaitTerminalEvent();
to->assertOnSuccessValue({"Hello, Jane Doe!", ":)"});
}
TEST(RequestResponseTest, FailureInResponse) {
folly::ScopedEventBaseThread worker;
auto server = makeServer(std::make_shared<GenericRequestResponseHandler>(
[](StringPair const& request) {
EXPECT_EQ(request.first, "foo");
EXPECT_EQ(request.second, "bar");
return error_response(std::runtime_error("whew!"));
}));
auto client = makeClient(worker.getEventBase(), *server->listeningPort());
auto requester = client->getRequester();
auto to = SingleTestObserver<StringPair>::create();
requester->requestResponse(Payload("foo", "bar"))
->map(payload_to_stringpair)
->subscribe(to);
to->awaitTerminalEvent();
to->assertOnErrorMessage("whew!");
}
TEST(RequestResponseTest, RequestOnDisconnectedClient) {
folly::ScopedEventBaseThread worker;
auto client = makeDisconnectedClient(worker.getEventBase());
auto requester = client->getRequester();
bool did_call_on_error = false;
folly::Baton<> wait_for_on_error;
requester->requestResponse(Payload("foo", "bar"))
->subscribe(
[](auto) {
// should not call onSuccess
FAIL();
},
[&](folly::exception_wrapper) {
did_call_on_error = true;
wait_for_on_error.post();
});
wait_for_on_error.timed_wait(std::chrono::milliseconds(100));
ASSERT(did_call_on_error);
}
// TODO: test that multiple requests on a requestResponse
// fail in a well-defined way (right now it'd nullptr deref)
TEST(RequestResponseTest, MultipleRequestsError) {
folly::ScopedEventBaseThread worker;
auto server = makeServer(std::make_shared<GenericRequestResponseHandler>(
[](StringPair const& request) {
EXPECT_EQ(request.first, "foo");
EXPECT_EQ(request.second, "bar");
return payload_response("baz", "quix");
}));
auto client = makeClient(worker.getEventBase(), *server->listeningPort());
auto requester = client->getRequester();
auto flowable = requester->requestResponse(Payload("foo", "bar"));
}
TEST(RequestResponseTest, FailureOnRequest) {
folly::ScopedEventBaseThread worker;
auto server = makeServer(
std::make_shared<GenericRequestResponseHandler>([](auto const&) {
ADD_FAILURE();
return payload_response("", "");
}));
auto client = makeClient(worker.getEventBase(), *server->listeningPort());
auto requester = client->getRequester();
VLOG(0) << "Shutting down server so client request fails";
server->shutdownAndWait();
server.reset();
VLOG(0) << "Done";
auto to = SingleTestObserver<StringPair>::create();
requester->requestResponse(Payload("foo", "bar"))
->map(payload_to_stringpair)
->subscribe(to);
to->awaitTerminalEvent();
EXPECT_TRUE(to->getError());
}