blob: 7654ec84995b9448236d87207abb5ba459f31eae [file] [log] [blame]
Jan Tattermusch15111f52015-02-05 18:15:14 -08001using System;
2using NUnit.Framework;
3using Google.GRPC.Core;
4using System.Threading;
5using System.Threading.Tasks;
6using Google.GRPC.Core.Utils;
7using System.Collections.Generic;
8
9namespace math.Tests
10{
11 /// <summary>
12 /// Math client talks to local math server.
13 /// </summary>
14 public class MathClientServerTest
15 {
Jan Tattermusch07fadea2015-02-13 10:03:37 -080016 string host = "localhost";
Jan Tattermusch15111f52015-02-05 18:15:14 -080017 Server server;
18 Channel channel;
19 MathGrpc.IMathServiceClient client;
20
21 [TestFixtureSetUp]
22 public void Init()
23 {
Jan Tattermusch23821ce2015-02-13 10:46:02 -080024 GrpcEnvironment.Initialize();
25
Jan Tattermusch15111f52015-02-05 18:15:14 -080026 server = new Server();
27 server.AddServiceDefinition(MathGrpc.BindService(new MathServiceImpl()));
Jan Tattermusch07fadea2015-02-13 10:03:37 -080028 int port = server.AddPort(host + ":0");
Jan Tattermusch15111f52015-02-05 18:15:14 -080029 server.Start();
Jan Tattermusch07fadea2015-02-13 10:03:37 -080030 channel = new Channel(host + ":" + port);
Jan Tattermusch15111f52015-02-05 18:15:14 -080031 client = MathGrpc.NewStub(channel);
32 }
33
34 [Test]
35 public void Div1()
36 {
37 DivReply response = client.Div(new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build());
38 Assert.AreEqual(3, response.Quotient);
39 Assert.AreEqual(1, response.Remainder);
40 }
41
42 [Test]
43 public void Div2()
44 {
45 DivReply response = client.Div(new DivArgs.Builder { Dividend = 0, Divisor = 1 }.Build());
46 Assert.AreEqual(0, response.Quotient);
47 Assert.AreEqual(0, response.Remainder);
48 }
49
50 // TODO: test division by zero
51
52 [Test]
53 public void DivAsync()
54 {
55 DivReply response = client.DivAsync(new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build()).Result;
56 Assert.AreEqual(3, response.Quotient);
57 Assert.AreEqual(1, response.Remainder);
58 }
59
60 [Test]
61 public void Fib()
62 {
63 var recorder = new RecordingObserver<Num>();
64 client.Fib(new FibArgs.Builder { Limit = 6 }.Build(), recorder);
65
66 CollectionAssert.AreEqual(new List<long>{1, 1, 2, 3, 5, 8},
67 recorder.ToList().Result.ConvertAll((n) => n.Num_));
68 }
69
70 // TODO: test Fib with limit=0 and cancellation
71 [Test]
72 public void Sum()
73 {
74 var res = client.Sum();
75 foreach (var num in new long[] { 10, 20, 30 }) {
76 res.Inputs.OnNext(Num.CreateBuilder().SetNum_(num).Build());
77 }
78 res.Inputs.OnCompleted();
79
80 Assert.AreEqual(60, res.Task.Result.Num_);
81 }
82
83 [Test]
84 public void DivMany()
85 {
86 List<DivArgs> divArgsList = new List<DivArgs>{
87 new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build(),
88 new DivArgs.Builder { Dividend = 100, Divisor = 21 }.Build(),
89 new DivArgs.Builder { Dividend = 7, Divisor = 2 }.Build()
90 };
91
92 var recorder = new RecordingObserver<DivReply>();
93 var requestObserver = client.DivMany(recorder);
94
95 foreach (var arg in divArgsList)
96 {
97 requestObserver.OnNext(arg);
98 }
99 requestObserver.OnCompleted();
100
101 var result = recorder.ToList().Result;
102
103 CollectionAssert.AreEqual(new long[] {3, 4, 3}, result.ConvertAll((divReply) => divReply.Quotient));
104 CollectionAssert.AreEqual(new long[] {1, 16, 1}, result.ConvertAll((divReply) => divReply.Remainder));
105 }
106
107 [TestFixtureTearDown]
108 public void Cleanup()
109 {
110 channel.Dispose();
111
112 server.ShutdownAsync().Wait();
113 GrpcEnvironment.Shutdown();
114 }
115 }
116}
117