Debian Codegraph @

closure-library/closure/goog/messaging/respondingchannel_test.js

raw
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
15 goog.provide('goog.messaging.RespondingChannelTest');
16 goog.setTestOnly('goog.messaging.RespondingChannelTest');
17
18 goog.require('goog.messaging.RespondingChannel');
19 goog.require('goog.testing.MockControl');
20 goog.require('goog.testing.jsunit');
21 goog.require('goog.testing.messaging.MockMessageChannel');
22
23 var CH1_REQUEST = {'request': 'quux1'};
24 var CH2_REQUEST = {'request': 'quux2'};
25 var CH1_RESPONSE = {'response': 'baz1'};
26 var CH2_RESPONSE = {'response': 'baz2'};
27 var SERVICE_NAME = 'serviceName';
28
29 var mockControl;
30 var ch1;
31 var ch2;
32 var respondingCh1;
33 var respondingCh2;
34
35 function setUp() {
36 mockControl = new goog.testing.MockControl();
37
38 ch1 = new goog.testing.messaging.MockMessageChannel(mockControl);
39 ch2 = new goog.testing.messaging.MockMessageChannel(mockControl);
40
41 respondingCh1 = new goog.messaging.RespondingChannel(ch1);
42 respondingCh2 = new goog.messaging.RespondingChannel(ch2);
43 }
44
45 function tearDown() {
46 respondingCh1.dispose();
47 respondingCh2.dispose();
48 mockControl.$verifyAll();
49 }
50
51 function testSendWithSignature() {
52 // 1 to 2 and back.
53 var message1Ch1Request = {'data': CH1_REQUEST, 'signature': 0};
54 var message1Ch2Response = {'data': CH2_RESPONSE, 'signature': 0};
55 var message2Ch1Request = {'data': CH1_REQUEST, 'signature': 1};
56 var message2Ch2Response = {'data': CH2_RESPONSE, 'signature': 1};
57 // 2 to 1 and back.
58 var message3Ch2Request = {'data': CH2_REQUEST, 'signature': 0};
59 var message3Ch1Response = {'data': CH1_RESPONSE, 'signature': 0};
60 var message4Ch2Request = {'data': CH2_REQUEST, 'signature': 1};
61 var message4Ch1Response = {'data': CH1_RESPONSE, 'signature': 1};
62
63 // 1 to 2 and back.
64 ch1.send('public:' + SERVICE_NAME, message1Ch1Request);
65 ch2.send('private:mics', message1Ch2Response);
66 ch1.send('public:' + SERVICE_NAME, message2Ch1Request);
67 ch2.send('private:mics', message2Ch2Response);
68
69 // 2 to 1 and back.
70 ch2.send('public:' + SERVICE_NAME, message3Ch2Request);
71 ch1.send('private:mics', message3Ch1Response);
72 ch2.send('public:' + SERVICE_NAME, message4Ch2Request);
73 ch1.send('private:mics', message4Ch1Response);
74
75 mockControl.$replayAll();
76
77 var hasInvokedCh1 = false;
78 var hasInvokedCh2 = false;
79 var hasReturnedFromCh1 = false;
80 var hasReturnedFromCh2 = false;
81
82 var serviceCallback1 = function(message) {
83 hasInvokedCh1 = true;
84 assertObjectEquals(CH2_REQUEST, message);
85 return CH1_RESPONSE;
86 };
87
88 var serviceCallback2 = function(message) {
89 hasInvokedCh2 = true;
90 assertObjectEquals(CH1_REQUEST, message);
91 return CH2_RESPONSE;
92 };
93
94 var invocationCallback1 = function(message) {
95 hasReturnedFromCh2 = true;
96 assertObjectEquals(CH2_RESPONSE, message);
97 };
98
99 var invocationCallback2 = function(message) {
100 hasReturnedFromCh1 = true;
101 assertObjectEquals(CH1_RESPONSE, message);
102 };
103
104 respondingCh1.registerService(SERVICE_NAME, serviceCallback1);
105 respondingCh2.registerService(SERVICE_NAME, serviceCallback2);
106
107 respondingCh1.send(SERVICE_NAME, CH1_REQUEST, invocationCallback1);
108 ch2.receive('public:' + SERVICE_NAME, message1Ch1Request);
109 ch1.receive('private:mics', message1Ch2Response);
110
111 respondingCh1.send(SERVICE_NAME, CH1_REQUEST, invocationCallback1);
112 ch2.receive('public:' + SERVICE_NAME, message2Ch1Request);
113 ch1.receive('private:mics', message2Ch2Response);
114
115 respondingCh2.send(SERVICE_NAME, CH2_REQUEST, invocationCallback2);
116 ch1.receive('public:' + SERVICE_NAME, message3Ch2Request);
117 ch2.receive('private:mics', message3Ch1Response);
118
119 respondingCh2.send(SERVICE_NAME, CH2_REQUEST, invocationCallback2);
120 ch1.receive('public:' + SERVICE_NAME, message4Ch2Request);
121 ch2.receive('private:mics', message4Ch1Response);
122
123 assertTrue(
124 hasInvokedCh1 && hasInvokedCh2 && hasReturnedFromCh1 &&
125 hasReturnedFromCh2);
126 }