Skip to content

Commit

Permalink
refactored async periodic timer
Browse files Browse the repository at this point in the history
  • Loading branch information
joshika39 committed Dec 18, 2023
1 parent 3b07635 commit 749df72
Show file tree
Hide file tree
Showing 17 changed files with 220 additions and 125 deletions.
54 changes: 0 additions & 54 deletions .github/workflows/modules-cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,6 @@ on:
- "*v*"

jobs:
build:
name: Build
env:
BUILD_CONFIG: 'Debug'
SOLUTION: 'Modules.sln'
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
submodules: recursive
- name: 'Get Version'
id: get_version
uses: battila7/get-version-action@v2
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 1.11
- name: Cache SonarQube packages
uses: actions/cache@v1
with:
path: ~\sonar\cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache SonarQube scanner
id: cache-sonar-scanner
uses: actions/cache@v1
with:
path: .\.sonar\scanner
key: ${{ runner.os }}-sonar-scanner
restore-keys: ${{ runner.os }}-sonar-scanner
- name: Install SonarQube scanner
if: steps.cache-sonar-scanner.outputs.cache-hit != 'true'
shell: powershell
run: |
New-Item -Path .\.sonar\scanner -ItemType Directory
dotnet tool update dotnet-sonarscanner --tool-path .\.sonar\scanner
- name: Install dotCover Global
shell: powershell
run: |
dotnet tool install --global JetBrains.dotCover.GlobalTool
- name: Build and analyze
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
shell: powershell
run: |
Write-Output ${{ steps.get_version.outputs.version-without-v }}
.\.sonar\scanner\dotnet-sonarscanner begin /k:"${{ secrets.SONAR_PROJECT }}" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="${{ secrets.SONAR_HOST_URL }}" /v:"${{ steps.get_version.outputs.version-without-v }}"
dotnet build $env:SOLUTION --configuration $env:BUILD_CONFIG
dotnet dotcover test $env:SOLUTION --dcReportType=HTML --dotCoverFilters="-:*Tests;-:testhost"
.\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"
deploy:
name: 'Build, Test & Deploy'
env:
Expand All @@ -71,9 +20,6 @@ jobs:
with:
fetch-depth: 0
submodules: recursive
- name: 'Get Version'
id: get_version
uses: battila7/get-version-action@v2

