implemented cancellation support for MathService.Fib

pull/2643/head
Jan Tattermusch 10 years ago
parent 0846b68eb7
commit f7cfc8a7b8
  1. 33
      src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs
  2. 9
      src/csharp/Grpc.Examples/MathServiceImpl.cs

@ -132,6 +132,39 @@ namespace math.Tests
}).Wait();
}
[Test]
public void FibWithCancel()
{
Task.Run(async () =>
{
var cts = new CancellationTokenSource();
using (var call = client.Fib(new FibArgs.Builder { Limit = 0 }.Build(),
cancellationToken: cts.Token))
{
List<long> responses = new List<long>();
try
{
while (await call.ResponseStream.MoveNext())
{
if (responses.Count == 0)
{
cts.CancelAfter(500); // make sure we cancel soon
}
responses.Add(call.ResponseStream.Current.Num_);
}
Assert.Fail();
}
catch (RpcException e)
{
Assert.IsTrue(responses.Count > 0);
Assert.AreEqual(StatusCode.Cancelled, e.Status.StatusCode);
}
}
}).Wait();
}
// TODO: test Fib with limit=0 and cancellation
[Test]
public void Sum()

@ -54,8 +54,13 @@ namespace math
{
if (request.Limit <= 0)
{
// TODO(jtattermusch): support cancellation
throw new NotImplementedException("Not implemented yet");
// keep streaming the sequence until cancelled.
IEnumerator<Num> fibEnumerator = FibInternal(long.MaxValue).GetEnumerator();
while (!context.CancellationToken.IsCancellationRequested && fibEnumerator.MoveNext())
{
await responseStream.WriteAsync(fibEnumerator.Current);
await Task.Delay(100);
}
}
if (request.Limit > 0)

Loading…
Cancel
Save