1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-08 16:36:15 +00:00

Test fixes

This commit is contained in:
JKorf 2018-12-04 16:06:44 +01:00
parent f215f3c53f
commit f09a731cde
4 changed files with 67 additions and 11 deletions

View File

@ -164,6 +164,7 @@ namespace CryptoExchange.Net.UnitTests
{ {
reconnected = true; reconnected = true;
rstEvent.Set(); rstEvent.Set();
return true;
}; };
// act // act
@ -229,5 +230,34 @@ namespace CryptoExchange.Net.UnitTests
// assert // assert
Assert.IsFalse(connectResult.Success); Assert.IsFalse(connectResult.Success);
} }
[Test]
public void WhenResubscribeFails_Socket_ShouldReconnect()
{
// arrange
int reconnected = 0;
var client = new TestSocketClient(new SocketClientOptions() { ReconnectInterval = TimeSpan.FromMilliseconds(1), LogVerbosity = LogVerbosity.Debug });
var socket = client.CreateSocket();
socket.ShouldReconnect = true;
socket.CanConnect = true;
socket.DisconnectTime = DateTime.UtcNow;
var sub = new SocketSubscription(socket);
client.ConnectSocketSub(sub);
var rstEvent = new ManualResetEvent(false);
client.OnReconnect += () =>
{
reconnected++;
rstEvent.Set();
return reconnected == 2;
};
// act
socket.InvokeClose();
rstEvent.WaitOne(1000);
Thread.Sleep(100);
// assert
Assert.IsTrue(reconnected == 2);
}
} }
} }

View File

@ -20,6 +20,7 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations
public int Id { get; } public int Id { get; }
public bool ShouldReconnect { get; set; } public bool ShouldReconnect { get; set; }
public TimeSpan Timeout { get; set; }
public Func<byte[], string> DataInterpreter { get; set; } public Func<byte[], string> DataInterpreter { get; set; }
public DateTime? DisconnectTime { get; set; } public DateTime? DisconnectTime { get; set; }
public string Url { get; } public string Url { get; }
@ -30,9 +31,24 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations
public TimeSpan PingInterval { get; set; } public TimeSpan PingInterval { get; set; }
public SslProtocols SSLProtocols { get; set; } public SslProtocols SSLProtocols { get; set; }
public int ConnectCalls { get; private set; }
public static int lastId = 0;
public static object lastIdLock = new object();
public TestSocket()
{
lock (lastIdLock)
{
Id = lastId + 1;
lastId++;
}
}
public Task<bool> Connect() public Task<bool> Connect()
{ {
Connected = CanConnect; Connected = CanConnect;
ConnectCalls++;
return Task.FromResult(CanConnect); return Task.FromResult(CanConnect);
} }
@ -45,6 +61,8 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations
public Task Close() public Task Close()
{ {
Connected = false; Connected = false;
DisconnectTime = DateTime.UtcNow;
OnClose?.Invoke();
return Task.FromResult(0); return Task.FromResult(0);
} }
@ -59,6 +77,7 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations
public void InvokeClose() public void InvokeClose()
{ {
Connected = false; Connected = false;
DisconnectTime = DateTime.UtcNow;
OnClose?.Invoke(); OnClose?.Invoke();
} }

View File

@ -9,7 +9,7 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations
{ {
public class TestSocketClient: SocketClient public class TestSocketClient: SocketClient
{ {
public Action OnReconnect { get; set; } public Func<bool> OnReconnect { get; set; }
public TestSocketClient() : this(new SocketClientOptions()) public TestSocketClient() : this(new SocketClientOptions())
{ {
@ -23,6 +23,7 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations
public TestSocket CreateSocket() public TestSocket CreateSocket()
{ {
Mock.Get(SocketFactory).Setup(f => f.CreateWebsocket(It.IsAny<Log>(), It.IsAny<string>())).Returns(new TestSocket());
return (TestSocket)CreateSocket(BaseAddress); return (TestSocket)CreateSocket(BaseAddress);
} }
@ -33,8 +34,7 @@ namespace CryptoExchange.Net.UnitTests.TestImplementations
protected override bool SocketReconnect(SocketSubscription subscription, TimeSpan disconnectedTime) protected override bool SocketReconnect(SocketSubscription subscription, TimeSpan disconnectedTime)
{ {
OnReconnect?.Invoke(); return OnReconnect.Invoke();
return true;
} }
} }
} }

View File

@ -76,8 +76,11 @@ namespace CryptoExchange.Net
socket.DataInterpreter = dataInterpreter; socket.DataInterpreter = dataInterpreter;
socket.OnClose += () => socket.OnClose += () =>
{ {
foreach (var sub in sockets) lock (sockets)
sub.ResetEvents(); {
foreach (var sub in sockets)
sub.ResetEvents();
}
SocketOnClose(socket); SocketOnClose(socket);
}; };
@ -93,7 +96,8 @@ namespace CryptoExchange.Net
protected virtual SocketSubscription GetBackgroundSocket(bool authenticated = false) protected virtual SocketSubscription GetBackgroundSocket(bool authenticated = false)
{ {
return sockets.SingleOrDefault(s => s.Type == (authenticated ? SocketType.BackgroundAuthenticated : SocketType.Background)); lock (sockets)
return sockets.SingleOrDefault(s => s.Type == (authenticated ? SocketType.BackgroundAuthenticated : SocketType.Background));
} }
protected virtual void SocketOpened(IWebsocket socket) { } protected virtual void SocketOpened(IWebsocket socket) { }
@ -197,7 +201,7 @@ namespace CryptoExchange.Net
socket.Dispose(); socket.Dispose();
lock (sockets) lock (sockets)
{ {
var subscription = sockets.SingleOrDefault(s => s.Socket == socket); var subscription = sockets.SingleOrDefault(s => s.Socket.Id == socket.Id);
if(subscription != null) if(subscription != null)
sockets.Remove(subscription); sockets.Remove(subscription);
} }
@ -248,8 +252,12 @@ namespace CryptoExchange.Net
await Task.Run(() => await Task.Run(() =>
{ {
var tasks = new List<Task>(); var tasks = new List<Task>();
foreach (var sub in new List<SocketSubscription>(sockets)) lock (sockets)
tasks.Add(sub.Close()); {
foreach (var sub in new List<SocketSubscription>(sockets))
tasks.Add(sub.Close());
}
Task.WaitAll(tasks.ToArray()); Task.WaitAll(tasks.ToArray());
}); });
} }
@ -257,8 +265,7 @@ namespace CryptoExchange.Net
public override void Dispose() public override void Dispose()
{ {
log.Write(LogVerbosity.Debug, "Disposing socket client, closing all subscriptions"); log.Write(LogVerbosity.Debug, "Disposing socket client, closing all subscriptions");
lock (sockets) UnsubscribeAll().Wait();
UnsubscribeAll().Wait();
base.Dispose(); base.Dispose();
} }