public async Task Connect_wrong_connectivity_info() { // arrange SetupWebSocket(new WebSocketClient(uri: "ws://foobar:8000")); InstantiateFayeClient(); // act try { await _fayeClient.Connect(); // assert Assert.Fail("Expected at least 1 exception to be thrown"); } catch (SocketException) { Assert.Pass("Got our socket exception"); } catch (FayeConnectionException e) { e.Message .Should().Contain("Timed out", "If we don't have a more graceful socket exception, then we should at least get a proper timeout exception"); } }
public async Task Connect_lost_connection_comes_back_before_publish() { // 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 json = JsonConvert.SerializeObject(messageToSend); var lostTcs = new TaskCompletionSource <bool>(); _connection.ConnectionLost += (sender, args) => lostTcs.SetResult(true); var backTcs = new TaskCompletionSource <bool>(); _connection.ConnectionReestablished += (sender, args) => backTcs.SetResult(true); // 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()); await backTcs.Task.WithTimeout(t => t, 20.Seconds()); await _connection.Publish(channel : channelName, message : json); // assert var task = tcs.Task; 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); }