- name: Install .NET Core
uses: actions/setup-dotnet@v3
Expand Down
2 changes: 1 addition & 1 deletion Nuget/.projects.conf
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Modules:1.3.3
Modules:1.3.4
5 changes: 2 additions & 3 deletions _src/Implementation/IO/Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,8 @@ public static string ReadLine(StreamReader streamReader, out bool fromConsole, b
var readInt = streamReader.Read();
var readChar = (char)readInt;
sb.Append(readChar);
var test1 = LastChars(sb, 2);


var test1 = LastChars(sb, Environment.NewLine.Length);

if (!useEndOfFile && test1 == Environment.NewLine)
{
return sb.ToString();
Expand Down
42 changes: 37 additions & 5 deletions _src/Implementation/Time/DefaultStopwatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Implementation.Time.Factories;
using Infrastructure.Time;
using Infrastructure.Time.Factories;
using Infrastructure.Time.Listeners;

namespace Implementation.Time
Expand All @@ -13,7 +15,7 @@ internal sealed class DefaultStopwatch : IStopwatch
{
private readonly CancellationToken _cancellationToken;
private readonly Stopwatch _stopwatch;
private readonly ICollection<PeriodicStopwatch> _periodicStopwatches;
private readonly ICollection<IPeriodicStopwatch> _periodicStopwatches;
private bool _disposed;
private readonly ICollection<ITickListener> _listeners;

Expand All @@ -23,7 +25,7 @@ internal sealed class DefaultStopwatch : IStopwatch
public DefaultStopwatch(CancellationToken cancellationToken)
{
_cancellationToken = cancellationToken;
_periodicStopwatches = new List<PeriodicStopwatch>();
_periodicStopwatches = new List<IPeriodicStopwatch>();
_stopwatch = new Stopwatch();
_listeners = new List<ITickListener>();
}
Expand Down Expand Up @@ -57,7 +59,7 @@ public async Task WaitAsync(int periodInMilliseconds, ITickListener listener)
{
if (Elapsed.TotalMilliseconds > startedTime.TotalMilliseconds + periodInMilliseconds)
{
listener.RaiseTick(0);
listener.RaiseTick(-1);
break;
}
}
Expand Down Expand Up @@ -102,14 +104,43 @@ public void Reset()
periodicStopwatch.Reset();
}
}

public void PeriodicOperation(int periodInMilliseconds, ITickListener listener, CancellationToken cancellationToken)
{
var periodicStopwatch = new PeriodicStopwatch(periodInMilliseconds, listener, cancellationToken);
var periodicStopwatch = new PeriodicStopwatch(this, periodInMilliseconds, listener, cancellationToken);
_periodicStopwatches.Add(periodicStopwatch);
periodicStopwatch.Start();
}

public IPeriodicStopwatchFactory GetPeriodicStopwatchFactory()
{
return new PeriodicStopwatchFactory(this, _cancellationToken);
}

public void UnregisterStopwatch(IPeriodicStopwatch stopwatch)
{
if (_periodicStopwatches.Contains(stopwatch))
{
_periodicStopwatches.Remove(stopwatch);
}
}

public void RegisterStopwatch(IPeriodicStopwatch stopwatch)
{
if (!_periodicStopwatches.Contains(stopwatch))
{
_periodicStopwatches.Add(stopwatch);
}
}

public IPeriodicStopwatch PeriodicOperation(int periodInMilliseconds, CancellationToken cancellationToken)
{
var periodicStopwatch = new PeriodicStopwatch(this, periodInMilliseconds, cancellationToken);
_periodicStopwatches.Add(periodicStopwatch);
periodicStopwatch.Start();
return periodicStopwatch;
}

public void AttachListener(ITickListener listener)
{
_listeners.Add(listener);
Expand All @@ -128,6 +159,7 @@ private void Dispose(bool disposing)
{
var stopwatch = _periodicStopwatches.First();
_periodicStopwatches.Remove(stopwatch);
stopwatch.Dispose();
}

while (_listeners.Any())
Expand Down
25 changes: 25 additions & 0 deletions _src/Implementation/Time/Factories/PeriodicStopwatchFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Threading;
using Infrastructure.Time;
using Infrastructure.Time.Factories;

namespace Implementation.Time.Factories;

public class PeriodicStopwatchFactory : IPeriodicStopwatchFactory
{
private readonly IStopwatch _parent;
private readonly CancellationToken _token;

public PeriodicStopwatchFactory(IStopwatch parent, CancellationToken token)
{
_parent = parent ?? throw new ArgumentNullException(nameof(parent));
_token = token;
}

public IPeriodicStopwatch CreatePeriodicStopwatch(int periodInMilliseconds)
{
var periodicStopwatch = new PeriodicStopwatch(_parent, periodInMilliseconds, _token);
_parent.RegisterStopwatch(periodicStopwatch);
return periodicStopwatch;
}
}
1 change: 0 additions & 1 deletion _src/Implementation/Time/Factories/StopwatchFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace Implementation.Time.Factories
{
internal class StopwatchFactory : IStopWatchFactory
{

public IStopwatch CreateStopwatch(CancellationToken token)
{
return new DefaultStopwatch(token);
Expand Down
33 changes: 31 additions & 2 deletions _src/Implementation/Time/PeriodicStopwatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,42 @@ namespace Implementation.Time
internal class PeriodicStopwatch : IPeriodicStopwatch
{
private readonly Stopwatch _stopwatch;
private readonly IStopwatch _parent;
private int _periodInMilliseconds;
private readonly CancellationToken _cancellationToken;
private int _round;
public TimeSpan Elapsed { get; private set; }
private readonly ICollection<ITickListener> _listeners;

public PeriodicStopwatch(int periodInMilliseconds, ITickListener listener, CancellationToken cancellationToken)
public bool IsRunning => _parent.IsRunning;

public TimeSpan Elapsed { get; private set; }

public PeriodicStopwatch(IStopwatch parent, int periodInMilliseconds, ITickListener listener, CancellationToken cancellationToken)
{
_parent = parent ?? throw new ArgumentNullException(nameof(parent));
_periodInMilliseconds = periodInMilliseconds;
_cancellationToken = cancellationToken;
_stopwatch = new Stopwatch();
_listeners = new List<ITickListener>();
_listeners.Add(listener);
}

public PeriodicStopwatch(IStopwatch parent, int periodInMilliseconds, CancellationToken cancellationToken)
{
_periodInMilliseconds = periodInMilliseconds;
_cancellationToken = cancellationToken;
_parent = parent ?? throw new ArgumentNullException(nameof(parent));
_stopwatch = new Stopwatch();
_listeners = new List<ITickListener>();
}

public void Start()
{
if (!_parent.IsRunning)
{
throw new InvalidOperationException("Parent stopwatch is not running");
}

_stopwatch.Start();
Task.Run(() =>
{
Expand All @@ -38,6 +57,7 @@ public void Start()
foreach (var listener in _listeners)
{
listener.RaiseTick(_round);
listener.ElapsedTime = Elapsed;
}
Elapsed += _stopwatch.Elapsed;
_stopwatch.Restart();
Expand Down Expand Up @@ -76,5 +96,14 @@ public void AttachListener(ITickListener listener)
{
_listeners.Add(listener);
}

public void Dispose()
{
_stopwatch.Stop();
_stopwatch.Reset();
_listeners.Clear();
_parent.UnregisterStopwatch(this);
GC.SuppressFinalize(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Threading;

namespace Infrastructure.Time.Factories;

public interface IPeriodicStopwatchFactory
{
IPeriodicStopwatch CreatePeriodicStopwatch(int periodInMilliseconds);
}
15 changes: 15 additions & 0 deletions _src/Infrastructure/Time/IAdjustableStopwatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using Infrastructure.Time.Listeners;

namespace Infrastructure.Time;

public interface IAdjustableStopwatch
{
bool IsRunning { get; }
TimeSpan Elapsed { get; }
void Start();
void Stop();
void Reset();
void AttachListener(ITickListener listener);

}
11 changes: 2 additions & 9 deletions _src/Infrastructure/Time/IPeriodicStopwatch.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
using System;
using Infrastructure.Time.Listeners;

namespace Infrastructure.Time
{
public interface IPeriodicStopwatch
public interface IPeriodicStopwatch : IAdjustableStopwatch, IDisposable
{
TimeSpan Elapsed { get; }

void Start();
void ChangePeriod(int periodInMilliseconds);
void Stop();
void Resume();
void Reset();

void AttachListener(ITickListener listener);

}
}
16 changes: 5 additions & 11 deletions _src/Infrastructure/Time/IStopwatch.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Infrastructure.Time.Factories;
using Infrastructure.Time.Listeners;

namespace Infrastructure.Time
{
public interface IStopwatch : IDisposable
public interface IStopwatch : IAdjustableStopwatch, IDisposable
{
bool IsRunning { get; }
TimeSpan Elapsed { get; }

void Wait(int periodInMilliseconds, ITickListener listener);
Task WaitAsync(int periodInMilliseconds, ITickListener listener);

void Start();
void Stop();
void Reset();

void PeriodicOperation(int periodInMilliseconds, ITickListener listener, CancellationToken cancellationToken);

void AttachListener(ITickListener listener);
IPeriodicStopwatchFactory GetPeriodicStopwatchFactory();
void RegisterStopwatch(IPeriodicStopwatch stopwatch);
void UnregisterStopwatch(IPeriodicStopwatch stopwatch);
}
}
Loading

0 comments on commit 749df72

Please sign in to comment.