public async Task StartCreatesAndOpensWebSocket() { var fakeWebSocketTransport = new FakeWebSocketTransport(); fakeWebSocketTransport.Setup("OpenWebSocket", () => { var tcs = new TaskCompletionSource <object>(); tcs.TrySetResult(null); return(tcs.Task); }); var fakeConnection = new FakeConnection { TransportConnectTimeout = new TimeSpan(0, 0, 0, 0, 100), TotalTransportConnectTimeout = new TimeSpan(0, 0, 0, 0, 100), Url = "http://fake.url", Protocol = new Version(1, 42), ConnectionToken = "MyConnToken", MessageId = "MsgId" }; // connect timeout unblocks this call hence the expected exception await Assert.ThrowsAsync <TimeoutException>( async() => await fakeWebSocketTransport.Start(fakeConnection, "test", CancellationToken.None)); Assert.Equal(1, fakeConnection.GetInvocations("Trace").Count()); Assert.Equal(1, fakeConnection.GetInvocations("PrepareRequest").Count()); var openWebSocketInvocations = fakeWebSocketTransport.GetInvocations("OpenWebSocket").ToArray(); Assert.Equal(1, openWebSocketInvocations.Length); Assert.StartsWith( "ws://fake.urlconnect/?clientProtocol=1.42&transport=webSockets&connectionData=test&connectionToken=MyConnToken&messageId=MsgId&noCache=", ((Uri)openWebSocketInvocations[0][1]).AbsoluteUri); }
public async Task StartCreatesAndOpensWebSocket() { var fakeWebSocketTransport = new FakeWebSocketTransport(); fakeWebSocketTransport.Setup("OpenWebSocket", () => { var tcs = new TaskCompletionSource<object>(); tcs.TrySetResult(null); return tcs.Task; }); var fakeConnection = new FakeConnection { TransportConnectTimeout = new TimeSpan(0, 0, 0, 0, 100), TotalTransportConnectTimeout = new TimeSpan(0, 0, 0, 0, 100), Url = "http://fake.url", Protocol = new Version(1, 42), ConnectionToken = "MyConnToken", MessageId = "MsgId" }; // connect timeout unblocks this call hence the expected exception await Assert.ThrowsAsync<TimeoutException>( async () => await fakeWebSocketTransport.Start(fakeConnection, "test", CancellationToken.None)); Assert.Equal(1, fakeConnection.GetInvocations("Trace").Count()); Assert.Equal(1, fakeConnection.GetInvocations("PrepareRequest").Count()); var openWebSocketInvocations = fakeWebSocketTransport.GetInvocations("OpenWebSocket").ToArray(); Assert.Equal(1, openWebSocketInvocations.Length); Assert.StartsWith( "ws://fake.urlconnect/?clientProtocol=1.42&transport=webSockets&connectionData=test&connectionToken=MyConnToken&messageId=MsgId", ((Uri)openWebSocketInvocations[0][1]).AbsoluteUri); }
public async Task ReconnectStopsReconnectingIfStartingWebSocketCancelled() { var fakeConnection = new FakeConnection { LastActiveAt = DateTime.UtcNow, ReconnectWindow = new TimeSpan(0, 0, 15), Url = "http://fakeserver/" }; fakeConnection.Setup("ChangeState", () => { fakeConnection.State = ConnectionState.Reconnecting; return(true); }); var fakeWebSocketTransport = new FakeWebSocketTransport { ReconnectDelay = new TimeSpan(0, 0, 0, 1) }; fakeWebSocketTransport.Setup <Task>("OpenWebSocket", () => { var tcs = new TaskCompletionSource <object>(); tcs.SetCanceled(); return(tcs.Task); }); await fakeWebSocketTransport.Reconnect(fakeConnection, null); Assert.Equal(1, fakeWebSocketTransport.GetInvocations("OpenWebSocket").Count()); Assert.Equal(0, fakeConnection.GetInvocations("Stop").Count()); Assert.Equal(0, fakeConnection.GetInvocations("OnError").Count()); }
public void MessageReceivedReadsAndProcessesMessages() { var fakeDataReader = new FakeDataReader { UnicodeEncoding = (UnicodeEncoding)(-1), UnconsumedBufferLength = 42 }; fakeDataReader.Setup("ReadString", () => "MessageBody"); var fakeWebSocketResponse = new FakeWebSocketResponse(); fakeWebSocketResponse.Setup("GetDataReader", () => fakeDataReader); var transport = new WebSocketTransport(); transport.Start(new FakeConnection(), string.Empty, CancellationToken.None); var fakeConnection = new FakeConnection(); transport.MessageReceived(fakeWebSocketResponse, fakeConnection); Assert.Equal(UnicodeEncoding.Utf8, fakeDataReader.UnicodeEncoding); fakeDataReader.Verify("ReadString", new List <object[]> { new object[] { 42u } }); // invoked by ProcessResponse Assert.Equal(1, fakeConnection.GetInvocations("MarkLastMessage").Count()); Assert.Equal(1, fakeConnection.GetInvocations("Trace").Count()); }
public async Task ReconnectStopsConnectionAndDoesNotStartNewWebSocketIfReconnectWindowExceeded() { var fakeConnection = new FakeConnection { LastActiveAt = DateTime.UtcNow.AddSeconds(-10), ReconnectWindow = new TimeSpan(0, 0, 5), Url = "http://fakeserver/" }; fakeConnection.Setup("ChangeState", () => { fakeConnection.State = ConnectionState.Reconnecting; return(true); }); var fakeWebSocketTransport = new FakeWebSocketTransport(); await fakeWebSocketTransport.Reconnect(fakeConnection, null); Assert.Equal(0, fakeWebSocketTransport.GetInvocations("OpenWebSocket").Count()); var stopInvocations = fakeConnection.GetInvocations("Stop").ToArray(); Assert.Equal(1, stopInvocations.Length); Assert.IsType(typeof(TimeoutException), stopInvocations[0][0]); }
public async Task ReconnectStartsNewWebSocket() { var fakeConnection = new FakeConnection { LastActiveAt = DateTime.Now.AddDays(1), ReconnectWindow = new TimeSpan(0, 0, 0), Url = "http://fakeserver/" }; fakeConnection.Setup("ChangeState", () => { fakeConnection.State = ConnectionState.Reconnecting; return(true); }); var fakeWebSocketTransport = new FakeWebSocketTransport(); fakeWebSocketTransport.Setup <Task>("OpenWebSocket", () => { var tcs = new TaskCompletionSource <object>(); tcs.TrySetResult(null); return(tcs.Task); }); await fakeWebSocketTransport.Reconnect(fakeConnection, "abc"); var openWebSocketInvocations = fakeWebSocketTransport.GetInvocations("OpenWebSocket").ToArray(); Assert.Equal(1, openWebSocketInvocations.Length); Assert.StartsWith("ws://fakeserver/reconnect?", ((Uri)openWebSocketInvocations[0][1]).AbsoluteUri); Assert.Contains("&connectionData=abc&", ((Uri)openWebSocketInvocations[0][1]).AbsoluteUri); Assert.Equal(1, fakeConnection.GetInvocations("OnReconnected").Count()); }
public async Task ReconnectRetriesReconnectingIfStartingWebSocketThrows() { var fakeConnection = new FakeConnection { LastActiveAt = DateTime.UtcNow, ReconnectWindow = new TimeSpan(0, 0, 15), Url = "http://fakeserver/" }; fakeConnection.Setup("ChangeState", () => { fakeConnection.State = ConnectionState.Reconnecting; return(true); }); var fakeWebSocketTransport = new FakeWebSocketTransport { ReconnectDelay = new TimeSpan(0, 0, 0, 1) }; var openWebSocketInvoked = false; var exception = new Exception(); fakeWebSocketTransport.Setup <Task>("OpenWebSocket", () => { if (!openWebSocketInvoked) { openWebSocketInvoked = true; throw exception; } var tcs = new TaskCompletionSource <object>(); tcs.TrySetResult(null); return(tcs.Task); }); await fakeWebSocketTransport.Reconnect(fakeConnection, null); Assert.Equal(2, fakeWebSocketTransport.GetInvocations("OpenWebSocket").Count()); var onErrorInvocations = fakeConnection.GetInvocations("OnError").ToArray(); Assert.Equal(1, onErrorInvocations.Length); Assert.Same(exception, onErrorInvocations[0][0]); Assert.Equal(1, fakeConnection.GetInvocations("OnReconnected").Count()); }
public async Task ReconnectDoesNotStartNewWebSocketIfDisconnectTokenTripped() { var fakeConnection = new FakeConnection { LastActiveAt = DateTime.UtcNow, ReconnectWindow = new TimeSpan(0, 0, 15), Url = "http://fakeserver/", State = ConnectionState.Reconnecting }; var fakeWebSocketTransport = new FakeWebSocketTransport(); // this is called just to initialize the disconnect token var _ = fakeWebSocketTransport.Start(new FakeConnection(), string.Empty, new CancellationToken(true)); await fakeWebSocketTransport.Reconnect(fakeConnection, null); Assert.Equal(0, fakeWebSocketTransport.GetInvocations("OpenWebSocket").Count()); Assert.Equal(0, fakeConnection.GetInvocations("Stop").Count()); }
public async Task ReconnectDoesNotStartNewWebSocketIfClientWasNotInConnectState() { var fakeConnection = new FakeConnection { LastActiveAt = DateTime.UtcNow, ReconnectWindow = new TimeSpan(0, 0, 15), Url = "http://fakeserver/" }; fakeConnection.Setup("ChangeState", () => { fakeConnection.State = ConnectionState.Disconnected; return(false); }); var fakeWebSocketTransport = new FakeWebSocketTransport(); await fakeWebSocketTransport.Reconnect(fakeConnection, null); Assert.Equal(0, fakeWebSocketTransport.GetInvocations("OpenWebSocket").Count()); Assert.Equal(0, fakeConnection.GetInvocations("Stop").Count()); }
public async Task ReconnectRetriesReconnectingIfStartingWebSocketThrows() { var fakeConnection = new FakeConnection { LastActiveAt = DateTime.UtcNow, ReconnectWindow = new TimeSpan(0, 0, 15), Url = "http://fakeserver/" }; fakeConnection.Setup("ChangeState", () => { fakeConnection.State = ConnectionState.Reconnecting; return true; }); var fakeWebSocketTransport = new FakeWebSocketTransport { ReconnectDelay = new TimeSpan(0, 0, 0, 1) }; var openWebSocketInvoked = false; var exception = new Exception(); fakeWebSocketTransport.Setup<Task>("OpenWebSocket", () => { if (!openWebSocketInvoked) { openWebSocketInvoked = true; throw exception; } var tcs = new TaskCompletionSource<object>(); tcs.TrySetResult(null); return tcs.Task; }); await fakeWebSocketTransport.Reconnect(fakeConnection, null); Assert.Equal(2, fakeWebSocketTransport.GetInvocations("OpenWebSocket").Count()); var onErrorInvocations = fakeConnection.GetInvocations("OnError").ToArray(); Assert.Equal(1, onErrorInvocations.Length); Assert.Same(exception, onErrorInvocations[0][0]); Assert.Equal(1, fakeConnection.GetInvocations("OnReconnected").Count()); }
public async Task ReconnectStopsReconnectingIfStartingWebSocketCancelled() { var fakeConnection = new FakeConnection { LastActiveAt = DateTime.UtcNow, ReconnectWindow = new TimeSpan(0, 0, 15), Url = "http://fakeserver/" }; fakeConnection.Setup("ChangeState", () => { fakeConnection.State = ConnectionState.Reconnecting; return true; }); var fakeWebSocketTransport = new FakeWebSocketTransport { ReconnectDelay = new TimeSpan(0, 0, 0, 1) }; fakeWebSocketTransport.Setup<Task>("OpenWebSocket", () => { var tcs = new TaskCompletionSource<object>(); tcs.SetCanceled(); return tcs.Task; }); await fakeWebSocketTransport.Reconnect(fakeConnection, null); Assert.Equal(1, fakeWebSocketTransport.GetInvocations("OpenWebSocket").Count()); Assert.Equal(0, fakeConnection.GetInvocations("Stop").Count()); Assert.Equal(0, fakeConnection.GetInvocations("OnError").Count()); }
public async Task ReconnectDoesNotStartNewWebSocketIfClientWasNotInConnectState() { var fakeConnection = new FakeConnection { LastActiveAt = DateTime.UtcNow, ReconnectWindow = new TimeSpan(0, 0, 15), Url = "http://fakeserver/" }; fakeConnection.Setup("ChangeState", () => { fakeConnection.State = ConnectionState.Disconnected; return false; }); var fakeWebSocketTransport = new FakeWebSocketTransport(); await fakeWebSocketTransport.Reconnect(fakeConnection, null); Assert.Equal(0, fakeWebSocketTransport.GetInvocations("OpenWebSocket").Count()); Assert.Equal(0, fakeConnection.GetInvocations("Stop").Count()); }
public async Task ReconnectStopsConnectionAndDoesNotStartNewWebSocketIfReconnectWindowExceeded() { var fakeConnection = new FakeConnection { LastActiveAt = DateTime.UtcNow.AddSeconds(-10), ReconnectWindow = new TimeSpan(0, 0, 5), Url = "http://fakeserver/" }; fakeConnection.Setup("ChangeState", () => { fakeConnection.State = ConnectionState.Reconnecting; return true; }); var fakeWebSocketTransport = new FakeWebSocketTransport(); await fakeWebSocketTransport.Reconnect(fakeConnection, null); Assert.Equal(0, fakeWebSocketTransport.GetInvocations("OpenWebSocket").Count()); var stopInvocations = fakeConnection.GetInvocations("Stop").ToArray(); Assert.Equal(1, stopInvocations.Length); Assert.IsType(typeof(TimeoutException), stopInvocations[0][0]); }
public async Task ReconnectStartsNewWebSocket() { var fakeConnection = new FakeConnection { LastActiveAt = DateTime.Now.AddDays(1), ReconnectWindow = new TimeSpan(0, 0, 0), Url = "http://fakeserver/" }; fakeConnection.Setup("ChangeState", () => { fakeConnection.State = ConnectionState.Reconnecting; return true; }); var fakeWebSocketTransport = new FakeWebSocketTransport(); fakeWebSocketTransport.Setup<Task>("OpenWebSocket", () => { var tcs = new TaskCompletionSource<object>(); tcs.TrySetResult(null); return tcs.Task; }); await fakeWebSocketTransport.Reconnect(fakeConnection, "abc"); var openWebSocketInvocations = fakeWebSocketTransport.GetInvocations("OpenWebSocket").ToArray(); Assert.Equal(1, openWebSocketInvocations.Length); Assert.StartsWith("ws://fakeserver/reconnect?", ((Uri)openWebSocketInvocations[0][1]).AbsoluteUri); Assert.Contains("&connectionData=abc", ((Uri)openWebSocketInvocations[0][1]).AbsoluteUri); Assert.Equal(1, fakeConnection.GetInvocations("OnReconnected").Count()); }
public void MessageReceivedReadsAndProcessesMessages() { var fakeDataReader = new FakeDataReader { UnicodeEncoding = (UnicodeEncoding)(-1), UnconsumedBufferLength = 42 }; fakeDataReader.Setup("ReadString", () => "MessageBody"); var fakeWebSocketResponse = new FakeWebSocketResponse(); fakeWebSocketResponse.Setup("GetDataReader", () => fakeDataReader); var transport = new WebSocketTransport(); transport.Start(new FakeConnection(), string.Empty, CancellationToken.None); var fakeConnection = new FakeConnection(); transport.MessageReceived(fakeWebSocketResponse, fakeConnection); Assert.Equal(UnicodeEncoding.Utf8, fakeDataReader.UnicodeEncoding); fakeDataReader.Verify("ReadString", new List<object[]> {new object[] { 42u}}); // invoked by ProcessResponse Assert.Equal(1, fakeConnection.GetInvocations("MarkLastMessage").Count()); Assert.Equal(1, fakeConnection.GetInvocations("Trace").Count()); }