This repository has been archived by the owner on Dec 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
future_generic_test.cc
654 lines (567 loc) · 21.4 KB
/
future_generic_test.cc
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
// Copyright 2018 Google LLC
//
// 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 "google/cloud/future_generic.h"
#include "google/cloud/testing_util/chrono_literals.h"
#include "google/cloud/testing_util/expect_future_error.h"
#include <gmock/gmock.h>
namespace google {
namespace cloud {
inline namespace GOOGLE_CLOUD_CPP_NS {
namespace {
using ::testing::HasSubstr;
using namespace testing_util::chrono_literals;
using testing_util::ExpectFutureError;
/// @test Verify that destructing a promise does not introduce race conditions.
TEST(FutureTestInt, DestroyInWaitingThread) {
for (int i = 0; i != 1000; ++i) {
std::thread t;
{
promise<int> p;
t = std::thread([&p] { p.set_value(42); });
p.get_future().get();
}
if (t.joinable()) t.join();
}
}
/// @test Verify that destructing a promise does not introduce race conditions.
TEST(FutureTestInt, DestroyInSignalingThread) {
for (int i = 0; i != 1000; ++i) {
std::thread t;
{
promise<int> p;
future<int> f = p.get_future();
t = std::thread([](promise<int> p) { p.set_value(42); }, std::move(p));
f.get();
}
if (t.joinable()) t.join();
}
}
/// @test Verify conformance with section 30.6.5 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_5_3) {
// TODO(coryan) - allocators are not supported for now.
static_assert(!std::uses_allocator<promise<int>, std::allocator<int>>::value,
"promise<int> should use allocators if provided");
}
/// @test Verify conformance with section 30.6.5 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_5_4_default) {
promise<int> p0;
auto f0 = p0.get_future();
p0.set_value(42);
ASSERT_EQ(std::future_status::ready, f0.wait_for(0_ms));
f0.get();
SUCCEED();
}
/// @test Verify conformance with section 30.6.5 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_5_5) {
// promise<R> move constructor clears the shared state on the moved-from
// promise.
promise<int> p0;
promise<int> p1(std::move(p0));
auto f1 = p1.get_future();
p1.set_value(42);
ASSERT_EQ(std::future_status::ready, f1.wait_for(0_ms));
f1.get();
ExpectFutureError([&] { p0.set_value(42); }, std::future_errc::no_state);
}
/// @test Verify conformance with section 30.6.5 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_5_7) {
// promise<R> destructor abandons the shared state, the associated future
// becomes satisfied with an exception.
future<int> f0;
{
promise<int> p0;
f0 = p0.get_future();
ASSERT_NE(std::future_status::ready, f0.wait_for(0_ms));
EXPECT_TRUE(f0.valid());
}
EXPECT_TRUE(f0.valid());
ASSERT_EQ(std::future_status::ready, f0.wait_for(0_ms));
#if GOOGLE_CLOUD_CPP_HAVE_EXCEPTIONS
EXPECT_THROW(try { f0.get(); } catch (std::future_error const& ex) {
EXPECT_EQ(std::future_errc::broken_promise, ex.code());
throw;
},
std::future_error);
#else
EXPECT_DEATH_IF_SUPPORTED(
f0.get(),
"future<T>::get\\(\\) had an exception but exceptions are disabled");
#endif // GOOGLE_CLOUD_CPP_HAVE_EXCEPTIONS
}
/// @test Verify conformance with section 30.6.5 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_5_8) {
// promise<R> move assignment clears the shared state in the moved-from
// promise.
promise<int> p0;
promise<int> p1;
p1 = std::move(p0);
auto f1 = p1.get_future();
p1.set_value(42);
ASSERT_EQ(std::future_status::ready, f1.wait_for(0_ms));
f1.get();
ExpectFutureError([&] { p0.set_value(42); }, std::future_errc::no_state);
}
/// @test Verify conformance with section 30.6.5 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_5_10) {
// promise<R>::swap() actually swaps shared states.
promise<int> p0;
promise<int> p1;
p0.set_value(42);
p0.swap(p1);
auto f0 = p0.get_future();
auto f1 = p1.get_future();
ASSERT_NE(std::future_status::ready, f0.wait_for(0_ms));
ASSERT_EQ(std::future_status::ready, f1.wait_for(0_ms));
f1.get();
SUCCEED();
p0.set_value(42);
SUCCEED();
ASSERT_EQ(std::future_status::ready, f0.wait_for(0_ms));
}
/// @test Verify conformance with section 30.6.5 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_5_14_1) {
// promise<R>::get_future() raises if future was already retrieved.
promise<int> p0;
auto f0 = p0.get_future();
ExpectFutureError([&] { p0.get_future(); },
std::future_errc::future_already_retrieved);
}
/// @test Verify conformance with section 30.6.5 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_5_14_2) {
// promise<R>::get_future() raises if there is no shared state.
promise<int> p0;
promise<int> p1(std::move(p0));
ExpectFutureError([&] { p0.get_future(); }, std::future_errc::no_state);
}
/// @test Verify conformance with section 30.6.5 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_5_15) {
// promise<R>::set_value() stores the value in the shared state and makes it
// ready.
promise<int> p0;
auto f0 = p0.get_future();
ASSERT_NE(std::future_status::ready, f0.wait_for(0_ms));
p0.set_value(42);
ASSERT_EQ(std::future_status::ready, f0.wait_for(0_ms));
EXPECT_EQ(42, f0.get());
}
/// @test Verify conformance with section 30.6.5 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_5_16_1) {
// promise<R>::set_value() raises if there is a value in the shared state.
promise<int> p0;
p0.set_value(42);
ExpectFutureError([&] { p0.set_value(42); },
std::future_errc::promise_already_satisfied);
}
/// @test Verify conformance with section 30.6.5 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_5_17_2) {
// promise<R>::set_value() raises if there is no shared state.
promise<int> p0;
promise<int> p1(std::move(p0));
ExpectFutureError([&] { p0.set_value(42); }, std::future_errc::no_state);
}
/// @test Verify conformance with section 30.6.5 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_5_18) {
// promise<R>::set_exception() sets an exception and makes the shared state
// ready.
promise<int> p0;
auto f0 = p0.get_future();
ASSERT_NE(std::future_status::ready, f0.wait_for(0_ms));
p0.set_exception(std::make_exception_ptr(std::runtime_error("testing")));
ASSERT_EQ(std::future_status::ready, f0.wait_for(0_ms));
#if GOOGLE_CLOUD_CPP_HAVE_EXCEPTIONS
EXPECT_THROW(try { f0.get(); } catch (std::runtime_error const& ex) {
EXPECT_EQ(std::string("testing"), ex.what());
throw;
},
std::runtime_error);
#else
EXPECT_DEATH_IF_SUPPORTED(
f0.get(),
"future<T>::get\\(\\) had an exception but exceptions are disabled");
#endif // GOOGLE_CLOUD_CPP_HAVE_EXCEPTIONS
}
/// @test Verify conformance with section 30.6.5 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_5_20_1_value) {
// promise<R>::set_exception() raises if the shared state is already storing
// a value.
promise<int> p0;
p0.set_value(42);
ExpectFutureError(
[&] {
p0.set_exception(
std::make_exception_ptr(std::runtime_error("testing")));
},
std::future_errc::promise_already_satisfied);
}
/// @test Verify conformance with section 30.6.5 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_5_20_1_exception) {
// promise<R>::set_exception() raises if the shared state is already storing
// an exception.
promise<int> p0;
p0.set_exception(std::make_exception_ptr(std::runtime_error("original ex")));
ExpectFutureError(
[&] {
p0.set_exception(
std::make_exception_ptr(std::runtime_error("testing")));
},
std::future_errc::promise_already_satisfied);
}
/// @test Verify conformance with section 30.6.5 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_5_20_2) {
// promise<R>::set_exception() raises if the promise does not have a shared
// state.
promise<int> p0;
promise<int> p1(std::move(p0));
ExpectFutureError(
[&] {
p0.set_exception( // NOLINT
std::make_exception_ptr(std::runtime_error("testing")));
},
std::future_errc::no_state);
}
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_3_a) {
// Calling get() on a future with `valid() == false` is undefined, but the
// implementation is encouraged to raise `future_error` with an error code
// of `future_errc::no_state`
future<int> f;
EXPECT_FALSE(f.valid());
ExpectFutureError([&] { f.get(); }, std::future_errc::no_state);
}
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_3_b) {
// Calling wait() on a future with `valid() == false` is undefined, but the
// implementation is encouraged to raise `future_error` with an error code
// of `future_errc::no_state`
future<int> f;
EXPECT_FALSE(f.valid());
ExpectFutureError([&] { f.wait(); }, std::future_errc::no_state);
}
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_3_c) {
// Calling wait_for() on a future with `valid() == false` is undefined, but
// the implementation is encouraged to raise `future_error` with an error code
// of `future_errc::no_state`
future<int> f;
EXPECT_FALSE(f.valid());
ExpectFutureError([&] { f.wait_for(3_ms); }, std::future_errc::no_state);
}
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_3_d) {
// Calling wait_until() on a future with `valid() == false` is undefined, but
// the implementation is encouraged to raise `future_error` with an error code
// of `future_errc::no_state`
future<int> f;
EXPECT_FALSE(f.valid());
ExpectFutureError(
[&] { f.wait_until(std::chrono::system_clock::now() + 3_ms); },
std::future_errc::no_state);
}
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_5) {
// future<int>::future() constructs an empty future with no shared state.
future<int> f;
EXPECT_FALSE(f.valid());
}
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_7) {
// future<int> move constructor is `noexcept`.
future<int> f0;
EXPECT_TRUE(noexcept(future<int>(std::move(f0))));
}
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_8_a) {
// future<int> move constructor transfers futures with valid state.
promise<int> p;
future<int> f0 = p.get_future();
EXPECT_TRUE(f0.valid());
future<int> f1(std::move(f0));
EXPECT_FALSE(f0.valid());
EXPECT_TRUE(f1.valid());
}
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_8_b) {
// future<int> move constructor transfers futures with no state.
future<int> f0;
EXPECT_FALSE(f0.valid());
future<int> f1(std::move(f0));
EXPECT_FALSE(f0.valid());
EXPECT_FALSE(f1.valid());
}
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_9) {
// future<int> destructor releases the shared state.
promise<int> p;
future<int> f0 = p.get_future();
EXPECT_TRUE(f0.valid());
// This behavior is not observable, but any violation should be detected by
// the ASAN builds.
}
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_10) {
// Move assignment is noexcept.
promise<int> p;
future<int> f0 = p.get_future();
EXPECT_TRUE(f0.valid());
future<int> f1;
EXPECT_TRUE(noexcept(f1 = std::move(f0)));
}
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_11_a) {
// future<int> move assignment transfers futures with valid state.
promise<int> p;
future<int> f0 = p.get_future();
EXPECT_TRUE(f0.valid());
future<int> f1;
f1 = std::move(f0);
EXPECT_FALSE(f0.valid());
EXPECT_TRUE(f1.valid());
}
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_11_b) {
// future<int> move assignment transfers futures with invalid state.
future<int> f0;
EXPECT_FALSE(f0.valid());
future<int> f1;
f1 = std::move(f0);
EXPECT_FALSE(f0.valid());
EXPECT_FALSE(f1.valid());
}
// Paragraphs 30.6.6.{12,13,14} are about shared_futures which we are
// not implementing yet.
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_15) {
// future<int>::get() only returns once the promise is satisfied.
promise<int> p;
// We use std::promise<> and std::future<> to test our promises and futures.
// This test uses a number of promises to track progress in a separate thread,
// and checks the expected conditions in each one.
std::promise<void> p_get_future_called;
std::promise<void> f_get_called;
std::thread t([&] {
future<int> f = p.get_future();
p_get_future_called.set_value();
f.get();
f_get_called.set_value();
});
p_get_future_called.get_future().get();
auto waiter = f_get_called.get_future();
// thread `t` cannot make progress until we set the promise value.
EXPECT_EQ(std::future_status::timeout, waiter.wait_for(2_ms));
p.set_value(42);
// now thread `t` can make progress.
EXPECT_EQ(std::future_status::ready, waiter.wait_for(500_ms));
waiter.get();
t.join();
}
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_16_3) {
// future<int>::get() returns void.
promise<int> p;
future<int> f = p.get_future();
EXPECT_TRUE((std::is_same<decltype(f.get()), int>::value));
}
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_17) {
// future<int>::get() throws if an exception was set in the promise.
promise<int> p;
future<int> f = p.get_future();
p.set_exception(std::make_exception_ptr(std::runtime_error("test message")));
#if GOOGLE_CLOUD_CPP_HAVE_EXCEPTIONS
EXPECT_THROW(try { f.get(); } catch (std::runtime_error const& ex) {
EXPECT_THAT(ex.what(), HasSubstr("test message"));
throw;
},
std::runtime_error);
#else
EXPECT_DEATH_IF_SUPPORTED(
f.get(),
"future<T>::get\\(\\) had an exception but exceptions are disabled");
#endif // GOOGLE_CLOUD_CPP_HAVE_EXCEPTIONS
}
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_18_a) {
// future<int>::get() releases the shared state.
promise<int> p;
future<int> f = p.get_future();
p.set_value(42);
EXPECT_EQ(42, f.get());
EXPECT_FALSE(f.valid());
}
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_18_b) {
// future<int>::get() throws releases the shared state.
promise<int> p;
future<int> f = p.get_future();
p.set_exception(std::make_exception_ptr(std::runtime_error("unused")));
#if GOOGLE_CLOUD_CPP_HAVE_EXCEPTIONS
EXPECT_THROW(f.get(), std::runtime_error);
EXPECT_FALSE(f.valid());
#else
EXPECT_DEATH_IF_SUPPORTED(
f.get(),
"future<T>::get\\(\\) had an exception but exceptions are disabled");
// Cannot evaluate side effects with EXPECT_DEATH*()
// EXPECT_FALSE(f.valid());
#endif // GOOGLE_CLOUD_CPP_HAVE_EXCEPTIONS
}
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_19_a) {
// future<int>::valid() returns true when the future has a shared state.
promise<int> p;
future<int> const f = p.get_future();
EXPECT_TRUE(noexcept(f.valid()));
EXPECT_TRUE(f.valid());
}
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_19_b) {
// future<int>::valid() returns false when the future has no shared state.
future<int> const f;
EXPECT_TRUE(noexcept(f.valid()));
EXPECT_FALSE(f.valid());
}
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_20) {
// future<int>::wait() blocks until state is ready.
promise<int> p;
future<int> const f = p.get_future();
// We use std::promise<> and std::future<> to test our promises and futures.
// This test uses a number of promises to track progress in a separate thread,
// and checks the expected conditions in each one.
std::promise<void> thread_started;
std::promise<void> f_wait_returned;
std::thread t([&] {
thread_started.set_value();
f.wait();
f_wait_returned.set_value();
});
thread_started.get_future().get();
auto waiter = f_wait_returned.get_future();
EXPECT_EQ(std::future_status::timeout, waiter.wait_for(2_ms));
p.set_value(42);
EXPECT_EQ(std::future_status::ready, waiter.wait_for(500_ms));
t.join();
}
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_21) {
// future<int>::wait_for() blocks until state is ready.
promise<int> p;
future<int> const f = p.get_future();
// We use std::promise<> and std::future<> to test our promises and futures.
// This test uses a number of promises to track progress in a separate thread,
// and checks the expected conditions in each one.
std::promise<void> thread_started;
std::promise<void> f_wait_returned;
std::thread t([&] {
thread_started.set_value();
f.wait_for(500_ms);
f_wait_returned.set_value();
});
thread_started.get_future().get();
auto waiter = f_wait_returned.get_future();
EXPECT_EQ(std::future_status::timeout, waiter.wait_for(2_ms));
p.set_value(42);
EXPECT_EQ(std::future_status::ready, waiter.wait_for(500_ms));
t.join();
}
// Paragraph 30.6.6.22.1 refers to futures that hold a deferred function (like
// those returned by std::async()), we are not implement those, so there is no
// test needed.
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_22_2) {
// wait_for() returns std::future_status::ready if the future is ready.
promise<int> p0;
auto f0 = p0.get_future();
p0.set_value(42);
auto s = f0.wait_for(0_ms);
EXPECT_EQ(std::future_status::ready, s);
EXPECT_EQ(42, f0.get());
}
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_22_3) {
// wait_for() returns std::future_status::timeout if the future is not ready.
promise<int> p0;
auto f0 = p0.get_future();
auto s = f0.wait_for(0_ms);
EXPECT_EQ(std::future_status::timeout, s);
ASSERT_NE(std::future_status::ready, f0.wait_for(0_ms));
}
// Paragraph 30.6.6.23 asserts that if the clock raises then wait_for() might
// raise too. We do not need to test for that, exceptions are always propagated,
// this is just giving implementors freedom.
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_24) {
// future<int>::wait_until() blocks until state is ready.
promise<int> p;
future<int> const f = p.get_future();
// We use std::promise<> and std::future<> to test our promises and futures.
// This test uses a number of promises to track progress in a separate thread,
// and checks the expected conditions in each one.
std::promise<void> thread_started;
std::promise<void> f_wait_returned;
std::thread t([&] {
thread_started.set_value();
f.wait_until(std::chrono::system_clock::now() + 500_ms);
f_wait_returned.set_value();
});
thread_started.get_future().get();
auto waiter = f_wait_returned.get_future();
EXPECT_EQ(std::future_status::timeout, waiter.wait_for(2_ms));
p.set_value(42);
EXPECT_EQ(std::future_status::ready, waiter.wait_for(500_ms));
t.join();
}
// Paragraph 30.6.6.25.1 refers to futures that hold a deferred function (like
// those returned by std::async()), we are not implement those, so there is no
// test needed.
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_25_2) {
// wait_until() returns std::future_status::ready if the future is ready.
promise<int> p0;
auto f0 = p0.get_future();
p0.set_value(42);
auto s = f0.wait_until(std::chrono::system_clock::now());
EXPECT_EQ(std::future_status::ready, s);
ASSERT_EQ(std::future_status::ready, f0.wait_for(0_ms));
EXPECT_EQ(42, f0.get());
}
/// @test Verify conformance with section 30.6.6 of the C++14 spec.
TEST(FutureTestInt, conform_30_6_6_25_3) {
// wait_until() returns std::future_status::timeout if the future is not
// ready.
promise<int> p0;
auto f0 = p0.get_future();
auto s = f0.wait_until(std::chrono::system_clock::now());
EXPECT_EQ(std::future_status::timeout, s);
ASSERT_NE(std::future_status::ready, f0.wait_for(0_ms));
}
// Paragraph 30.6.6.26 asserts that if the clock raises then wait_until() might
// raise too. We do not need to test for that, exceptions are always propagated,
// this is just giving implementors freedom.
/// @test Verify the behavior around cancellation.
TEST(FutureTestInt, cancellation) {
bool cancelled = false;
promise<int> p0([&cancelled] { cancelled = true; });
auto f0 = p0.get_future();
f0.cancel();
auto s = f0.wait_until(std::chrono::system_clock::now());
EXPECT_EQ(std::future_status::timeout, s);
ASSERT_NE(std::future_status::ready, f0.wait_for(0_ms));
ASSERT_EQ(true, p0.cancelled());
ASSERT_EQ(true, cancelled);
}
} // namespace
} // namespace GOOGLE_CLOUD_CPP_NS
} // namespace cloud
} // namespace google