public async Task Subscribe_twice() { // arrange _fayeServerProcess.StartThinServer(); var socket = new WebSocketClient(uri: TEST_SERVER_URL); SetupWebSocket(socket); InstantiateFayeClient(); _connection = await _fayeClient.Connect(); var secondClient = GetFayeClient(new WebSocketClient(uri: TEST_SERVER_URL)); _connection2 = await secondClient.Connect(); var tcs = new TaskCompletionSource <string>(); var tcs2 = new TaskCompletionSource <string>(); const string channelName = "/somechannel"; var messageToSend = new TestMsg { Stuff = "the message" }; var json = JsonConvert.SerializeObject(messageToSend); var task = tcs.Task; // act await _connection.Subscribe(channelName, tcs.SetResult); await _connection.Subscribe(channelName, tcs2.SetResult); await _connection2.Publish(channel : channelName, message : json); // assert var result = await task.Timeout(5.Seconds()); if (result == Result.Timeout) { Assert.Fail("Timed out waiting for pub/sub to work"); } var jsonReceived = task.Result; var objectReceived = JsonConvert.DeserializeObject <TestMsg>(jsonReceived); objectReceived .ShouldBeEquivalentTo(messageToSend); task = tcs2.Task; result = await task.Timeout(5.Seconds()); if (result == Result.Timeout) { Assert.Fail("Timed out waiting for pub/sub to work"); } jsonReceived = task.Result; objectReceived = JsonConvert.DeserializeObject <TestMsg>(jsonReceived); objectReceived .ShouldBeEquivalentTo(messageToSend); }
public async Task Connect_lost_connection_retry_not_allowed() { // arrange TriggerNoRetriesAllowed(); // port 8133 const int inputPort = THIN_SERVER_PORT + 1; _fayeServerProcess.StartThinServer(); _socatInterceptor = StartWritableSocket(hostname: "localhost", inputPort: inputPort); const string urlThroughSocat = "ws://localhost:8133/bayeux"; var socket = new WebSocketClient(uri: urlThroughSocat); SetupWebSocket(socket); InstantiateFayeClient(); _connection = await _fayeClient.Connect(); var tcs = new TaskCompletionSource <string>(); const string channelName = "/somechannel"; var lostTcs = new TaskCompletionSource <bool>(); _connection.ConnectionLost += (sender, args) => lostTcs.SetResult(true); var backTcs = new TaskCompletionSource <bool>(); _connection.ConnectionReestablished += (sender, args) => backTcs.SetResult(true); var tryToRestablishTask = backTcs.Task; // act await _connection.Subscribe(channelName, tcs.SetResult); // ReSharper disable once CSharpWarnings::CS4014 Task.Factory.StartNew(() => { _socatInterceptor.Kill(); _socatInterceptor = StartWritableSocket(hostname: "localhost", inputPort: inputPort); }); await lostTcs.Task.WithTimeout(t => t, 20.Seconds()); // assert tryToRestablishTask .Status .Should() .Be(TaskStatus.WaitingForActivation, "should not try and re-establish since we disabled retries"); await Task.Delay(10.Seconds()); tryToRestablishTask .Status .Should() .Be(TaskStatus.WaitingForActivation, "Still should not try and re-establish since we disabled retries"); }
public async Task Unsubscribe() { // arrange _fayeServerProcess.StartThinServer(); var socket = new WebSocketClient(uri: TEST_SERVER_URL); SetupWebSocket(socket); InstantiateFayeClient(); _connection = await _fayeClient.Connect(); var secondClient = GetFayeClient(new WebSocketClient(uri: TEST_SERVER_URL)); _connection2 = await secondClient.Connect(); const string channelName = "/somechannel"; var tcs = new TaskCompletionSource <object>(); var messageToSendObj = new TestMsg { Stuff = "the message" }; var messageToSend = JsonConvert.SerializeObject(messageToSendObj); await _connection.Subscribe(channelName, tcs.SetResult); // should never hit this // act await _connection.Unsubscribe(channelName); // assert await _connection2.Publish(channelName, messageToSend); await Task.Delay(100.Milliseconds()); tcs.Task .Status .Should() .Be(TaskStatus.WaitingForActivation, "We should never fire our event since we unsubscribe before the 1st message"); }
public async Task Connect_lost_connection_comes_back_after_attempt_to_publish() { // arrange// port 8133 const int inputPort = THIN_SERVER_PORT + 1; _fayeServerProcess.StartThinServer(); _socatInterceptor = StartWritableSocket(hostname: "localhost", inputPort: inputPort); const string urlThroughSocat = "ws://localhost:8133/bayeux"; var socket = new WebSocketClient(uri: urlThroughSocat); SetupWebSocket(socket); InstantiateFayeClient(); _connection = await _fayeClient.Connect(); var tcs = new TaskCompletionSource <string>(); const string channelName = "/somechannel"; var messageToSend = new TestMsg { Stuff = "the message" }; var lostTcs = new TaskCompletionSource <bool>(); _connection.ConnectionLost += (sender, args) => lostTcs.SetResult(true); var json = JsonConvert.SerializeObject(messageToSend); // act await _connection.Subscribe(channelName, tcs.SetResult); // ReSharper disable once CSharpWarnings::CS4014 Task.Factory.StartNew(() => { Logger.Info("Killing socat"); _socatInterceptor.Kill(); Logger.Info("Sleeping"); Thread.Sleep(5.Seconds()); Logger.Info("Restarting socat"); _socatInterceptor = StartWritableSocket(hostname: "localhost", inputPort: inputPort); }); Logger.Info("Waiting for websocket to acknowledge disconnect"); await lostTcs.Task.WithTimeout(t => t, 20.Seconds()); Logger.Info("Disconnect acknowledged, publishing"); await _connection.Publish(channel : channelName, message : json); // assert var task = tcs.Task; var result = await task.Timeout(20.Seconds()); if (result == Result.Timeout) { Assert.Fail("Timed out waiting for pub/sub to work"); } var jsonReceived = task.Result; var objectReceived = JsonConvert.DeserializeObject <TestMsg>(jsonReceived); objectReceived .ShouldBeEquivalentTo(messageToSend); }