forked from KSDaemon/wampy.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-bondy-transports.mjs
More file actions
159 lines (135 loc) · 5.35 KB
/
test-bondy-transports.mjs
File metadata and controls
159 lines (135 loc) · 5.35 KB
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
/**
* Test wampy.js HTTP Longpoll and HTTP Longpoll-SSE transports against Bondy.
*
* Usage:
* node test-bondy-transports.mjs [--url http://localhost:18080/wamp/longpoll] \
* [--sse-url http://localhost:18080/wamp/sse] [--realm com.example.realm]
*
* Defaults:
* url: http://localhost:18080/wamp/longpoll
* sse-url: http://localhost:18080/wamp/sse
* realm: com.leapsight.test
*/
import { Wampy, LongpollTransport, SSETransport } from './src/wampy.js';
// ---------------------------------------------------------------------------
// CLI args
// ---------------------------------------------------------------------------
const args = process.argv.slice(2);
function flag(name, fallback) {
const idx = args.indexOf(name);
return idx !== -1 && idx + 1 < args.length ? args[idx + 1] : fallback;
}
const LONGPOLL_URL = flag('--url', 'http://localhost:18080/wamp/longpoll');
const SSE_URL = flag('--sse-url', 'http://localhost:18080/wamp/sse');
const REALM = flag('--realm', 'com.leapsight.test');
const SUFFIX = Date.now();
const TOPIC = `com.example.test.onhello.${SUFFIX}`;
const RPC = `com.example.test.add2.${SUFFIX}`;
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function log(transport, ...msg) {
const ts = new Date().toISOString().slice(11, 23);
console.log(`[${ts}] [${transport}]`, ...msg);
}
function sleep(ms) {
return new Promise(r => setTimeout(r, ms));
}
// ---------------------------------------------------------------------------
// Test scenario (runs once per transport)
// ---------------------------------------------------------------------------
async function runTest(transportName) {
const url = transportName === 'sse' ? SSE_URL : LONGPOLL_URL;
log(transportName, `Connecting to ${url} realm=${REALM} ...`);
const wampy = new Wampy(url, {
realm: REALM,
transport: transportName,
autoReconnect: false,
debug: false,
onClose: () => log(transportName, 'Session closed'),
onError: (err) => log(transportName, 'ERROR', err),
});
// 1. Connect
await wampy.connect();
log(transportName, `Connected! sessionId=${wampy.getSessionId()}`);
// 2. Register an RPC
let rpcCalled = false;
await wampy.register(RPC, (invocation) => {
rpcCalled = true;
const [a, b] = invocation.argsList;
log(transportName, `RPC invoked: ${a} + ${b} = ${a + b}`);
return { argsList: [a + b] };
});
log(transportName, `Registered RPC "${RPC}"`);
// 3. Subscribe to a topic
let eventReceived = false;
const sub = await wampy.subscribe(TOPIC, (eventArgs) => {
eventReceived = true;
log(transportName, `Event received:`, eventArgs);
});
log(transportName, `Subscribed to "${TOPIC}" (subscriptionId=${sub.subscriptionId})`);
// 4. Publish an event
await wampy.publish(TOPIC, { argsList: ['hello from ' + transportName] }, { exclude_me: false });
log(transportName, `Published to "${TOPIC}"`);
// Give Bondy a moment to deliver the event
await sleep(1000);
// 5. Call the RPC we registered
const result = await wampy.call(RPC, { argsList: [17, 25] });
log(transportName, `RPC result:`, result);
// 6. Unsubscribe & unregister
await wampy.unsubscribe(sub.subscriptionKey);
log(transportName, `Unsubscribed from "${TOPIC}"`);
await wampy.unregister(RPC);
log(transportName, `Unregistered RPC "${RPC}"`);
// 7. Disconnect
await wampy.disconnect();
log(transportName, 'Disconnected');
// Summary
console.log();
log(transportName, '--- Results ---');
log(transportName, ` RPC called: ${rpcCalled ? 'YES' : 'NO'}`);
log(transportName, ` Event received: ${eventReceived ? 'YES' : 'NO'}`);
console.log();
return { rpcCalled, eventReceived };
}
// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
async function main() {
console.log('='.repeat(60));
console.log(' wampy.js — Bondy HTTP Transport Test');
console.log(' Longpoll URL: ', LONGPOLL_URL);
console.log(' SSE URL: ', SSE_URL);
console.log(' Realm: ', REALM);
console.log('='.repeat(60));
console.log();
const results = {};
for (const transport of ['longpoll', 'sse']) {
try {
results[transport] = await runTest(transport);
} catch (err) {
log(transport, 'FAILED:', err.message || err);
if (err.stack) console.error(err.stack);
results[transport] = { error: err.message || String(err) };
}
console.log('-'.repeat(60));
}
// Final summary
console.log();
console.log('='.repeat(60));
console.log(' SUMMARY');
console.log('='.repeat(60));
for (const [t, r] of Object.entries(results)) {
if (r.error) {
console.log(` ${t.padEnd(10)} FAIL — ${r.error}`);
} else {
const ok = r.rpcCalled && r.eventReceived;
console.log(` ${t.padEnd(10)} ${ok ? 'PASS' : 'PARTIAL'} rpc=${r.rpcCalled} event=${r.eventReceived}`);
}
}
console.log();
}
main().catch(err => {
console.error('Fatal error:', err);
process.exit(1);
});