Skip to content

Commit

Permalink
added actual invocaton
Browse files Browse the repository at this point in the history
  • Loading branch information
joshika39 committed Apr 17, 2024
1 parent 08a2502 commit f424d83
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion _src/Implementation/Time/DefaultStopwatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal sealed class DefaultStopwatch : IStopwatch
private readonly ICollection<IPeriodicStopwatch> _periodicStopwatches;
private bool _disposed;
private readonly ICollection<ITickListener> _listeners;
private readonly string _name = Guid.NewGuid().ToString();

public bool IsRunning => _stopwatch.IsRunning;
public TimeSpan Elapsed { get; private set; }
Expand All @@ -44,7 +45,8 @@ public void Wait(int periodInMilliseconds, ITickListener listener)
{
if (Elapsed.TotalMilliseconds > startedTime.TotalMilliseconds + periodInMilliseconds)
{
listener.RaiseTick(0);
listener.RaiseTick(this, _name, -1);
listener.RaiseTick(-1);
break;
}
}
Expand All @@ -59,6 +61,7 @@ await Task.Run(() =>
{
if (Elapsed.TotalMilliseconds > startedTime.TotalMilliseconds + periodInMilliseconds)
{
listener.RaiseTick(this, _name, -1);
listener.RaiseTick(-1);
break;
}
Expand Down
1 change: 1 addition & 0 deletions _src/Implementation/Time/PeriodicStopwatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public void Start()
foreach (var listener in _listeners)
{
listener.RaiseTick(_round);
listener.RaiseTick(Parent, Name, _round);
listener.ElapsedTime = Elapsed;
}

Expand Down
18 changes: 18 additions & 0 deletions _src/_Tests/ImplementationTest/Time/StopwatchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,23 @@ public void ST_0011_Given_Stopwatch_When_ManyListeners_Then_NotAffectsPerformanc
Assert.False(stopwatch.IsRunning);
Assert.False(pWatch1.IsRunning);
}

[Fact]
public async Task ST_0021_Given_Stopwatch_When_WaitAsyncIsCalled_Then_ListenerGetsNotifiedWithNamedRaiseTick()
{
var listenerMock = new Mock<ITickListener>();
var cancellationTokenSource = new CancellationTokenSource();
using IStopwatch stopwatch = new DefaultStopwatch(cancellationTokenSource.Token);
Assert.NotNull(stopwatch);
stopwatch.Start();
for (var i = 0; i < 10; i++)
{
await stopwatch.WaitAsync(250, listenerMock.Object);
}

stopwatch.Stop();
Assert.True(stopwatch.Elapsed.TotalMilliseconds > 2500);
listenerMock.Verify(l => l.RaiseTick(-1), Times.Exactly(10));
}
}
}

0 comments on commit f424d83

Please sign in to comment.