blob: c86da65af47bd23d2048d9e2ad82d32b63d90465 [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
2
Jan Tattermuschaf77b3d2015-02-13 11:22:21 -08003// Copyright 2015, Google Inc.
Jan Tattermuscha7fff862015-02-13 11:08:08 -08004// All rights reserved.
Craig Tiller190d3602015-02-18 09:23:38 -08005//
Jan Tattermuscha7fff862015-02-13 11:08:08 -08006// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
Craig Tiller190d3602015-02-18 09:23:38 -08009//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080010// * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16// * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
Craig Tiller190d3602015-02-18 09:23:38 -080019//
Jan Tattermuscha7fff862015-02-13 11:08:08 -080020// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32#endregion
33
Jan Tattermusch15111f52015-02-05 18:15:14 -080034using System;
Jan Tattermusch30868622015-02-19 09:22:33 -080035using System.Collections.Generic;
Jan Tattermusch15111f52015-02-05 18:15:14 -080036using System.Threading;
37using System.Threading.Tasks;
Jan Tattermusch30868622015-02-19 09:22:33 -080038using Grpc.Core;
39using Grpc.Core.Utils;
40using NUnit.Framework;
Jan Tattermusch15111f52015-02-05 18:15:14 -080041
42namespace math.Tests
43{
44 /// <summary>
45 /// Math client talks to local math server.
46 /// </summary>
47 public class MathClientServerTest
48 {
Jan Tattermusch07fadea2015-02-13 10:03:37 -080049 string host = "localhost";
Jan Tattermusch15111f52015-02-05 18:15:14 -080050 Server server;
51 Channel channel;
52 MathGrpc.IMathServiceClient client;
53
54 [TestFixtureSetUp]
55 public void Init()
56 {
Jan Tattermusch23821ce2015-02-13 10:46:02 -080057 GrpcEnvironment.Initialize();
58
Jan Tattermusch15111f52015-02-05 18:15:14 -080059 server = new Server();
60 server.AddServiceDefinition(MathGrpc.BindService(new MathServiceImpl()));
Jan Tattermusch07fadea2015-02-13 10:03:37 -080061 int port = server.AddPort(host + ":0");
Jan Tattermusch15111f52015-02-05 18:15:14 -080062 server.Start();
Jan Tattermusch07fadea2015-02-13 10:03:37 -080063 channel = new Channel(host + ":" + port);
Jan Tattermusch15111f52015-02-05 18:15:14 -080064 client = MathGrpc.NewStub(channel);
65 }
66
Jan Tattermusch607307d2015-02-18 11:05:45 -080067 [TestFixtureTearDown]
68 public void Cleanup()
69 {
70 channel.Dispose();
71
72 server.ShutdownAsync().Wait();
73 GrpcEnvironment.Shutdown();
74 }
75
Jan Tattermusch15111f52015-02-05 18:15:14 -080076 [Test]
77 public void Div1()
78 {
79 DivReply response = client.Div(new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build());
80 Assert.AreEqual(3, response.Quotient);
81 Assert.AreEqual(1, response.Remainder);
82 }
83
84 [Test]
85 public void Div2()
86 {
87 DivReply response = client.Div(new DivArgs.Builder { Dividend = 0, Divisor = 1 }.Build());
88 Assert.AreEqual(0, response.Quotient);
89 Assert.AreEqual(0, response.Remainder);
90 }
91
92 // TODO: test division by zero
93
94 [Test]
95 public void DivAsync()
96 {
97 DivReply response = client.DivAsync(new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build()).Result;
98 Assert.AreEqual(3, response.Quotient);
99 Assert.AreEqual(1, response.Remainder);
100 }
101
102 [Test]
103 public void Fib()
104 {
105 var recorder = new RecordingObserver<Num>();
106 client.Fib(new FibArgs.Builder { Limit = 6 }.Build(), recorder);
107
Jan Tattermusch075dde42015-03-11 18:21:00 -0700108 CollectionAssert.AreEqual(new List<long> { 1, 1, 2, 3, 5, 8 },
Jan Tattermusch15111f52015-02-05 18:15:14 -0800109 recorder.ToList().Result.ConvertAll((n) => n.Num_));
110 }
111
112 // TODO: test Fib with limit=0 and cancellation
113 [Test]
114 public void Sum()
115 {
116 var res = client.Sum();
Jan Tattermusch075dde42015-03-11 18:21:00 -0700117 foreach (var num in new long[] { 10, 20, 30 })
118 {
Jan Tattermusch15111f52015-02-05 18:15:14 -0800119 res.Inputs.OnNext(Num.CreateBuilder().SetNum_(num).Build());
120 }
121 res.Inputs.OnCompleted();
122
123 Assert.AreEqual(60, res.Task.Result.Num_);
124 }
125
126 [Test]
127 public void DivMany()
128 {
Jan Tattermusch075dde42015-03-11 18:21:00 -0700129 List<DivArgs> divArgsList = new List<DivArgs>
130 {
Jan Tattermusch15111f52015-02-05 18:15:14 -0800131 new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build(),
132 new DivArgs.Builder { Dividend = 100, Divisor = 21 }.Build(),
133 new DivArgs.Builder { Dividend = 7, Divisor = 2 }.Build()
134 };
135
136 var recorder = new RecordingObserver<DivReply>();
137 var requestObserver = client.DivMany(recorder);
138
139 foreach (var arg in divArgsList)
140 {
141 requestObserver.OnNext(arg);
142 }
143 requestObserver.OnCompleted();
144
145 var result = recorder.ToList().Result;
146
Jan Tattermusch075dde42015-03-11 18:21:00 -0700147 CollectionAssert.AreEqual(new long[] { 3, 4, 3 }, result.ConvertAll((divReply) => divReply.Quotient));
148 CollectionAssert.AreEqual(new long[] { 1, 16, 1 }, result.ConvertAll((divReply) => divReply.Remainder));
Jan Tattermusch15111f52015-02-05 18:15:14 -0800149 }
Jan Tattermusch15111f52015-02-05 18:15:14 -0800150 }
151}