Skip to content
Merged
Changes from all commits
Commits
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
41 changes: 41 additions & 0 deletions tests/OpenClaw.Shared.Tests/WebSocketClientBaseTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

Expand All @@ -17,6 +18,7 @@ public class TestWebSocketClient : WebSocketClientBase
public int OnErrorCallCount { get; private set; }
public Exception? LastError { get; private set; }
public int OnDisposingCallCount { get; private set; }
public bool AutoReconnectEnabled { get; set; } = true;

protected override int ReceiveBufferSize => 8192;
protected override string ClientRole => "test";
Expand Down Expand Up @@ -52,6 +54,8 @@ protected override void OnDisposing()
OnDisposingCallCount++;
}

protected override bool ShouldAutoReconnect() => AutoReconnectEnabled;

// Expose protected members for testing
public void TestRaiseStatusChanged(ConnectionStatus status)
=> RaiseStatusChanged(status);
Expand Down Expand Up @@ -232,6 +236,43 @@ public async Task ConnectAsync_RaisesStatusChangedConnecting()
Assert.Contains(ConnectionStatus.Connecting, statuses);
client.Dispose();
}

[Fact]
public async Task ConnectAsync_WhenConnectionFails_StartsReconnectLoop()
{
var client = new TestWebSocketClient("ws://127.0.0.1:1", "token", _logger);
var statuses = new List<ConnectionStatus>();
client.StatusChanged += (_, s) => statuses.Add(s);

await client.ConnectAsync();
await Task.Delay(150);

Assert.Contains(ConnectionStatus.Error, statuses);
Assert.True(statuses.Count(s => s == ConnectionStatus.Connecting) >= 2);
Assert.Contains(_logger.Logs, line => line.Contains("reconnecting in 1000ms", StringComparison.OrdinalIgnoreCase));

client.Dispose();
}

[Fact]
public async Task ConnectAsync_WhenAutoReconnectDisabled_DoesNotStartReconnectLoop()
{
var client = new TestWebSocketClient("ws://127.0.0.1:1", "token", _logger)
{
AutoReconnectEnabled = false
};
var statuses = new List<ConnectionStatus>();
client.StatusChanged += (_, s) => statuses.Add(s);

await client.ConnectAsync();
await Task.Delay(150);

Assert.Contains(ConnectionStatus.Error, statuses);
Assert.Single(statuses, s => s == ConnectionStatus.Connecting);
Assert.DoesNotContain(_logger.Logs, line => line.Contains("reconnecting in", StringComparison.OrdinalIgnoreCase));

client.Dispose();
}
}

public class TestLogger : IOpenClawLogger
Expand Down
Loading