-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
166 lines (138 loc) · 4.12 KB
/
test.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
160
161
162
163
164
165
166
var childProcess = require('child_process');
var Factory = require('./lib/factory');
var mq = new Factory();
var forkServer = false;
var noexit = false;
var port;
var host;
var protocol;
function usage() {
console.log('node test.js [--noexit] [-h host] [-p port] [-s schema]');
exit();
}
function exit() {
process.exit(0);
}
function exitCheck() {
if (!noexit)
exit();
}
function errorUnknownOption (opt) {
console.log("Unknown option: " + opt);
exit();
}
var param = 2;
if (process.argv.length > 2) {
for (; param < process.argv.length; ++param) {
var paramStr = process.argv[param];
var o = paramStr.charAt(0);
if (o === '-' && paramStr.length > 1) {
var c = paramStr.charAt(1);
switch (c) {
case '?':
usage();
break;
case 'p':
port = process.argv[++param];
break;
case 'h':
host = process.argv[++param];
break;
case 's':
protocol = process.argv[++param];
break;
case '-': {
// long option
var cc = paramStr.substr(2);
if (cc === 'noexit')
noexit = true;
else if (cc == 'port')
port = process.argv[++param];
else if (cc == 'protocol')
protocol = process.argv[++param];
break;
}
}
}
else {
errorUnknownOption(paramStr);
}
}
}
mq.host = host;
mq.port = port;
mq.protocol = protocol;
var main = function () {
var producer;
var consumer;
function disconnectConsumer () {
consumer.disconnect();
}
mq.createProducer("TYO Lab")
.then(function (p) {
console.log('Producer: ' + p.getId());
producer = p;
// Reset the onConnect Listener
producer.on('connect', function () {
console.log('producer\'s own connect listener');
});
return mq.createConsumer("TYO Lab Tester");
})
.then(function (c) {
console.log('Subscriber: ' + c.getId());
consumer = c;
var test = 0;
// this listener will be only effective after the current connection is lost
// and get reconnected again
consumer.on('connect', function () {
console.log('consumer\'s own connect listener');
});
consumer.subscribe(producer.name, 'tyo-mq-mt-default', (data, from) => {
if (from)
console.log("Received message from " + from);
test += 1;
if (data === 'test-a')
console.log('test1 succeeded!');
else
console.log('test1 failed');
if (test === 2) disconnectConsumer();
});
consumer.subscribe(producer.name, 'tyo-mq-mt-test2', (data, from) => {
test += 1;
if (data === 'test-b')
console.log('test2 succeeded!');
else
console.log('test2 failed');
if (test === 2) disconnectConsumer();
});
producer.onSubscriberLost(function (from) {
test += 1;
console.log('Informed that connection with a subscriber (' + JSON.stringify(from) + ') was lost');
if (test === 3) exitCheck();
});
// wait for 3 seconds before we produce message
setTimeout(function () {
console.log("Sending messages...");
producer.produce('tyo-mq-mt-default', 'test-a');
producer.produce('tyo-mq-mt-test2', 'test-b');
}, 3000);
});
}
if (forkServer)
{
try {
var child = childProcess.fork('server.js');
child.on('message', function(m) {
console.log(m);
main();
});
child.on('exit', (code, signal) => {
});
}
catch (err) {
console.error('cannot fork server.js');
main();
}
}
else
main();