-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_time_jumps.cpp
2310 lines (1865 loc) · 91.1 KB
/
test_time_jumps.cpp
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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/time.h>
#endif
#include "boost/bind.hpp"
#include "boost/chrono.hpp"
#include "boost/chrono/ceil.hpp"
#include "boost/date_time.hpp"
#include "boost/thread/concurrent_queues/sync_priority_queue.hpp"
#include "boost/thread/concurrent_queues/sync_timed_queue.hpp"
#include "boost/thread/future.hpp"
#include "boost/thread/mutex.hpp"
#include "boost/thread/recursive_mutex.hpp"
#include "boost/thread/shared_lock_guard.hpp"
#include "boost/thread/shared_mutex.hpp"
#include "boost/thread/thread.hpp"
#include <iomanip>
#ifdef TEST_CPP14_FEATURES
#include <future>
#include <mutex>
#include <shared_mutex>
#include <thread>
#endif
/******************************************************************************/
/*
* Summary:
*
* This code tests the behavior of time-related functions in the presence of
* system clock changes (jumps). It requires root/Administrator privileges in
* order to run because it changes the system clock. NTP should also be disabled
* while running this code so that NTP can't change the system clock.
*
* Each function to be tested is executed five times. The amount of time the
* function waits before returning is measured against the amount of time the
* function was expected to wait. If the difference exceeds a threshold value
* (defined below) then the test fails.
*
* The following values are intentially:
* - more than 200 milliseconds
* - more than 200 milliseconds apart
* - not a multiple of 100 milliseconds
* - not a multiple of each other
* - don't sum or diff to a multiple of 100 milliseconds
*/
const long long s_waitMs = 580;
const long long s_shortJumpMs = 230;
const long long s_longJumpMs = 870; // Causes additional, unavoidable failures when BOOST_THREAD_HAS_CONDATTR_SET_CLOCK_MONOTONIC is disabled
const long long s_sleepBeforeJumpMs = 110;
#ifdef _WIN32
const long long s_maxEarlyErrorMs = 10
+ 100; // Windows is unpredictable, especially in a VM, so allow extra time if the function returns early
const long long s_maxLateErrorMs = 110 // due to polling, functions may not return for up to 100 milliseconds after they are supposed to
+ 100; // Windows is slow, especially in a VM, so allow extra time for the functions to return
#else
const long long s_maxEarlyErrorMs = 10;
const long long s_maxLateErrorMs = 110; // Due to polling, functions may not return for up to 100 milliseconds after they are supposed to
#endif
int g_numTestsRun = 0;
int g_numTestsPassed = 0;
int g_numTestsFailed = 0;
/******************************************************************************/
// A custom clock based off the system clock but with a different epoch.
namespace custom
{
class custom_boost_clock
{
public:
typedef boost::chrono::microseconds duration; // intentionally not nanoseconds
typedef duration::rep rep;
typedef duration::period period;
typedef boost::chrono::time_point<custom_boost_clock> time_point;
static bool is_steady;
static time_point now();
};
bool custom_boost_clock::is_steady = false;
custom_boost_clock::time_point custom_boost_clock::now()
{
return time_point(boost::chrono::ceil<duration>(boost::chrono::system_clock::now().time_since_epoch()) - boost::chrono::hours(10 * 365 * 24));
}
#ifdef TEST_CPP14_FEATURES
class custom_std_clock
{
public:
typedef std::chrono::microseconds duration; // intentionally not nanoseconds
typedef duration::rep rep;
typedef duration::period period;
typedef std::chrono::time_point<custom_std_clock> time_point;
static bool is_steady;
static time_point now();
};
bool custom_std_clock::is_steady = false;
custom_std_clock::time_point custom_std_clock::now()
{
return time_point(std::chrono::duration_cast<duration>(std::chrono::system_clock::now().time_since_epoch()) - std::chrono::hours(10 * 365 * 24));
}
#endif
}
/******************************************************************************/
template <typename MutexType = boost::mutex, typename CondType = boost::condition_variable>
struct BoostHelper
{
typedef MutexType mutex;
typedef CondType cond;
typedef boost::lock_guard<MutexType> lock_guard;
typedef boost::unique_lock<MutexType> unique_lock;
typedef boost::chrono::milliseconds milliseconds;
typedef boost::chrono::nanoseconds nanoseconds;
typedef boost::chrono::system_clock system_clock;
typedef boost::chrono::steady_clock steady_clock;
typedef custom::custom_boost_clock custom_clock;
typedef system_clock::time_point system_time_point;
typedef steady_clock::time_point steady_time_point;
typedef custom_clock::time_point custom_time_point;
typedef boost::cv_status cv_status;
typedef boost::future_status future_status;
typedef boost::packaged_task<bool> packaged_task;
typedef boost::future<bool> future;
typedef boost::shared_future<bool> shared_future;
typedef boost::thread thread;
static const milliseconds waitDur;
template <typename T>
static void sleep_for(T d)
{
boost::this_thread::sleep_for(d);
}
template <typename T>
static void sleep_for_no_int(T d)
{
boost::this_thread::no_interruption_point::sleep_for(d);
}
template <typename T>
static void sleep_until(T t)
{
boost::this_thread::sleep_until(t);
}
template <typename T>
static void sleep_until_no_int(T t)
{
boost::this_thread::no_interruption_point::sleep_until(t);
}
static system_time_point systemNow()
{
return system_clock::now();
}
static steady_time_point steadyNow()
{
return steady_clock::now();
}
static custom_time_point customNow()
{
return custom_clock::now();
}
template <class ToDuration, class Rep, class Period>
static ToDuration duration_cast(const boost::chrono::duration<Rep, Period>& d)
{
return boost::chrono::duration_cast<ToDuration>(d);
}
static milliseconds zero()
{
return milliseconds(0);
}
};
template <typename MutexType, typename CondType>
const typename BoostHelper<MutexType, CondType>::milliseconds
BoostHelper<MutexType, CondType>::waitDur = typename BoostHelper<MutexType, CondType>::milliseconds(s_waitMs);
#ifdef TEST_CPP14_FEATURES
template <typename MutexType = std::mutex, typename CondType = std::condition_variable>
struct StdHelper
{
typedef MutexType mutex;
typedef CondType cond;
typedef std::lock_guard<MutexType> lock_guard;
typedef std::unique_lock<MutexType> unique_lock;
typedef std::chrono::milliseconds milliseconds;
typedef std::chrono::nanoseconds nanoseconds;
typedef std::chrono::system_clock system_clock;
typedef std::chrono::steady_clock steady_clock;
typedef custom::custom_std_clock custom_clock;
typedef system_clock::time_point system_time_point;
typedef steady_clock::time_point steady_time_point;
typedef custom_clock::time_point custom_time_point;
typedef std::cv_status cv_status;
typedef std::future_status future_status;
typedef std::packaged_task<bool()> packaged_task;
typedef std::future<bool> future;
typedef std::shared_future<bool> shared_future;
typedef std::thread thread;
static const milliseconds waitDur;
template <typename T>
static void sleep_for(T d)
{
std::this_thread::sleep_for(d);
}
template <typename T>
static void sleep_until(T t)
{
std::this_thread::sleep_until(t);
}
static system_time_point systemNow()
{
return system_clock::now();
}
static steady_time_point steadyNow()
{
return steady_clock::now();
}
static custom_time_point customNow()
{
return custom_clock::now();
}
template <class ToDuration, class Rep, class Period>
static ToDuration duration_cast(const std::chrono::duration<Rep, Period>& d)
{
return std::chrono::duration_cast<ToDuration>(d);
}
static milliseconds zero()
{
return milliseconds(0);
}
};
template <typename MutexType, typename CondType>
const typename StdHelper<MutexType, CondType>::milliseconds
StdHelper<MutexType, CondType>::waitDur = typename StdHelper<MutexType, CondType>::milliseconds(s_waitMs);
#endif
/******************************************************************************/
#ifdef _WIN32
void changeSystemTime(long long changeMs)
{
Sleep(s_sleepBeforeJumpMs);
SYSTEMTIME systemTime;
GetSystemTime(&systemTime);
FILETIME fileTime;
if (!SystemTimeToFileTime(&systemTime, &fileTime))
{
std::cout << "ERROR: Couldn't convert system time to file time" << std::endl;
}
ULARGE_INTEGER largeInt;
largeInt.LowPart = fileTime.dwLowDateTime;
largeInt.HighPart = fileTime.dwHighDateTime;
largeInt.QuadPart += changeMs * 10000;
fileTime.dwLowDateTime = largeInt.LowPart;
fileTime.dwHighDateTime = largeInt.HighPart;
if (!FileTimeToSystemTime(&fileTime, &systemTime))
{
std::cout << "ERROR: Couldn't convert file time to system time" << std::endl;
}
if (!SetSystemTime(&systemTime))
{
std::cout << "ERROR: Couldn't set system time" << std::endl;
}
}
#else
void changeSystemTime(long long changeMs)
{
struct timespec sleepTs;
sleepTs.tv_sec = (s_sleepBeforeJumpMs / 1000);
sleepTs.tv_nsec = (s_sleepBeforeJumpMs % 1000) * 1000000;
nanosleep(&sleepTs, NULL);
struct timeval tv;
if (gettimeofday(&tv, NULL) != 0)
{
std::cout << "ERROR: Couldn't get system time" << std::endl;
}
changeMs += tv.tv_sec * 1000;
changeMs += tv.tv_usec / 1000;
tv.tv_sec = (changeMs / 1000);
tv.tv_usec = (changeMs % 1000) * 1000;
if (settimeofday(&tv, NULL) != 0)
{
std::cout << "ERROR: Couldn't set system time" << std::endl;
}
}
#endif
enum RcEnum
{
e_no_timeout,
e_timeout,
e_failed_bad,
e_failed_good,
e_succeeded_bad,
e_succeeded_good,
e_ready_bad,
e_not_ready_good,
e_na
};
template <typename Helper>
void checkWaitTime(typename Helper::nanoseconds expected, typename Helper::nanoseconds actual, RcEnum rc)
{
if (expected != Helper::zero() && expected < typename Helper::milliseconds(s_sleepBeforeJumpMs))
{
expected = typename Helper::milliseconds(s_sleepBeforeJumpMs);
}
typename Helper::milliseconds expectedMs = Helper::template duration_cast<typename Helper::milliseconds>(expected);
typename Helper::milliseconds actualMs = Helper::template duration_cast<typename Helper::milliseconds>(actual);
std::cout << "Expected: " << std::setw(4) << expectedMs.count() << " ms"
<< ", Actual: " << std::setw(4) << actualMs.count() << " ms"
<< ", Returned: ";
switch (rc)
{
case e_no_timeout : std::cout << "no_timeout, "; break;
case e_timeout : std::cout << "timeout, "; break;
case e_failed_bad : std::cout << "failed, "; break;
case e_failed_good : std::cout << "failed, "; break;
case e_succeeded_bad : std::cout << "succeeded, "; break;
case e_succeeded_good : std::cout << "succeeded, "; break;
case e_ready_bad : std::cout << "ready, "; break;
case e_not_ready_good : std::cout << "not_ready, "; break;
default : std::cout << "N/A, "; break;
}
if (expectedMs == Helper::zero())
{
std::cout << "FAILED: SKIPPED (test would lock up if run)";
g_numTestsFailed++;
}
else if (actual < expected - typename Helper::milliseconds(s_maxEarlyErrorMs))
{
std::cout << "FAILED: TOO SHORT";
if (rc == e_timeout) // bad
{
std::cout << ", RETURNED TIMEOUT";
}
else if (rc == e_failed_bad)
{
std::cout << ", RETURNED FAILED";
}
else if (rc == e_succeeded_bad)
{
std::cout << ", RETURNED SUCCEEDED";
}
else if (rc == e_ready_bad)
{
std::cout << ", RETURNED READY";
}
g_numTestsFailed++;
}
else if (actual > expected + typename Helper::milliseconds(s_maxLateErrorMs))
{
std::cout << "FAILED: TOO LONG";
if (rc == e_no_timeout) // bad
{
std::cout << ", RETURNED NO_TIMEOUT";
}
else if (rc == e_failed_bad)
{
std::cout << ", RETURNED FAILED";
}
else if (rc == e_succeeded_bad)
{
std::cout << ", RETURNED SUCCEEDED";
}
else if (rc == e_ready_bad)
{
std::cout << ", RETURNED READY";
}
g_numTestsFailed++;
}
else if (rc == e_no_timeout) // bad
{
std::cout << "FAILED: RETURNED NO_TIMEOUT";
g_numTestsFailed++;
}
else if (rc == e_failed_bad)
{
std::cout << "FAILED: RETURNED FAILED";
g_numTestsFailed++;
}
else if (rc == e_succeeded_bad)
{
std::cout << "FAILED: RETURNED SUCCEEDED";
g_numTestsFailed++;
}
else if (rc == e_ready_bad)
{
std::cout << "FAILED: RETURNED READY";
g_numTestsFailed++;
}
else
{
std::cout << "Passed";
g_numTestsPassed++;
}
std::cout << std::endl;
g_numTestsRun++;
}
void sleepForLongTime()
{
#ifdef _WIN32
Sleep(10000);
#else
struct timespec ts = {5, 0};
nanosleep(&ts, NULL);
#endif
}
bool returnFalse()
{
return false;
}
/******************************************************************************/
// Run the test in the context provided, which may be the current thread or a separate thread.
template <typename Helper, typename Context, typename Function>
void runTestInContext(Context context, Function func, const std::string name)
{
std::cout << name << ":" << std::endl;
{
std::cout << " While system clock remains stable: ";
context(func, 0);
}
{
std::cout << " While system clock jumps back (short): ";
typename Helper::thread t(boost::bind(changeSystemTime, -s_shortJumpMs));
context(func, -s_shortJumpMs);
t.join();
}
{
std::cout << " While system clock jumps back (long): ";
typename Helper::thread t(boost::bind(changeSystemTime, -s_longJumpMs));
context(func, -s_longJumpMs);
t.join();
}
{
std::cout << " While system clock jumps forward (short): ";
typename Helper::thread t(boost::bind(changeSystemTime, s_shortJumpMs));
context(func, s_shortJumpMs);
t.join();
}
{
std::cout << " While system clock jumps forward (long): ";
typename Helper::thread t(boost::bind(changeSystemTime, s_longJumpMs));
context(func, s_longJumpMs);
t.join();
}
}
//--------------------------------------
template <typename Helper, typename Function>
void noThreadContext(Function func, const long long jumpMs)
{
func(jumpMs);
}
template <typename Helper, typename Function>
void threadContextWithNone(Function func, const long long jumpMs)
{
typename Helper::thread t(boost::bind(func, jumpMs));
t.join();
}
template <typename Helper, typename Function>
void threadContextWithUnique(Function func, const long long jumpMs)
{
typename Helper::mutex m;
typename Helper::lock_guard g(m);
typename Helper::thread t(boost::bind(func, boost::ref(m), jumpMs));
t.join();
}
template <typename Helper, typename Function>
void threadContextWithShared(Function func, const long long jumpMs)
{
typename Helper::mutex m;
boost::shared_lock_guard<typename Helper::mutex> g(m);
typename Helper::thread t(boost::bind(func, boost::ref(m), jumpMs));
t.join();
}
template <typename Helper, typename Function>
void threadContextWithUpgrade(Function func, const long long jumpMs)
{
typename Helper::mutex m;
boost::upgrade_lock<typename Helper::mutex> g(m);
typename Helper::thread t(boost::bind(func, boost::ref(m), jumpMs));
t.join();
}
//--------------------------------------
// Run the test in the current thread.
template <typename Helper, typename Function>
void runTest(Function func, const std::string name)
{
runTestInContext<Helper>(noThreadContext<Helper, Function>, func, name);
}
// Run the test in a separate thread.
template <typename Helper, typename Function>
void runTestWithNone(Function func, const std::string name)
{
runTestInContext<Helper>(threadContextWithNone<Helper, Function>, func, name);
}
// Run the test in a separate thread. Pass a locked mutex to the function under test.
template <typename Helper, typename Function>
void runTestWithUnique(Function func, const std::string name)
{
runTestInContext<Helper>(threadContextWithUnique<Helper, Function>, func, name);
}
// Run the test in a separate thread. Pass a shared-locked mutex to the function under test.
template <typename Helper, typename Function>
void runTestWithShared(Function func, const std::string name)
{
runTestInContext<Helper>(threadContextWithShared<Helper, Function>, func, name);
}
// Run the test in a separate thread. Pass an upgrade-locked mutex to the function under test.
template <typename Helper, typename Function>
void runTestWithUpgrade(Function func, const std::string name)
{
runTestInContext<Helper>(threadContextWithUpgrade<Helper, Function>, func, name);
}
/******************************************************************************/
// Test Sleep
template <typename Helper>
void testSleepFor(const long long jumpMs)
{
typename Helper::steady_time_point before(Helper::steadyNow());
Helper::sleep_for(Helper::waitDur);
typename Helper::steady_time_point after(Helper::steadyNow());
checkWaitTime<Helper>(Helper::waitDur, after - before, e_na);
}
template <typename Helper>
void testSleepUntilSteady(const long long jumpMs)
{
typename Helper::steady_time_point before(Helper::steadyNow());
Helper::sleep_until(Helper::steadyNow() + Helper::waitDur);
typename Helper::steady_time_point after(Helper::steadyNow());
checkWaitTime<Helper>(Helper::waitDur, after - before, e_na);
}
template <typename Helper>
void testSleepUntilSystem(const long long jumpMs)
{
typename Helper::steady_time_point before(Helper::steadyNow());
Helper::sleep_until(Helper::systemNow() + Helper::waitDur);
typename Helper::steady_time_point after(Helper::steadyNow());
checkWaitTime<Helper>(Helper::waitDur - typename Helper::milliseconds(jumpMs), after - before, e_na);
}
template <typename Helper>
void testSleepUntilCustom(const long long jumpMs)
{
typename Helper::steady_time_point before(Helper::steadyNow());
Helper::sleep_until(Helper::customNow() + Helper::waitDur);
typename Helper::steady_time_point after(Helper::steadyNow());
checkWaitTime<Helper>(Helper::waitDur - typename Helper::milliseconds(jumpMs), after - before, e_na);
}
//--------------------------------------
template <typename Helper>
void testSleepRelative(const long long jumpMs)
{
#ifndef SKIP_DATETIME_FUNCTIONS
typename Helper::steady_time_point before(Helper::steadyNow());
boost::posix_time::milliseconds ptDur(boost::chrono::duration_cast<boost::chrono::milliseconds>(Helper::waitDur).count());
boost::this_thread::sleep(ptDur);
typename Helper::steady_time_point after(Helper::steadyNow());
checkWaitTime<Helper>(Helper::waitDur, after - before, e_na);
#else
checkWaitTime<Helper>(Helper::zero(), Helper::zero(), e_na);
#endif
}
template <typename Helper>
void testSleepAbsolute(const long long jumpMs)
{
#ifndef SKIP_DATETIME_FUNCTIONS
typename Helper::steady_time_point before(Helper::steadyNow());
boost::posix_time::ptime ptNow(boost::posix_time::microsec_clock::universal_time());
boost::posix_time::milliseconds ptDur(boost::chrono::duration_cast<boost::chrono::milliseconds>(Helper::waitDur).count());
boost::this_thread::sleep(ptNow + ptDur);
typename Helper::steady_time_point after(Helper::steadyNow());
checkWaitTime<Helper>(Helper::waitDur - typename Helper::milliseconds(jumpMs), after - before, e_na);
#else
checkWaitTime<Helper>(Helper::zero(), Helper::zero(), e_na);
#endif
}
//--------------------------------------
template <typename Helper>
void testSleepStd(const std::string& name)
{
std::cout << std::endl;
runTestWithNone<Helper>(testSleepFor <Helper>, name + "::this_thread::sleep_for()");
runTestWithNone<Helper>(testSleepUntilSteady<Helper>, name + "::this_thread::sleep_until(), steady time");
runTestWithNone<Helper>(testSleepUntilSystem<Helper>, name + "::this_thread::sleep_until(), system time");
runTestWithNone<Helper>(testSleepUntilCustom<Helper>, name + "::this_thread::sleep_until(), custom time");
}
template <typename Helper>
void testSleepBoost(const std::string& name)
{
testSleepStd<Helper>(name);
// Boost-only functions
runTestWithNone<Helper>(testSleepRelative<Helper>, name + "::this_thread::sleep(), relative time");
runTestWithNone<Helper>(testSleepAbsolute<Helper>, name + "::this_thread::sleep(), absolute time");
}
template <typename Helper>
void testSleepNoThreadStd(const std::string& name)
{
std::cout << std::endl;
runTest<Helper>(testSleepFor <Helper>, name + "::this_thread::sleep_for(), no thread");
runTest<Helper>(testSleepUntilSteady<Helper>, name + "::this_thread::sleep_until(), no thread, steady time");
runTest<Helper>(testSleepUntilSystem<Helper>, name + "::this_thread::sleep_until(), no thread, system time");
runTest<Helper>(testSleepUntilCustom<Helper>, name + "::this_thread::sleep_until(), no thread, custom time");
}
template <typename Helper>
void testSleepNoThreadBoost(const std::string& name)
{
testSleepNoThreadStd<Helper>(name);
// Boost-only functions
runTest<Helper>(testSleepRelative<Helper>, name + "::this_thread::sleep(), no thread, relative time");
runTest<Helper>(testSleepAbsolute<Helper>, name + "::this_thread::sleep(), no thread, absolute time");
}
/******************************************************************************/
// Test Sleep, No Interruption Point
template <typename Helper>
void testSleepForNoInt(const long long jumpMs)
{
typename Helper::steady_time_point before(Helper::steadyNow());
Helper::sleep_for_no_int(Helper::waitDur);
typename Helper::steady_time_point after(Helper::steadyNow());
checkWaitTime<Helper>(Helper::waitDur, after - before, e_na);
}
template <typename Helper>
void testSleepUntilNoIntSteady(const long long jumpMs)
{
typename Helper::steady_time_point before(Helper::steadyNow());
Helper::sleep_until_no_int(Helper::steadyNow() + Helper::waitDur);
typename Helper::steady_time_point after(Helper::steadyNow());
checkWaitTime<Helper>(Helper::waitDur, after - before, e_na);
}
template <typename Helper>
void testSleepUntilNoIntSystem(const long long jumpMs)
{
typename Helper::steady_time_point before(Helper::steadyNow());
Helper::sleep_until_no_int(Helper::systemNow() + Helper::waitDur);
typename Helper::steady_time_point after(Helper::steadyNow());
checkWaitTime<Helper>(Helper::waitDur - typename Helper::milliseconds(jumpMs), after - before, e_na);
}
template <typename Helper>
void testSleepUntilNoIntCustom(const long long jumpMs)
{
typename Helper::steady_time_point before(Helper::steadyNow());
Helper::sleep_until_no_int(Helper::customNow() + Helper::waitDur);
typename Helper::steady_time_point after(Helper::steadyNow());
checkWaitTime<Helper>(Helper::waitDur - typename Helper::milliseconds(jumpMs), after - before, e_na);
}
//--------------------------------------
#ifndef SKIP_NO_INT_SLEEP
template <typename Helper>
void testSleepNoIntRelative(const long long jumpMs)
{
#ifndef SKIP_DATETIME_FUNCTIONS
typename Helper::steady_time_point before(Helper::steadyNow());
boost::posix_time::milliseconds ptDur(boost::chrono::duration_cast<boost::chrono::milliseconds>(Helper::waitDur).count());
boost::this_thread::no_interruption_point::sleep(ptDur);
typename Helper::steady_time_point after(Helper::steadyNow());
checkWaitTime<Helper>(Helper::waitDur, after - before, e_na);
#else
checkWaitTime<Helper>(Helper::zero(), Helper::zero(), e_na);
#endif
}
template <typename Helper>
void testSleepNoIntAbsolute(const long long jumpMs)
{
#ifndef SKIP_DATETIME_FUNCTIONS
typename Helper::steady_time_point before(Helper::steadyNow());
boost::posix_time::ptime ptNow(boost::posix_time::microsec_clock::universal_time());
boost::posix_time::milliseconds ptDur(boost::chrono::duration_cast<boost::chrono::milliseconds>(Helper::waitDur).count());
boost::this_thread::no_interruption_point::sleep(ptNow + ptDur);
typename Helper::steady_time_point after(Helper::steadyNow());
checkWaitTime<Helper>(Helper::waitDur - typename Helper::milliseconds(jumpMs), after - before, e_na);
#else
checkWaitTime<Helper>(Helper::zero(), Helper::zero(), e_na);
#endif
}
#endif
//--------------------------------------
// Only Boost supports no_interruption_point
template <typename Helper>
void testSleepNoIntBoost(const std::string& name)
{
std::cout << std::endl;
runTestWithNone<Helper>(testSleepForNoInt <Helper>, name + "::this_thread::no_interruption_point::sleep_for()");
runTestWithNone<Helper>(testSleepUntilNoIntSteady<Helper>, name + "::this_thread::no_interruption_point::sleep_until(), steady time");
runTestWithNone<Helper>(testSleepUntilNoIntSystem<Helper>, name + "::this_thread::no_interruption_point::sleep_until(), system time");
runTestWithNone<Helper>(testSleepUntilNoIntCustom<Helper>, name + "::this_thread::no_interruption_point::sleep_until(), custom time");
#ifndef SKIP_NO_INT_SLEEP
runTestWithNone<Helper>(testSleepNoIntRelative<Helper>, name + "::this_thread::no_interruption_point::sleep(), relative time");
runTestWithNone<Helper>(testSleepNoIntAbsolute<Helper>, name + "::this_thread::no_interruption_point::sleep(), absolute time");
#endif
}
template <typename Helper>
void testSleepNoThreadNoIntBoost(const std::string& name)
{
std::cout << std::endl;
runTest<Helper>(testSleepForNoInt <Helper>, name + "::this_thread::no_interruption_point::sleep_for(), no thread");
runTest<Helper>(testSleepUntilNoIntSteady<Helper>, name + "::this_thread::no_interruption_point::sleep_until(), no thread, steady time");
runTest<Helper>(testSleepUntilNoIntSystem<Helper>, name + "::this_thread::no_interruption_point::sleep_until(), no thread, system time");
runTest<Helper>(testSleepUntilNoIntCustom<Helper>, name + "::this_thread::no_interruption_point::sleep_until(), no thread, custom time");
#ifndef SKIP_NO_INT_SLEEP
runTest<Helper>(testSleepNoIntRelative<Helper>, name + "::this_thread::no_interruption_point::sleep(), no thread, relative time");
runTest<Helper>(testSleepNoIntAbsolute<Helper>, name + "::this_thread::no_interruption_point::sleep(), no thread, absolute time");
#endif
}
/******************************************************************************/
// Test Try Join
template <typename Helper>
void testTryJoinFor(const long long jumpMs)
{
typename Helper::steady_time_point before(Helper::steadyNow());
typename Helper::thread t3(sleepForLongTime);
bool succeeded = t3.try_join_for(Helper::waitDur);
typename Helper::steady_time_point after(Helper::steadyNow());
checkWaitTime<Helper>(Helper::waitDur, after - before, succeeded ? e_succeeded_bad : e_failed_good);
}
template <typename Helper>
void testTryJoinUntilSteady(const long long jumpMs)
{
typename Helper::steady_time_point before(Helper::steadyNow());
typename Helper::thread t3(sleepForLongTime);
bool succeeded = t3.try_join_until(Helper::steadyNow() + Helper::waitDur);
typename Helper::steady_time_point after(Helper::steadyNow());
checkWaitTime<Helper>(Helper::waitDur, after - before, succeeded ? e_succeeded_bad : e_failed_good);
}
template <typename Helper>
void testTryJoinUntilSystem(const long long jumpMs)
{
typename Helper::steady_time_point before(Helper::steadyNow());
typename Helper::thread t3(sleepForLongTime);
bool succeeded = t3.try_join_until(Helper::systemNow() + Helper::waitDur);
typename Helper::steady_time_point after(Helper::steadyNow());
checkWaitTime<Helper>(Helper::waitDur - typename Helper::milliseconds(jumpMs), after - before, succeeded ? e_succeeded_bad : e_failed_good);
}
template <typename Helper>
void testTryJoinUntilCustom(const long long jumpMs)
{
typename Helper::steady_time_point before(Helper::steadyNow());
typename Helper::thread t3(sleepForLongTime);
bool succeeded = t3.try_join_until(Helper::customNow() + Helper::waitDur);
typename Helper::steady_time_point after(Helper::steadyNow());
checkWaitTime<Helper>(Helper::waitDur - typename Helper::milliseconds(jumpMs), after - before, succeeded ? e_succeeded_bad : e_failed_good);
}
//--------------------------------------
template <typename Helper>
void testTimedJoinRelative(const long long jumpMs)
{
#ifndef SKIP_DATETIME_FUNCTIONS
typename Helper::steady_time_point before(Helper::steadyNow());
boost::posix_time::milliseconds ptDur(boost::chrono::duration_cast<boost::chrono::milliseconds>(Helper::waitDur).count());
typename Helper::thread t3(sleepForLongTime);
bool succeeded = t3.timed_join(ptDur);
typename Helper::steady_time_point after(Helper::steadyNow());
checkWaitTime<Helper>(Helper::waitDur, after - before, succeeded ? e_succeeded_bad : e_failed_good);
#else
checkWaitTime<Helper>(Helper::zero(), Helper::zero(), e_na);
#endif
}
template <typename Helper>
void testTimedJoinAbsolute(const long long jumpMs)
{
#ifndef SKIP_DATETIME_FUNCTIONS
typename Helper::steady_time_point before(Helper::steadyNow());
boost::posix_time::ptime ptNow(boost::posix_time::microsec_clock::universal_time());
boost::posix_time::milliseconds ptDur(boost::chrono::duration_cast<boost::chrono::milliseconds>(Helper::waitDur).count());
typename Helper::thread t3(sleepForLongTime);
bool succeeded = t3.timed_join(ptNow + ptDur);
typename Helper::steady_time_point after(Helper::steadyNow());
checkWaitTime<Helper>(Helper::waitDur - typename Helper::milliseconds(jumpMs), after - before, succeeded ? e_succeeded_bad : e_failed_good);
#else
checkWaitTime<Helper>(Helper::zero(), Helper::zero(), e_na);
#endif
}
//--------------------------------------
// Only Boost supports timed try_join functions
template <typename Helper>
void testJoinBoost(const std::string& name)
{
std::cout << std::endl;
runTestWithNone<Helper>(testTryJoinFor <Helper>, name + "::thread::try_join_for()");
runTestWithNone<Helper>(testTryJoinUntilSteady<Helper>, name + "::thread::try_join_until(), steady time");
runTestWithNone<Helper>(testTryJoinUntilSystem<Helper>, name + "::thread::try_join_until(), system time");
runTestWithNone<Helper>(testTryJoinUntilCustom<Helper>, name + "::thread::try_join_until(), custom time");
runTestWithNone<Helper>(testTimedJoinRelative <Helper>, name + "::thread::timed_join(), relative time");
runTestWithNone<Helper>(testTimedJoinAbsolute <Helper>, name + "::thread::timed_join(), absolute time");
}
/******************************************************************************/
// Test Condition Variable Wait
template <typename Helper>
void testCondVarWaitFor(const long long jumpMs)
{
typename Helper::cond cv;
typename Helper::mutex m;
typename Helper::unique_lock g(m);
typename Helper::steady_time_point before(Helper::steadyNow());
bool noTimeout = (cv.wait_for(g, Helper::waitDur) == Helper::cv_status::no_timeout);
typename Helper::steady_time_point after(Helper::steadyNow());
checkWaitTime<Helper>(Helper::waitDur, after - before, noTimeout ? e_no_timeout : e_timeout);
}
template <typename Helper>
void testCondVarWaitUntilSteady(const long long jumpMs)
{
typename Helper::cond cv;
typename Helper::mutex m;
typename Helper::unique_lock g(m);
typename Helper::steady_time_point before(Helper::steadyNow());
bool noTimeout = (cv.wait_until(g, Helper::steadyNow() + Helper::waitDur) == Helper::cv_status::no_timeout);
typename Helper::steady_time_point after(Helper::steadyNow());
checkWaitTime<Helper>(Helper::waitDur, after - before, noTimeout ? e_no_timeout : e_timeout);
}
template <typename Helper>
void testCondVarWaitUntilSystem(const long long jumpMs)
{
typename Helper::cond cv;
typename Helper::mutex m;
typename Helper::unique_lock g(m);
typename Helper::steady_time_point before(Helper::steadyNow());
bool noTimeout = (cv.wait_until(g, Helper::systemNow() + Helper::waitDur) == Helper::cv_status::no_timeout);
typename Helper::steady_time_point after(Helper::steadyNow());
checkWaitTime<Helper>(Helper::waitDur - typename Helper::milliseconds(jumpMs), after - before, noTimeout ? e_no_timeout : e_timeout);
}
template <typename Helper>
void testCondVarWaitUntilCustom(const long long jumpMs)
{
typename Helper::cond cv;
typename Helper::mutex m;
typename Helper::unique_lock g(m);
typename Helper::steady_time_point before(Helper::steadyNow());
bool noTimeout = (cv.wait_until(g, Helper::customNow() + Helper::waitDur) == Helper::cv_status::no_timeout);
typename Helper::steady_time_point after(Helper::steadyNow());
checkWaitTime<Helper>(Helper::waitDur - typename Helper::milliseconds(jumpMs), after - before, noTimeout ? e_no_timeout : e_timeout);
}
//--------------------------------------
template <typename Helper>
void testCondVarTimedWaitRelative(const long long jumpMs)
{
#ifndef SKIP_DATETIME_FUNCTIONS
typename Helper::cond cv;
typename Helper::mutex m;
typename Helper::unique_lock g(m);
typename Helper::steady_time_point before(Helper::steadyNow());
boost::posix_time::milliseconds ptDur(boost::chrono::duration_cast<boost::chrono::milliseconds>(Helper::waitDur).count());