Skip to content

Commit

Permalink
Removed unnecessary use of logger from connectionStatebase and other …
Browse files Browse the repository at this point in the history
…children
  • Loading branch information
sacOO7 committed Aug 10, 2023
1 parent 3e072d8 commit 86cb85b
Show file tree
Hide file tree
Showing 17 changed files with 35 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/IO.Ably.Shared/AblyRealtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ internal AblyRealtime(ClientOptions options, Func<ClientOptions, IMobileDevice,
Channels = new RealtimeChannels(this, Connection, 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
12 changes: 5 additions & 7 deletions src/IO.Ably.Shared/Realtime/Workflows/RealtimeState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,18 @@ public long IncrementSerial()
public readonly List<MessageAndCallback> WaitingForAck = new List<MessageAndCallback>();

public void AddAckMessage(ProtocolMessage message, Action<bool, ErrorInfo> callback) =>
WaitingForAck.Add(new MessageAndCallback(message, callback));
WaitingForAck.Add(new MessageAndCallback(message, callback, Logger));

public RealtimeState()
: this(null)
{
}

public RealtimeState(List<string> fallbackHosts, Func<DateTimeOffset> now = null)
public RealtimeState(List<string> fallbackHosts, ILogger logger, Func<DateTimeOffset> now = null)
{
Logger = logger;
Connection = new ConnectionData(fallbackHosts);
AttemptsInfo = new ConnectionAttemptsInfo(now);
PendingMessages = new List<MessageAndCallback>();
}

public ILogger Logger { get; set; }

public JObject WhatDoIHave()
{
var stateJson = new JObject
Expand Down
13 changes: 6 additions & 7 deletions src/IO.Ably.Shared/Realtime/Workflows/RealtimeWorkflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public RealtimeWorkflow(AblyRealtime client, ILogger logger)

private void SetInitialConnectionState()
{
var initialState = new ConnectionInitializedState(ConnectionManager, Logger);
var initialState = new ConnectionInitializedState(ConnectionManager);
State.Connection.CurrentStateObject = initialState;
SetRecoverKeyIfPresent(Client.Options.Recover);
}
Expand Down Expand Up @@ -630,8 +630,7 @@ private void HandleConnectedCommand(SetConnectedStateCommand cmd)
var connectedState = new ConnectionConnectedState(
ConnectionManager,
cmd.Message.Error,
cmd.IsUpdate,
Logger);
cmd.IsUpdate);

SetState(connectedState);

Expand Down Expand Up @@ -716,7 +715,7 @@ private async Task<RealtimeCommand> HandleSetStateCommand(RealtimeCommand comman
switch (command)
{
case ForceStateInitializationCommand _:
var initState = new ConnectionInitializedState(ConnectionManager, Logger);
var initState = new ConnectionInitializedState(ConnectionManager);
SetState(initState);
break;
case SetConnectedStateCommand cmd:
Expand Down Expand Up @@ -779,7 +778,7 @@ private async Task<RealtimeCommand> HandleSetStateCommand(RealtimeCommand comman
ClearAckQueueAndFailMessages(ErrorInfo.ReasonFailed);

var error = TransformIfTokenErrorAndNotRetryable();
var failedState = new ConnectionFailedState(ConnectionManager, error, Logger);
var failedState = new ConnectionFailedState(ConnectionManager, error);
SetState(failedState);

ConnectionManager.DestroyTransport();
Expand Down Expand Up @@ -886,7 +885,7 @@ ErrorInfo TransformIfTokenErrorAndNotRetryable()
State.Connection.ClearKeyAndId();
ClearAckQueueAndFailMessages(ErrorInfo.ReasonClosed);

var closedState = new ConnectionClosedState(ConnectionManager, cmd.Error, Logger)
var closedState = new ConnectionClosedState(ConnectionManager, cmd.Error)
{
Exception = cmd.Exception,
};
Expand Down Expand Up @@ -1017,7 +1016,7 @@ public void QueueAck(ProtocolMessage message, Action<bool, ErrorInfo> callback)
{
if (message.AckRequired)
{
State.WaitingForAck.Add(new MessageAndCallback(message, callback));
State.WaitingForAck.Add(new MessageAndCallback(message, callback, Logger));
if (Logger.IsDebug)
{
Logger.Debug($"Message ({message.Action}) with serial ({message.MsgSerial}) was queued to get Ack");
Expand Down
4 changes: 2 additions & 2 deletions src/IO.Ably.Shared/Transport/MessageAndCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ internal class MessageAndCallback

public Action<bool, ErrorInfo> Callback { get; }

public MessageAndCallback(ProtocolMessage message, Action<bool, ErrorInfo> callback, ILogger logger = null)
public MessageAndCallback(ProtocolMessage message, Action<bool, ErrorInfo> callback, ILogger logger)
{
Message = message;
Callback = callback;
Logger = logger ?? DefaultLogger.LoggerInstance;
Logger = logger;
}

protected bool Equals(MessageAndCallback other)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ namespace IO.Ably.Transport.States.Connection
{
internal class ConnectionConnectedState : ConnectionStateBase
{
public ConnectionConnectedState(IConnectionContext context,
public ConnectionConnectedState(
IConnectionContext context,
ErrorInfo error = null,
bool isUpdate = false)
: base(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ internal class ConnectionFailedState : ConnectionStateBase
{
public override ErrorInfo DefaultErrorInfo => ErrorInfo.ReasonFailed;

public ConnectionFailedState(IConnectionContext context, ErrorInfo error) : base(context)
public ConnectionFailedState(IConnectionContext context, ErrorInfo error)
: base(context)
{
Error = error ?? ErrorInfo.ReasonFailed;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ protected ConnectionStateBase(IConnectionContext context)
Context = context;
}

internal ILogger Logger { get; private set; }

protected readonly IConnectionContext Context;

public abstract ConnectionState State { get; }
Expand Down
10 changes: 4 additions & 6 deletions src/IO.Ably.Tests.Shared/Realtime/ChannelSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ public async Task WhenConnectionIsClosedClosingSuspendedOrFailed_ShouldCallCallb
var client = await GetConnectedClient();

// Closed
client.Workflow.SetState(new ConnectionClosedState(client.ConnectionManager, Logger));
client.Workflow.SetState(new ConnectionClosedState(client.ConnectionManager));
var closedAttach = await client.Channels.Get("closed").AttachAsync();
AssertAttachResultIsFailure(closedAttach);

Expand All @@ -503,8 +503,7 @@ public async Task WhenConnectionIsClosedClosingSuspendedOrFailed_ShouldCallCallb
// Failed
client.Workflow.SetState(new ConnectionFailedState(
client.ConnectionManager,
ErrorInfo.ReasonFailed,
Logger));
ErrorInfo.ReasonFailed));

var failedAttach = await client.Channels.Get("failed").AttachAsync();
AssertAttachResultIsFailure(failedAttach);
Expand Down Expand Up @@ -534,7 +533,7 @@ public async Task WhenConnectionIsClosedClosingSuspendedOrFailed_ShouldThrowErro
switch (state)
{
case ConnectionState.Closed:
client.Workflow.SetState(new ConnectionClosedState(client.ConnectionManager, Logger));
client.Workflow.SetState(new ConnectionClosedState(client.ConnectionManager));
break;
case ConnectionState.Closing:
client.Workflow.SetState(new ConnectionClosingState(client.ConnectionManager, false, Logger));
Expand All @@ -545,8 +544,7 @@ public async Task WhenConnectionIsClosedClosingSuspendedOrFailed_ShouldThrowErro
case ConnectionState.Failed:
client.Workflow.SetState(new ConnectionFailedState(
client.ConnectionManager,
ErrorInfo.ReasonFailed,
Logger));
ErrorInfo.ReasonFailed));
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace IO.Ably.Tests.Realtime
{
public class ConnectionAttemptsInfoSpecs : MockHttpRealtimeSpecs
{
private readonly RealtimeState _state = new RealtimeState();
private readonly RealtimeState _state = new RealtimeState(null, DefaultLogger.LoggerInstance);

private ConnectionAttemptsInfo Info => _state.AttemptsInfo;

Expand Down Expand Up @@ -46,7 +46,7 @@ public void ShouldSuspend_WhenFirstAttemptIsWithinConnectionStateTtl_ShouldRetur
public void ShouldSuspend_WhenFirstAttemptEqualOrGreaterThanConnectionStateTtl_ShouldReturnTrue()
{
var now = new Now();
var state = new RealtimeState(null, now.ValueFn);
var state = new RealtimeState(null, DefaultLogger.LoggerInstance, now.ValueFn);

state.AttemptsInfo.Attempts.Add(new ConnectionAttempt(now.Value));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ClosedStateSpecs(ITestOutputHelper output)

_logger = InternalLogger.Create(Defaults.DefaultLogLevel, sink);
var context = new FakeConnectionContext();
_state = new ConnectionClosedState(context, _logger);
_state = new ConnectionClosedState(context);
}

[Fact]
Expand All @@ -45,7 +45,7 @@ public void WhenConnectCalled_MovesToConnectingState()
public void WhenCloseCalled_ShouldDoNothing()
{
// Act
new ConnectionClosedState(null, _logger).Close();
new ConnectionClosedState(null).Close();
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public async Task OnErrorReceived_TimerIsAbortedAndStateIsFailedState()

private ConnectionClosingState GetState(ErrorInfo info = null, bool connectedTransport = true)
{
return new ConnectionClosingState(_context, info, connectedTransport, _timer, Logger);
return new ConnectionClosingState(_context, info, connectedTransport, _timer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public ConnectingStateSpecs(ITestOutputHelper output)
{
_context = new FakeConnectionContext();
_timer = new FakeTimer();
_state = new ConnectionConnectingState(_context, _timer, Logger);
_state = new ConnectionConnectingState(_context, _timer);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public async Task AfterAnInterval_ShouldRetryConnection()

private ConnectionDisconnectedState GetState(ErrorInfo error = null)
{
return new ConnectionDisconnectedState(_context, error, _timer, Logger);
return new ConnectionDisconnectedState(_context, error, _timer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public async Task ShouldNotHandleInboundMessages(ProtocolMessage.MessageAction a

private ConnectionFailedState GetState(ErrorInfo info = null)
{
return new ConnectionFailedState(_context, info, Logger);
return new ConnectionFailedState(_context, info);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public InitializedStateSpecs(ITestOutputHelper output)
: base(output)
{
var context = new FakeConnectionContext();
_state = new ConnectionInitializedState(context, Logger);
_state = new ConnectionInitializedState(context);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class SuspendedStateSpecs : AblySpecs

private ConnectionSuspendedState GetState(ErrorInfo info = null)
{
return new ConnectionSuspendedState(_context, info, _timer, Logger);
return new ConnectionSuspendedState(_context, info, _timer);
}

public SuspendedStateSpecs(ITestOutputHelper output)
Expand Down
6 changes: 3 additions & 3 deletions src/IO.Ably.Tests.Shared/Realtime/RealtimeWorkflowSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public async Task OnAttached_ClearsAckQueue()
{
var client = GetDisconnectedClient();

client.State.WaitingForAck.Add(new MessageAndCallback(new ProtocolMessage(), null));
client.State.WaitingForAck.Add(new MessageAndCallback(new ProtocolMessage(), null, client.Logger));

client.ExecuteCommand(SetSuspendedStateCommand.Create(null));

Expand Down Expand Up @@ -267,7 +267,7 @@ public async Task ShouldClearsAckQueue()
{
var client = GetDisconnectedClient();

client.State.WaitingForAck.Add(new MessageAndCallback(new ProtocolMessage(), null));
client.State.WaitingForAck.Add(new MessageAndCallback(new ProtocolMessage(), null, client.Logger));

client.ExecuteCommand(SetClosedStateCommand.Create());

Expand Down Expand Up @@ -307,7 +307,7 @@ public async Task OnAttached_ClearsAckQueue()
{
var client = GetDisconnectedClient();

client.State.WaitingForAck.Add(new MessageAndCallback(new ProtocolMessage(), null));
client.State.WaitingForAck.Add(new MessageAndCallback(new ProtocolMessage(), null, client.Logger));

client.ExecuteCommand(SetClosedStateCommand.Create());

Expand Down

0 comments on commit 86cb85b

Please sign in to comment.