PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Driver.Core.Tests/Core/Misc/MetronomeTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 128 lines | 99 code | 15 blank | 14 comment | 0 complexity | ba240b4080a13c84e23b4fd5462e688a MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2013-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using FluentAssertions;
  19. using MongoDB.Driver.Core.Async;
  20. using MongoDB.Driver.Core.Misc;
  21. using Xunit;
  22. namespace MongoDB.Driver.Core.Async
  23. {
  24. public class MetronomeTests
  25. {
  26. private FrozenClock _clock;
  27. private readonly TimeSpan _halfPeriod = TimeSpan.FromMilliseconds(500);
  28. private readonly TimeSpan _period = TimeSpan.FromMilliseconds(1000);
  29. private readonly TimeSpan _quarterPeriod = TimeSpan.FromMilliseconds(250);
  30. private Metronome _subject;
  31. private readonly TimeSpan _threeQuarterPeriod = TimeSpan.FromMilliseconds(750);
  32. public MetronomeTests()
  33. {
  34. _clock = FrozenClock.FreezeUtcNow();
  35. _subject = new Metronome(_period, _clock);
  36. }
  37. [Fact]
  38. public void Constructor_should_initialize_instance()
  39. {
  40. _subject.NextTick.Should().Be(_clock.UtcNow);
  41. _subject.Period.Should().Be(_period);
  42. }
  43. [Fact]
  44. public void Constructor_should_throw_if_clock_is_null()
  45. {
  46. Action act = () => new Metronome(_period, null);
  47. act.ShouldThrow<ArgumentNullException>();
  48. }
  49. [Fact]
  50. public void Constructor_should_throw_if_period_is_negative()
  51. {
  52. var period = TimeSpan.FromMilliseconds(-2);
  53. Action act = () => new Metronome(period);
  54. act.ShouldThrow<ArgumentException>();
  55. }
  56. [Fact]
  57. public void GetNextTickDelay_should_be_infinite_if_period_is_infinite()
  58. {
  59. var period = Timeout.InfiniteTimeSpan;
  60. var subject = new Metronome(period);
  61. subject.GetNextTickDelay().Should().Be(Timeout.InfiniteTimeSpan);
  62. }
  63. [Fact]
  64. public void GetNextTickDelay_should_be_threeQuarterPeriod_when_oneQuarterPeriod_past_the_last_tick()
  65. {
  66. var now = _clock.UtcNow;
  67. _clock.UtcNow = _clock.UtcNow.Add(_quarterPeriod);
  68. _subject.GetNextTickDelay().Should().Be(_threeQuarterPeriod);
  69. _subject.NextTick.Should().Be(now + _period);
  70. }
  71. [Fact]
  72. public void GetNextTickDelay_should_be_zero_when_first_instantiated()
  73. {
  74. _subject.GetNextTickDelay().Should().Be(TimeSpan.Zero);
  75. _subject.NextTick.Should().Be(_clock.UtcNow);
  76. }
  77. [Fact]
  78. public void GetNextTickDelay_should_be_zero_when_time_equals_nextTick()
  79. {
  80. var now = _clock.UtcNow;
  81. _clock.UtcNow = _clock.UtcNow.Add(_period);
  82. _subject.GetNextTickDelay().Should().Be(TimeSpan.Zero);
  83. _subject.NextTick.Should().Be(now + _period);
  84. }
  85. [Fact]
  86. public void GetNextTickDelay_should_not_advance_nextTick_when_called_more_than_once_during_the_same_period()
  87. {
  88. var now = _clock.UtcNow;
  89. _clock.UtcNow = _clock.UtcNow.Add(_quarterPeriod);
  90. _subject.GetNextTickDelay().Should().Be(_threeQuarterPeriod);
  91. _subject.NextTick.Should().Be(now + _period);
  92. _clock.UtcNow = _clock.UtcNow.Add(_quarterPeriod);
  93. _subject.GetNextTickDelay().Should().Be(_halfPeriod);
  94. _subject.NextTick.Should().Be(now + _period);
  95. }
  96. [Fact]
  97. public void GetNextTickDelay_should_skip_one_missed_tick()
  98. {
  99. var now = _clock.UtcNow;
  100. _clock.UtcNow = _clock.UtcNow.Add(_period + _quarterPeriod);
  101. _subject.GetNextTickDelay().Should().Be(_threeQuarterPeriod);
  102. _subject.NextTick.Should().Be(now + _period + _period);
  103. }
  104. [Fact]
  105. public void GetNextTickDelay_should_skip_two_missed_ticks()
  106. {
  107. var now = _clock.UtcNow;
  108. _clock.UtcNow = _clock.UtcNow.Add(_period + _period + _quarterPeriod);
  109. _subject.GetNextTickDelay().Should().Be(_threeQuarterPeriod);
  110. _subject.NextTick.Should().Be(now + _period + _period + _period);
  111. }
  112. }
  113. }