Repository: dotnet/aspnetcore
File: src/SignalR/clients/csharp/Client/test/UnitTests/HubConnectionTests.Protocol.cs (C#)

Code before:
            }
        }

        [Fact]
        public async Task HandlerRegisteredWithOnIsFiredWhenInvocationReceived()
        {
            var connection = new TestConnection();
            var hubConnection = CreateHubConnection(connection);
            var handlerCalled = new TaskCompletionSource<object[]>();
            try
            {
                await hubConnection.StartAsync().DefaultTimeout();

                hubConnection.On<int, string, float>("Foo", (r1, r2, r3) => handlerCalled.TrySetResult(new object[] { r1, r2, r3 }));

                var args = new object[] { 1, "Foo", 2.0f };
                await connection.ReceiveJsonMessage(new { invocationId = "1", type = 1, target = "Foo", arguments = args }).DefaultTimeout();

                Assert.Equal(args, await handlerCalled.Task.DefaultTimeout());
            }
            finally
            {
                await hubConnection.DisposeAsync().DefaultTimeout();
                await connection.DisposeAsync().DefaultTimeout();
            }
        }

        [Fact]
        public async Task HandlerIsRemovedProperlyWithOff()
        {
            var connection = new TestConnection();
            var hubConnection = CreateHubConnection(connection);
            var handlerCalled = new TaskCompletionSource<int>();
            try
            {
                await hubConnection.StartAsync().DefaultTimeout();

                hubConnection.On<int>("Foo", (val) =>
                {
                    handlerCalled.TrySetResult(val);
                });

                hubConnection.Remove("Foo");
                await connection.ReceiveJsonMessage(new { invocationId = "1", type = 1, target = "Foo", arguments = 1 }).DefaultTimeout();
                var handlerTask = handlerCalled.Task;

                // We expect the handler task to timeout since the handler has been removed with the call to Remove("Foo")
                var ex = Assert.ThrowsAsync<TimeoutException>(async () => await handlerTask.DefaultTimeout(2000));

                // Ensure that the task from the WhenAny is not the handler task
                Assert.False(handlerCalled.Task.IsCompleted);

Review comment (suggestion):
The array literal syntax ["1"] may not be valid in C#. It should be replaced with a valid array literal, such as new[] { "1" }.
```suggestion
                    Assert.Equal(new[] { "1" }, notifications.ToArray());
```

Code after:
            }
        }

        [Fact]
        public async Task StreamBindingErrorLogsError()
        {
            using (StartVerifiableLog(expectedErrorsFilter: w => w.EventId.Name == "StreamBindingFailure"))
            {
                var connection = new TestConnection();
                var hubConnection = CreateHubConnection(connection, loggerFactory: LoggerFactory);
                try
                {
                    await hubConnection.StartAsync().DefaultTimeout();

                    var channel = await hubConnection.StreamAsChannelAsync<string>("Foo").DefaultTimeout();

                    // Expects string, send int
                    await connection.ReceiveJsonMessage(new { invocationId = "1", type = 2, item = 1 }).DefaultTimeout();
                    // Check that connection is still active, i.e. we ignore stream failures and keep things working.
                    await connection.ReceiveJsonMessage(new { invocationId = "1", type = 2, item = "1" }).DefaultTimeout();
                    await connection.ReceiveJsonMessage(new { invocationId = "1", type = 3 }).DefaultTimeout();

                    var notifications = await channel.ReadAndCollectAllAsync().DefaultTimeout();

                    Assert.Contains(TestSink.Writes, w => w.EventId.Name == "StreamBindingFailure");
                    Assert.Equal(["1"], notifications.ToArray());
                }
                finally
                {
                    await hubConnection.DisposeAsync().DefaultTimeout();
                    await connection.DisposeAsync().DefaultTimeout();
                }
            }
        }

        [Fact]
        public async Task HandlerRegisteredWithOnIsFiredWhenInvocationReceived()
        {
            var connection = new TestConnection();
            var hubConnection = CreateHubConnection(connection);
            var handlerCalled = new TaskCompletionSource<object[]>();
            try
            {
                await hubConnection.StartAsync().DefaultTimeout();

                hubConnection.On<int, string, float>("Foo", (r1, r2, r3) => handlerCalled.TrySetResult(new object[] { r1, r2, r3 }));

                var args = new object[] { 1, "Foo", 2.0f };
                await connection.ReceiveJsonMessage(new { invocationId = "1", type = 1, target = "Foo", arguments = args }).DefaultTimeout();

                Assert.Equal(args, await handlerCalled.Task.DefaultTimeout());