respondingchannel_test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2012 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.messaging.RespondingChannelTest');
  15. goog.setTestOnly('goog.messaging.RespondingChannelTest');
  16. goog.require('goog.messaging.RespondingChannel');
  17. goog.require('goog.testing.MockControl');
  18. goog.require('goog.testing.jsunit');
  19. goog.require('goog.testing.messaging.MockMessageChannel');
  20. var CH1_REQUEST = {'request': 'quux1'};
  21. var CH2_REQUEST = {'request': 'quux2'};
  22. var CH1_RESPONSE = {'response': 'baz1'};
  23. var CH2_RESPONSE = {'response': 'baz2'};
  24. var SERVICE_NAME = 'serviceName';
  25. var mockControl;
  26. var ch1;
  27. var ch2;
  28. var respondingCh1;
  29. var respondingCh2;
  30. function setUp() {
  31. mockControl = new goog.testing.MockControl();
  32. ch1 = new goog.testing.messaging.MockMessageChannel(mockControl);
  33. ch2 = new goog.testing.messaging.MockMessageChannel(mockControl);
  34. respondingCh1 = new goog.messaging.RespondingChannel(ch1);
  35. respondingCh2 = new goog.messaging.RespondingChannel(ch2);
  36. }
  37. function tearDown() {
  38. respondingCh1.dispose();
  39. respondingCh2.dispose();
  40. mockControl.$verifyAll();
  41. }
  42. function testSendWithSignature() {
  43. // 1 to 2 and back.
  44. var message1Ch1Request = {'data': CH1_REQUEST, 'signature': 0};
  45. var message1Ch2Response = {'data': CH2_RESPONSE, 'signature': 0};
  46. var message2Ch1Request = {'data': CH1_REQUEST, 'signature': 1};
  47. var message2Ch2Response = {'data': CH2_RESPONSE, 'signature': 1};
  48. // 2 to 1 and back.
  49. var message3Ch2Request = {'data': CH2_REQUEST, 'signature': 0};
  50. var message3Ch1Response = {'data': CH1_RESPONSE, 'signature': 0};
  51. var message4Ch2Request = {'data': CH2_REQUEST, 'signature': 1};
  52. var message4Ch1Response = {'data': CH1_RESPONSE, 'signature': 1};
  53. // 1 to 2 and back.
  54. ch1.send('public:' + SERVICE_NAME, message1Ch1Request);
  55. ch2.send('private:mics', message1Ch2Response);
  56. ch1.send('public:' + SERVICE_NAME, message2Ch1Request);
  57. ch2.send('private:mics', message2Ch2Response);
  58. // 2 to 1 and back.
  59. ch2.send('public:' + SERVICE_NAME, message3Ch2Request);
  60. ch1.send('private:mics', message3Ch1Response);
  61. ch2.send('public:' + SERVICE_NAME, message4Ch2Request);
  62. ch1.send('private:mics', message4Ch1Response);
  63. mockControl.$replayAll();
  64. var hasInvokedCh1 = false;
  65. var hasInvokedCh2 = false;
  66. var hasReturnedFromCh1 = false;
  67. var hasReturnedFromCh2 = false;
  68. var serviceCallback1 = function(message) {
  69. hasInvokedCh1 = true;
  70. assertObjectEquals(CH2_REQUEST, message);
  71. return CH1_RESPONSE;
  72. };
  73. var serviceCallback2 = function(message) {
  74. hasInvokedCh2 = true;
  75. assertObjectEquals(CH1_REQUEST, message);
  76. return CH2_RESPONSE;
  77. };
  78. var invocationCallback1 = function(message) {
  79. hasReturnedFromCh2 = true;
  80. assertObjectEquals(CH2_RESPONSE, message);
  81. };
  82. var invocationCallback2 = function(message) {
  83. hasReturnedFromCh1 = true;
  84. assertObjectEquals(CH1_RESPONSE, message);
  85. };
  86. respondingCh1.registerService(SERVICE_NAME, serviceCallback1);
  87. respondingCh2.registerService(SERVICE_NAME, serviceCallback2);
  88. respondingCh1.send(SERVICE_NAME, CH1_REQUEST, invocationCallback1);
  89. ch2.receive('public:' + SERVICE_NAME, message1Ch1Request);
  90. ch1.receive('private:mics', message1Ch2Response);
  91. respondingCh1.send(SERVICE_NAME, CH1_REQUEST, invocationCallback1);
  92. ch2.receive('public:' + SERVICE_NAME, message2Ch1Request);
  93. ch1.receive('private:mics', message2Ch2Response);
  94. respondingCh2.send(SERVICE_NAME, CH2_REQUEST, invocationCallback2);
  95. ch1.receive('public:' + SERVICE_NAME, message3Ch2Request);
  96. ch2.receive('private:mics', message3Ch1Response);
  97. respondingCh2.send(SERVICE_NAME, CH2_REQUEST, invocationCallback2);
  98. ch1.receive('public:' + SERVICE_NAME, message4Ch2Request);
  99. ch2.receive('private:mics', message4Ch1Response);
  100. assertTrue(
  101. hasInvokedCh1 && hasInvokedCh2 && hasReturnedFromCh1 &&
  102. hasReturnedFromCh2);
  103. }