This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
159 lines (130 loc) · 4.65 KB
/
index.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
159
var _ = require('lodash');
var ClientActions = require('./lib/actions.js');
/**
* Constructor Function
* @param {obj} io socket.io-client instance
* @param {string} socketUrl socket url to connect to
* @param {obj} socketOptions socket.io-client connection options
*/
var TestSocketIOSync = function(io, socketUrl, socketOptions) {
this.io = io;
this.socketUrl = socketUrl;
this.socketOptions = socketOptions;
this.timeout = 25;
this.clientsReturned = [];
this.clients = [];
//used by waitForOtherClients
this.waitCounters = {};
this.waitCallbacks = {};
this.done = null;
tester = this;
this.clientReturns = function clientReturns(err, client) {
client.disconnect();
if (err) {
return tester.done(err);
}
tester.clientsReturned.push(client.label);
if (tester.clientsReturned.length === tester.clients.length) {
tester.done();
}
};
this.incrementWaitCounter = function incrementWaitCounter(label) {
if (typeof tester.waitCounters[label] === 'undefined') {
tester.waitCounters[label] = 1;
tester.waitCallbacks[label] = [];
} else {
tester.waitCounters[label]++;
}
};
this.addWaitForClientsCallback = function addWaitForClientsCallback(label, callback) {
tester.waitCallbacks[label].push(callback);
if (tester.waitCallbacks[label].length === tester.waitCounters[label]) {
_.forEach(tester.waitCallbacks[label], function(callback) {
callback();
});
delete(tester.waitCallbacks[label]);
delete(tester.waitCounters[label]);
}
};
this.run = function run(clients, done) {
if (!_.isArray(clients)) {
throw new Error('clients must be array');
}
if (clients.length === 0) {
throw new Error('clients must be non-empty array');
}
tester.done = done;
tester.clientsReturned = [];
tester.clients = [];
for (var i=0; i<clients.length; i++) {
tester.clients.push(clients[i].label);
clients[i].run();
}
};
this.Client = function Client(label, socketUrl, socketOptions) {
this.label = label || 'Client';
this.socketUrl = socketUrl || tester.socketUrl;
this.socketOptions = _.clone(tester.socketOptions);
if (_.isPlainObject(socketOptions)) {
_.merge(this.socketOptions, socketOptions);
}
this.actions = [];
this.waitLabels = [];
//used by NonBlocking functions
this.failed = false;
this.nonBlockingActionsRunning = 0;
this.nonBlockingActionsCallback = null;
this.connect = function connect() {
this.socketClient = tester.io.connect(this.socketUrl, this.socketOptions);
};
this.disconnect = function disconnect() {
if (this.socketClient != null) {
this.socketClient.disconnect();
this.socketClient = null;
}
}
this.emit = function emit(event, data) {
this.actions.push(new ClientActions.emit(event, data));
return this;
};
this.waitFor = function waitFor(event, data, timeout) {
var eventTimeout = timeout || tester.timeout;
this.actions.push(new ClientActions.waitFor(event, data, eventTimeout));
return this;
};
this.failIfReceived = function failIfReceived(event, data, timeout) {
var eventTimeout = timeout || tester.timeout;
this.actions.push(new ClientActions.failIfReceived(event, data, eventTimeout));
return this;
};
this.failIfReceivedNonBlocking = function failIfReceivedNonBlocking(event, data, timeout) {
var eventTimeout = timeout || tester.timeout;
this.actions.push(new ClientActions.failIfReceivedNonBlocking(event, data, eventTimeout));
return this;
};
this.executeFunction = function executeFunction(func, timeout) {
var eventTimeout = timeout || tester.timeout;
this.actions.push(new ClientActions.executeFunction(func, eventTimeout));
return this;
};
this.waitForOtherClients = function waitForOtherClients(label, timeout) {
var eventTimeout = timeout || tester.timeout;
if (_.includes(this.waitLabels, label)) {
throw new Error('[' + this.label + '] wait label "' + label + '" can not be registered two times');
}
this.waitLabels.push(label);
tester.incrementWaitCounter(label);
this.actions.push(new ClientActions.waitForOtherClients(label, eventTimeout, tester));
return this;
};
this.run = function run() {
if (this.actions.length === 0) {
throw new Error('Client ' + this.label + ' has no actions configured');
}
this.connect();
var firstAction = this.actions.shift();
firstAction.execute(this, tester.clientReturns);
};
}
};
module.exports = TestSocketIOSync;