@@ -39,22 +39,37 @@ template <typename T> class JSONTransportTest : public PipePairTest {
39
39
}
40
40
41
41
template <typename P>
42
- void
43
- RunOnce (std::function<void (llvm::Expected<P>)> callback,
44
- std::chrono::milliseconds timeout = std::chrono::milliseconds(100 )) {
42
+ Expected<P>
43
+ RunOnce (std::chrono::milliseconds timeout = std::chrono::seconds(1 )) {
44
+ std::promise<Expected<P>> promised_message;
45
+ RunUntil<P>(
46
+ [&](Expected<P> message) {
47
+ promised_message.set_value (std::move (message));
48
+ return /* keep_going*/ false ;
49
+ },
50
+ timeout);
51
+ return promised_message.get_future ().get ();
52
+ }
53
+
54
+ // / RunUntil runs the event loop until the callback returns `false` or a
55
+ // / timeout has occured.
56
+ template <typename P>
57
+ void RunUntil (std::function<bool (Expected<P>)> callback,
58
+ std::chrono::milliseconds timeout = std::chrono::seconds(1 )) {
45
59
auto handle = transport->RegisterReadObject <P>(
46
60
loop, [&](MainLoopBase &loop, llvm::Expected<P> message) {
47
- callback (std::move (message));
48
- loop.RequestTermination ();
61
+ bool keep_going = callback (std::move (message));
62
+ if (!keep_going)
63
+ loop.RequestTermination ();
49
64
});
50
65
loop.AddCallback (
51
66
[&](MainLoopBase &loop) {
52
67
loop.RequestTermination ();
53
- FAIL () << " timeout waiting for read callback " ;
68
+ callback ( createStringError ( " timeout" )) ;
54
69
},
55
70
timeout);
56
- ASSERT_THAT_EXPECTED (handle, Succeeded ());
57
- ASSERT_THAT_ERROR (loop.Run ().takeError (), Succeeded ());
71
+ EXPECT_THAT_EXPECTED (handle, Succeeded ());
72
+ EXPECT_THAT_ERROR (loop.Run ().takeError (), Succeeded ());
58
73
}
59
74
};
60
75
@@ -89,10 +104,8 @@ TEST_F(HTTPDelimitedJSONTransportTest, MalformedRequests) {
89
104
ASSERT_THAT_EXPECTED (
90
105
input.Write (malformed_header.data (), malformed_header.size ()),
91
106
Succeeded ());
92
- RunOnce<JSONTestType>([&](llvm::Expected<JSONTestType> message) {
93
- ASSERT_THAT_EXPECTED (message,
94
- FailedWithMessage (" invalid content length: -1" ));
95
- });
107
+ ASSERT_THAT_EXPECTED (RunOnce<JSONTestType>(),
108
+ FailedWithMessage (" invalid content length: -1" ));
96
109
}
97
110
98
111
TEST_F (HTTPDelimitedJSONTransportTest, Read) {
@@ -103,8 +116,32 @@ TEST_F(HTTPDelimitedJSONTransportTest, Read) {
103
116
.str ();
104
117
ASSERT_THAT_EXPECTED (input.Write (message.data (), message.size ()),
105
118
Succeeded ());
106
- RunOnce<JSONTestType>([&](llvm::Expected<JSONTestType> message) {
107
- ASSERT_THAT_EXPECTED (message, HasValue (testing::FieldsAre (/* str=*/ " foo" )));
119
+ ASSERT_THAT_EXPECTED (RunOnce<JSONTestType>(),
120
+ HasValue (testing::FieldsAre (/* str=*/ " foo" )));
121
+ }
122
+
123
+ TEST_F (HTTPDelimitedJSONTransportTest, ReadMultipleMessages) {
124
+ std::string json1 = R"json( {"str": "one"})json" ;
125
+ std::string json2 = R"json( {"str": "two"})json" ;
126
+ std::string message = formatv (" Content-Length: {0}\r\n Content-type: "
127
+ " text/json\r\n\r\n {1}Content-Length: "
128
+ " {2}\r\n Content-type: text/json\r\n\r\n {3}" ,
129
+ json1.size (), json1, json2.size (), json2)
130
+ .str ();
131
+ ASSERT_THAT_EXPECTED (input.Write (message.data (), message.size ()),
132
+ Succeeded ());
133
+ unsigned count = 0 ;
134
+ RunUntil<JSONTestType>([&](Expected<JSONTestType> message) -> bool {
135
+ if (count == 0 ) {
136
+ EXPECT_THAT_EXPECTED (message,
137
+ HasValue (testing::FieldsAre (/* str=*/ " one" )));
138
+ } else if (count == 1 ) {
139
+ EXPECT_THAT_EXPECTED (message,
140
+ HasValue (testing::FieldsAre (/* str=*/ " two" )));
141
+ }
142
+
143
+ count++;
144
+ return count < 2 ;
108
145
});
109
146
}
110
147
@@ -115,10 +152,8 @@ TEST_F(HTTPDelimitedJSONTransportTest, ReadAcrossMultipleChunks) {
115
152
formatv (" Content-Length: {0}\r\n\r\n {1}" , json.size (), json).str ();
116
153
ASSERT_THAT_EXPECTED (input.Write (message.data (), message.size ()),
117
154
Succeeded ());
118
- RunOnce<JSONTestType>([&](llvm::Expected<JSONTestType> message) {
119
- ASSERT_THAT_EXPECTED (message,
120
- HasValue (testing::FieldsAre (/* str=*/ long_str)));
121
- });
155
+ ASSERT_THAT_EXPECTED (RunOnce<JSONTestType>(),
156
+ HasValue (testing::FieldsAre (/* str=*/ long_str)));
122
157
}
123
158
124
159
TEST_F (HTTPDelimitedJSONTransportTest, ReadPartialMessage) {
@@ -134,16 +169,13 @@ TEST_F(HTTPDelimitedJSONTransportTest, ReadPartialMessage) {
134
169
ASSERT_THAT_EXPECTED (input.Write (part2.data (), part2.size ()), Succeeded ());
135
170
});
136
171
137
- RunOnce<JSONTestType>([&](llvm::Expected<JSONTestType> message) {
138
- ASSERT_THAT_EXPECTED (message, HasValue (testing::FieldsAre (/* str=*/ " foo" )));
139
- });
172
+ ASSERT_THAT_EXPECTED (RunOnce<JSONTestType>(),
173
+ HasValue (testing::FieldsAre (/* str=*/ " foo" )));
140
174
}
141
175
142
176
TEST_F (HTTPDelimitedJSONTransportTest, ReadWithEOF) {
143
177
input.CloseWriteFileDescriptor ();
144
- RunOnce<JSONTestType>([&](llvm::Expected<JSONTestType> message) {
145
- ASSERT_THAT_EXPECTED (message, Failed<TransportEOFError>());
146
- });
178
+ ASSERT_THAT_EXPECTED (RunOnce<JSONTestType>(), Failed<TransportEOFError>());
147
179
}
148
180
149
181
TEST_F (HTTPDelimitedJSONTransportTest, ReaderWithUnhandledData) {
@@ -156,9 +188,8 @@ TEST_F(HTTPDelimitedJSONTransportTest, ReaderWithUnhandledData) {
156
188
ASSERT_THAT_EXPECTED (input.Write (message.data (), message.size () - 1 ),
157
189
Succeeded ());
158
190
input.CloseWriteFileDescriptor ();
159
- RunOnce<JSONTestType>([&](llvm::Expected<JSONTestType> message) {
160
- ASSERT_THAT_EXPECTED (message, Failed<TransportUnhandledContentsError>());
161
- });
191
+ ASSERT_THAT_EXPECTED (RunOnce<JSONTestType>(),
192
+ Failed<TransportUnhandledContentsError>());
162
193
}
163
194
164
195
TEST_F (HTTPDelimitedJSONTransportTest, InvalidTransport) {
@@ -184,18 +215,16 @@ TEST_F(JSONRPCTransportTest, MalformedRequests) {
184
215
ASSERT_THAT_EXPECTED (
185
216
input.Write (malformed_header.data (), malformed_header.size ()),
186
217
Succeeded ());
187
- RunOnce<JSONTestType>(
188
- [&](auto message) { ASSERT_THAT_EXPECTED (message, llvm::Failed ()); });
218
+ ASSERT_THAT_EXPECTED (RunOnce<JSONTestType>(), llvm::Failed ());
189
219
}
190
220
191
221
TEST_F (JSONRPCTransportTest, Read) {
192
222
std::string json = R"json( {"str": "foo"})json" ;
193
223
std::string message = formatv (" {0}\n " , json).str ();
194
224
ASSERT_THAT_EXPECTED (input.Write (message.data (), message.size ()),
195
225
Succeeded ());
196
- RunOnce<JSONTestType>([&](auto message) {
197
- ASSERT_THAT_EXPECTED (message, HasValue (testing::FieldsAre (/* str=*/ " foo" )));
198
- });
226
+ ASSERT_THAT_EXPECTED (RunOnce<JSONTestType>(),
227
+ HasValue (testing::FieldsAre (/* str=*/ " foo" )));
199
228
}
200
229
201
230
TEST_F (JSONRPCTransportTest, ReadAcrossMultipleChunks) {
@@ -204,10 +233,8 @@ TEST_F(JSONRPCTransportTest, ReadAcrossMultipleChunks) {
204
233
std::string message = formatv (" {0}\n " , json).str ();
205
234
ASSERT_THAT_EXPECTED (input.Write (message.data (), message.size ()),
206
235
Succeeded ());
207
- RunOnce<JSONTestType>([&](llvm::Expected<JSONTestType> message) {
208
- ASSERT_THAT_EXPECTED (message,
209
- HasValue (testing::FieldsAre (/* str=*/ long_str)));
210
- });
236
+ ASSERT_THAT_EXPECTED (RunOnce<JSONTestType>(),
237
+ HasValue (testing::FieldsAre (/* str=*/ long_str)));
211
238
}
212
239
213
240
TEST_F (JSONRPCTransportTest, ReadPartialMessage) {
@@ -222,16 +249,13 @@ TEST_F(JSONRPCTransportTest, ReadPartialMessage) {
222
249
ASSERT_THAT_EXPECTED (input.Write (part2.data (), part2.size ()), Succeeded ());
223
250
});
224
251
225
- RunOnce<JSONTestType>([&](llvm::Expected<JSONTestType> message) {
226
- ASSERT_THAT_EXPECTED (message, HasValue (testing::FieldsAre (/* str=*/ " foo" )));
227
- });
252
+ ASSERT_THAT_EXPECTED (RunOnce<JSONTestType>(),
253
+ HasValue (testing::FieldsAre (/* str=*/ " foo" )));
228
254
}
229
255
230
256
TEST_F (JSONRPCTransportTest, ReadWithEOF) {
231
257
input.CloseWriteFileDescriptor ();
232
- RunOnce<JSONTestType>([&](llvm::Expected<JSONTestType> message) {
233
- ASSERT_THAT_EXPECTED (message, Failed<TransportEOFError>());
234
- });
258
+ ASSERT_THAT_EXPECTED (RunOnce<JSONTestType>(), Failed<TransportEOFError>());
235
259
}
236
260
237
261
TEST_F (JSONRPCTransportTest, ReaderWithUnhandledData) {
@@ -241,9 +265,8 @@ TEST_F(JSONRPCTransportTest, ReaderWithUnhandledData) {
241
265
ASSERT_THAT_EXPECTED (input.Write (message.data (), message.size () - 1 ),
242
266
Succeeded ());
243
267
input.CloseWriteFileDescriptor ();
244
- RunOnce<JSONTestType>([&](llvm::Expected<JSONTestType> message) {
245
- ASSERT_THAT_EXPECTED (message, Failed<TransportUnhandledContentsError>());
246
- });
268
+ ASSERT_THAT_EXPECTED (RunOnce<JSONTestType>(),
269
+ Failed<TransportUnhandledContentsError>());
247
270
}
248
271
249
272
TEST_F (JSONRPCTransportTest, Write) {
0 commit comments