Exemplo n.º 1
0
        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());
        }
Exemplo n.º 2
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());
        }
Exemplo n.º 3
0
        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 void SpecificCommand()
        {
            var connection = new FakeConnection();
            var cmdToReturn = new FakeCommand(new ScalarCommandResult("SELECT count(*) FROM users", null){Result = 201});
            connection.Setup(cmdToReturn);

            var cmd = connection.CreateCommand();
            cmd.CommandText = "SELECT count(*) FROM users";
            var result = cmd.ExecuteScalar();

           Assert.Equal(201, result);
        }
Exemplo n.º 5
0
        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());
        }
Exemplo n.º 6
0
        public void SpecificCommand()
        {
            var connection  = new FakeConnection();
            var cmdToReturn = new FakeCommand(new ScalarCommandResult("SELECT count(*) FROM users", null)
            {
                Result = 201
            });

            connection.Setup(cmdToReturn);

            var cmd = connection.CreateCommand();

            cmd.CommandText = "SELECT count(*) FROM users";
            var result = cmd.ExecuteScalar();

            Assert.Equal(201, result);
        }
        public void AutomaticResult()
        {
            var table = new FakeTable(new[]
                {
                    new object[] {1, "jonas"},
                    new object[] {2, "arne"},
                });
            var connection = new FakeConnection();
            connection.Setup(new FakeCommand(table));

            var cmd = connection.CreateCommand();
            cmd.CommandText = "SELECT Id, UserName FROM users";
            var result = cmd.ExecuteReader();
            
            Assert.True(result.Read());
            Assert.Equal(1, (int)result[0]);
            Assert.Equal("jonas", result[1]);
        }
Exemplo n.º 8
0
        public void AutomaticResult()
        {
            var table = new FakeTable(new[]
            {
                new object[] { 1, "jonas" },
                new object[] { 2, "arne" },
            });
            var connection = new FakeConnection();

            connection.Setup(new FakeCommand(table));

            var cmd = connection.CreateCommand();

            cmd.CommandText = "SELECT Id, UserName FROM users";
            var result = cmd.ExecuteReader();

            Assert.True(result.Read());
            Assert.Equal(1, (int)result[0]);
            Assert.Equal("jonas", result[1]);
        }
Exemplo n.º 9
0
        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 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 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 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());
        }