@@ -176,7 +176,7 @@ TEST(Channel, ConcurrentSendNonConcurrentReceiveWithSufficientBufferSize) {
176
176
sum += i;
177
177
}
178
178
});
179
- std::this_thread::sleep_for (std::chrono::milliseconds (100 )); // wait 0.5 sec
179
+ std::this_thread::sleep_for (std::chrono::milliseconds (100 )); // wait 0.1 sec
180
180
EXPECT_EQ (sum, 45U );
181
181
182
182
CloseChannel (ch);
@@ -194,10 +194,7 @@ TEST(Channel, RecevingOrderEqualToSendingOrderWithBufferedChannel) {
194
194
RecevingOrderEqualToSendingOrder (ch);
195
195
}
196
196
197
- // This tests that closing a buffered channel also unblocks
198
- // any receivers waiting on the channel
199
- TEST (Channel, BufferedChannelCloseUnblocksReceiversTest) {
200
- auto ch = MakeChannel<int >(1 );
197
+ void ChannelCloseUnblocksReceiversTest (Channel<int > *ch) {
201
198
size_t num_threads = 5 ;
202
199
std::thread t[num_threads];
203
200
bool thread_ended[num_threads];
@@ -208,15 +205,14 @@ TEST(Channel, BufferedChannelCloseUnblocksReceiversTest) {
208
205
t[i] = std::thread (
209
206
[&](bool *p) {
210
207
int data;
211
- // All reads should return false
212
208
EXPECT_EQ (ch->Receive (&data), false );
213
209
*p = true ;
214
210
},
215
211
&thread_ended[i]);
216
212
}
217
- std::this_thread::sleep_for (std::chrono::milliseconds (100 )); // wait
213
+ std::this_thread::sleep_for (std::chrono::milliseconds (100 )); // wait 0.1 sec
218
214
219
- // Verify that all threads are blocked
215
+ // Verify that all the threads are blocked
220
216
for (size_t i = 0 ; i < num_threads; i++) {
221
217
EXPECT_EQ (thread_ended[i], false );
222
218
}
@@ -225,21 +221,20 @@ TEST(Channel, BufferedChannelCloseUnblocksReceiversTest) {
225
221
// This should unblock all receivers
226
222
CloseChannel (ch);
227
223
228
- std::this_thread::sleep_for (std::chrono::milliseconds (200 )); // wait
224
+ std::this_thread::sleep_for (std::chrono::milliseconds (100 )); // wait 0.1 sec
229
225
230
226
// Verify that all threads got unblocked
231
227
for (size_t i = 0 ; i < num_threads; i++) {
232
228
EXPECT_EQ (thread_ended[i], true );
233
229
}
234
230
235
231
for (size_t i = 0 ; i < num_threads; i++) t[i].join ();
236
- delete ch;
237
232
}
238
233
239
- // This tests that closing a buffered channel also unblocks
240
- // any senders waiting for channel to have write space
241
- TEST (Channel, BufferedChannelCloseUnblocksSendersTest) {
242
- auto ch = MakeChannel< int >( 1 );
234
+ void ChannelCloseUnblocksSendersTest (Channel< int > *ch) {
235
+ using paddle::framework::details::Buffered;
236
+ using paddle::framework::details::UnBuffered;
237
+
243
238
size_t num_threads = 5 ;
244
239
std::thread t[num_threads];
245
240
bool thread_ended[num_threads];
@@ -259,116 +254,72 @@ TEST(Channel, BufferedChannelCloseUnblocksSendersTest) {
259
254
}
260
255
std::this_thread::sleep_for (std::chrono::milliseconds (100 )); // wait
261
256
262
- // Verify that atleast 4 threads are blocked
263
- int ct = 0 ;
264
- for (size_t i = 0 ; i < num_threads; i++) {
265
- if (thread_ended[i] == false ) ct++;
257
+ if (dynamic_cast <Buffered<int > *>(ch)) {
258
+ // If ch is Buffered, atleast 4 threads must be blocked.
259
+ int ct = 0 ;
260
+ for (size_t i = 0 ; i < num_threads; i++) {
261
+ if (!thread_ended[i]) ct++;
262
+ }
263
+ EXPECT_GE (ct, 4 );
264
+ } else {
265
+ // If ch is UnBuffered, all the threads should be blocked.
266
+ for (size_t i = 0 ; i < num_threads; i++) {
267
+ EXPECT_EQ (thread_ended[i], false );
268
+ }
266
269
}
267
- // Atleast 4 threads must be blocked
268
- EXPECT_GE (ct, 4 );
269
-
270
270
// Explicitly close the thread
271
271
// This should unblock all senders
272
272
CloseChannel (ch);
273
273
274
- std::this_thread::sleep_for (std::chrono::milliseconds (200 )); // wait
274
+ std::this_thread::sleep_for (std::chrono::milliseconds (100 )); // wait
275
275
276
276
// Verify that all threads got unblocked
277
277
for (size_t i = 0 ; i < num_threads; i++) {
278
278
EXPECT_EQ (thread_ended[i], true );
279
279
}
280
280
281
- // Verify that only 1 send was successful
282
- ct = 0 ;
283
- for (size_t i = 0 ; i < num_threads; i++) {
284
- if (send_success[i]) ct++;
281
+ if (dynamic_cast <Buffered<int > *>(ch)) {
282
+ // Verify that only 1 send was successful
283
+ int ct = 0 ;
284
+ for (size_t i = 0 ; i < num_threads; i++) {
285
+ if (send_success[i]) ct++;
286
+ }
287
+ // Only 1 send must be successful
288
+ EXPECT_EQ (ct, 1 );
285
289
}
286
- // Only 1 send must be successful
287
- EXPECT_EQ (ct, 1 );
288
290
289
291
for (size_t i = 0 ; i < num_threads; i++) t[i].join ();
292
+ }
293
+
294
+ // This tests that closing a buffered channel also unblocks
295
+ // any receivers waiting on the channel
296
+ TEST (Channel, BufferedChannelCloseUnblocksReceiversTest) {
297
+ auto ch = MakeChannel<int >(1 );
298
+ ChannelCloseUnblocksReceiversTest (ch);
299
+ delete ch;
300
+ }
301
+
302
+ // This tests that closing a buffered channel also unblocks
303
+ // any senders waiting for channel to have write space
304
+ TEST (Channel, BufferedChannelCloseUnblocksSendersTest) {
305
+ auto ch = MakeChannel<int >(1 );
306
+ ChannelCloseUnblocksSendersTest (ch);
290
307
delete ch;
291
308
}
292
309
293
310
// This tests that closing an unbuffered channel also unblocks
294
311
// unblocks any receivers waiting for senders
295
312
TEST (Channel, UnbufferedChannelCloseUnblocksReceiversTest) {
296
313
auto ch = MakeChannel<int >(0 );
297
- size_t num_threads = 5 ;
298
- std::thread t[num_threads];
299
- bool thread_ended[num_threads];
300
-
301
- // Launches threads that try to read and are blocked becausew of no writers
302
- for (size_t i = 0 ; i < num_threads; i++) {
303
- thread_ended[i] = false ;
304
- t[i] = std::thread (
305
- [&](bool *p) {
306
- int data;
307
- EXPECT_EQ (ch->Receive (&data), false );
308
- *p = true ;
309
- },
310
- &thread_ended[i]);
311
- }
312
- std::this_thread::sleep_for (std::chrono::milliseconds (500 )); // wait 0.5 sec
313
-
314
- // Verify that all the threads are blocked
315
- for (size_t i = 0 ; i < num_threads; i++) {
316
- EXPECT_EQ (thread_ended[i], false );
317
- }
318
-
319
- // Explicitly close the thread
320
- // This should unblock all receivers
321
- CloseChannel (ch);
322
-
323
- std::this_thread::sleep_for (std::chrono::milliseconds (500 )); // wait 0.5 sec
324
-
325
- // Verify that all threads got unblocked
326
- for (size_t i = 0 ; i < num_threads; i++) {
327
- EXPECT_EQ (thread_ended[i], true );
328
- }
329
-
330
- for (size_t i = 0 ; i < num_threads; i++) t[i].join ();
314
+ ChannelCloseUnblocksReceiversTest (ch);
331
315
delete ch;
332
316
}
333
317
334
318
// This tests that closing an unbuffered channel also unblocks
335
319
// unblocks any senders waiting for senders
336
320
TEST (Channel, UnbufferedChannelCloseUnblocksSendersTest) {
337
321
auto ch = MakeChannel<int >(0 );
338
- size_t num_threads = 5 ;
339
- std::thread t[num_threads];
340
- bool thread_ended[num_threads];
341
-
342
- // Launches threads that try to read and are blocked becausew of no writers
343
- for (size_t i = 0 ; i < num_threads; i++) {
344
- thread_ended[i] = false ;
345
- t[i] = std::thread (
346
- [&](bool *p) {
347
- int data = 10 ;
348
- EXPECT_EQ (ch->Send (&data), false );
349
- *p = true ;
350
- },
351
- &thread_ended[i]);
352
- }
353
- std::this_thread::sleep_for (std::chrono::milliseconds (500 )); // wait 0.5 sec
354
-
355
- // Verify that all the threads are blocked
356
- for (size_t i = 0 ; i < num_threads; i++) {
357
- EXPECT_EQ (thread_ended[i], false );
358
- }
359
-
360
- // Explicitly close the thread
361
- // This should unblock all receivers
362
- CloseChannel (ch);
363
-
364
- std::this_thread::sleep_for (std::chrono::milliseconds (500 )); // wait 0.5 sec
365
-
366
- // Verify that all threads got unblocked
367
- for (size_t i = 0 ; i < num_threads; i++) {
368
- EXPECT_EQ (thread_ended[i], true );
369
- }
370
-
371
- for (size_t i = 0 ; i < num_threads; i++) t[i].join ();
322
+ ChannelCloseUnblocksReceiversTest (ch);
372
323
delete ch;
373
324
}
374
325
0 commit comments