-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClockQuantizerTests.cs
775 lines (591 loc) · 30.8 KB
/
ClockQuantizerTests.cs
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
using ClockQuantization.Tests.Assets;
using System;
using System.Collections.Generic;
using Xunit;
namespace ClockQuantization.Tests
{
public class ClockQuantizerTests
{
[Fact]
public void ClockQuantizer_WithTestClock_AddWithGap_RaisesAdvanceEventWithDetectedGapTimeSpan()
{
var metronomeOptions = new MetronomeOptions
{
IsManual = true,
MaxIntervalTimeSpan = TimeSpan.FromMilliseconds(5),
};
var now = DateTimeOffset.UtcNow;
var offset = TimeSpan.FromMinutes(1);
var context = new SystemClockTemporalContext(now, metronomeOptions);
var advanceEventRaised = false;
// Pre-requisites check
Assert.False(context.HasExternalClock);
var quantizer = new ClockQuantizer(context, metronomeOptions.MaxIntervalTimeSpan);
quantizer.Advance(); // Advance once to ensure that we have a CurrentInterval
Assert.NotNull(quantizer.CurrentInterval);
Assert.Equal(context.DateTimeOffsetToClockOffset(now), quantizer.CurrentInterval!.ClockOffset);
quantizer.Advanced += Quantizer_Advanced;
// Execute
context.Add(offset);
// Test
Assert.True(advanceEventRaised);
void Quantizer_Advanced(object? sender, ClockQuantizer.NewIntervalEventArgs e)
{
advanceEventRaised = true;
// Test
Assert.Equal(now + offset, e.DateTimeOffset);
Assert.False(e.IsMetronomic);
Assert.True(e.GapToPriorIntervalExpectedEnd.HasValue);
Assert.Equal(offset - metronomeOptions.MaxIntervalTimeSpan, e.GapToPriorIntervalExpectedEnd!);
}
}
[Fact]
public void ClockQuantizer_WithTestClock_AddWithoutGap_RaisesAdvanceEventWithoutDetectedGapTimeSpan()
{
var metronomeOptions = new MetronomeOptions
{
IsManual = true,
MaxIntervalTimeSpan = TimeSpan.FromMinutes(5),
};
var now = DateTimeOffset.UtcNow;
var offset = TimeSpan.FromMinutes(5);
var context = new SystemClockTemporalContext(now, metronomeOptions);
var advanceEventRaised = false;
// Pre-requisites check
Assert.False(context.HasExternalClock);
var quantizer = new ClockQuantizer(context, metronomeOptions.MaxIntervalTimeSpan);
quantizer.Advance(); // Advance once to ensure that we have a CurrentInterval
Assert.NotNull(quantizer.CurrentInterval);
Assert.Equal(context.DateTimeOffsetToClockOffset(now), quantizer.CurrentInterval!.ClockOffset);
quantizer.Advanced += Quantizer_Advanced;
// Execute
context.Add(offset);
// Test
Assert.True(advanceEventRaised);
void Quantizer_Advanced(object? sender, ClockQuantizer.NewIntervalEventArgs e)
{
advanceEventRaised = true;
// Test
Assert.Equal(now + offset, e.DateTimeOffset);
Assert.False(e.IsMetronomic);
Assert.False(e.GapToPriorIntervalExpectedEnd.HasValue);
}
}
[Fact]
public void ClockQuantizer_WithTestClock_AdjustClockWithGap_RaisesAdvanceEventWithDetectedGapTimeSpan()
{
var metronomeOptions = new MetronomeOptions
{
IsManual = true,
MaxIntervalTimeSpan = TimeSpan.FromMilliseconds(5),
};
var now = DateTimeOffset.UtcNow;
var adjusted = now + TimeSpan.FromMinutes(1);
var context = new SystemClockTemporalContext(now, metronomeOptions);
var advanceEventRaised = false;
// Pre-requisites check
Assert.False(context.HasExternalClock);
var quantizer = new ClockQuantizer(context, metronomeOptions.MaxIntervalTimeSpan);
quantizer.Advance(); // Advance once to ensure that we have a CurrentInterval
Assert.NotNull(quantizer.CurrentInterval);
Assert.Equal(context.DateTimeOffsetToClockOffset(now), quantizer.CurrentInterval!.ClockOffset);
quantizer.Advanced += Quantizer_Advanced;
// Execute
context.AdjustClock(adjusted);
// Test
Assert.True(advanceEventRaised);
void Quantizer_Advanced(object? sender, ClockQuantizer.NewIntervalEventArgs e)
{
advanceEventRaised = true;
// Test
Assert.Equal(adjusted, e.DateTimeOffset);
Assert.False(e.IsMetronomic);
Assert.True(e.GapToPriorIntervalExpectedEnd.HasValue);
Assert.Equal(adjusted - now - metronomeOptions.MaxIntervalTimeSpan, e.GapToPriorIntervalExpectedEnd!);
}
}
[Fact]
public void ClockQuantizer_WithTestClock_AdjustClockWithoutGap_RaisesAdvanceEventWithoutDetectedGapTimeSpan()
{
var metronomeOptions = new MetronomeOptions
{
IsManual = true,
MaxIntervalTimeSpan = TimeSpan.FromMinutes(5),
};
var now = DateTimeOffset.UtcNow;
var adjusted = now + TimeSpan.FromMinutes(5);
var context = new SystemClockTemporalContext(now, metronomeOptions);
var advanceEventRaised = false;
// Pre-requisites check
Assert.False(context.HasExternalClock);
var quantizer = new ClockQuantizer(context, metronomeOptions.MaxIntervalTimeSpan);
quantizer.Advance(); // Advance once to ensure that we have a CurrentInterval
Assert.NotNull(quantizer.CurrentInterval);
Assert.Equal(context.DateTimeOffsetToClockOffset(now), quantizer.CurrentInterval!.ClockOffset);
quantizer.Advanced += Quantizer_Advanced;
// Execute
context.AdjustClock(adjusted);
// Test
Assert.True(advanceEventRaised);
void Quantizer_Advanced(object? sender, ClockQuantizer.NewIntervalEventArgs e)
{
advanceEventRaised = true;
// Test
Assert.Equal(adjusted, e.DateTimeOffset);
Assert.False(e.IsMetronomic);
Assert.False(e.GapToPriorIntervalExpectedEnd.HasValue);
}
}
[Fact]
public void ClockQuantizer_WithExternalManualMetronome_FireMetronomeTicked_RaisesEventsInOrderWithExpectedDateTimeOffset()
{
var metronomeOptions = MetronomeOptions.Manual;
var context = new SystemClockTemporalContext(metronomeOptions);
var now = context.UtcNow;
// Pre-requisites check
Assert.False(context.HasExternalClock);
var quantizer = new ClockQuantizer(context, metronomeOptions.MaxIntervalTimeSpan);
quantizer.MetronomeTicked += Quantizer_MetronomeTicked;
quantizer.Advanced += Quantizer_Advanced;
DateTimeOffset advanceEventRaisedAt = default;
DateTimeOffset metronomeEventRaisedAt = default;
DateTimeOffset firedAt = DateTimeOffset.UtcNow;
context.FireMetronomeTicked();
// Assert that expected events were raised ...
Assert.True(advanceEventRaisedAt != default);
Assert.True(metronomeEventRaisedAt != default);
// ... in the correct order ...
Assert.True(advanceEventRaisedAt < metronomeEventRaisedAt);
// ... and after the metronome tick was fired
Assert.True(advanceEventRaisedAt > firedAt);
void Quantizer_Advanced(object? sender, ClockQuantizer.NewIntervalEventArgs e)
{
advanceEventRaisedAt = DateTimeOffset.UtcNow;
Assert.Equal(e.DateTimeOffset, now);
Assert.True(e.IsMetronomic);
}
void Quantizer_MetronomeTicked(object? sender, ClockQuantizer.NewIntervalEventArgs e)
{
metronomeEventRaisedAt = DateTimeOffset.UtcNow;
Assert.Equal(e.DateTimeOffset, now);
Assert.True(e.IsMetronomic);
}
}
[Fact]
public void ClockQuantizer_WithExternalManualMetronome_FireMetronomeTickedWithFutureDateTimeOffset_RaisesEventsInOrderWithExpectedDateTimeOffset()
{
var metronomeOptions = MetronomeOptions.Manual;
var context = new SystemClockTemporalContext(metronomeOptions);
var now = DateTimeOffset.UtcNow; // note, not the clock's UtcNow!
// Pre-requisites check
Assert.False(context.HasExternalClock);
var quantizer = new ClockQuantizer(context, metronomeOptions.MaxIntervalTimeSpan);
quantizer.Advanced += Quantizer_Advanced;
quantizer.MetronomeTicked += Quantizer_MetronomeTicked;
DateTimeOffset advanceEventRaisedAt = default;
DateTimeOffset metronomeEventRaisedAt = default;
DateTimeOffset firedAt = DateTimeOffset.UtcNow;
context.FireMetronomeTicked(now);
// Assert that expected events were raised ...
Assert.True(advanceEventRaisedAt != default);
Assert.True(metronomeEventRaisedAt != default);
// ... in the correct order ...
Assert.True(advanceEventRaisedAt < metronomeEventRaisedAt);
// ... and after the metronome tick was fired
Assert.True(advanceEventRaisedAt > firedAt);
void Quantizer_Advanced(object? sender, ClockQuantizer.NewIntervalEventArgs e)
{
advanceEventRaisedAt = DateTimeOffset.UtcNow;
Assert.Equal(e.DateTimeOffset, now);
Assert.True(e.IsMetronomic);
}
void Quantizer_MetronomeTicked(object? sender, ClockQuantizer.NewIntervalEventArgs e)
{
metronomeEventRaisedAt = DateTimeOffset.UtcNow;
Assert.Equal(e.DateTimeOffset, now);
Assert.True(e.IsMetronomic);
}
}
[Fact]
public void ClockQuantizer_WithExternalManualMetronome_FireMetronomeTickedWithFutureDateTimeOffsetAndGap_RaisesEventsInOrderWithExpectedDateTimeOffsetAndDetectedGap()
{
var metronomeOptions = MetronomeOptions.Manual;
var context = new SystemClockTemporalContext(metronomeOptions);
var now = DateTimeOffset.UtcNow; // note, not the clock's UtcNow!
var gap = now - (context.UtcNow + metronomeOptions.MaxIntervalTimeSpan);
// Pre-requisites check
Assert.False(context.HasExternalClock);
Assert.True(gap > TimeSpan.Zero);
var quantizer = new ClockQuantizer(context, metronomeOptions.MaxIntervalTimeSpan);
quantizer.Advance();
Assert.NotNull(quantizer.CurrentInterval);
quantizer.Advanced += Quantizer_Advanced;
quantizer.MetronomeTicked += Quantizer_MetronomeTicked;
DateTimeOffset advanceEventRaisedAt = default;
DateTimeOffset metronomeEventRaisedAt = default;
DateTimeOffset firedAt = DateTimeOffset.UtcNow;
context.FireMetronomeTicked(now);
// Assert that expected events were raised ...
Assert.True(advanceEventRaisedAt != default);
Assert.True(metronomeEventRaisedAt != default);
// ... in the correct order ...
Assert.True(advanceEventRaisedAt < metronomeEventRaisedAt);
// ... and after the metronome tick was fired
Assert.True(advanceEventRaisedAt > firedAt);
void Quantizer_Advanced(object? sender, ClockQuantizer.NewIntervalEventArgs e)
{
advanceEventRaisedAt = DateTimeOffset.UtcNow;
Assert.Equal(e.DateTimeOffset, now);
Assert.True(e.IsMetronomic);
Assert.True(e.GapToPriorIntervalExpectedEnd.HasValue);
Assert.Equal(gap, e.GapToPriorIntervalExpectedEnd);
}
void Quantizer_MetronomeTicked(object? sender, ClockQuantizer.NewIntervalEventArgs e)
{
metronomeEventRaisedAt = DateTimeOffset.UtcNow;
Assert.Equal(e.DateTimeOffset, now);
Assert.True(e.IsMetronomic);
Assert.True(e.GapToPriorIntervalExpectedEnd.HasValue);
Assert.Equal(gap, e.GapToPriorIntervalExpectedEnd);
}
}
[Fact]
public void ClockQuantizer_WithInternalMetronome_RaisesPeriodicMetronomeTickedEventsWithMetronomeJitterGapsIgnored()
{
// Juggling interval parameters to ensure we have an as little flaky as possible P95 test within approx. 1 second
const double targetP = 0.95;
const int intervalCount = 25;
var maxIntervalTimeSpan = TimeSpan.FromMilliseconds(42.5);
var tolerance = TimeSpan.FromMilliseconds(15); // this is just about Timer precision?
var context = new SystemClockTemporalContext(() => DateTimeOffset.UtcNow, metronomeOptions: null);
var quantizer = new ClockQuantizer(context, maxIntervalTimeSpan);
var considered = new System.Threading.Semaphore(intervalCount, intervalCount);
var visited = new System.Threading.Semaphore(0, intervalCount);
var list = new List<long>(intervalCount);
quantizer.MetronomeTicked += Quantizer_MetronomeTicked;
// Kick off!
var start = quantizer.Advance().ClockOffset;
for (int i = 0; i < intervalCount; i++)
{
visited.WaitOne(maxIntervalTimeSpan + TimeSpan.FromMilliseconds(50));
}
Assert.Equal(intervalCount, list.Count);
// Estimate weighed average skew
double weighedSkewSum = 0.0;
for (int i = 0; i < intervalCount; i++)
{
double skew = (double)list[i] - (i * maxIntervalTimeSpan.TotalMilliseconds * context.ClockOffsetUnitsPerMillisecond) - start;
var weighedSkewSumContribution = skew * (i + 1);
weighedSkewSum += weighedSkewSumContribution;
}
var weighedAverageSkew = weighedSkewSum / (intervalCount * (intervalCount + 1) / 2.0);
var jitters = 0;
var outliers = 0;
for (int i = 0; i < intervalCount; i++)
{
var delta = list[i] - (start + weighedAverageSkew);
var lower = (i * maxIntervalTimeSpan - tolerance).TotalMilliseconds * context.ClockOffsetUnitsPerMillisecond;
var upper = (i * maxIntervalTimeSpan + tolerance).TotalMilliseconds * context.ClockOffsetUnitsPerMillisecond;
if (!(delta >= lower && (delta <= upper)))
{
outliers++;
}
if (delta > upper)
{
jitters++;
}
}
#if false
// Ensure that we observed at least 1 jitter and validated that the gap was not registered in the event
Assert.True(jitters > 0, $"#jitters: {jitters}");
#endif
// Ensure targetP % of measurements within tolerance
var p = (double)outliers / (double)intervalCount;
Assert.True(p <= (1 - targetP), $"P{(int) (targetP * 100.0)} not achieved; actual: {1.0 - p}");
void Quantizer_MetronomeTicked(object? sender, ClockQuantizer.NewIntervalEventArgs e)
{
if (considered.WaitOne(0))
{
list.Add(context.DateTimeOffsetToClockOffset(e.DateTimeOffset));
// Jitter should not be administered on *internal* metronome events
Assert.False(e.GapToPriorIntervalExpectedEnd.HasValue);
visited.Release();
}
}
}
[Fact]
public void ClockQuantizer_EnsureInitializedExactClockOffsetSerialPosition_WithDefaultRef_ByDefinitionMustInitializeExactClockOffsetSerialPosition()
{
var metronomeOptions = MetronomeOptions.Manual;
var now = DateTimeOffset.UtcNow;
var context = new SystemClockTemporalContext(now, metronomeOptions);
// Pre-requisites check
Assert.False(context.HasExternalClock);
var quantizer = new ClockQuantizer(context, metronomeOptions.MaxIntervalTimeSpan);
var interval = quantizer.Advance();
// Execute
var position = default(LazyClockOffsetSerialPosition);
quantizer.EnsureInitializedExactClockOffsetSerialPosition(ref position, advance: false);
// Test
Assert.True(position.HasValue);
Assert.True(position.IsExact);
}
[Fact]
public void ClockQuantizer_EnsureInitializedExactClockOffsetSerialPosition_WithoutAdvanceAndWithExactRef_ByDefinitionRemainsUntouched()
{
var metronomeOptions = MetronomeOptions.Manual;
var now = DateTimeOffset.UtcNow;
var context = new SystemClockTemporalContext(now, metronomeOptions);
// Pre-requisites check
Assert.False(context.HasExternalClock);
var quantizer = new ClockQuantizer(context, metronomeOptions.MaxIntervalTimeSpan);
var interval = quantizer.Advance();
var position = default(LazyClockOffsetSerialPosition);
quantizer.EnsureInitializedExactClockOffsetSerialPosition(ref position, advance: false);
Assert.True(position.HasValue);
Assert.True(position.IsExact);
var copy = position;
// Execute
quantizer.EnsureInitializedExactClockOffsetSerialPosition(ref position, advance: false);
// Test
Assert.Equal(copy, position);
}
[Fact]
public void ClockQuantizer_EnsureInitializedExactClockOffsetSerialPosition_WithAdvanceAndWithExactRef_ByDefinitionRemainsUntouchedAndDoesNotAdvanceNorRaiseAdvancedEvent()
{
var metronomeOptions = MetronomeOptions.Manual;
var now = DateTimeOffset.UtcNow;
var context = new SystemClockTemporalContext(now, metronomeOptions);
var visited = new System.Threading.ManualResetEvent(false);
// Pre-requisites check
Assert.False(context.HasExternalClock);
var quantizer = new ClockQuantizer(context, metronomeOptions.MaxIntervalTimeSpan);
var interval = quantizer.Advance();
var position = default(LazyClockOffsetSerialPosition);
quantizer.EnsureInitializedExactClockOffsetSerialPosition(ref position, advance: false);
Assert.True(position.HasValue);
Assert.True(position.IsExact);
var copy = position;
quantizer.Advanced += Quantizer_Advanced;
// Execute
quantizer.EnsureInitializedExactClockOffsetSerialPosition(ref position, advance: true);
Assert.Same(interval, quantizer.CurrentInterval);
// Test
Assert.Equal(copy, position);
Assert.False(visited.WaitOne(TimeSpan.FromMilliseconds(250)));
void Quantizer_Advanced(object? sender, ClockQuantizer.NewIntervalEventArgs e)
{
// We should not get here...
visited.Set();
}
}
[Fact]
public void ClockQuantizer_EnsureInitializedExactClockOffsetSerialPosition_WithNonExactRef_ByDefinitionMustReInitializeExactClockOffsetSerialPosition()
{
var metronomeOptions = MetronomeOptions.Manual;
var now = DateTimeOffset.UtcNow;
var context = new SystemClockTemporalContext(now, metronomeOptions);
// Pre-requisites check
Assert.False(context.HasExternalClock);
var quantizer = new ClockQuantizer(context, metronomeOptions.MaxIntervalTimeSpan);
var interval = quantizer.Advance();
var position = default(LazyClockOffsetSerialPosition);
quantizer.EnsureInitializedClockOffsetSerialPosition(ref position);
Assert.True(position.HasValue);
Assert.False(position.IsExact);
var copy = position;
// Execute
quantizer.EnsureInitializedExactClockOffsetSerialPosition(ref position, advance: false);
// Test
Assert.NotEqual(copy, position);
Assert.True(position.IsExact);
}
[Fact]
public void ClockQuantizer_EnsureInitializedClockOffsetSerialPosition_AfterFirstAdvanceWithDefaultRef_ByDefinitionMustInitializeNonExactClockOffsetSerialPosition()
{
var metronomeOptions = MetronomeOptions.Manual;
var now = DateTimeOffset.UtcNow;
var context = new SystemClockTemporalContext(now, metronomeOptions);
// Pre-requisites check
Assert.False(context.HasExternalClock);
var quantizer = new ClockQuantizer(context, metronomeOptions.MaxIntervalTimeSpan);
quantizer.Advance();
// Execute
var position = default(LazyClockOffsetSerialPosition);
quantizer.EnsureInitializedClockOffsetSerialPosition(ref position);
// Test
Assert.True(position.HasValue);
Assert.False(position.IsExact);
Assert.Equal(quantizer.CurrentInterval!.ClockOffset, position.ClockOffset);
}
[Fact]
public void ClockQuantizer_EnsureInitializedClockOffsetSerialPosition_BeforeFirstAdvanceWithDefaultRef_ByDefinitionMustInitializeExactClockOffsetSerialPosition()
{
var metronomeOptions = MetronomeOptions.Manual;
var now = DateTimeOffset.UtcNow;
var context = new SystemClockTemporalContext(now, metronomeOptions);
// Pre-requisites check
Assert.False(context.HasExternalClock);
var quantizer = new ClockQuantizer(context, metronomeOptions.MaxIntervalTimeSpan);
// Execute
var position = default(LazyClockOffsetSerialPosition);
quantizer.EnsureInitializedClockOffsetSerialPosition(ref position);
// Test
Assert.True(position.HasValue);
Assert.True(position.IsExact);
Assert.Null(quantizer.CurrentInterval);
}
[Fact]
public void ClockQuantizer_EnsureInitializedClockOffsetSerialPosition_WithExactRef_ByDefinitionRemainsUntouched()
{
var metronomeOptions = MetronomeOptions.Manual;
var now = DateTimeOffset.UtcNow;
var context = new SystemClockTemporalContext(now, metronomeOptions);
// Pre-requisites check
Assert.False(context.HasExternalClock);
var quantizer = new ClockQuantizer(context, metronomeOptions.MaxIntervalTimeSpan);
var interval = quantizer.Advance();
var position = default(LazyClockOffsetSerialPosition);
quantizer.EnsureInitializedExactClockOffsetSerialPosition(ref position, advance: false);
Assert.True(position.HasValue);
Assert.True(position.IsExact);
var copy = position;
// Execute
quantizer.EnsureInitializedClockOffsetSerialPosition(ref position);
// Test
Assert.Equal(copy, position);
}
[Fact]
public void ClockQuantizer_EnsureInitializedClockOffsetSerialPosition_WithNonExactRef_ByDefinitionRemainsUntouched()
{
var metronomeOptions = MetronomeOptions.Manual;
var now = DateTimeOffset.UtcNow;
var context = new SystemClockTemporalContext(now, metronomeOptions);
// Pre-requisites check
Assert.False(context.HasExternalClock);
var quantizer = new ClockQuantizer(context, metronomeOptions.MaxIntervalTimeSpan);
var interval = quantizer.Advance();
var position = default(LazyClockOffsetSerialPosition);
quantizer.EnsureInitializedClockOffsetSerialPosition(ref position);
Assert.True(position.HasValue);
Assert.False(position.IsExact);
var copy = position;
// Execute
quantizer.EnsureInitializedClockOffsetSerialPosition(ref position);
// Test
Assert.Equal(copy, position);
}
[Fact]
public void ClockQuantizer_Advance_YieldsNewSealedIntervalAndRaisesEvent()
{
var metronomeOptions = MetronomeOptions.Manual;
var context = new SystemClockTemporalContext(() => DateTimeOffset.UtcNow, metronomeOptions);
var isAdvancedEventRaised = false;
// Pre-requisites check
Assert.True(context.HasExternalClock);
var quantizer = new ClockQuantizer(context, metronomeOptions.MaxIntervalTimeSpan);
var interval1 = quantizer.Advance();
quantizer.Advanced += Quantizer_Advanced;
// Execute
var interval2 = quantizer.Advance();
// Test
Assert.NotSame(interval1, interval2);
Assert.True(interval1.ClockOffset <= interval2.ClockOffset);
// Ensure interval2 was sealed (and hence a new position can by definition not be "exact")
var position = default(LazyClockOffsetSerialPosition);
Interval.EnsureInitializedClockOffsetSerialPosition(interval2, ref position);
Assert.False(position.IsExact);
Assert.True(isAdvancedEventRaised);
void Quantizer_Advanced(object? sender, ClockQuantizer.NewIntervalEventArgs e)
{
isAdvancedEventRaised = true;
}
}
[Fact]
public void ClockQuantizer_EnsureInitializedExactClockOffsetSerialPosition_WithoutAdvanceAndWithDefaultRef_DoesNotAdvanceCurrentInterval()
{
var metronomeOptions = MetronomeOptions.Manual;
var context = new SystemClockTemporalContext(() => DateTimeOffset.UtcNow, metronomeOptions);
// Pre-requisites check
Assert.True(context.HasExternalClock);
var quantizer = new ClockQuantizer(context, metronomeOptions.MaxIntervalTimeSpan);
var interval = quantizer.Advance();
Assert.Same(interval, quantizer.CurrentInterval);
// Execute
var position = default(LazyClockOffsetSerialPosition);
quantizer.EnsureInitializedExactClockOffsetSerialPosition(ref position, advance: false);
// Test
Assert.True(position.HasValue);
Assert.True(position.IsExact);
Assert.Same(interval, quantizer.CurrentInterval);
Assert.True(quantizer.CurrentInterval!.ClockOffset <= position.ClockOffset);
}
[Fact]
public void ClockQuantizer_EnsureInitializedExactClockOffsetSerialPosition_WithAdvanceAndWithDefaultRef_AdvancesCurrentIntervalAndRaisesEvent()
{
var metronomeOptions = MetronomeOptions.Manual;
var context = new SystemClockTemporalContext(() => DateTimeOffset.UtcNow, metronomeOptions);
var isAdvancedEventRaised = false;
// Pre-requisites check
Assert.True(context.HasExternalClock);
var quantizer = new ClockQuantizer(context, metronomeOptions.MaxIntervalTimeSpan);
var interval = quantizer.Advance();
Assert.Same(interval, quantizer.CurrentInterval);
quantizer.Advanced += Quantizer_Advanced;
// Execute
var position = default(LazyClockOffsetSerialPosition);
quantizer.EnsureInitializedExactClockOffsetSerialPosition(ref position, advance: true);
// Test
Assert.True(position.HasValue);
Assert.True(position.IsExact);
Assert.NotSame(interval, quantizer.CurrentInterval);
Assert.True(quantizer.CurrentInterval!.ClockOffset == position.ClockOffset);
Assert.True(isAdvancedEventRaised);
void Quantizer_Advanced(object? sender, ClockQuantizer.NewIntervalEventArgs e)
{
isAdvancedEventRaised = true;
}
}
[Fact]
public void ClockQuantizer_EnsureInitializedExactClockOffsetSerialPosition_WithAdvanceAndWithDefaultRef_DoesNotGetBrokenByInteractionInAdvancedEvent()
{
var metronomeOptions = MetronomeOptions.Manual;
var context = new SystemClockTemporalContext(() => DateTimeOffset.UtcNow, metronomeOptions);
var isAdvancedEventRaised = false;
// Pre-requisites check
Assert.True(context.HasExternalClock);
var quantizer = new ClockQuantizer(context, metronomeOptions.MaxIntervalTimeSpan);
var interval = quantizer.Advance();
Assert.Same(interval, quantizer.CurrentInterval);
quantizer.Advanced += Quantizer_Advanced;
// Execute
var position = default(LazyClockOffsetSerialPosition);
quantizer.EnsureInitializedExactClockOffsetSerialPosition(ref position, advance: true);
// Test
Assert.True(position.HasValue);
Assert.True(position.IsExact);
Assert.NotSame(interval, quantizer.CurrentInterval);
// quantizer.CurrentInterval advanced once more in the event handler, so we cannot validate position.Offset against it
Assert.True(quantizer.CurrentInterval!.ClockOffset >= position.ClockOffset);
Assert.True(isAdvancedEventRaised);
void Quantizer_Advanced(object? sender, ClockQuantizer.NewIntervalEventArgs e)
{
var recurse = !isAdvancedEventRaised;
isAdvancedEventRaised = true;
// Let's see if we can break the advance/serial logic
var currentInterval = quantizer.CurrentInterval;
// Test that quantizer.CurrentInterval already advanced
Assert.NotNull(currentInterval);
Assert.NotSame(interval, currentInterval);
var interferingClockOffsetSerialPosition1 = default(LazyClockOffsetSerialPosition);
Interval.EnsureInitializedClockOffsetSerialPosition(interval, ref interferingClockOffsetSerialPosition1);
Assert.True(interferingClockOffsetSerialPosition1.HasValue);
var interferingClockOffsetSerialPosition2 = currentInterval!.NewClockOffsetSerialPosition();
Assert.True(interferingClockOffsetSerialPosition2.HasValue);
var interferingClockOffsetSerialPosition3 = default(LazyClockOffsetSerialPosition);
quantizer.EnsureInitializedExactClockOffsetSerialPosition(ref interferingClockOffsetSerialPosition3, advance: recurse /* let's not recurse more than once... */);
Assert.True(interferingClockOffsetSerialPosition3.IsExact);
}
}
}
}