Skip to content

Commit

Permalink
Register for cancellation callback to detect timeout on replicator co…
Browse files Browse the repository at this point in the history
…nnect (#1020) (PR #1033)

Inability to connect is not reported by TcpClient.  There needs to be logic to handle the case where the other side does not respond.
  • Loading branch information
Sandychuang8 authored and borrrden committed Jun 6, 2018
1 parent 78ba460 commit d3eb920
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
40 changes: 29 additions & 11 deletions src/Couchbase.Lite.Shared/Sync/WebSocketWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,21 +197,39 @@ public void Start()
}
// STEP 2: Open the socket connection to the remote host
var cts = new CancellationTokenSource();
cts.CancelAfter(ConnectTimeout);
try {
_client.ConnectAsync(_logic.UrlRequest.Host, _logic.UrlRequest.Port).ContinueWith(t =>
{
if (!NetworkTaskSuccessful(t)) {
return;
}
var cts = new CancellationTokenSource(ConnectTimeout);
var tok = cts.Token;
try
{
_client.ConnectAsync(_logic.UrlRequest.Host, _logic.UrlRequest.Port)
.ContinueWith(t =>
{
if (!NetworkTaskSuccessful(t)) {
return;
}
_queue.DispatchAsync(StartInternal);
_queue.DispatchAsync(StartInternal);
}, cts.Token);
} catch (Exception e) {
}, tok);
}
catch (Exception e)
{
// Yes, unfortunately exceptions can either be thrown here or in the task...
DidClose(e);
}
var cancelCallback = default(CancellationTokenRegistration);
cancelCallback = tok.Register(() =>
{
if (!_client.Connected) {
// TODO: Should this be transient?
DidClose(new OperationCanceledException());
_client.Dispose();
}
cancelCallback.Dispose();
cts.Dispose();
});
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/Couchbase.Lite.Tests.Shared/ReplicationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ public async Task TestReplicatorStopsWhenEndpointInvalid()
{
repl.Start();
var count = 0;
while (count++ <= 20 && repl.Status.Activity != ReplicatorActivityLevel.Stopped)
while (count++ <= 35 && repl.Status.Activity != ReplicatorActivityLevel.Stopped)
{
WriteLine($"Replication status still {repl.Status.Activity}, waiting for stopped...");
await Task.Delay(500);
}

count.Should().BeLessThan(20, "because otherwise the replicator never stopped");
count.Should().BeLessThan(35, "because otherwise the replicator never stopped");
}
}

Expand Down

0 comments on commit d3eb920

Please sign in to comment.