Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK-3770] Fix logging issue across multiple rest/realtime/push instances #1251

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1946461
Updated defaultLogger to not have singleton logging instance
sacOO7 Aug 2, 2023
da06e52
Added internal logger as default for clientOptions
sacOO7 Aug 10, 2023
ba1e9e9
Updated ablyrest and realtime instances to use internal logger
sacOO7 Aug 10, 2023
c0b7d05
Updated channelWaiter class, added logger to the same
sacOO7 Aug 10, 2023
3e072d8
Removed use of logger from connection state base and related connecti…
sacOO7 Aug 10, 2023
86cb85b
Removed unnecessary use of logger from connectionStatebase and other …
sacOO7 Aug 10, 2023
b44808c
Refactored ablyHttpClient with logger, updated exceptions for null op…
sacOO7 Aug 10, 2023
8b916b9
Refactored logger, moved test related methods under message extensions
sacOO7 Aug 10, 2023
375c0e4
Refactored code for client options supplied logger
sacOO7 Aug 10, 2023
1cab5b8
Refactored IOC class, Updated ablyrealtime and ablyrest for the same
sacOO7 Aug 11, 2023
85e94c4
Added logger for handling connecting token error, fixed logger issues
sacOO7 Aug 11, 2023
5b1b15c
Fixed closed state spec, updated connection instance init
sacOO7 Aug 11, 2023
047c2c1
Fixed logging related tests
sacOO7 Aug 11, 2023
2f0c827
Skipped localdevice test
sacOO7 Aug 12, 2023
60268fc
Skipped failing test, fixed channel spec test for testloggersink
sacOO7 Aug 12, 2023
13a0940
Fixed channel sandboxspec test, updated defaultlogger
sacOO7 Aug 12, 2023
893f12e
Updated defaultLogger to create a singleTon, updated realtimeClient with
sacOO7 Aug 12, 2023
d8eb29f
Added calculationDelay timeout to reduce test flakiness for incremental
sacOO7 Aug 12, 2023
884dad1
Removed unnecessary setting mobileDevice for IOC, unskipped failing t…
sacOO7 Aug 12, 2023
9afbfdf
Unskipped localdevice flaky tests
sacOO7 Aug 12, 2023
9b88479
Refactored platform and IOC classes, removed ImobileDevice from platf…
sacOO7 Aug 12, 2023
c4c0ab7
Merge pull request #1254 from ably/fix/fix-ioc-class
sacOO7 Aug 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions src/IO.Ably.Android/Platform.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
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()
public void RegisterOsNetworkStateChanged(ILogger logger)
{
lock (_lock)
lock (Lock)
{
if (HookedUpToNetworkEvents == false)
{
NetworkChange.NetworkAvailabilityChanged += (sender, eventArgs) =>
Connection.NotifyOperatingSystemNetworkState(eventArgs.IsAvailable ? NetworkState.Online : NetworkState.Offline);
Connection.NotifyOperatingSystemNetworkState(eventArgs.IsAvailable ? NetworkState.Online : NetworkState.Offline, logger);
}

HookedUpToNetworkEvents = true;
Expand Down
28 changes: 6 additions & 22 deletions src/IO.Ably.NETFramework/Platform.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,24 @@
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()
public void RegisterOsNetworkStateChanged(ILogger logger)
{
lock (_lock)
lock (Lock)
{
if (HookedUpToNetworkEvents == false)
{
NetworkChange.NetworkAvailabilityChanged += (sender, eventArgs) =>
Connection.NotifyOperatingSystemNetworkState(eventArgs.IsAvailable ? NetworkState.Online : NetworkState.Offline);
Connection.NotifyOperatingSystemNetworkState(eventArgs.IsAvailable ? NetworkState.Online : NetworkState.Offline, logger);
}

HookedUpToNetworkEvents = true;
Expand Down
24 changes: 4 additions & 20 deletions src/IO.Ably.NETStandard20/Platform.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -25,23 +14,18 @@ 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()
public void RegisterOsNetworkStateChanged(ILogger logger)
{
lock (Lock)
{
if (HookedUpToNetworkEvents == false)
{
NetworkChange.NetworkAvailabilityChanged += (sender, eventArgs) =>
Connection.NotifyOperatingSystemNetworkState(eventArgs.IsAvailable ? NetworkState.Online : NetworkState.Offline);
Connection.NotifyOperatingSystemNetworkState(eventArgs.IsAvailable ? NetworkState.Online : NetworkState.Offline, logger);
}

HookedUpToNetworkEvents = true;
Expand Down
17 changes: 9 additions & 8 deletions src/IO.Ably.Push.Android/AndroidMobileDevice.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Net;
using System.Runtime.CompilerServices;
using Android.App;
using Android.Content;
using Android.Gms.Tasks;
Expand All @@ -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;
}

/// <summary>
Expand All @@ -44,11 +45,11 @@ public static IRealtimeClient Initialise(ClientOptions ablyClientOptions, Action
/// <returns>Initialised Ably instance which supports push notification registrations.</returns>
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;
}
Expand Down Expand Up @@ -100,15 +101,15 @@ public void RequestRegistrationToken(Action<Result<RegistrationToken>> 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<RegistrationToken>(errorInfo));
}
Expand Down
18 changes: 9 additions & 9 deletions src/IO.Ably.Push.iOS/AppleMobileDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <summary>
Expand All @@ -42,9 +42,9 @@ public static IRealtimeClient Initialise(ClientOptions ablyClientOptions, Action
/// <returns>Initialised Ably instance which supports push notification registrations.</returns>
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;
}
Expand Down Expand Up @@ -95,7 +95,7 @@ public static void OnRegistrationTokenFailed(ErrorInfo error)
/// <inheritdoc/>
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);
}

Expand All @@ -108,14 +108,14 @@ public string GetPreference(string key, string groupName)
/// <inheritdoc/>
public void RemovePreference(string key, string groupName)
{
_logger.Debug($"Removing preference: {groupName}:{key}");
Logger.Debug($"Removing preference: {groupName}:{key}");
Preferences.Remove(key, groupName);
}

/// <inheritdoc/>
public void ClearPreferences(string groupName)
{
_logger.Debug($"Clearing preferences group: {groupName}");
Logger.Debug($"Clearing preferences group: {groupName}");
Preferences.Clear(groupName);
}

Expand All @@ -136,7 +136,7 @@ public void RequestRegistrationToken(Action<Result<RegistrationToken>> unusedAct
}
else
{
_logger.Error($"Error signing up for remote notifications: {error.LocalizedDescription}");
Logger.Error($"Error signing up for remote notifications: {error.LocalizedDescription}");
}
});
}
Expand Down
18 changes: 10 additions & 8 deletions src/IO.Ably.Shared/AblyRealtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class AblyRealtime : IRealtimeClient, IDisposable
{
private SynchronizationContext _synchronizationContext;

internal ILogger Logger { get; set; } = DefaultLogger.LoggerInstance;
internal ILogger Logger { get; set; }

internal RealtimeWorkflow Workflow { get; private set; }

Expand All @@ -40,7 +40,7 @@ public AblyRealtime(string key)
/// </summary>
/// <param name="options"><see cref="ClientOptions"/>.</param>
public AblyRealtime(ClientOptions options)
: this(options, CreateRestFunc, IoC.MobileDevice)
: this(options, CreateRestFunc)
{
}

Expand All @@ -51,11 +51,12 @@ internal AblyRealtime(ClientOptions options, IMobileDevice mobileDevice)

internal AblyRealtime(ClientOptions options, Func<ClientOptions, IMobileDevice, AblyRest> createRestFunc, IMobileDevice mobileDevice = null)
{
if (options.Logger != null)
if (options == null)
{
Logger = options.Logger;
throw new AblyException("No clientOptions provided to AblyRealtime instance");
}

Logger = options.Logger;
Logger.LogLevel = options.LogLevel;

if (options.LogHandler != null)
Expand All @@ -64,21 +65,22 @@ internal AblyRealtime(ClientOptions options, Func<ClientOptions, IMobileDevice,
}

CaptureSynchronizationContext(options);

RestClient = createRestFunc != null ? createRestFunc.Invoke(options, mobileDevice) : new AblyRest(options, mobileDevice);
Push = new PushRealtime(RestClient, Logger);

Connection = new Connection(this, options.NowFunc, options.Logger);
Connection = new Connection(this, options.NowFunc);
Connection.Initialise();

if (options.AutomaticNetworkStateMonitoring)
{
IoC.RegisterOsNetworkStateChanged();
IoC.RegisterOsNetworkStateChanged(Logger);
}

Channels = new RealtimeChannels(this, Connection, mobileDevice);
Channels = new RealtimeChannels(this, Connection, RestClient.MobileDevice);
RestClient.AblyAuth.OnAuthUpdated = ConnectionManager.OnAuthUpdated;

State = new RealtimeState(options.GetFallbackHosts()?.Shuffle().ToList(), options.NowFunc);
State = new RealtimeState(options.GetFallbackHosts()?.Shuffle().ToList(), Logger, options.NowFunc);

Workflow = new RealtimeWorkflow(this, Logger);
Workflow.Start();
Expand Down
26 changes: 11 additions & 15 deletions src/IO.Ably.Shared/AblyRest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ public AblyRest(Action<ClientOptions> init)
{
Options = new ClientOptions();
init(Options);
InitializeAbly(IoC.MobileDevice);
InitializeAbly();
}

/// <summary>
/// Initialize the library with a custom set of options.
/// </summary>
/// <param name="clientOptions">instance of clientOptions.</param>
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);
Expand Down Expand Up @@ -106,7 +106,7 @@ public LocalDevice Device
{
lock (_deviceLock)
{
return LocalDevice.GetInstance(MobileDevice, Auth.ClientId);
return LocalDevice.GetInstance(MobileDevice, Auth.ClientId, Logger);
}
}

Expand All @@ -119,22 +119,17 @@ public LocalDevice Device

internal ClientOptions Options { get; }

internal ILogger Logger { get; set; } = DefaultLogger.LoggerInstance;
internal ILogger Logger { get; set; }

/// <summary>Initializes the rest client and validates the passed in options.</summary>
private void InitializeAbly(IMobileDevice mobileDevice)
private void InitializeAbly(IMobileDevice mobileDevice = null)
{
if (Options == null)
{
Logger.Error("No options provider to Ably rest");
throw new AblyException("Invalid options");
}

if (Options.Logger != null)
{
Logger = Options.Logger;
throw new AblyException("No clientOptions provided to AblyRest instance");
}

Logger = Options.Logger;
Logger.LogLevel = Options.LogLevel;

if (Options.LogHandler != null)
Expand All @@ -149,9 +144,10 @@ private void InitializeAbly(IMobileDevice mobileDevice)
HttpClient = new AblyHttpClient(new AblyHttpOptions(Options));
ExecuteHttpRequest = HttpClient.Execute;
AblyAuth = new AblyAuth(Options, this);
Channels = new RestChannels(this, mobileDevice);
Push = new PushRest(this, Logger);

MobileDevice = mobileDevice;
Channels = new RestChannels(this, MobileDevice);
Push = new PushRest(this, Logger);
AblyAuth.OnClientIdChanged = OnAuthClientIdChanged;
}

Expand Down
2 changes: 1 addition & 1 deletion src/IO.Ably.Shared/ClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ internal Func<DateTimeOffset> NowFunc
}

[JsonIgnore]
internal ILogger Logger { get; set; } = DefaultLogger.LoggerInstance;
internal ILogger Logger { get; set; } = InternalLogger.Create();

internal bool SkipInternetCheck { get; set; }

Expand Down
Loading
Loading