blob: 579c45824cdf4f3b531a34de1b0bfe137447cfbe [file] [log] [blame]
Jon Skeet0aac0e42009-09-09 18:48:02 +01001#region Copyright notice and license
Jon Skeet60c059b2008-10-23 21:17:56 +01002// Protocol Buffers - Google's data interchange format
3// Copyright 2008 Google Inc. All rights reserved.
4// http://github.com/jskeet/dotnet-protobufs/
5// Original C++/Java/Python code:
Jon Skeet68036862008-10-22 13:30:34 +01006// http://code.google.com/p/protobuf/
7//
Jon Skeet60c059b2008-10-23 21:17:56 +01008// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
Jon Skeet68036862008-10-22 13:30:34 +010011//
Jon Skeet60c059b2008-10-23 21:17:56 +010012// * Redistributions of source code must retain the above copyright
13// notice, this list of conditions and the following disclaimer.
14// * Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following disclaimer
16// in the documentation and/or other materials provided with the
17// distribution.
18// * Neither the name of Google Inc. nor the names of its
19// contributors may be used to endorse or promote products derived from
20// this software without specific prior written permission.
Jon Skeet68036862008-10-22 13:30:34 +010021//
Jon Skeet60c059b2008-10-23 21:17:56 +010022// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Jon Skeet0aac0e42009-09-09 18:48:02 +010033#endregion
34
Jon Skeet68036862008-10-22 13:30:34 +010035using System;
36using Google.ProtocolBuffers.Descriptors;
37using Google.ProtocolBuffers.TestProtos;
38using NUnit.Framework;
39using Rhino.Mocks;
40using Rhino.Mocks.Constraints;
41
42namespace Google.ProtocolBuffers {
43
44 /// <summary>
45 /// Tests for generated service classes.
46 /// TODO(jonskeet): Convert the mocking tests using Rhino.Mocks.
47 /// </summary>
48 [TestFixture]
49 public class ServiceTest {
50
51 delegate void Action<T1, T2>(T1 t1, T2 t2);
52
csharptestf1816be2011-05-19 12:01:16 -050053 private static readonly MethodDescriptor FooDescriptor = TestGenericService.Descriptor.Methods[0];
54 private static readonly MethodDescriptor BarDescriptor = TestGenericService.Descriptor.Methods[1];
Jon Skeet68036862008-10-22 13:30:34 +010055
56 [Test]
57 public void GetRequestPrototype() {
csharptestf1816be2011-05-19 12:01:16 -050058 TestGenericService service = new TestServiceImpl();
Jon Skeet68036862008-10-22 13:30:34 +010059
60 Assert.AreSame(service.GetRequestPrototype(FooDescriptor), FooRequest.DefaultInstance);
61 Assert.AreSame(service.GetRequestPrototype(BarDescriptor), BarRequest.DefaultInstance);
62 }
63
64 [Test]
65 public void GetResponsePrototype() {
csharptestf1816be2011-05-19 12:01:16 -050066 TestGenericService service = new TestServiceImpl();
Jon Skeet68036862008-10-22 13:30:34 +010067
68 Assert.AreSame(service.GetResponsePrototype(FooDescriptor), FooResponse.DefaultInstance);
69 Assert.AreSame(service.GetResponsePrototype(BarDescriptor), BarResponse.DefaultInstance);
70 }
71
72 [Test]
73 public void CallMethodFoo() {
csharptestf1816be2011-05-19 12:01:16 -050074 MockRepository mocks = new MockRepository();
75 FooRequest fooRequest = FooRequest.CreateBuilder().Build();
Jon Skeet68036862008-10-22 13:30:34 +010076 FooResponse fooResponse = FooResponse.CreateBuilder().Build();
Jon Skeete1e84312009-06-25 16:55:08 +010077 IRpcController controller = mocks.StrictMock<IRpcController>();
Jon Skeet68036862008-10-22 13:30:34 +010078
79 bool fooCalled = false;
80
csharptestf1816be2011-05-19 12:01:16 -050081 TestGenericService service = new TestServiceImpl((request, responseAction) => {
Jon Skeet68036862008-10-22 13:30:34 +010082 Assert.AreSame(fooRequest, request);
83 fooCalled = true;
84 responseAction(fooResponse);
85 }, null, controller);
86
87 bool doneHandlerCalled = false;
88 Action<IMessage> doneHandler = (response => {
89 Assert.AreSame(fooResponse, response);
90 doneHandlerCalled = true;
91 });
92
93 using (mocks.Record()) {
94 // No mock interactions to record
95 }
96
97 service.CallMethod(FooDescriptor, controller, fooRequest, doneHandler);
98
99 Assert.IsTrue(doneHandlerCalled);
100 Assert.IsTrue(fooCalled);
101 mocks.VerifyAll();
102 }
103
104 delegate void CallFooDelegate(MethodDescriptor descriptor, IRpcController controller,
105 IMessage request, IMessage response, Action<IMessage> doneHandler);
106
107 /// <summary>
108 /// Tests the generated stub handling of Foo. By this stage we're reasonably confident
109 /// that the choice between Foo and Bar is arbitrary, hence the lack of a corresponding Bar
110 /// test.
111 /// </summary>
112 [Test]
Jon Skeete1e84312009-06-25 16:55:08 +0100113 [Ignore("Crashes Mono - needs further investigation")]
Jon Skeet68036862008-10-22 13:30:34 +0100114 public void GeneratedStubFooCall() {
115 FooRequest fooRequest = FooRequest.CreateBuilder().Build();
116 MockRepository mocks = new MockRepository();
Jon Skeete1e84312009-06-25 16:55:08 +0100117 IRpcChannel mockChannel = mocks.StrictMock<IRpcChannel>();
csharptestf1816be2011-05-19 12:01:16 -0500118 IRpcController mockController = mocks.StrictMock<IRpcController>();
119 TestGenericService service = TestGenericService.CreateStub(mockChannel);
Jon Skeete1e84312009-06-25 16:55:08 +0100120 Action<FooResponse> doneHandler = mocks.StrictMock<Action<FooResponse>>();
Jon Skeet68036862008-10-22 13:30:34 +0100121
122 using (mocks.Record()) {
123
124 // Nasty way of mocking out "the channel calls the done handler".
125 Expect.Call(() => mockChannel.CallMethod(null, null, null, null, null))
126 .IgnoreArguments()
127 .Constraints(Is.Same(FooDescriptor), Is.Same(mockController), Is.Same(fooRequest),
128 Is.Same(FooResponse.DefaultInstance), Is.Anything())
csharptestf1816be2011-05-19 12:01:16 -0500129 .Do((CallFooDelegate) ((p1, p2, p3, response, done) => done(response)));
Jon Skeete1e84312009-06-25 16:55:08 +0100130 doneHandler(FooResponse.DefaultInstance);
Jon Skeet68036862008-10-22 13:30:34 +0100131 }
132
133 service.Foo(mockController, fooRequest, doneHandler);
134
135 mocks.VerifyAll();
136 }
137
138 [Test]
139 public void CallMethodBar() {
csharptestf1816be2011-05-19 12:01:16 -0500140 MockRepository mocks = new MockRepository();
141 BarRequest barRequest = BarRequest.CreateBuilder().Build();
Jon Skeet68036862008-10-22 13:30:34 +0100142 BarResponse barResponse = BarResponse.CreateBuilder().Build();
Jon Skeete1e84312009-06-25 16:55:08 +0100143 IRpcController controller = mocks.StrictMock<IRpcController>();
Jon Skeet68036862008-10-22 13:30:34 +0100144
145 bool barCalled = false;
146
csharptestf1816be2011-05-19 12:01:16 -0500147 TestGenericService service = new TestServiceImpl(null, (request, responseAction) => {
Jon Skeet68036862008-10-22 13:30:34 +0100148 Assert.AreSame(barRequest, request);
149 barCalled = true;
150 responseAction(barResponse);
151 }, controller);
152
153 bool doneHandlerCalled = false;
154 Action<IMessage> doneHandler = (response => {
155 Assert.AreSame(barResponse, response);
156 doneHandlerCalled = true;
157 });
158
159 using (mocks.Record()) {
160 // No mock interactions to record
161 }
162
163 service.CallMethod(BarDescriptor, controller, barRequest, doneHandler);
164
165 Assert.IsTrue(doneHandlerCalled);
166 Assert.IsTrue(barCalled);
167 mocks.VerifyAll();
168 }
169
170
csharptestf1816be2011-05-19 12:01:16 -0500171 class TestServiceImpl : TestGenericService {
Jon Skeet68036862008-10-22 13:30:34 +0100172 private readonly Action<FooRequest, Action<FooResponse>> fooHandler;
173 private readonly Action<BarRequest, Action<BarResponse>> barHandler;
174 private readonly IRpcController expectedController;
175
176 internal TestServiceImpl() {
csharptestf1816be2011-05-19 12:01:16 -0500177 }
178
179 internal TestServiceImpl(Action<FooRequest, Action<FooResponse>> fooHandler,
Jon Skeet68036862008-10-22 13:30:34 +0100180 Action<BarRequest, Action<BarResponse>> barHandler,
181 IRpcController expectedController) {
182 this.fooHandler = fooHandler;
183 this.barHandler = barHandler;
184 this.expectedController = expectedController;
185 }
186
187 public override void Foo(IRpcController controller, FooRequest request, Action<FooResponse> done) {
188 Assert.AreSame(expectedController, controller);
189 fooHandler(request, done);
190 }
191
192 public override void Bar(IRpcController controller, BarRequest request, Action<BarResponse> done) {
193 Assert.AreSame(expectedController, controller);
194 barHandler(request, done);
195 }
196 }
197 }
198}