From 1cab5b828194a0b3266908a7a3690f9d2b32495f Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Fri, 11 Aug 2023 19:51:48 +0530 Subject: [PATCH 01/12] Refactored IOC class, Updated ablyrealtime and ablyrest for the same --- src/IO.Ably.Shared/AblyRealtime.cs | 7 +-- src/IO.Ably.Shared/AblyRest.cs | 20 ++++++--- src/IO.Ably.Shared/IoC.cs | 45 ++++++------------- .../Infrastructure/AblySpecs.cs | 1 + .../Infrastructure/ConnectionAwaiter.cs | 1 + .../Infrastructure/FakeTransport.cs | 1 + .../Infrastructure/TestTransportWrapper.cs | 1 + src/IO.Ably.Tests.Shared/LoggerTests.cs | 1 + .../ConnectionSpecs/EventEmitterSpecs.cs | 1 + src/IO.Ably.Tests.Shared/Rest/SandboxSpecs.cs | 1 + .../Samples/DocumentationSamples.cs | 1 + 11 files changed, 39 insertions(+), 41 deletions(-) diff --git a/src/IO.Ably.Shared/AblyRealtime.cs b/src/IO.Ably.Shared/AblyRealtime.cs index 8bbd84e1c..75c13c7b2 100644 --- a/src/IO.Ably.Shared/AblyRealtime.cs +++ b/src/IO.Ably.Shared/AblyRealtime.cs @@ -40,7 +40,7 @@ public AblyRealtime(string key) /// /// . public AblyRealtime(ClientOptions options) - : this(options, CreateRestFunc, IoC.MobileDevice) + : this(options, CreateRestFunc) { } @@ -65,6 +65,7 @@ internal AblyRealtime(ClientOptions options, Func> ExecuteHttpRequest; private LocalDevice _device; + internal IoC Ioc { get; set; } + /// /// Initializes a new instance of the class using an api key. /// Full api key. @@ -42,7 +44,7 @@ public AblyRest(Action init) { Options = new ClientOptions(); init(Options); - InitializeAbly(IoC.MobileDevice); + InitializeAbly(); } /// @@ -50,11 +52,11 @@ public AblyRest(Action init) /// /// instance of clientOptions. public AblyRest(ClientOptions clientOptions) - : this(clientOptions, IoC.MobileDevice) + : this(clientOptions, null) { } - internal AblyRest(ClientOptions clientOptions, IMobileDevice mobileDevice) + internal AblyRest(ClientOptions clientOptions, IMobileDevice mobileDevice = null) { Options = clientOptions; InitializeAbly(mobileDevice); @@ -122,7 +124,7 @@ public LocalDevice Device internal ILogger Logger { get; set; } /// Initializes the rest client and validates the passed in options. - private void InitializeAbly(IMobileDevice mobileDevice) + private void InitializeAbly(IMobileDevice mobileDevice = null) { if (Options == null) { @@ -144,9 +146,15 @@ private void InitializeAbly(IMobileDevice mobileDevice) HttpClient = new AblyHttpClient(new AblyHttpOptions(Options)); ExecuteHttpRequest = HttpClient.Execute; AblyAuth = new AblyAuth(Options, this); - Channels = new RestChannels(this, mobileDevice); + Ioc = new IoC(Logger); + if (mobileDevice != null) + { + Ioc.MobileDevice = mobileDevice; + } + + MobileDevice = Ioc.MobileDevice; + Channels = new RestChannels(this, MobileDevice); Push = new PushRest(this, Logger); - MobileDevice = mobileDevice; AblyAuth.OnClientIdChanged = OnAuthClientIdChanged; } diff --git a/src/IO.Ably.Shared/IoC.cs b/src/IO.Ably.Shared/IoC.cs index ac39f9de5..06ceb144e 100644 --- a/src/IO.Ably.Shared/IoC.cs +++ b/src/IO.Ably.Shared/IoC.cs @@ -1,47 +1,28 @@ using System; -using System.IO; -using System.Reflection; using IO.Ably.Push; using IO.Ably.Transport; namespace IO.Ably { - /// This class initializes dynamically-injected platform dependencies. - internal static class IoC + /// This class initializes Platform. + internal class IoC { - private static readonly IPlatform Platform; + public static readonly Platform Platform = new Platform(); - /// Load AblyPlatform.dll, instantiate AblyPlatform.PlatformImpl type. - static IoC() + public static ITransportFactory TransportFactory => Platform?.TransportFactory ?? new MsWebSocketTransport.TransportFactory(); + + public static Agent.PlatformRuntime PlatformId => Platform?.PlatformId ?? Agent.PlatformRuntime.Other; + + public IoC(ILogger logger) { - try - { - var name = new AssemblyName("IO.Ably"); - var asm = Assembly.Load(name); - var type = asm.GetType("IO.Ably.Platform"); - if (type != null) - { - var obj = Activator.CreateInstance(type); - Platform = obj as IPlatform; - } - else - { - DefaultLogger.Debug("Platform class does not exist. Defaulting Microsoft Websocket library."); - } - } - catch (FileNotFoundException e) - { - DefaultLogger.Debug($"Assembly cannot be loaded. Defaulting Microsoft Websocket library. ({e.Message})"); - } + Logger = logger; } - public static ITransportFactory TransportFactory => Platform?.TransportFactory ?? new MsWebSocketTransport.TransportFactory(); - - public static void RegisterOsNetworkStateChanged(ILogger logger) => Platform.RegisterOsNetworkStateChanged(logger); + public ILogger Logger { get; set; } - public static Agent.PlatformRuntime PlatformId => Platform?.PlatformId ?? Agent.PlatformRuntime.Other; + public void RegisterOsNetworkStateChanged() => Platform.RegisterOsNetworkStateChanged(Logger); - public static IMobileDevice MobileDevice + public IMobileDevice MobileDevice { get { @@ -51,7 +32,7 @@ public static IMobileDevice MobileDevice } catch (Exception e) when (e is NotImplementedException) { - DefaultLogger.Error("Mobile Device is no supported on the current platform.", e); + Logger.Error("Mobile Device is no supported on the current platform.", e); return null; } } diff --git a/src/IO.Ably.Tests.Shared/Infrastructure/AblySpecs.cs b/src/IO.Ably.Tests.Shared/Infrastructure/AblySpecs.cs index 991bc3ccb..4eae12e46 100644 --- a/src/IO.Ably.Tests.Shared/Infrastructure/AblySpecs.cs +++ b/src/IO.Ably.Tests.Shared/Infrastructure/AblySpecs.cs @@ -1,4 +1,5 @@ using System; +using IO.Ably.Tests.Shared.Helpers; using Xunit.Abstractions; namespace IO.Ably.Tests diff --git a/src/IO.Ably.Tests.Shared/Infrastructure/ConnectionAwaiter.cs b/src/IO.Ably.Tests.Shared/Infrastructure/ConnectionAwaiter.cs index bb80eccc3..bf04991ea 100644 --- a/src/IO.Ably.Tests.Shared/Infrastructure/ConnectionAwaiter.cs +++ b/src/IO.Ably.Tests.Shared/Infrastructure/ConnectionAwaiter.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Threading.Tasks; using IO.Ably.Realtime; +using IO.Ably.Tests.Shared.Helpers; namespace IO.Ably.Tests.Infrastructure { diff --git a/src/IO.Ably.Tests.Shared/Infrastructure/FakeTransport.cs b/src/IO.Ably.Tests.Shared/Infrastructure/FakeTransport.cs index 0a98c0533..1956fe2ae 100644 --- a/src/IO.Ably.Tests.Shared/Infrastructure/FakeTransport.cs +++ b/src/IO.Ably.Tests.Shared/Infrastructure/FakeTransport.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading.Tasks; using IO.Ably.Realtime; +using IO.Ably.Tests.Shared.Helpers; using IO.Ably.Transport; using IO.Ably.Types; diff --git a/src/IO.Ably.Tests.Shared/Infrastructure/TestTransportWrapper.cs b/src/IO.Ably.Tests.Shared/Infrastructure/TestTransportWrapper.cs index 30954183b..7f55443ff 100644 --- a/src/IO.Ably.Tests.Shared/Infrastructure/TestTransportWrapper.cs +++ b/src/IO.Ably.Tests.Shared/Infrastructure/TestTransportWrapper.cs @@ -3,6 +3,7 @@ using System.Net.Sockets; using IO.Ably.MessageEncoders; using IO.Ably.Realtime; +using IO.Ably.Tests.Shared.Helpers; using IO.Ably.Transport; using IO.Ably.Types; diff --git a/src/IO.Ably.Tests.Shared/LoggerTests.cs b/src/IO.Ably.Tests.Shared/LoggerTests.cs index 43ad4c117..66676b690 100644 --- a/src/IO.Ably.Tests.Shared/LoggerTests.cs +++ b/src/IO.Ably.Tests.Shared/LoggerTests.cs @@ -1,5 +1,6 @@ using System; using FluentAssertions; +using IO.Ably.Tests.Shared.Helpers; using Xunit; namespace IO.Ably.AcceptanceTests diff --git a/src/IO.Ably.Tests.Shared/Realtime/ConnectionSpecs/EventEmitterSpecs.cs b/src/IO.Ably.Tests.Shared/Realtime/ConnectionSpecs/EventEmitterSpecs.cs index 6abe4bbeb..48c140d5b 100644 --- a/src/IO.Ably.Tests.Shared/Realtime/ConnectionSpecs/EventEmitterSpecs.cs +++ b/src/IO.Ably.Tests.Shared/Realtime/ConnectionSpecs/EventEmitterSpecs.cs @@ -5,6 +5,7 @@ using FluentAssertions; using IO.Ably.Realtime; using IO.Ably.Tests.Infrastructure; +using IO.Ably.Tests.Shared.Helpers; using IO.Ably.Types; using Xunit; using Xunit.Abstractions; diff --git a/src/IO.Ably.Tests.Shared/Rest/SandboxSpecs.cs b/src/IO.Ably.Tests.Shared/Rest/SandboxSpecs.cs index fb0f419d4..47a7e717a 100644 --- a/src/IO.Ably.Tests.Shared/Rest/SandboxSpecs.cs +++ b/src/IO.Ably.Tests.Shared/Rest/SandboxSpecs.cs @@ -6,6 +6,7 @@ using IO.Ably.Push; using IO.Ably.Realtime; using IO.Ably.Tests.Infrastructure; +using IO.Ably.Tests.Shared.Helpers; using Xunit; using Xunit.Abstractions; diff --git a/src/IO.Ably.Tests.Shared/Samples/DocumentationSamples.cs b/src/IO.Ably.Tests.Shared/Samples/DocumentationSamples.cs index 7cd82011f..be8709d7d 100644 --- a/src/IO.Ably.Tests.Shared/Samples/DocumentationSamples.cs +++ b/src/IO.Ably.Tests.Shared/Samples/DocumentationSamples.cs @@ -5,6 +5,7 @@ using IO.Ably.Encryption; using IO.Ably.Realtime; +using IO.Ably.Tests.Shared.Helpers; namespace IO.Ably.Tests.Samples { From 85e94c4d985949e3d8da0a8329e7f0c43bc8e71d Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Fri, 11 Aug 2023 22:32:48 +0530 Subject: [PATCH 02/12] Added logger for handling connecting token error, fixed logger issues --- src/IO.Ably.Shared/Realtime/Workflows/RealtimeCommands.cs | 4 ++-- src/IO.Ably.Shared/Realtime/Workflows/RealtimeWorkflow.cs | 2 +- src/IO.Ably.Tests.Shared/Push/LocalDeviceTests.cs | 3 ++- .../Realtime/ConnectionAttemptsInfoSpecs.cs | 1 + 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/IO.Ably.Shared/Realtime/Workflows/RealtimeCommands.cs b/src/IO.Ably.Shared/Realtime/Workflows/RealtimeCommands.cs index 84529ade3..3e40b99fb 100644 --- a/src/IO.Ably.Shared/Realtime/Workflows/RealtimeCommands.cs +++ b/src/IO.Ably.Shared/Realtime/Workflows/RealtimeCommands.cs @@ -378,14 +378,14 @@ private HandleConnectingTokenErrorCommand(ErrorInfo error) Error = error; } - public static RealtimeCommand Create(ErrorInfo error) + public static RealtimeCommand Create(ErrorInfo error, ILogger logger) { if (error == null || error.IsTokenError == false) { #if DEBUG throw new ArgumentException("Cannot create a TokenError command with an error that is not a token error."); #else - DefaultLogger.Warning("Cannot create a TokenError command with an error that is not a token error"); + logger.Warning("Cannot create a TokenError command with an error that is not a token error"); // TODO: Sentry alert return EmptyCommand.Instance; diff --git a/src/IO.Ably.Shared/Realtime/Workflows/RealtimeWorkflow.cs b/src/IO.Ably.Shared/Realtime/Workflows/RealtimeWorkflow.cs index 219d0a3a0..d93f2ebd7 100644 --- a/src/IO.Ably.Shared/Realtime/Workflows/RealtimeWorkflow.cs +++ b/src/IO.Ably.Shared/Realtime/Workflows/RealtimeWorkflow.cs @@ -430,7 +430,7 @@ async Task AttemptANewConnection() if (error.IsTokenError) { - return HandleConnectingTokenErrorCommand.Create(error) + return HandleConnectingTokenErrorCommand.Create(error, Logger) .TriggeredBy(cmd); } diff --git a/src/IO.Ably.Tests.Shared/Push/LocalDeviceTests.cs b/src/IO.Ably.Tests.Shared/Push/LocalDeviceTests.cs index eecb7af3f..73a86ee73 100644 --- a/src/IO.Ably.Tests.Shared/Push/LocalDeviceTests.cs +++ b/src/IO.Ably.Tests.Shared/Push/LocalDeviceTests.cs @@ -6,6 +6,7 @@ using IO.Ably.Realtime; using IO.Ably.Tests.Infrastructure; using IO.Ably.Tests.Realtime; +using IO.Ably.Tests.Shared.Helpers; using IO.Ably.Types; using Newtonsoft.Json.Linq; using Xunit; @@ -65,7 +66,7 @@ public async Task LoadPersistedLocalDevice_ShouldLoadAllSavedProperties() const string token = "registration_token"; SetSetting(PersistKeys.Device.Token, token); - var loadResult = LocalDevice.LoadPersistedLocalDevice(mobileDevice, out var localDevice); + var loadResult = LocalDevice.LoadPersistedLocalDevice(mobileDevice, out var localDevice, DefaultLogger.LoggerInstance); loadResult.Should().BeTrue(); localDevice.Platform.Should().Be(mobileDevice.DevicePlatform); localDevice.FormFactor.Should().Be(mobileDevice.FormFactor); diff --git a/src/IO.Ably.Tests.Shared/Realtime/ConnectionAttemptsInfoSpecs.cs b/src/IO.Ably.Tests.Shared/Realtime/ConnectionAttemptsInfoSpecs.cs index 17e0e753f..fe8220080 100644 --- a/src/IO.Ably.Tests.Shared/Realtime/ConnectionAttemptsInfoSpecs.cs +++ b/src/IO.Ably.Tests.Shared/Realtime/ConnectionAttemptsInfoSpecs.cs @@ -4,6 +4,7 @@ using FluentAssertions; using IO.Ably.Realtime; using IO.Ably.Realtime.Workflow; +using IO.Ably.Tests.Shared.Helpers; using IO.Ably.Transport; using Xunit; using Xunit.Abstractions; From 5b1b15ccfbac00c929bd7e00efcc34d6dbea36b0 Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Fri, 11 Aug 2023 22:48:30 +0530 Subject: [PATCH 03/12] Fixed closed state spec, updated connection instance init --- .../Infrastructure/FakeConnectionContext.cs | 2 +- .../Realtime/ConnectionStateTests/ClosedStateSpecs.cs | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/IO.Ably.Tests.Shared/Infrastructure/FakeConnectionContext.cs b/src/IO.Ably.Tests.Shared/Infrastructure/FakeConnectionContext.cs index 707e4cd40..2cf641246 100644 --- a/src/IO.Ably.Tests.Shared/Infrastructure/FakeConnectionContext.cs +++ b/src/IO.Ably.Tests.Shared/Infrastructure/FakeConnectionContext.cs @@ -13,7 +13,7 @@ internal class FakeConnectionContext : IConnectionContext { public FakeConnectionContext() { - Connection = new Connection(new AblyRealtime(new ClientOptions() { AutoConnect = false }), TestHelpers.NowFunc()); + Connection = new Connection(new AblyRealtime(new ClientOptions() { Key = "dummy", AutoConnect = false }), TestHelpers.NowFunc()); Connection.Initialise(); } diff --git a/src/IO.Ably.Tests.Shared/Realtime/ConnectionStateTests/ClosedStateSpecs.cs b/src/IO.Ably.Tests.Shared/Realtime/ConnectionStateTests/ClosedStateSpecs.cs index 040e136ad..12fa9f6e6 100644 --- a/src/IO.Ably.Tests.Shared/Realtime/ConnectionStateTests/ClosedStateSpecs.cs +++ b/src/IO.Ably.Tests.Shared/Realtime/ConnectionStateTests/ClosedStateSpecs.cs @@ -13,14 +13,10 @@ namespace IO.Ably.Tests public class ClosedStateSpecs : AblySpecs { private readonly ConnectionClosedState _state; - private readonly IInternalLogger _logger; public ClosedStateSpecs(ITestOutputHelper output) : base(output) { - var sink = new TestLoggerSink(); - - _logger = InternalLogger.Create(Defaults.DefaultLogLevel, sink); var context = new FakeConnectionContext(); _state = new ConnectionClosedState(context); } From 047c2c1af590114dc48ed1b3b1b4058271c858cd Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Fri, 11 Aug 2023 23:18:04 +0530 Subject: [PATCH 04/12] Fixed logging related tests --- src/IO.Ably.Tests.Shared/LoggerTests.cs | 4 ++-- src/IO.Ably.Tests.Shared/Rest/RestSpecs.cs | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/IO.Ably.Tests.Shared/LoggerTests.cs b/src/IO.Ably.Tests.Shared/LoggerTests.cs index 66676b690..fae3fe8df 100644 --- a/src/IO.Ably.Tests.Shared/LoggerTests.cs +++ b/src/IO.Ably.Tests.Shared/LoggerTests.cs @@ -57,10 +57,10 @@ public void TestLogger() } [Fact] - public void ClientOptionsWithNoLoggerSpecified_ShouldUseTheDefaultLogger() + public void ClientOptionsWithNoLoggerSpecified_ShouldUseTheInternalLogger() { var opts = new ClientOptions(); - Assert.Same(opts.Logger, DefaultLogger.LoggerInstance); + Assert.IsType(opts.Logger); } [Fact] diff --git a/src/IO.Ably.Tests.Shared/Rest/RestSpecs.cs b/src/IO.Ably.Tests.Shared/Rest/RestSpecs.cs index 2eb0a54bf..c37f3700c 100644 --- a/src/IO.Ably.Tests.Shared/Rest/RestSpecs.cs +++ b/src/IO.Ably.Tests.Shared/Rest/RestSpecs.cs @@ -8,6 +8,7 @@ using IO.Ably.AcceptanceTests; using FluentAssertions; +using IO.Ably.Tests.Shared.Helpers; using Xunit; using Xunit.Abstractions; @@ -354,9 +355,10 @@ public void LogEvent(LogLevel level, string message) { } [Trait("spec", "TO3c")] public void WithLogHandler_ShouldUseNewLogHandler() { - _ = new AblyRest(new ClientOptions(ValidKey) { LogHandler = new TestLogHandler() }); + var restClient = new AblyRest(new ClientOptions(ValidKey) { LogHandler = new TestLogHandler() }); - Logger.LoggerSink.Should().BeOfType(); + restClient.Logger.LoggerSink.Should().NotBeOfType(); + restClient.Logger.LoggerSink.Should().BeOfType(); } [Fact] From 2f0c8274a5fbc244ea8509ac01aa1de388b4a6ab Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Sat, 12 Aug 2023 07:39:24 +0530 Subject: [PATCH 05/12] Skipped localdevice test --- src/IO.Ably.Shared/Push/LocalDevice.cs | 2 +- src/IO.Ably.Tests.Shared/Push/LocalDeviceTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/IO.Ably.Shared/Push/LocalDevice.cs b/src/IO.Ably.Shared/Push/LocalDevice.cs index 1331e7074..5d183811d 100644 --- a/src/IO.Ably.Shared/Push/LocalDevice.cs +++ b/src/IO.Ably.Shared/Push/LocalDevice.cs @@ -137,7 +137,7 @@ internal static LocalDevice GetInstance(IMobileDevice mobileDevice, string clien if (mobileDevice is null) { throw new AblyException( - "Cannot initialise LocalDevice instance before initialising the MobileDevice class. For Android call AndroidMobileDevice.Initialise() and for iOS call AppleMobileDevice.Initialise()"); + "Cannot initialise LocalDevice instance before initializing the MobileDevice class. For Android call AndroidMobileDevice.Initialise() and for iOS call AppleMobileDevice.Initialise()"); } switch (Instance) diff --git a/src/IO.Ably.Tests.Shared/Push/LocalDeviceTests.cs b/src/IO.Ably.Tests.Shared/Push/LocalDeviceTests.cs index 73a86ee73..ef53605ef 100644 --- a/src/IO.Ably.Tests.Shared/Push/LocalDeviceTests.cs +++ b/src/IO.Ably.Tests.Shared/Push/LocalDeviceTests.cs @@ -116,7 +116,7 @@ public void WhenPlatformsSupportsPushNotifications_ShouldBeAbleToRetrieveLocalDe realtime.Device.Should().NotBeNull(); } - [Fact] + [Fact(Skip = "Intermittently fails")] [Trait("spec", "RSH8")] public void WhenPlatformsDoesNotSupportPushNotifications_DeviceShouldBeNull() { From 60268fcdc75cd82b7f8fb120b5fe0e5aba4edcd3 Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Sat, 12 Aug 2023 08:20:54 +0530 Subject: [PATCH 06/12] Skipped failing test, fixed channel spec test for testloggersink --- src/IO.Ably.Tests.Shared/Push/PushChannelTests.cs | 2 +- src/IO.Ably.Tests.Shared/Realtime/ChannelSpecs.cs | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/IO.Ably.Tests.Shared/Push/PushChannelTests.cs b/src/IO.Ably.Tests.Shared/Push/PushChannelTests.cs index 3bb736410..9e18f52cb 100644 --- a/src/IO.Ably.Tests.Shared/Push/PushChannelTests.cs +++ b/src/IO.Ably.Tests.Shared/Push/PushChannelTests.cs @@ -60,7 +60,7 @@ public WhenPlatformSupportsPushNotifications(ITestOutputHelper output) [Trait("spec", "RSH7")] public class WhenPlatformDoesNotSupportPushNotifications : AblyRealtimeSpecs { - [Fact] + [Fact(Skip = "Tests failing because push is not null set by earlier instances")] public void RealtimeChannel_ShouldReturnNull() { var channel = GetRealtimeClient().Channels.Get("test"); diff --git a/src/IO.Ably.Tests.Shared/Realtime/ChannelSpecs.cs b/src/IO.Ably.Tests.Shared/Realtime/ChannelSpecs.cs index bb71dddb8..e40a4c615 100644 --- a/src/IO.Ably.Tests.Shared/Realtime/ChannelSpecs.cs +++ b/src/IO.Ably.Tests.Shared/Realtime/ChannelSpecs.cs @@ -1304,11 +1304,13 @@ public async Task MessageHandler.EncodePayloads(otherChannelOptions.ToDecodingContext(client.Logger), new[] { message }); var testSink = new TestLoggerSink(); - using (DefaultLogger.SetTempDestination(testSink)) - { - client.FakeMessageReceived(message, channel.Name); - await client.ProcessCommands(); - } + var oldSink = client.Logger.LoggerSink; + client.Logger.LoggerSink = testSink; + + client.FakeMessageReceived(message, channel.Name); + await client.ProcessCommands(); + + client.Logger.LoggerSink = oldSink; receivedMessage.Encoding.Should().Be(message.Encoding); testSink.Messages.Should().Contain(x => x.Contains("Error decrypting payload")); From 13a094026ef632d03be01c7d2f2a30b6878a128a Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Sat, 12 Aug 2023 12:24:52 +0530 Subject: [PATCH 07/12] Fixed channel sandboxspec test, updated defaultlogger --- src/IO.Ably.Tests.Shared/Helpers/DefaultLogger.cs | 7 ------- src/IO.Ably.Tests.Shared/Rest/ChannelSandboxSpecs.cs | 3 +-- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/IO.Ably.Tests.Shared/Helpers/DefaultLogger.cs b/src/IO.Ably.Tests.Shared/Helpers/DefaultLogger.cs index 78e054a01..5edf2c6c0 100644 --- a/src/IO.Ably.Tests.Shared/Helpers/DefaultLogger.cs +++ b/src/IO.Ably.Tests.Shared/Helpers/DefaultLogger.cs @@ -30,13 +30,6 @@ public static ILoggerSink LoggerSink /// public static bool IsDebug => LoggerInstance.LogLevel == LogLevel.Debug; - internal static IDisposable SetTempDestination(ILoggerSink loggerSink) - { - ILoggerSink oldLoggerSink = LoggerInstance.LoggerSink; - LoggerInstance.LoggerSink = loggerSink; - return new ActionOnDispose(() => LoggerInstance.LoggerSink = oldLoggerSink); - } - /// Log an error message. internal static void Error(string message, Exception ex) { diff --git a/src/IO.Ably.Tests.Shared/Rest/ChannelSandboxSpecs.cs b/src/IO.Ably.Tests.Shared/Rest/ChannelSandboxSpecs.cs index e103e4f92..0f0132c4e 100644 --- a/src/IO.Ably.Tests.Shared/Rest/ChannelSandboxSpecs.cs +++ b/src/IO.Ably.Tests.Shared/Rest/ChannelSandboxSpecs.cs @@ -634,9 +634,8 @@ public async Task WithTestMessagePayloadsWhenDecoding_ShouldEncodeMessagesAsPerS public async Task WithEncryptionCipherAlgorithmMismatch_ShouldLeaveMessageEncryptedAndLogError(Protocol protocol) { var loggerSink = new TestLoggerSink(); - var logger = InternalLogger.Create(Defaults.DefaultLogLevel, loggerSink); - var client = await GetRestClient(protocol); + var client = await GetRestClient(protocol, options => options.LogHandler = loggerSink); var channel1 = client.Channels.Get("persisted:encryption", GetOptions(_examples)); const string payload = "test payload"; From 893f12e39f3d27959827b9c8e87e7a8a81396e63 Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Sat, 12 Aug 2023 18:34:01 +0530 Subject: [PATCH 08/12] Updated defaultLogger to create a singleTon, updated realtimeClient with testSink --- .../Helpers/DefaultLogger.cs | 21 ++++++++++++++++++- .../Realtime/DeltaSandboxSpecs.cs | 2 +- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/IO.Ably.Tests.Shared/Helpers/DefaultLogger.cs b/src/IO.Ably.Tests.Shared/Helpers/DefaultLogger.cs index 5edf2c6c0..dd68deda8 100644 --- a/src/IO.Ably.Tests.Shared/Helpers/DefaultLogger.cs +++ b/src/IO.Ably.Tests.Shared/Helpers/DefaultLogger.cs @@ -5,7 +5,26 @@ namespace IO.Ably.Tests.Shared.Helpers /// An utility class for logging various messages. public static class DefaultLogger { - internal static ILogger LoggerInstance => InternalLogger.Create(); + private static readonly object SyncLock = new object(); + private static ILogger _loggerInstance; + + internal static ILogger LoggerInstance + { + get + { + if (_loggerInstance == null) + { + lock (SyncLock) + { + _loggerInstance = InternalLogger.Create(); + } + } + + return _loggerInstance; + } + + set => _loggerInstance = value; + } /// Maximum level to log. /// E.g. set to LogLevel.Warning to have only errors and warnings in the log. diff --git a/src/IO.Ably.Tests.Shared/Realtime/DeltaSandboxSpecs.cs b/src/IO.Ably.Tests.Shared/Realtime/DeltaSandboxSpecs.cs index b3d13cb70..e18abb32b 100644 --- a/src/IO.Ably.Tests.Shared/Realtime/DeltaSandboxSpecs.cs +++ b/src/IO.Ably.Tests.Shared/Realtime/DeltaSandboxSpecs.cs @@ -231,7 +231,7 @@ public async Task WhenDeltaDecodeFail_ShouldSetStateToAttachingLogTheErrorAndDis var firstMessageReceived = new TaskCompletionAwaiter(); using (((IInternalLogger)Logger).CreateDisposableLoggingContext(testSink)) { - var realtime = await GetRealtimeClient(protocol); + var realtime = await GetRealtimeClient(protocol, (options, _) => options.LogHandler = testSink); var channel = realtime.Channels.Get(channelName, new ChannelOptions(channelParams: new ChannelParams { { "delta", "vcdiff" } })); var received = new List(); From d8eb29fb2a49b48bcba5bd4783a57c9d9abec437 Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Sat, 12 Aug 2023 18:51:28 +0530 Subject: [PATCH 09/12] Added calculationDelay timeout to reduce test flakiness for incremental backoff --- .../Realtime/ConnectionSpecs/ConnectionFailureSpecs.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/IO.Ably.Tests.Shared/Realtime/ConnectionSpecs/ConnectionFailureSpecs.cs b/src/IO.Ably.Tests.Shared/Realtime/ConnectionSpecs/ConnectionFailureSpecs.cs index 417f0a58f..d8e8a99b3 100644 --- a/src/IO.Ably.Tests.Shared/Realtime/ConnectionSpecs/ConnectionFailureSpecs.cs +++ b/src/IO.Ably.Tests.Shared/Realtime/ConnectionSpecs/ConnectionFailureSpecs.cs @@ -302,12 +302,13 @@ public async Task WhenInDisconnectedState_ReconnectUsingIncrementalBackoffTimeou // Upper bound = min((retryAttempt + 2) / 3, 2) * initialTimeout // Lower bound = 0.8 * Upper bound - disconnectedRetryTimeouts[0].Should().BeInRange(4, 5); - disconnectedRetryTimeouts[1].Should().BeInRange(5.33, 6.66); - disconnectedRetryTimeouts[2].Should().BeInRange(6.66, 8.33); + const double calculationDelayTimeout = 0.15; + disconnectedRetryTimeouts[0].Should().BeInRange(4, 5 + calculationDelayTimeout); + disconnectedRetryTimeouts[1].Should().BeInRange(5.33, 6.66 + calculationDelayTimeout); + disconnectedRetryTimeouts[2].Should().BeInRange(6.66, 8.33 + calculationDelayTimeout); for (var i = 3; i < disconnectedRetryTimeouts.Count; i++) { - disconnectedRetryTimeouts[i].Should().BeInRange(8, 10); + disconnectedRetryTimeouts[i].Should().BeInRange(8, 10 + calculationDelayTimeout); } } From 884dad1737e6e53bba128bea9707863e6496fbf7 Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Sun, 13 Aug 2023 01:30:20 +0530 Subject: [PATCH 10/12] Removed unnecessary setting mobileDevice for IOC, unskipped failing tests --- src/IO.Ably.Shared/AblyRest.cs | 7 +------ src/IO.Ably.Shared/IoC.cs | 1 - src/IO.Ably.Tests.Shared/Push/LocalDeviceTests.cs | 2 +- src/IO.Ably.Tests.Shared/Push/PushChannelTests.cs | 2 +- 4 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/IO.Ably.Shared/AblyRest.cs b/src/IO.Ably.Shared/AblyRest.cs index c3fd21cb6..031a1d8da 100644 --- a/src/IO.Ably.Shared/AblyRest.cs +++ b/src/IO.Ably.Shared/AblyRest.cs @@ -147,12 +147,7 @@ private void InitializeAbly(IMobileDevice mobileDevice = null) ExecuteHttpRequest = HttpClient.Execute; AblyAuth = new AblyAuth(Options, this); Ioc = new IoC(Logger); - if (mobileDevice != null) - { - Ioc.MobileDevice = mobileDevice; - } - - MobileDevice = Ioc.MobileDevice; + MobileDevice = mobileDevice ?? Ioc.MobileDevice; Channels = new RestChannels(this, MobileDevice); Push = new PushRest(this, Logger); AblyAuth.OnClientIdChanged = OnAuthClientIdChanged; diff --git a/src/IO.Ably.Shared/IoC.cs b/src/IO.Ably.Shared/IoC.cs index 06ceb144e..5ba77a600 100644 --- a/src/IO.Ably.Shared/IoC.cs +++ b/src/IO.Ably.Shared/IoC.cs @@ -36,7 +36,6 @@ public IMobileDevice MobileDevice return null; } } - set => Platform.MobileDevice = value; } } } diff --git a/src/IO.Ably.Tests.Shared/Push/LocalDeviceTests.cs b/src/IO.Ably.Tests.Shared/Push/LocalDeviceTests.cs index ef53605ef..73a86ee73 100644 --- a/src/IO.Ably.Tests.Shared/Push/LocalDeviceTests.cs +++ b/src/IO.Ably.Tests.Shared/Push/LocalDeviceTests.cs @@ -116,7 +116,7 @@ public void WhenPlatformsSupportsPushNotifications_ShouldBeAbleToRetrieveLocalDe realtime.Device.Should().NotBeNull(); } - [Fact(Skip = "Intermittently fails")] + [Fact] [Trait("spec", "RSH8")] public void WhenPlatformsDoesNotSupportPushNotifications_DeviceShouldBeNull() { diff --git a/src/IO.Ably.Tests.Shared/Push/PushChannelTests.cs b/src/IO.Ably.Tests.Shared/Push/PushChannelTests.cs index 9e18f52cb..3bb736410 100644 --- a/src/IO.Ably.Tests.Shared/Push/PushChannelTests.cs +++ b/src/IO.Ably.Tests.Shared/Push/PushChannelTests.cs @@ -60,7 +60,7 @@ public WhenPlatformSupportsPushNotifications(ITestOutputHelper output) [Trait("spec", "RSH7")] public class WhenPlatformDoesNotSupportPushNotifications : AblyRealtimeSpecs { - [Fact(Skip = "Tests failing because push is not null set by earlier instances")] + [Fact] public void RealtimeChannel_ShouldReturnNull() { var channel = GetRealtimeClient().Channels.Get("test"); From 9afbfdf76bbce6c0e28ad0fb63ecf85b00c8057c Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Sun, 13 Aug 2023 01:53:12 +0530 Subject: [PATCH 11/12] Unskipped localdevice flaky tests --- src/IO.Ably.Tests.Shared/Push/LocalDeviceTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/IO.Ably.Tests.Shared/Push/LocalDeviceTests.cs b/src/IO.Ably.Tests.Shared/Push/LocalDeviceTests.cs index 73a86ee73..c640ef514 100644 --- a/src/IO.Ably.Tests.Shared/Push/LocalDeviceTests.cs +++ b/src/IO.Ably.Tests.Shared/Push/LocalDeviceTests.cs @@ -132,7 +132,7 @@ public void WhenPlatformsDoesNotSupportPushNotifications_DeviceShouldBeNull() rest.MobileDevice.Should().BeNull(); } - [Fact(Skip = "Intermittently fails")] + [Fact] [Trait("spec", "RSH8a")] public void LocalDevice_IsOnlyInitialisedOnce() { @@ -157,7 +157,7 @@ public void LocalDevice_WhenInitialised_ShouldHave_CorrectProperties_set() device.FormFactor.Should().NotBeEmpty(); } - [Fact(Skip = "Intermittently fails")] + [Fact] [Trait("spec", "RSH8a")] [Trait("spec", "RSH8b")] public void LocalDevice_WhenRestClientContainsClientId_ShouldHaveTheSameClientId() From 9b884799b44aa7c158ba1f2ad0e69ec0bbf459c4 Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Sun, 13 Aug 2023 03:03:52 +0530 Subject: [PATCH 12/12] Refactored platform and IOC classes, removed ImobileDevice from platforms --- src/IO.Ably.Android/Platform.cs | 15 +++------ src/IO.Ably.NETFramework/Platform.cs | 24 +++----------- src/IO.Ably.NETStandard20/Platform.cs | 20 ++--------- .../AndroidMobileDevice.cs | 17 +++++----- src/IO.Ably.Push.iOS/AppleMobileDevice.cs | 18 +++++----- src/IO.Ably.Shared/AblyRealtime.cs | 2 +- src/IO.Ably.Shared/AblyRest.cs | 6 ++-- src/IO.Ably.Shared/Defaults.cs | 2 +- src/IO.Ably.Shared/IPlatform.cs | 4 --- src/IO.Ably.Shared/IoC.cs | 33 ++----------------- .../Infrastructure/TestTransportFactory.cs | 2 +- .../Realtime/RealtimeSpecs.cs | 4 +-- src/IO.Ably.iOS/Platform.cs | 15 ++++----- 13 files changed, 43 insertions(+), 119 deletions(-) diff --git a/src/IO.Ably.Android/Platform.cs b/src/IO.Ably.Android/Platform.cs index 341ce5677..b6bb5778e 100644 --- a/src/IO.Ably.Android/Platform.cs +++ b/src/IO.Ably.Android/Platform.cs @@ -1,24 +1,19 @@ -using IO.Ably.Transport; -using System.Net.NetworkInformation; -using IO.Ably.Push; +using System.Net.NetworkInformation; using IO.Ably.Realtime; namespace IO.Ably { internal class Platform : IPlatform { - private static readonly object _lock = new object(); - - internal static bool HookedUpToNetworkEvents { get; private set; } public Agent.PlatformRuntime PlatformId => Agent.PlatformRuntime.XamarinAndroid; - public bool SyncContextDefault => true; - public ITransportFactory TransportFactory => null; - public IMobileDevice MobileDevice { get; set; } + private static readonly object Lock = new object(); + + internal static bool HookedUpToNetworkEvents { get; set; } public void RegisterOsNetworkStateChanged(ILogger logger) { - lock (_lock) + lock (Lock) { if (HookedUpToNetworkEvents == false) { diff --git a/src/IO.Ably.NETFramework/Platform.cs b/src/IO.Ably.NETFramework/Platform.cs index a2527929e..c5e27a216 100644 --- a/src/IO.Ably.NETFramework/Platform.cs +++ b/src/IO.Ably.NETFramework/Platform.cs @@ -1,35 +1,19 @@ -using IO.Ably.Transport; -using System.Net.NetworkInformation; -using IO.Ably.Push; +using System.Net.NetworkInformation; using IO.Ably.Realtime; namespace IO.Ably { internal class Platform : IPlatform { - private static readonly object _lock = new object(); - - static Platform() - { - Initialize(); - } - - internal static bool HookedUpToNetworkEvents { get; private set; } - public Agent.PlatformRuntime PlatformId => Agent.PlatformRuntime.Framework; - public ITransportFactory TransportFactory => null; - - public IMobileDevice MobileDevice { get; set; } + private static readonly object Lock = new object(); - internal static void Initialize() - { - HookedUpToNetworkEvents = false; - } + internal static bool HookedUpToNetworkEvents { get; set; } public void RegisterOsNetworkStateChanged(ILogger logger) { - lock (_lock) + lock (Lock) { if (HookedUpToNetworkEvents == false) { diff --git a/src/IO.Ably.NETStandard20/Platform.cs b/src/IO.Ably.NETStandard20/Platform.cs index 1333c84ce..9d295c8e4 100644 --- a/src/IO.Ably.NETStandard20/Platform.cs +++ b/src/IO.Ably.NETStandard20/Platform.cs @@ -1,21 +1,10 @@ using System.Net.NetworkInformation; -using IO.Ably.Push; using IO.Ably.Realtime; -using IO.Ably.Transport; namespace IO.Ably { internal class Platform : IPlatform { - private static readonly object Lock = new object(); - - static Platform() - { - Initialize(); - } - - internal static bool HookedUpToNetworkEvents { get; private set; } - // Defined as per https://learn.microsoft.com/en-us/dotnet/standard/frameworks#preprocessor-symbols #if NET6_0 public Agent.PlatformRuntime PlatformId => Agent.PlatformRuntime.Net6; @@ -25,14 +14,9 @@ static Platform() public Agent.PlatformRuntime PlatformId => Agent.PlatformRuntime.Netstandard20; #endif - public ITransportFactory TransportFactory => null; - - public IMobileDevice MobileDevice { get; set; } + private static readonly object Lock = new object(); - internal static void Initialize() - { - HookedUpToNetworkEvents = false; - } + internal static bool HookedUpToNetworkEvents { get; set; } public void RegisterOsNetworkStateChanged(ILogger logger) { diff --git a/src/IO.Ably.Push.Android/AndroidMobileDevice.cs b/src/IO.Ably.Push.Android/AndroidMobileDevice.cs index a96937cab..bfab35995 100644 --- a/src/IO.Ably.Push.Android/AndroidMobileDevice.cs +++ b/src/IO.Ably.Push.Android/AndroidMobileDevice.cs @@ -1,5 +1,6 @@ using System; using System.Net; +using System.Runtime.CompilerServices; using Android.App; using Android.Content; using Android.Gms.Tasks; @@ -12,13 +13,13 @@ namespace IO.Ably.Push.Android public class AndroidMobileDevice : IMobileDevice { private const string TokenType = "fcm"; - private readonly ILogger _logger; private static AblyRealtime _realtimeInstance; - internal AndroidMobileDevice(PushCallbacks callbacks, ILogger logger) + private ILogger Logger { get; set; } + + internal AndroidMobileDevice(PushCallbacks callbacks) { Callbacks = callbacks; - _logger = logger; } /// @@ -44,11 +45,11 @@ public static IRealtimeClient Initialise(ClientOptions ablyClientOptions, Action /// Initialised Ably instance which supports push notification registrations. public static IRealtimeClient Initialise(ClientOptions ablyClientOptions, PushCallbacks callbacks = null) { - var androidMobileDevice = new AndroidMobileDevice(callbacks ?? new PushCallbacks(), DefaultLogger.LoggerInstance); - IoC.MobileDevice = androidMobileDevice; + var androidMobileDevice = new AndroidMobileDevice(callbacks ?? new PushCallbacks()); // Create the instance of ably used for Push registrations _realtimeInstance = new AblyRealtime(ablyClientOptions, androidMobileDevice); + androidMobileDevice.Logger = _realtimeInstance.Logger; _realtimeInstance.Push.InitialiseStateMachine(); return _realtimeInstance; } @@ -100,15 +101,15 @@ public void RequestRegistrationToken(Action> callback) { try { - _logger.Debug("Requesting a new Registration token"); + Logger.Debug("Requesting a new Registration token"); var messagingInstance = FirebaseMessaging.Instance; var resultTask = messagingInstance.GetToken(); - resultTask.AddOnCompleteListener(new RequestTokenCompleteListener(callback, _logger)); + resultTask.AddOnCompleteListener(new RequestTokenCompleteListener(callback, Logger)); } catch (Exception e) { - _logger.Error("Error while requesting a new Registration token.", e); + Logger.Error("Error while requesting a new Registration token.", e); var errorInfo = new ErrorInfo($"Failed to request AndroidToken. Error: {e?.Message}.", 50000, HttpStatusCode.InternalServerError, e); callback(Result.Fail(errorInfo)); } diff --git a/src/IO.Ably.Push.iOS/AppleMobileDevice.cs b/src/IO.Ably.Push.iOS/AppleMobileDevice.cs index 5a37ba3f9..fe3833070 100644 --- a/src/IO.Ably.Push.iOS/AppleMobileDevice.cs +++ b/src/IO.Ably.Push.iOS/AppleMobileDevice.cs @@ -12,13 +12,13 @@ public class AppleMobileDevice : IMobileDevice { private const string TokenType = "apns"; - private readonly ILogger _logger; private static AblyRealtime _realtimeInstance; - private AppleMobileDevice(PushCallbacks callbacks, ILogger logger) + private ILogger Logger { get; set; } + + private AppleMobileDevice(PushCallbacks callbacks) { Callbacks = callbacks; - _logger = logger; } /// @@ -42,9 +42,9 @@ public static IRealtimeClient Initialise(ClientOptions ablyClientOptions, Action /// Initialised Ably instance which supports push notification registrations. public static IRealtimeClient Initialise(ClientOptions ablyClientOptions, PushCallbacks callbacks = null) { - var mobileDevice = new AppleMobileDevice(callbacks, DefaultLogger.LoggerInstance); - IoC.MobileDevice = mobileDevice; + var mobileDevice = new AppleMobileDevice(callbacks); _realtimeInstance = new AblyRealtime(ablyClientOptions, mobileDevice); + mobileDevice.Logger = _realtimeInstance.Logger; _realtimeInstance.Push.InitialiseStateMachine(); return _realtimeInstance; } @@ -95,7 +95,7 @@ public static void OnRegistrationTokenFailed(ErrorInfo error) /// public void SetPreference(string key, string value, string groupName) { - _logger.Debug($"Setting preferences: {groupName}:{key} with value {value}"); + Logger.Debug($"Setting preferences: {groupName}:{key} with value {value}"); Preferences.Set(key, value, groupName); } @@ -108,14 +108,14 @@ public string GetPreference(string key, string groupName) /// public void RemovePreference(string key, string groupName) { - _logger.Debug($"Removing preference: {groupName}:{key}"); + Logger.Debug($"Removing preference: {groupName}:{key}"); Preferences.Remove(key, groupName); } /// public void ClearPreferences(string groupName) { - _logger.Debug($"Clearing preferences group: {groupName}"); + Logger.Debug($"Clearing preferences group: {groupName}"); Preferences.Clear(groupName); } @@ -136,7 +136,7 @@ public void RequestRegistrationToken(Action> unusedAct } else { - _logger.Error($"Error signing up for remote notifications: {error.LocalizedDescription}"); + Logger.Error($"Error signing up for remote notifications: {error.LocalizedDescription}"); } }); } diff --git a/src/IO.Ably.Shared/AblyRealtime.cs b/src/IO.Ably.Shared/AblyRealtime.cs index 75c13c7b2..64c9787b3 100644 --- a/src/IO.Ably.Shared/AblyRealtime.cs +++ b/src/IO.Ably.Shared/AblyRealtime.cs @@ -74,7 +74,7 @@ internal AblyRealtime(ClientOptions options, Func> ExecuteHttpRequest; private LocalDevice _device; - internal IoC Ioc { get; set; } - /// /// Initializes a new instance of the class using an api key. /// Full api key. @@ -146,8 +144,8 @@ private void InitializeAbly(IMobileDevice mobileDevice = null) HttpClient = new AblyHttpClient(new AblyHttpOptions(Options)); ExecuteHttpRequest = HttpClient.Execute; AblyAuth = new AblyAuth(Options, this); - Ioc = new IoC(Logger); - MobileDevice = mobileDevice ?? Ioc.MobileDevice; + + MobileDevice = mobileDevice; Channels = new RestChannels(this, MobileDevice); Push = new PushRest(this, Logger); AblyAuth.OnClientIdChanged = OnAuthClientIdChanged; diff --git a/src/IO.Ably.Shared/Defaults.cs b/src/IO.Ably.Shared/Defaults.cs index f712d890f..cac77f496 100644 --- a/src/IO.Ably.Shared/Defaults.cs +++ b/src/IO.Ably.Shared/Defaults.cs @@ -48,7 +48,7 @@ internal static string GetVersion() public static readonly TimeSpan ConnectionStateTtl = TimeSpan.FromSeconds(120); // https://sdk.ably.com/builds/ably/specification/main/features/#DF1a public static readonly TimeSpan FallbackRetryTimeout = TimeSpan.FromMinutes(10); // https://sdk.ably.com/builds/ably/specification/main/features/#TO3l10 - public static readonly ITransportFactory WebSocketTransportFactory = IoC.TransportFactory; + public static readonly ITransportFactory WebSocketTransportFactory = new MsWebSocketTransport.TransportFactory(); internal const int TokenErrorCodesRangeStart = 40140; internal const int TokenErrorCodesRangeEnd = 40149; diff --git a/src/IO.Ably.Shared/IPlatform.cs b/src/IO.Ably.Shared/IPlatform.cs index 65fadb531..1e32b7a91 100644 --- a/src/IO.Ably.Shared/IPlatform.cs +++ b/src/IO.Ably.Shared/IPlatform.cs @@ -16,10 +16,6 @@ internal interface IPlatform { Agent.PlatformRuntime PlatformId { get; } - ITransportFactory TransportFactory { get; } - - IMobileDevice MobileDevice { get; set; } - /// /// This method when implemented in each Platform class includes logic to subscribe to /// NetworkStatus changes from the operating system. It is then exposed through diff --git a/src/IO.Ably.Shared/IoC.cs b/src/IO.Ably.Shared/IoC.cs index 5ba77a600..f7471c869 100644 --- a/src/IO.Ably.Shared/IoC.cs +++ b/src/IO.Ably.Shared/IoC.cs @@ -1,41 +1,12 @@ -using System; -using IO.Ably.Push; -using IO.Ably.Transport; - -namespace IO.Ably +namespace IO.Ably { /// This class initializes Platform. internal class IoC { public static readonly Platform Platform = new Platform(); - public static ITransportFactory TransportFactory => Platform?.TransportFactory ?? new MsWebSocketTransport.TransportFactory(); - public static Agent.PlatformRuntime PlatformId => Platform?.PlatformId ?? Agent.PlatformRuntime.Other; - public IoC(ILogger logger) - { - Logger = logger; - } - - public ILogger Logger { get; set; } - - public void RegisterOsNetworkStateChanged() => Platform.RegisterOsNetworkStateChanged(Logger); - - public IMobileDevice MobileDevice - { - get - { - try - { - return Platform.MobileDevice; - } - catch (Exception e) when (e is NotImplementedException) - { - Logger.Error("Mobile Device is no supported on the current platform.", e); - return null; - } - } - } + public static void RegisterOsNetworkStateChanged(ILogger logger) => Platform.RegisterOsNetworkStateChanged(logger); } } diff --git a/src/IO.Ably.Tests.Shared/Infrastructure/TestTransportFactory.cs b/src/IO.Ably.Tests.Shared/Infrastructure/TestTransportFactory.cs index 9485b5dcc..7bb87bca9 100644 --- a/src/IO.Ably.Tests.Shared/Infrastructure/TestTransportFactory.cs +++ b/src/IO.Ably.Tests.Shared/Infrastructure/TestTransportFactory.cs @@ -25,7 +25,7 @@ internal TestTransportFactory(Action onWrappedTransportCre public ITransport CreateTransport(TransportParams parameters) { - var factory = IoC.TransportFactory; + var factory = Defaults.WebSocketTransportFactory; var transport = new TestTransportWrapper(factory.CreateTransport(parameters), parameters.UseBinaryProtocol ? Defaults.Protocol : Protocol.Json); diff --git a/src/IO.Ably.Tests.Shared/Realtime/RealtimeSpecs.cs b/src/IO.Ably.Tests.Shared/Realtime/RealtimeSpecs.cs index 9760c9288..046305d9c 100644 --- a/src/IO.Ably.Tests.Shared/Realtime/RealtimeSpecs.cs +++ b/src/IO.Ably.Tests.Shared/Realtime/RealtimeSpecs.cs @@ -137,9 +137,7 @@ public void AutomaticNetworkDetectionCanBeDisabledByClientOption(bool enabled) { // Because this test depends on static state in the 'Platform' type we need // to (re)Initialize the static 'Platform' state before each test run. - - Platform.Initialize(); - + Platform.HookedUpToNetworkEvents = false; _ = new AblyRealtime(new ClientOptions(ValidKey) { AutomaticNetworkStateMonitoring = enabled, diff --git a/src/IO.Ably.iOS/Platform.cs b/src/IO.Ably.iOS/Platform.cs index 05290245c..d29a2281d 100644 --- a/src/IO.Ably.iOS/Platform.cs +++ b/src/IO.Ably.iOS/Platform.cs @@ -1,22 +1,19 @@ -using IO.Ably.Transport; -using System.Net.NetworkInformation; -using IO.Ably.Push; +using System.Net.NetworkInformation; using IO.Ably.Realtime; namespace IO.Ably { internal class Platform : IPlatform { - private static readonly object _lock = new object(); - - internal static bool HookedUpToNetworkEvents { get; private set; } public Agent.PlatformRuntime PlatformId => Agent.PlatformRuntime.XamarinIos; - public ITransportFactory TransportFactory => null; - public IMobileDevice MobileDevice { get; set; } + + private static readonly object Lock = new object(); + + internal static bool HookedUpToNetworkEvents { get; set; } public void RegisterOsNetworkStateChanged(ILogger logger) { - lock (_lock) + lock (Lock) { if (HookedUpToNetworkEvents == false) {