ServiceTest.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #region Copyright notice and license
  2. // 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:
  6. // http://code.google.com/p/protobuf/
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. //
  12. // * 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.
  21. //
  22. // 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.
  33. #endregion
  34. using System;
  35. using Google.ProtocolBuffers.Descriptors;
  36. using Google.ProtocolBuffers.TestProtos;
  37. using NUnit.Framework;
  38. using Rhino.Mocks;
  39. using Rhino.Mocks.Constraints;
  40. namespace Google.ProtocolBuffers
  41. {
  42. /// <summary>
  43. /// Tests for generated service classes.
  44. /// TODO(jonskeet): Convert the mocking tests using Rhino.Mocks.
  45. /// </summary>
  46. [TestFixture]
  47. public class ServiceTest
  48. {
  49. private delegate void Action<T1, T2>(T1 t1, T2 t2);
  50. private static readonly MethodDescriptor FooDescriptor = TestGenericService.Descriptor.Methods[0];
  51. private static readonly MethodDescriptor BarDescriptor = TestGenericService.Descriptor.Methods[1];
  52. [Test]
  53. public void GetRequestPrototype()
  54. {
  55. TestGenericService service = new TestServiceImpl();
  56. Assert.AreSame(service.GetRequestPrototype(FooDescriptor), FooRequest.DefaultInstance);
  57. Assert.AreSame(service.GetRequestPrototype(BarDescriptor), BarRequest.DefaultInstance);
  58. }
  59. [Test]
  60. public void GetResponsePrototype()
  61. {
  62. TestGenericService service = new TestServiceImpl();
  63. Assert.AreSame(service.GetResponsePrototype(FooDescriptor), FooResponse.DefaultInstance);
  64. Assert.AreSame(service.GetResponsePrototype(BarDescriptor), BarResponse.DefaultInstance);
  65. }
  66. [Test]
  67. public void CallMethodFoo()
  68. {
  69. MockRepository mocks = new MockRepository();
  70. FooRequest fooRequest = FooRequest.CreateBuilder().Build();
  71. FooResponse fooResponse = FooResponse.CreateBuilder().Build();
  72. IRpcController controller = mocks.StrictMock<IRpcController>();
  73. bool fooCalled = false;
  74. TestGenericService service = new TestServiceImpl((request, responseAction) =>
  75. {
  76. Assert.AreSame(fooRequest, request);
  77. fooCalled = true;
  78. responseAction(fooResponse);
  79. }, null, controller);
  80. bool doneHandlerCalled = false;
  81. Action<IMessage> doneHandler = (response =>
  82. {
  83. Assert.AreSame(fooResponse, response);
  84. doneHandlerCalled = true;
  85. });
  86. using (mocks.Record())
  87. {
  88. // No mock interactions to record
  89. }
  90. service.CallMethod(FooDescriptor, controller, fooRequest, doneHandler);
  91. Assert.IsTrue(doneHandlerCalled);
  92. Assert.IsTrue(fooCalled);
  93. mocks.VerifyAll();
  94. }
  95. private delegate void CallFooDelegate(MethodDescriptor descriptor, IRpcController controller,
  96. IMessage request, IMessage response, Action<IMessage> doneHandler);
  97. /// <summary>
  98. /// Tests the generated stub handling of Foo. By this stage we're reasonably confident
  99. /// that the choice between Foo and Bar is arbitrary, hence the lack of a corresponding Bar
  100. /// test.
  101. /// </summary>
  102. [Test]
  103. [Ignore("Crashes Mono - needs further investigation")]
  104. public void GeneratedStubFooCall()
  105. {
  106. FooRequest fooRequest = FooRequest.CreateBuilder().Build();
  107. MockRepository mocks = new MockRepository();
  108. IRpcChannel mockChannel = mocks.StrictMock<IRpcChannel>();
  109. IRpcController mockController = mocks.StrictMock<IRpcController>();
  110. TestGenericService service = TestGenericService.CreateStub(mockChannel);
  111. Action<FooResponse> doneHandler = mocks.StrictMock<Action<FooResponse>>();
  112. using (mocks.Record())
  113. {
  114. // Nasty way of mocking out "the channel calls the done handler".
  115. Expect.Call(() => mockChannel.CallMethod(null, null, null, null, null))
  116. .IgnoreArguments()
  117. .Constraints(Is.Same(FooDescriptor), Is.Same(mockController), Is.Same(fooRequest),
  118. Is.Same(FooResponse.DefaultInstance), Is.Anything())
  119. .Do((CallFooDelegate) ((p1, p2, p3, response, done) => done(response)));
  120. doneHandler(FooResponse.DefaultInstance);
  121. }
  122. service.Foo(mockController, fooRequest, doneHandler);
  123. mocks.VerifyAll();
  124. }
  125. [Test]
  126. public void CallMethodBar()
  127. {
  128. MockRepository mocks = new MockRepository();
  129. BarRequest barRequest = BarRequest.CreateBuilder().Build();
  130. BarResponse barResponse = BarResponse.CreateBuilder().Build();
  131. IRpcController controller = mocks.StrictMock<IRpcController>();
  132. bool barCalled = false;
  133. TestGenericService service = new TestServiceImpl(null, (request, responseAction) =>
  134. {
  135. Assert.AreSame(barRequest, request);
  136. barCalled = true;
  137. responseAction(barResponse);
  138. }, controller);
  139. bool doneHandlerCalled = false;
  140. Action<IMessage> doneHandler = (response =>
  141. {
  142. Assert.AreSame(barResponse, response);
  143. doneHandlerCalled = true;
  144. });
  145. using (mocks.Record())
  146. {
  147. // No mock interactions to record
  148. }
  149. service.CallMethod(BarDescriptor, controller, barRequest, doneHandler);
  150. Assert.IsTrue(doneHandlerCalled);
  151. Assert.IsTrue(barCalled);
  152. mocks.VerifyAll();
  153. }
  154. private class TestServiceImpl : TestGenericService
  155. {
  156. private readonly Action<FooRequest, Action<FooResponse>> fooHandler;
  157. private readonly Action<BarRequest, Action<BarResponse>> barHandler;
  158. private readonly IRpcController expectedController;
  159. internal TestServiceImpl()
  160. {
  161. }
  162. internal TestServiceImpl(Action<FooRequest, Action<FooResponse>> fooHandler,
  163. Action<BarRequest, Action<BarResponse>> barHandler,
  164. IRpcController expectedController)
  165. {
  166. this.fooHandler = fooHandler;
  167. this.barHandler = barHandler;
  168. this.expectedController = expectedController;
  169. }
  170. public override void Foo(IRpcController controller, FooRequest request, Action<FooResponse> done)
  171. {
  172. Assert.AreSame(expectedController, controller);
  173. fooHandler(request, done);
  174. }
  175. public override void Bar(IRpcController controller, BarRequest request, Action<BarResponse> done)
  176. {
  177. Assert.AreSame(expectedController, controller);
  178. barHandler(request, done);
  179. }
  180. }
  181. }
  182. }