ServiceTest.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://github.com/jskeet/dotnet-protobufs/
  4. // Original C++/Java/Python code:
  5. // http://code.google.com/p/protobuf/
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are
  9. // met:
  10. //
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Google Inc. nor the names of its
  18. // contributors may be used to endorse or promote products derived from
  19. // this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. using System;
  33. using Google.ProtocolBuffers.Descriptors;
  34. using Google.ProtocolBuffers.TestProtos;
  35. using NUnit.Framework;
  36. using Rhino.Mocks;
  37. using Rhino.Mocks.Constraints;
  38. namespace Google.ProtocolBuffers {
  39. /// <summary>
  40. /// Tests for generated service classes.
  41. /// TODO(jonskeet): Convert the mocking tests using Rhino.Mocks.
  42. /// </summary>
  43. [TestFixture]
  44. public class ServiceTest {
  45. delegate void Action<T1, T2>(T1 t1, T2 t2);
  46. private static readonly MethodDescriptor FooDescriptor = TestService.Descriptor.Methods[0];
  47. private static readonly MethodDescriptor BarDescriptor = TestService.Descriptor.Methods[1];
  48. [Test]
  49. public void GetRequestPrototype() {
  50. TestService service = new TestServiceImpl();
  51. Assert.AreSame(service.GetRequestPrototype(FooDescriptor), FooRequest.DefaultInstance);
  52. Assert.AreSame(service.GetRequestPrototype(BarDescriptor), BarRequest.DefaultInstance);
  53. }
  54. [Test]
  55. public void GetResponsePrototype() {
  56. TestService service = new TestServiceImpl();
  57. Assert.AreSame(service.GetResponsePrototype(FooDescriptor), FooResponse.DefaultInstance);
  58. Assert.AreSame(service.GetResponsePrototype(BarDescriptor), BarResponse.DefaultInstance);
  59. }
  60. [Test]
  61. public void CallMethodFoo() {
  62. MockRepository mocks = new MockRepository();
  63. FooRequest fooRequest = FooRequest.CreateBuilder().Build();
  64. FooResponse fooResponse = FooResponse.CreateBuilder().Build();
  65. IRpcController controller = mocks.StrictMock<IRpcController>();
  66. bool fooCalled = false;
  67. TestService service = new TestServiceImpl((request, responseAction) => {
  68. Assert.AreSame(fooRequest, request);
  69. fooCalled = true;
  70. responseAction(fooResponse);
  71. }, null, controller);
  72. bool doneHandlerCalled = false;
  73. Action<IMessage> doneHandler = (response => {
  74. Assert.AreSame(fooResponse, response);
  75. doneHandlerCalled = true;
  76. });
  77. using (mocks.Record()) {
  78. // No mock interactions to record
  79. }
  80. service.CallMethod(FooDescriptor, controller, fooRequest, doneHandler);
  81. Assert.IsTrue(doneHandlerCalled);
  82. Assert.IsTrue(fooCalled);
  83. mocks.VerifyAll();
  84. }
  85. delegate void CallFooDelegate(MethodDescriptor descriptor, IRpcController controller,
  86. IMessage request, IMessage response, Action<IMessage> doneHandler);
  87. /// <summary>
  88. /// Tests the generated stub handling of Foo. By this stage we're reasonably confident
  89. /// that the choice between Foo and Bar is arbitrary, hence the lack of a corresponding Bar
  90. /// test.
  91. /// </summary>
  92. [Test]
  93. public void GeneratedStubFooCall() {
  94. FooRequest fooRequest = FooRequest.CreateBuilder().Build();
  95. MockRepository mocks = new MockRepository();
  96. IRpcChannel mockChannel = mocks.StrictMock<IRpcChannel>();
  97. IRpcController mockController = mocks.StrictMock<IRpcController>();
  98. TestService service = TestService.CreateStub(mockChannel);
  99. Action<FooResponse> doneHandler = mocks.StrictMock<Action<FooResponse>>();
  100. using (mocks.Record()) {
  101. // Nasty way of mocking out "the channel calls the done handler".
  102. Expect.Call(() => mockChannel.CallMethod(null, null, null, null, null))
  103. .IgnoreArguments()
  104. .Constraints(Is.Same(FooDescriptor), Is.Same(mockController), Is.Same(fooRequest),
  105. Is.Same(FooResponse.DefaultInstance), Is.Anything())
  106. .Do((CallFooDelegate) ((p1, p2, p3, response, done) => done(response)));
  107. doneHandler.Invoke(FooResponse.DefaultInstance);
  108. }
  109. service.Foo(mockController, fooRequest, doneHandler);
  110. mocks.VerifyAll();
  111. }
  112. [Test]
  113. public void CallMethodBar() {
  114. MockRepository mocks = new MockRepository();
  115. BarRequest barRequest = BarRequest.CreateBuilder().Build();
  116. BarResponse barResponse = BarResponse.CreateBuilder().Build();
  117. IRpcController controller = mocks.StrictMock<IRpcController>();
  118. bool barCalled = false;
  119. TestService service = new TestServiceImpl(null, (request, responseAction) => {
  120. Assert.AreSame(barRequest, request);
  121. barCalled = true;
  122. responseAction(barResponse);
  123. }, controller);
  124. bool doneHandlerCalled = false;
  125. Action<IMessage> doneHandler = (response => {
  126. Assert.AreSame(barResponse, response);
  127. doneHandlerCalled = true;
  128. });
  129. using (mocks.Record()) {
  130. // No mock interactions to record
  131. }
  132. service.CallMethod(BarDescriptor, controller, barRequest, doneHandler);
  133. Assert.IsTrue(doneHandlerCalled);
  134. Assert.IsTrue(barCalled);
  135. mocks.VerifyAll();
  136. }
  137. class TestServiceImpl : TestService {
  138. private readonly Action<FooRequest, Action<FooResponse>> fooHandler;
  139. private readonly Action<BarRequest, Action<BarResponse>> barHandler;
  140. private readonly IRpcController expectedController;
  141. internal TestServiceImpl() {
  142. }
  143. internal TestServiceImpl(Action<FooRequest, Action<FooResponse>> fooHandler,
  144. Action<BarRequest, Action<BarResponse>> barHandler,
  145. IRpcController expectedController) {
  146. this.fooHandler = fooHandler;
  147. this.barHandler = barHandler;
  148. this.expectedController = expectedController;
  149. }
  150. public override void Foo(IRpcController controller, FooRequest request, Action<FooResponse> done) {
  151. Assert.AreSame(expectedController, controller);
  152. fooHandler(request, done);
  153. }
  154. public override void Bar(IRpcController controller, BarRequest request, Action<BarResponse> done) {
  155. Assert.AreSame(expectedController, controller);
  156. barHandler(request, done);
  157. }
  158. }
  159. }
  160. }