diff --git a/src/SecTester.Core/Utils/ITimerProvider.cs b/src/SecTester.Core/Utils/ITimerProvider.cs deleted file mode 100644 index d4228e3..0000000 --- a/src/SecTester.Core/Utils/ITimerProvider.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.Timers; - -namespace SecTester.Core.Utils; - -public interface ITimerProvider -{ - void Start(); - void Stop(); - double Interval { get; set; } - event ElapsedEventHandler Elapsed; -} diff --git a/src/SecTester.Core/Utils/SystemTimerProvider.cs b/src/SecTester.Core/Utils/SystemTimerProvider.cs deleted file mode 100644 index ad1d9ec..0000000 --- a/src/SecTester.Core/Utils/SystemTimerProvider.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System.Timers; - -namespace SecTester.Core.Utils; - -public class SystemTimerProvider : Timer, ITimerProvider -{ - public SystemTimerProvider() - { - } - - public SystemTimerProvider(double interval) : base(interval) - { - } -} diff --git a/src/SecTester.Repeater/Bus/DefaultRepeaterBusFactory.cs b/src/SecTester.Repeater/Bus/DefaultRepeaterBusFactory.cs index db3ae74..9e79d0c 100644 --- a/src/SecTester.Repeater/Bus/DefaultRepeaterBusFactory.cs +++ b/src/SecTester.Repeater/Bus/DefaultRepeaterBusFactory.cs @@ -1,8 +1,6 @@ using System; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using SecTester.Core; -using SecTester.Core.Utils; using SocketIO.Serializer.MessagePack; using SocketIOClient; using SocketIOClient.Transport; @@ -13,13 +11,11 @@ public class DefaultRepeaterBusFactory : IRepeaterBusFactory { private readonly Configuration _config; private readonly ILoggerFactory _loggerFactory; - private readonly IServiceScopeFactory _scopeFactory; - public DefaultRepeaterBusFactory(Configuration config, ILoggerFactory loggerFactory, IServiceScopeFactory scopeFactory) + public DefaultRepeaterBusFactory(Configuration config, ILoggerFactory loggerFactory) { _config = config ?? throw new ArgumentNullException(nameof(config)); _loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory)); - _scopeFactory = scopeFactory ?? throw new ArgumentNullException(nameof(scopeFactory)); } public IRepeaterBus Create(string repeaterId) @@ -46,9 +42,6 @@ public IRepeaterBus Create(string repeaterId) }; var wrapper = new SocketIoConnection(client); - var scope = _scopeFactory.CreateAsyncScope(); - var timerProvider = scope.ServiceProvider.GetRequiredService(); - - return new SocketIoRepeaterBus(options, wrapper, timerProvider, _loggerFactory.CreateLogger()); + return new SocketIoRepeaterBus(options, wrapper, _loggerFactory.CreateLogger()); } } diff --git a/src/SecTester.Repeater/Bus/SocketIoRepeaterBus.cs b/src/SecTester.Repeater/Bus/SocketIoRepeaterBus.cs index db5e7fd..2f027a0 100644 --- a/src/SecTester.Repeater/Bus/SocketIoRepeaterBus.cs +++ b/src/SecTester.Repeater/Bus/SocketIoRepeaterBus.cs @@ -2,9 +2,7 @@ using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; -using System.Timers; using Microsoft.Extensions.Logging; -using SecTester.Core.Utils; namespace SecTester.Repeater.Bus; @@ -12,16 +10,14 @@ internal sealed class SocketIoRepeaterBus : IRepeaterBus { private static readonly TimeSpan PingInterval = TimeSpan.FromSeconds(10); - private readonly ITimerProvider _heartbeat; private readonly ISocketIoConnection _connection; private readonly ILogger _logger; private readonly SocketIoRepeaterBusOptions _options; - internal SocketIoRepeaterBus(SocketIoRepeaterBusOptions options, ISocketIoConnection connection, ITimerProvider heartbeat, ILogger logger) + internal SocketIoRepeaterBus(SocketIoRepeaterBusOptions options, ISocketIoConnection connection, ILogger logger) { _options = options ?? throw new ArgumentNullException(nameof(options)); _connection = connection ?? throw new ArgumentNullException(nameof(connection)); - _heartbeat = heartbeat ?? throw new ArgumentNullException(nameof(heartbeat)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } @@ -37,8 +33,6 @@ public async Task Connect() await _connection.Connect().ConfigureAwait(false); - await SchedulePing().ConfigureAwait(false); - _logger.LogDebug("Repeater connected to {BaseUrl}", _options.BaseUrl); } } @@ -75,8 +69,6 @@ public async ValueTask DisposeAsync() { if (_connection is { Connected: true }) { - _heartbeat.Elapsed -= Ping; - _heartbeat.Stop(); await _connection.Disconnect().ConfigureAwait(false); _logger.LogDebug("Repeater disconnected from {BaseUrl}", _options.BaseUrl); } @@ -111,22 +103,4 @@ public async Task Deploy(string repeaterId, CancellationToken? cancellationToken _connection.Off("deployed"); } } - - private async Task SchedulePing() - { - await Ping().ConfigureAwait(false); - _heartbeat.Interval = PingInterval.TotalMilliseconds; - _heartbeat.Elapsed += Ping; - _heartbeat.Start(); - } - - private async void Ping(object sender, ElapsedEventArgs args) - { - await Ping().ConfigureAwait(false); - } - - private async Task Ping() - { - await _connection.EmitAsync("ping").ConfigureAwait(false); - } } diff --git a/src/SecTester.Repeater/Extensions/ServiceCollectionExtensions.cs b/src/SecTester.Repeater/Extensions/ServiceCollectionExtensions.cs index 92dd443..cff39b6 100644 --- a/src/SecTester.Repeater/Extensions/ServiceCollectionExtensions.cs +++ b/src/SecTester.Repeater/Extensions/ServiceCollectionExtensions.cs @@ -23,7 +23,6 @@ public static IServiceCollection AddSecTesterRepeater(this IServiceCollection co .AddSingleton() .AddScoped() .AddScoped() - .AddScoped() .AddScoped() .AddScoped(sp => protocol => sp.GetServices().FirstOrDefault(x => x.Protocol == protocol) diff --git a/test/SecTester.Repeater.Tests/Bus/DefaultRepeaterBusFactoryTests.cs b/test/SecTester.Repeater.Tests/Bus/DefaultRepeaterBusFactoryTests.cs index 842c0c7..a7d8b29 100644 --- a/test/SecTester.Repeater.Tests/Bus/DefaultRepeaterBusFactoryTests.cs +++ b/test/SecTester.Repeater.Tests/Bus/DefaultRepeaterBusFactoryTests.cs @@ -7,19 +7,9 @@ public class DefaultRepeaterBusFactoryTests : IDisposable private const string Token = "0zmcwpe.nexr.0vlon8mp7lvxzjuvgjy88olrhadhiukk"; private readonly ILoggerFactory _loggerFactory = Substitute.For(); - private readonly ITimerProvider _timerProvider = Substitute.For(); - private readonly IServiceScopeFactory _serviceScopeFactory = Substitute.For(); - - public DefaultRepeaterBusFactoryTests() - { - // ADHOC: since GetRequiredService is part of extension we should explicitly mock an instance method - _serviceScopeFactory.CreateAsyncScope().ServiceProvider.GetService(typeof(ITimerProvider)).Returns(_timerProvider); - } public void Dispose() { - _timerProvider.ClearSubstitute(); - _serviceScopeFactory.ClearSubstitute(); _loggerFactory.ClearSubstitute(); GC.SuppressFinalize(this); } @@ -29,7 +19,7 @@ public async Task Create_CreatesBus() { // arrange Configuration config = new(Hostname, new Credentials(Token)); - DefaultRepeaterBusFactory sut = new(config, _loggerFactory, _serviceScopeFactory); + DefaultRepeaterBusFactory sut = new(config, _loggerFactory); // act await using var bus = sut.Create(Id); @@ -43,7 +33,7 @@ public async Task Create_CredentialsNotDefined_ThrowsError() { // arrange Configuration config = new(Hostname); - DefaultRepeaterBusFactory sut = new(config, _loggerFactory, _serviceScopeFactory); + DefaultRepeaterBusFactory sut = new(config, _loggerFactory); // act var act = async () => diff --git a/test/SecTester.Repeater.Tests/Bus/SocketIoRepeaterBusTests.cs b/test/SecTester.Repeater.Tests/Bus/SocketIoRepeaterBusTests.cs index 17351e8..6df2bda 100644 --- a/test/SecTester.Repeater.Tests/Bus/SocketIoRepeaterBusTests.cs +++ b/test/SecTester.Repeater.Tests/Bus/SocketIoRepeaterBusTests.cs @@ -7,21 +7,19 @@ public sealed class SocketIoRepeaterBusTests : IDisposable private static readonly SocketIoRepeaterBusOptions Options = new(Url); private readonly ISocketIoConnection _connection = Substitute.For(); - private readonly ITimerProvider _heartbeat = Substitute.For(); private readonly ILogger _logger = Substitute.For>(); private readonly ISocketIoMessage _socketIoMessage = Substitute.For(); private readonly SocketIoRepeaterBus _sut; public SocketIoRepeaterBusTests() { - _sut = new SocketIoRepeaterBus(Options, _connection, _heartbeat, _logger); + _sut = new SocketIoRepeaterBus(Options, _connection, _logger); } public void Dispose() { _socketIoMessage.ClearSubstitute(); _connection.ClearSubstitute(); - _heartbeat.ClearSubstitute(); _logger.ClearSubstitute(); GC.SuppressFinalize(this); @@ -117,36 +115,6 @@ public async Task Connect_AlreadyConnected_DoNothing() await _connection.DidNotReceive().Connect(); } - [Fact] - public async Task Connect_SchedulesPing() - { - // arrange - _connection.Connect().Returns(Task.CompletedTask); - - // act - await _sut.Connect(); - - // assert - _heartbeat.Received().Elapsed += Arg.Any(); - _heartbeat.Received().Start(); - } - - [Fact] - public async Task Connect_ShouldSendPingMessage() - { - // arrange - var elapsedEventArgs = EventArgs.Empty as ElapsedEventArgs; - _connection.Connect().Returns(Task.CompletedTask); - await _sut.Connect(); - - // act - _heartbeat.Elapsed += Raise.Event(new object(), elapsedEventArgs); - - // assert - _heartbeat.Interval.Should().BeGreaterOrEqualTo(10_000); - await _connection.Received(2).EmitAsync("ping"); - } - [Fact] public async Task Deploy_Success() { @@ -198,17 +166,4 @@ public async Task DisposeAsync_NotConnectedYet_Success() await _connection.DidNotReceive().Disconnect(); _connection.Received().Dispose(); } - - [Fact] - public async Task DisposeAsync_StopsPingMessages() - { - // arrange - _connection.Connected.Returns(true); - - // act - await _sut.DisposeAsync(); - - // assert - _heartbeat.Received().Stop(); - } } diff --git a/test/SecTester.Repeater.Tests/Extensions/ServiceCollectionExtensionsTests.cs b/test/SecTester.Repeater.Tests/Extensions/ServiceCollectionExtensionsTests.cs index 91e4b39..4991bd2 100644 --- a/test/SecTester.Repeater.Tests/Extensions/ServiceCollectionExtensionsTests.cs +++ b/test/SecTester.Repeater.Tests/Extensions/ServiceCollectionExtensionsTests.cs @@ -30,16 +30,6 @@ public void AddSecTesterRepeater_RegistersRepeaters() _sut.Received().AddScoped(); } - [Fact] - public void AddSecTesterRepeater_RegistersTimerProvider() - { - // act - _sut.AddSecTesterRepeater(); - - // assert - _sut.Received().AddScoped(); - } - [Fact] public void AddSecTesterRepeater_WithTimeout_RegistersHttpClient() { diff --git a/test/SecTester.Repeater.Tests/Usings.cs b/test/SecTester.Repeater.Tests/Usings.cs index 6b72f15..e288e47 100644 --- a/test/SecTester.Repeater.Tests/Usings.cs +++ b/test/SecTester.Repeater.Tests/Usings.cs @@ -1,26 +1,18 @@ global using System.Net; -global using System.Net.Http.Json; global using System.Net.Sockets; global using System.Text; -global using System.Text.Json; -global using System.Text.Json.Serialization; global using System.Text.RegularExpressions; -global using System.Timers; global using FluentAssertions; global using Microsoft.Extensions.DependencyInjection; global using Microsoft.Extensions.DependencyInjection.Extensions; global using Microsoft.Extensions.Logging; global using NSubstitute; global using NSubstitute.ClearExtensions; -global using NSubstitute.ExceptionExtensions; global using NSubstitute.ReturnsExtensions; global using RichardSzalay.MockHttp; global using SecTester.Core; global using SecTester.Core.Bus; -global using SecTester.Core.Commands; -global using SecTester.Core.Dispatchers; global using SecTester.Core.Exceptions; -global using SecTester.Core.RetryStrategies; global using SecTester.Core.Logger; global using SecTester.Core.Utils; global using SecTester.Repeater.Api;