forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlivedata_server_tests.js
158 lines (139 loc) · 3.85 KB
/
livedata_server_tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
var Fiber = Npm.require('fibers');
Tinytest.addAsync(
"livedata server - connectionHandle.onClose()",
function (test, onComplete) {
makeTestConnection(
test,
function (clientConn, serverConn) {
// On the server side, wait for the connection to be closed.
serverConn.onClose(function () {
test.isTrue(true);
// Add a new onClose after the connection is already
// closed. See that it fires.
serverConn.onClose(function () {
onComplete();
});
});
// Close the connection from the client.
clientConn.disconnect();
},
onComplete
);
}
);
Tinytest.addAsync(
"livedata server - connectionHandle.close()",
function (test, onComplete) {
makeTestConnection(
test,
function (clientConn, serverConn) {
// Wait for the connection to be closed from the server side.
simplePoll(
function () {
return ! clientConn.status().connected;
},
onComplete,
function () {
test.fail("timeout waiting for the connection to be closed on the server side");
onComplete();
}
);
// Close the connection from the server.
serverConn.close();
},
onComplete
);
}
);
testAsyncMulti(
"livedata server - onConnection doesn't get callback after stop.",
[function (test, expect) {
var afterStop = false;
var expectStop1 = expect();
var stopHandle1 = Meteor.onConnection(function (conn) {
stopHandle2.stop();
stopHandle1.stop();
afterStop = true;
// yield to the event loop for a moment to see that no other calls
// to listener2 are called.
Meteor.setTimeout(expectStop1, 10);
});
var stopHandle2 = Meteor.onConnection(function (conn) {
test.isFalse(afterStop);
});
// trigger a connection
var expectConnection = expect();
makeTestConnection(
test,
function (clientConn, serverConn) {
// Close the connection from the client.
clientConn.disconnect();
expectConnection();
},
expectConnection
);
}]
);
Meteor.methods({
livedata_server_test_inner: function () {
return this.connection.id;
},
livedata_server_test_outer: function () {
return Meteor.call('livedata_server_test_inner');
}
});
Tinytest.addAsync(
"livedata server - connection in method invocation",
function (test, onComplete) {
makeTestConnection(
test,
function (clientConn, serverConn) {
var res = clientConn.call('livedata_server_test_inner');
test.equal(res, serverConn.id);
clientConn.disconnect();
onComplete();
},
onComplete
);
}
);
Tinytest.addAsync(
"livedata server - connection in nested method invocation",
function (test, onComplete) {
makeTestConnection(
test,
function (clientConn, serverConn) {
var res = clientConn.call('livedata_server_test_outer');
test.equal(res, serverConn.id);
clientConn.disconnect();
onComplete();
},
onComplete
);
}
);
// connectionId -> callback
var onSubscription = {};
Meteor.publish("livedata_server_test_sub", function (connectionId) {
var callback = onSubscription[connectionId];
if (callback)
callback(this);
this.stop();
});
Tinytest.addAsync(
"livedata server - connection in publish function",
function (test, onComplete) {
makeTestConnection(
test,
function (clientConn, serverConn) {
onSubscription[serverConn.id] = function (subscription) {
delete onSubscription[serverConn.id];
test.equal(subscription.connection.id, serverConn.id);
clientConn.disconnect();
onComplete();
};
clientConn.subscribe("livedata_server_test_sub", serverConn.id);
}
);
}
);