|
|
|
@ -108,123 +108,105 @@ namespace math.Tests |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[Test] |
|
|
|
|
public void DivAsync() |
|
|
|
|
public async Task DivAsync() |
|
|
|
|
{ |
|
|
|
|
Task.Run(async () => |
|
|
|
|
{ |
|
|
|
|
DivReply response = await client.DivAsync(new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build()); |
|
|
|
|
Assert.AreEqual(3, response.Quotient); |
|
|
|
|
Assert.AreEqual(1, response.Remainder); |
|
|
|
|
}).Wait(); |
|
|
|
|
DivReply response = await client.DivAsync(new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build()); |
|
|
|
|
Assert.AreEqual(3, response.Quotient); |
|
|
|
|
Assert.AreEqual(1, response.Remainder); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[Test] |
|
|
|
|
public void Fib() |
|
|
|
|
public async Task Fib() |
|
|
|
|
{ |
|
|
|
|
Task.Run(async () => |
|
|
|
|
using (var call = client.Fib(new FibArgs.Builder { Limit = 6 }.Build())) |
|
|
|
|
{ |
|
|
|
|
using (var call = client.Fib(new FibArgs.Builder { Limit = 6 }.Build())) |
|
|
|
|
{ |
|
|
|
|
var responses = await call.ResponseStream.ToList(); |
|
|
|
|
CollectionAssert.AreEqual(new List<long> { 1, 1, 2, 3, 5, 8 }, |
|
|
|
|
responses.ConvertAll((n) => n.Num_)); |
|
|
|
|
} |
|
|
|
|
}).Wait(); |
|
|
|
|
var responses = await call.ResponseStream.ToList(); |
|
|
|
|
CollectionAssert.AreEqual(new List<long> { 1, 1, 2, 3, 5, 8 }, |
|
|
|
|
responses.ConvertAll((n) => n.Num_)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[Test] |
|
|
|
|
public void FibWithCancel() |
|
|
|
|
public async Task FibWithCancel() |
|
|
|
|
{ |
|
|
|
|
Task.Run(async () => |
|
|
|
|
var cts = new CancellationTokenSource(); |
|
|
|
|
|
|
|
|
|
using (var call = client.Fib(new FibArgs.Builder { Limit = 0 }.Build(), |
|
|
|
|
cancellationToken: cts.Token)) |
|
|
|
|
{ |
|
|
|
|
var cts = new CancellationTokenSource(); |
|
|
|
|
List<long> responses = new List<long>(); |
|
|
|
|
|
|
|
|
|
using (var call = client.Fib(new FibArgs.Builder { Limit = 0 }.Build(), |
|
|
|
|
cancellationToken: cts.Token)) |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
List<long> responses = new List<long>(); |
|
|
|
|
|
|
|
|
|
try |
|
|
|
|
while (await call.ResponseStream.MoveNext()) |
|
|
|
|
{ |
|
|
|
|
while (await call.ResponseStream.MoveNext()) |
|
|
|
|
if (responses.Count == 0) |
|
|
|
|
{ |
|
|
|
|
if (responses.Count == 0) |
|
|
|
|
{ |
|
|
|
|
cts.CancelAfter(500); // make sure we cancel soon |
|
|
|
|
} |
|
|
|
|
responses.Add(call.ResponseStream.Current.Num_); |
|
|
|
|
cts.CancelAfter(500); // make sure we cancel soon |
|
|
|
|
} |
|
|
|
|
Assert.Fail(); |
|
|
|
|
} |
|
|
|
|
catch (RpcException e) |
|
|
|
|
{ |
|
|
|
|
Assert.IsTrue(responses.Count > 0); |
|
|
|
|
Assert.AreEqual(StatusCode.Cancelled, e.Status.StatusCode); |
|
|
|
|
responses.Add(call.ResponseStream.Current.Num_); |
|
|
|
|
} |
|
|
|
|
Assert.Fail(); |
|
|
|
|
} |
|
|
|
|
catch (RpcException e) |
|
|
|
|
{ |
|
|
|
|
Assert.IsTrue(responses.Count > 0); |
|
|
|
|
Assert.AreEqual(StatusCode.Cancelled, e.Status.StatusCode); |
|
|
|
|
} |
|
|
|
|
}).Wait(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[Test] |
|
|
|
|
public void FibWithDeadline() |
|
|
|
|
public async Task FibWithDeadline() |
|
|
|
|
{ |
|
|
|
|
Task.Run(async () => |
|
|
|
|
using (var call = client.Fib(new FibArgs.Builder { Limit = 0 }.Build(), |
|
|
|
|
deadline: DateTime.UtcNow.AddMilliseconds(500))) |
|
|
|
|
{ |
|
|
|
|
using (var call = client.Fib(new FibArgs.Builder { Limit = 0 }.Build(), |
|
|
|
|
deadline: DateTime.UtcNow.AddMilliseconds(500))) |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
await call.ResponseStream.ToList(); |
|
|
|
|
Assert.Fail(); |
|
|
|
|
} |
|
|
|
|
catch (RpcException e) |
|
|
|
|
{ |
|
|
|
|
Assert.AreEqual(StatusCode.DeadlineExceeded, e.Status.StatusCode); |
|
|
|
|
} |
|
|
|
|
await call.ResponseStream.ToList(); |
|
|
|
|
Assert.Fail(); |
|
|
|
|
} |
|
|
|
|
catch (RpcException e) |
|
|
|
|
{ |
|
|
|
|
Assert.AreEqual(StatusCode.DeadlineExceeded, e.Status.StatusCode); |
|
|
|
|
} |
|
|
|
|
}).Wait(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// TODO: test Fib with limit=0 and cancellation |
|
|
|
|
[Test] |
|
|
|
|
public void Sum() |
|
|
|
|
public async Task Sum() |
|
|
|
|
{ |
|
|
|
|
Task.Run(async () => |
|
|
|
|
using (var call = client.Sum()) |
|
|
|
|
{ |
|
|
|
|
using (var call = client.Sum()) |
|
|
|
|
{ |
|
|
|
|
var numbers = new List<long> { 10, 20, 30 }.ConvertAll( |
|
|
|
|
n => Num.CreateBuilder().SetNum_(n).Build()); |
|
|
|
|
var numbers = new List<long> { 10, 20, 30 }.ConvertAll( |
|
|
|
|
n => Num.CreateBuilder().SetNum_(n).Build()); |
|
|
|
|
|
|
|
|
|
await call.RequestStream.WriteAll(numbers); |
|
|
|
|
var result = await call.ResponseAsync; |
|
|
|
|
Assert.AreEqual(60, result.Num_); |
|
|
|
|
} |
|
|
|
|
}).Wait(); |
|
|
|
|
await call.RequestStream.WriteAll(numbers); |
|
|
|
|
var result = await call.ResponseAsync; |
|
|
|
|
Assert.AreEqual(60, result.Num_); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[Test] |
|
|
|
|
public void DivMany() |
|
|
|
|
public async Task DivMany() |
|
|
|
|
{ |
|
|
|
|
Task.Run(async () => |
|
|
|
|
var divArgsList = new List<DivArgs> |
|
|
|
|
{ |
|
|
|
|
var divArgsList = new List<DivArgs> |
|
|
|
|
{ |
|
|
|
|
new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build(), |
|
|
|
|
new DivArgs.Builder { Dividend = 100, Divisor = 21 }.Build(), |
|
|
|
|
new DivArgs.Builder { Dividend = 7, Divisor = 2 }.Build() |
|
|
|
|
}; |
|
|
|
|
new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build(), |
|
|
|
|
new DivArgs.Builder { Dividend = 100, Divisor = 21 }.Build(), |
|
|
|
|
new DivArgs.Builder { Dividend = 7, Divisor = 2 }.Build() |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
using (var call = client.DivMany()) |
|
|
|
|
{ |
|
|
|
|
await call.RequestStream.WriteAll(divArgsList); |
|
|
|
|
var result = await call.ResponseStream.ToList(); |
|
|
|
|
using (var call = client.DivMany()) |
|
|
|
|
{ |
|
|
|
|
await call.RequestStream.WriteAll(divArgsList); |
|
|
|
|
var result = await call.ResponseStream.ToList(); |
|
|
|
|
|
|
|
|
|
CollectionAssert.AreEqual(new long[] { 3, 4, 3 }, result.ConvertAll((divReply) => divReply.Quotient)); |
|
|
|
|
CollectionAssert.AreEqual(new long[] { 1, 16, 1 }, result.ConvertAll((divReply) => divReply.Remainder)); |
|
|
|
|
} |
|
|
|
|
}).Wait(); |
|
|
|
|
CollectionAssert.AreEqual(new long[] { 3, 4, 3 }, result.ConvertAll((divReply) => divReply.Quotient)); |
|
|
|
|
CollectionAssert.AreEqual(new long[] { 1, 16, 1 }, result.ConvertAll((divReply) => divReply.Remainder)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|