node-bible-sync 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/build.yml +2 -1
- package/README.md +16 -1
- package/package.json +11 -1
- package/scripts/prepare_biblesync.js +33 -6
- package/src/napi_module/biblesync_session.cpp +25 -5
- package/test/biblesync_integration.test.js +216 -2
- package/test/biblesync_prepare.test.js +14 -0
- package/test/fixtures/biblesync_packet_timers.cpp +89 -0
- package/test/fixtures/biblesync_peer.js +9 -0
|
@@ -36,4 +36,5 @@ jobs:
|
|
|
36
36
|
sudo apt-get install -y libbiblesync-dev
|
|
37
37
|
cc -shared -fPIC test/fixtures/biblesync_fail_send.c -ldl -o "$RUNNER_TEMP/biblesync-fail-send.so"
|
|
38
38
|
c++ -std=c++11 -Isrc/biblesync_compat -I/usr/include/biblesync test/fixtures/biblesync_system_peer.cpp -lbiblesync -o "$RUNNER_TEMP/biblesync-system-peer"
|
|
39
|
-
|
|
39
|
+
c++ -std=c++11 -pthread -Isrc/biblesync_compat -Ibuild/Release/obj/gen test/fixtures/biblesync_packet_timers.cpp build/Release/obj/gen/biblesync.cc -o "$RUNNER_TEMP/biblesync-packet-timers"
|
|
40
|
+
BIBLESYNC_INTEGRATION=1 BIBLESYNC_FAIL_SEND_LIBRARY="$RUNNER_TEMP/biblesync-fail-send.so" BIBLESYNC_SYSTEM_PEER="$RUNNER_TEMP/biblesync-system-peer" BIBLESYNC_TIMER_PEER="$RUNNER_TEMP/biblesync-packet-timers" npm test -- --runInBand test/biblesync_integration.test.js
|
package/README.md
CHANGED
|
@@ -72,11 +72,20 @@ conversations, but does not protect them from observation or spoofing. Do not
|
|
|
72
72
|
use it as a password or rely on it on an untrusted network. IPv4 multicast
|
|
73
73
|
`239.225.27.227`, UDP port `22272`, must be supported by the network and firewall.
|
|
74
74
|
|
|
75
|
+
The worker checks for incoming packets approximately every 100 ms while active;
|
|
76
|
+
beacon transmission and speaker aging still use a one-second maintenance tick.
|
|
77
|
+
Discovery of an already-running speaker also depends on when its next beacon
|
|
78
|
+
arrives: standard speakers send one roughly every 10 seconds. These polling
|
|
79
|
+
intervals are not latency guarantees, especially under load.
|
|
80
|
+
|
|
75
81
|
Mobile hosts are responsible for permissions and lifecycle handling. Android
|
|
76
82
|
may require Wi-Fi/multicast permissions and a `WifiManager.MulticastLock`; iOS
|
|
77
83
|
may require a local-network usage description and multicast entitlement. Stop
|
|
78
84
|
or suspend the session when the host cannot receive multicast. A successful
|
|
79
85
|
native build does not establish device networking compatibility.
|
|
86
|
+
The timed polling increases idle worker wakeups compared with a one-second
|
|
87
|
+
poll; its battery impact depends on the device and has not been measured on
|
|
88
|
+
mobile hardware.
|
|
80
89
|
|
|
81
90
|
## Example
|
|
82
91
|
|
|
@@ -104,11 +113,17 @@ BibleSync-library interoperability run. To run multicast and fault tests locally
|
|
|
104
113
|
|
|
105
114
|
```sh
|
|
106
115
|
cc -shared -fPIC test/fixtures/biblesync_fail_send.c -ldl -o build/biblesync-fail-send.so
|
|
116
|
+
c++ -std=c++11 -pthread -Isrc/biblesync_compat -Ibuild/Release/obj/gen \
|
|
117
|
+
test/fixtures/biblesync_packet_timers.cpp build/Release/obj/gen/biblesync.cc \
|
|
118
|
+
-o build/biblesync-packet-timers
|
|
107
119
|
BIBLESYNC_INTEGRATION=1 BIBLESYNC_FAIL_SEND_LIBRARY="$PWD/build/biblesync-fail-send.so" \
|
|
120
|
+
BIBLESYNC_TIMER_PEER="$PWD/build/biblesync-packet-timers" \
|
|
108
121
|
npm test -- --runInBand test/biblesync_integration.test.js
|
|
109
122
|
```
|
|
110
123
|
|
|
111
|
-
These tests require local IPv4 multicast
|
|
124
|
+
These tests require local IPv4 multicast; the compiler commands shown are for
|
|
125
|
+
Linux. The timing test runs for roughly 80 seconds to observe several
|
|
126
|
+
standard beacon cycles and subsequent expiry. Set `BIBLESYNC_SYSTEM_PEER` to the
|
|
112
127
|
compiled `test/fixtures/biblesync_system_peer.cpp` linked against an installed
|
|
113
128
|
BibleSync library to also run bidirectional interoperability tests. See the
|
|
114
129
|
Linux CI workflow for compilation.
|
package/package.json
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-bible-sync",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Node.js wrapper of biblesync, a multicast shared co-navigation library for Bible programs",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"bible",
|
|
7
|
+
"biblesync",
|
|
8
|
+
"bible-study",
|
|
9
|
+
"scripture",
|
|
10
|
+
"navigation",
|
|
11
|
+
"co-navigation",
|
|
12
|
+
"multicast",
|
|
13
|
+
"node-addon"
|
|
14
|
+
],
|
|
5
15
|
"main": "index.js",
|
|
6
16
|
"gypfile": true,
|
|
7
17
|
"license": "GPL-2.0+",
|
|
@@ -39,6 +39,14 @@ function replaceOnce(from, to) {
|
|
|
39
39
|
source = source.slice(0, first) + to + source.slice(first + from.length);
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
function replaceHeaderOnce(from, to) {
|
|
43
|
+
const first = header.indexOf(from);
|
|
44
|
+
if (first < 0 || header.indexOf(from, first + from.length) !== -1) {
|
|
45
|
+
throw new Error(`Unexpected BibleSync 2.2.0 header; cannot apply change: ${from.slice(0, 60)}`);
|
|
46
|
+
}
|
|
47
|
+
header = header.slice(0, first) + to + header.slice(first + from.length);
|
|
48
|
+
}
|
|
49
|
+
|
|
42
50
|
// Avoid std::bind being selected instead of the socket function by libc++.
|
|
43
51
|
replaceOnce('if (bind(server_fd, (struct sockaddr*)&server,',
|
|
44
52
|
'if (::bind(server_fd, (struct sockaddr*)&server,');
|
|
@@ -87,6 +95,22 @@ replaceOnce(` return;
|
|
|
87
95
|
replaceOnce('while ((recv_size = InitSelectRead(dump, &source, &bsp)) > 0)',
|
|
88
96
|
'for (unsigned packets = 0; packets < 64 && (recv_size = InitSelectRead(dump, &source, &bsp)) > 0; ++packets)');
|
|
89
97
|
|
|
98
|
+
// Keep upstream Receive() maintenance semantics, while allowing packet-only polls.
|
|
99
|
+
replaceOnce('return ((BibleSync *)myself)->ReceiveInternal();',
|
|
100
|
+
`return ((BibleSync *)myself)->ReceiveInternal(true);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
int BibleSync::ReceivePackets(void *myself)
|
|
104
|
+
{
|
|
105
|
+
return ((BibleSync *)myself)->ReceiveInternal(false);`);
|
|
106
|
+
replaceOnce('int BibleSync::ReceiveInternal()', 'int BibleSync::ReceiveInternal(bool maintenance)');
|
|
107
|
+
replaceOnce(` // beacon-related tasks: others' aging and sending our beacon.
|
|
108
|
+
ageSpeakers();`, ` if (!maintenance)
|
|
109
|
+
return TRUE;
|
|
110
|
+
|
|
111
|
+
// beacon-related tasks: others' aging and sending our beacon.
|
|
112
|
+
ageSpeakers();`);
|
|
113
|
+
|
|
90
114
|
// Do not announce a session when socket setup or the initial beacon failed.
|
|
91
115
|
replaceOnce('if ((mode == BSP_MODE_PERSONAL) || (mode == BSP_MODE_SPEAKER))\n\t{\n\t TransmitInternal(BSP_BEACON);',
|
|
92
116
|
'if (ok_so_far && ((mode == BSP_MODE_PERSONAL) || (mode == BSP_MODE_SPEAKER)))\n\t{\n\t TransmitInternal(BSP_BEACON);');
|
|
@@ -174,16 +198,19 @@ void BibleSync::InterfaceAddress()
|
|
|
174
198
|
|
|
175
199
|
` + source.slice(end + '#endif /* linux */\n\n'.length);
|
|
176
200
|
|
|
177
|
-
fs.mkdirSync(path.dirname(output), { recursive: true });
|
|
178
|
-
fs.writeFileSync(output, source);
|
|
179
201
|
const passphraseGetter = ' inline string getPassphrase(void) { return passphrase; };';
|
|
180
|
-
|
|
181
|
-
header = header.replace(passphraseGetter, `${passphraseGetter}
|
|
202
|
+
replaceHeaderOnce(passphraseGetter, `${passphraseGetter}
|
|
182
203
|
inline string getDevice(void) { return device; };`);
|
|
183
204
|
const socketFields = ' int server_fd, client_fd;';
|
|
184
|
-
|
|
185
|
-
header = header.replace(socketFields, `${socketFields}
|
|
205
|
+
replaceHeaderOnce(socketFields, `${socketFields}
|
|
186
206
|
#ifdef WIN32
|
|
187
207
|
bool winsock_initialized = false;
|
|
188
208
|
#endif`);
|
|
209
|
+
replaceHeaderOnce(' int ReceiveInternal();\t\t// C++ object context.',
|
|
210
|
+
' int ReceiveInternal(bool maintenance);\t// C++ object context.');
|
|
211
|
+
replaceHeaderOnce(' static int Receive(void *myself); // assume C context: poll from timeout.',
|
|
212
|
+
` static int Receive(void *myself); // assume C context: poll from timeout.
|
|
213
|
+
static int ReceivePackets(void *myself); // drain packets without advancing timers.`);
|
|
214
|
+
fs.mkdirSync(path.dirname(output), { recursive: true });
|
|
215
|
+
fs.writeFileSync(output, source);
|
|
189
216
|
fs.writeFileSync(path.join(path.dirname(output), 'biblesync.hh'), header);
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
|
|
19
19
|
#include "biblesync_session.hpp"
|
|
20
20
|
|
|
21
|
+
#include <algorithm>
|
|
21
22
|
#include <chrono>
|
|
22
23
|
#include <utility>
|
|
23
24
|
#include <biblesync.hh>
|
|
@@ -335,13 +336,18 @@ void BibleSyncSession::Run(std::string id, std::string mode)
|
|
|
335
336
|
}
|
|
336
337
|
Emit(std::move(result), false);
|
|
337
338
|
|
|
338
|
-
|
|
339
|
+
using Clock = std::chrono::steady_clock;
|
|
340
|
+
const auto packetInterval = std::chrono::milliseconds(100);
|
|
341
|
+
const auto maintenanceInterval = std::chrono::seconds(1);
|
|
342
|
+
auto nextPacket = Clock::now() + packetInterval;
|
|
343
|
+
auto nextMaintenance = Clock::now() + maintenanceInterval;
|
|
339
344
|
while (started && !overflow_) {
|
|
340
345
|
Command command;
|
|
341
346
|
bool haveCommand = false;
|
|
342
347
|
{
|
|
343
348
|
std::unique_lock<std::mutex> lock(mutex_);
|
|
344
|
-
wake_.wait_until(lock,
|
|
349
|
+
wake_.wait_until(lock, std::min(nextPacket, nextMaintenance),
|
|
350
|
+
[this] { return closing_ || !commands_.empty(); });
|
|
345
351
|
if (!commands_.empty()) {
|
|
346
352
|
command = std::move(commands_.front());
|
|
347
353
|
commands_.pop_front();
|
|
@@ -393,13 +399,27 @@ void BibleSyncSession::Run(std::string id, std::string mode)
|
|
|
393
399
|
}
|
|
394
400
|
Emit(std::move(response), false);
|
|
395
401
|
}
|
|
396
|
-
|
|
397
|
-
|
|
402
|
+
auto now = Clock::now();
|
|
403
|
+
if (!overflow_ && (now >= nextPacket || now >= nextMaintenance)) {
|
|
404
|
+
const bool maintenanceDue = now >= nextMaintenance;
|
|
405
|
+
if (maintenanceDue) {
|
|
406
|
+
BibleSync::Receive(&sync);
|
|
407
|
+
} else {
|
|
408
|
+
BibleSync::ReceivePackets(&sync);
|
|
409
|
+
}
|
|
398
410
|
if (sync.getMode() == BSP_MODE_DISABLE) {
|
|
399
411
|
fatalError_ = "BibleSync stopped after a network failure";
|
|
400
412
|
overflow_ = true;
|
|
401
413
|
}
|
|
402
|
-
|
|
414
|
+
now = Clock::now();
|
|
415
|
+
if (maintenanceDue) {
|
|
416
|
+
nextMaintenance += maintenanceInterval;
|
|
417
|
+
if (nextMaintenance <= now) nextMaintenance = now + maintenanceInterval;
|
|
418
|
+
}
|
|
419
|
+
if (nextPacket <= now) {
|
|
420
|
+
nextPacket += packetInterval;
|
|
421
|
+
if (nextPacket <= now) nextPacket = now + packetInterval;
|
|
422
|
+
}
|
|
403
423
|
}
|
|
404
424
|
}
|
|
405
425
|
{
|
|
@@ -17,12 +17,114 @@
|
|
|
17
17
|
If not, see <http://www.gnu.org/licenses/>. */
|
|
18
18
|
|
|
19
19
|
const path = require('path');
|
|
20
|
-
const { fork, spawn } = require('child_process');
|
|
20
|
+
const { fork, spawn, spawnSync } = require('child_process');
|
|
21
21
|
const { randomBytes } = require('crypto');
|
|
22
22
|
const readline = require('readline');
|
|
23
|
+
const dgram = require('dgram');
|
|
24
|
+
const { performance } = require('perf_hooks');
|
|
23
25
|
|
|
24
26
|
const integration = process.env.BIBLESYNC_INTEGRATION === '1' ? test : test.skip;
|
|
25
|
-
jest.setTimeout(
|
|
27
|
+
jest.setTimeout(110000);
|
|
28
|
+
|
|
29
|
+
const timerIntegration = process.env.BIBLESYNC_TIMER_PEER ? integration : test.skip;
|
|
30
|
+
timerIntegration('packet-only polls preserve maintenance counters and bounded batches', () => {
|
|
31
|
+
const result = spawnSync(process.env.BIBLESYNC_TIMER_PEER, [], { encoding: 'utf8', timeout: 10000 });
|
|
32
|
+
expect(result.error).toBeUndefined();
|
|
33
|
+
expect({ status: result.status, stderr: result.stderr }).toEqual({ status: 0, stderr: '' });
|
|
34
|
+
expect(result.stdout).toContain('packet-only timers and bounded batch verified');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
integration('measures discovery after a running speaker sends its next beacon', async () => {
|
|
38
|
+
const passphrase = `nsi-test-${randomBytes(12).toString('hex')}`;
|
|
39
|
+
const observer = dgram.createSocket({ type: 'udp4', reuseAddr: true });
|
|
40
|
+
const beacons = [];
|
|
41
|
+
let wakeBeacon;
|
|
42
|
+
observer.on('message', packet => {
|
|
43
|
+
if (packet[5] === 3 && packet.includes(Buffer.from(passphrase))) {
|
|
44
|
+
beacons.push(performance.now());
|
|
45
|
+
if (wakeBeacon) wakeBeacon();
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
await new Promise((resolve, reject) => {
|
|
49
|
+
observer.once('error', reject);
|
|
50
|
+
observer.bind(22272, () => {
|
|
51
|
+
try {
|
|
52
|
+
observer.addMembership('239.225.27.227');
|
|
53
|
+
resolve();
|
|
54
|
+
} catch (error) {
|
|
55
|
+
reject(error);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
async function nextBeacon(index) {
|
|
60
|
+
while (beacons.length <= index) {
|
|
61
|
+
await new Promise((resolve, reject) => {
|
|
62
|
+
const timer = setTimeout(() => reject(new Error('Timed out waiting for beacon')), 15000);
|
|
63
|
+
wakeBeacon = () => { clearTimeout(timer); wakeBeacon = null; resolve(); };
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
return beacons[index];
|
|
67
|
+
}
|
|
68
|
+
const speaker = peer('speaker', 'latency-speaker', passphrase);
|
|
69
|
+
let audience;
|
|
70
|
+
const latencies = [];
|
|
71
|
+
const intervals = [];
|
|
72
|
+
const joinWaits = [];
|
|
73
|
+
try {
|
|
74
|
+
await speaker.waitFor(message => message.type === 'ready');
|
|
75
|
+
let previous = await nextBeacon(0);
|
|
76
|
+
for (const delay of [250, 5000]) {
|
|
77
|
+
await new Promise(resolve => setTimeout(resolve, Math.max(0, previous + delay - performance.now())));
|
|
78
|
+
audience = peer('audience', `latency-audience-${delay}`, passphrase);
|
|
79
|
+
await audience.waitFor(message => message.type === 'ready');
|
|
80
|
+
const joined = performance.now();
|
|
81
|
+
const discovery = audience.waitFor(message => message.type === 'speaker', 15000)
|
|
82
|
+
.then(message => ({ message, time: performance.now() }));
|
|
83
|
+
const arrival = await nextBeacon(latencies.length + 1);
|
|
84
|
+
const found = await discovery;
|
|
85
|
+
expect(found.message.data).toMatchObject({ user: 'latency-speaker', listening: false });
|
|
86
|
+
expect(arrival - previous).toBeGreaterThan(8500);
|
|
87
|
+
expect(arrival - previous).toBeLessThan(12000);
|
|
88
|
+
expect(arrival).toBeGreaterThan(joined);
|
|
89
|
+
intervals.push(arrival - previous);
|
|
90
|
+
joinWaits.push(arrival - joined);
|
|
91
|
+
const latency = found.time - arrival;
|
|
92
|
+
latencies.push(latency);
|
|
93
|
+
if (process.env.BIBLESYNC_LATENCY_BASELINE !== '1') {
|
|
94
|
+
expect(latency).toBeLessThan(500);
|
|
95
|
+
}
|
|
96
|
+
if (delay !== 5000) {
|
|
97
|
+
await audience.command('stop');
|
|
98
|
+
audience.child.disconnect();
|
|
99
|
+
audience = null;
|
|
100
|
+
}
|
|
101
|
+
previous = arrival;
|
|
102
|
+
}
|
|
103
|
+
console.log(`Beacon arrival-to-discovery latency (ms): ${latencies.map(n => n.toFixed(1)).join(', ')}`);
|
|
104
|
+
for (let i = 3; i <= 5; i++) {
|
|
105
|
+
const arrival = await nextBeacon(i);
|
|
106
|
+
expect(arrival - previous).toBeGreaterThan(8500);
|
|
107
|
+
expect(arrival - previous).toBeLessThan(12000);
|
|
108
|
+
expect(audience.events.filter(message => message.type === 'speakerRemoved')).toEqual([]);
|
|
109
|
+
intervals.push(arrival - previous);
|
|
110
|
+
previous = arrival;
|
|
111
|
+
}
|
|
112
|
+
console.log(`Beacon intervals (ms): ${intervals.map(n => n.toFixed(1)).join(', ')}`);
|
|
113
|
+
console.log(`Audience join-to-beacon waits (ms): ${joinWaits.map(n => n.toFixed(1)).join(', ')}`);
|
|
114
|
+
await speaker.command('stop');
|
|
115
|
+
await audience.waitFor(message => message.type === 'speakerRemoved', 35000);
|
|
116
|
+
const lifetime = performance.now() - previous;
|
|
117
|
+
expect(lifetime).toBeGreaterThan(27000);
|
|
118
|
+
expect(lifetime).toBeLessThan(35000);
|
|
119
|
+
console.log(`Last beacon-to-expiry (ms): ${lifetime.toFixed(1)}`);
|
|
120
|
+
} finally {
|
|
121
|
+
if (audience && audience.child.connected) await audience.command('stop').catch(() => {});
|
|
122
|
+
if (audience && audience.child.connected) audience.child.disconnect();
|
|
123
|
+
if (speaker.child.connected) await speaker.command('stop').catch(() => {});
|
|
124
|
+
if (speaker.child.connected) speaker.child.disconnect();
|
|
125
|
+
observer.close();
|
|
126
|
+
}
|
|
127
|
+
});
|
|
26
128
|
|
|
27
129
|
function peer(mode, name, passphrase, immediate = false, environment = {}) {
|
|
28
130
|
const child = fork(path.join(__dirname, 'fixtures', 'biblesync_peer.js'), [mode, name, passphrase, immediate ? 'immediate' : ''], {
|
|
@@ -265,3 +367,115 @@ integration('personal peers can explicitly follow one another in both directions
|
|
|
265
367
|
if (first.child.exitCode === null) first.child.kill();
|
|
266
368
|
}
|
|
267
369
|
});
|
|
370
|
+
|
|
371
|
+
integration('multiple speakers require separate approval and restart cleanly', async () => {
|
|
372
|
+
const passphrase = `nsi-test-${randomBytes(12).toString('hex')}`;
|
|
373
|
+
const audience = peer('audience', 'main-audience', passphrase);
|
|
374
|
+
const others = [];
|
|
375
|
+
try {
|
|
376
|
+
await audience.waitFor(message => message.type === 'ready');
|
|
377
|
+
const presenceOnly = peer('audience', 'presence-only', passphrase);
|
|
378
|
+
others.push(presenceOnly);
|
|
379
|
+
await presenceOnly.waitFor(message => message.type === 'ready');
|
|
380
|
+
await audience.waitFor(message => message.type === 'presence' && message.data.user === 'presence-only');
|
|
381
|
+
expect(audience.events.filter(message => message.type === 'speaker')).toEqual([]);
|
|
382
|
+
|
|
383
|
+
const first = peer('speaker', 'first-speaker', passphrase);
|
|
384
|
+
others.push(first);
|
|
385
|
+
await first.waitFor(message => message.type === 'ready');
|
|
386
|
+
const firstSpeaker = (await audience.waitFor(message => message.type === 'speaker')).data;
|
|
387
|
+
const second = peer('speaker', 'second-speaker', passphrase);
|
|
388
|
+
others.push(second);
|
|
389
|
+
await second.waitFor(message => message.type === 'ready');
|
|
390
|
+
const secondSpeaker = (await audience.waitFor(message => message.type === 'speaker')).data;
|
|
391
|
+
expect(firstSpeaker.listening).toBe(false);
|
|
392
|
+
expect(secondSpeaker.listening).toBe(false);
|
|
393
|
+
expect(firstSpeaker.speakerId).not.toBe(secondSpeaker.speakerId);
|
|
394
|
+
await audience.command('listen', { speakerId: firstSpeaker.speakerId, listening: true });
|
|
395
|
+
await first.command('send', { data: { bible: 'KJV', reference: 'John.3.16' } });
|
|
396
|
+
await second.command('send', { data: { bible: 'KJV', reference: 'John.3.17' } });
|
|
397
|
+
expect((await audience.waitFor(message => message.type === 'navigation')).data.speakerId)
|
|
398
|
+
.toBe(firstSpeaker.speakerId);
|
|
399
|
+
await audience.waitFor(message => message.type === 'diagnostic' && message.data.message.startsWith('sync: second-speaker'));
|
|
400
|
+
expect(audience.events.filter(message => message.type === 'navigation')).toEqual([]);
|
|
401
|
+
await audience.command('listen', { speakerId: firstSpeaker.speakerId, listening: false });
|
|
402
|
+
await audience.command('listen', { speakerId: secondSpeaker.speakerId, listening: true });
|
|
403
|
+
await second.command('send', { data: { bible: 'KJV', reference: 'John.3.18' } });
|
|
404
|
+
expect((await audience.waitFor(message => message.type === 'navigation')).data.speakerId)
|
|
405
|
+
.toBe(secondSpeaker.speakerId);
|
|
406
|
+
await audience.command('stop');
|
|
407
|
+
audience.events.length = 0;
|
|
408
|
+
await second.command('send', { data: { bible: 'KJV', reference: 'John.3.19' } });
|
|
409
|
+
await new Promise(resolve => setTimeout(resolve, 300));
|
|
410
|
+
expect(audience.events).toEqual([]);
|
|
411
|
+
await audience.command('start', { mode: 'audience' });
|
|
412
|
+
const rediscovered = (await audience.waitFor(message => message.type === 'speaker', 15000)).data;
|
|
413
|
+
expect(rediscovered.listening).toBe(false);
|
|
414
|
+
} finally {
|
|
415
|
+
for (const other of others) {
|
|
416
|
+
if (other.child.connected) await other.command('stop').catch(() => {});
|
|
417
|
+
if (other.child.connected) other.child.disconnect();
|
|
418
|
+
if (other.child.exitCode === null) other.child.kill();
|
|
419
|
+
}
|
|
420
|
+
if (audience.child.connected) await audience.command('stop').catch(() => {});
|
|
421
|
+
if (audience.child.connected) audience.child.disconnect();
|
|
422
|
+
if (audience.child.exitCode === null) audience.child.kill();
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
integration('busy receive queue reports overflow and permits restart', async () => {
|
|
427
|
+
const passphrase = `nsi-test-${randomBytes(12).toString('hex')}`;
|
|
428
|
+
const audience = peer('audience', 'busy-audience', passphrase);
|
|
429
|
+
let speaker;
|
|
430
|
+
try {
|
|
431
|
+
await audience.waitFor(message => message.type === 'ready');
|
|
432
|
+
speaker = peer('speaker', 'busy-speaker', passphrase);
|
|
433
|
+
await speaker.waitFor(message => message.type === 'ready');
|
|
434
|
+
const discovery = await audience.waitFor(message => message.type === 'speaker');
|
|
435
|
+
await audience.command('listen', { speakerId: discovery.data.speakerId, listening: true });
|
|
436
|
+
const block = audience.command('block', { duration: 3500 });
|
|
437
|
+
await audience.waitFor(message => message.type === 'blocking');
|
|
438
|
+
await speaker.command('burst', { count: 500, interval: 4 });
|
|
439
|
+
await block;
|
|
440
|
+
const failure = await audience.waitFor(message => message.type === 'error' && message.message.includes('event queue overflow'));
|
|
441
|
+
expect(failure.message).toContain('session stopped');
|
|
442
|
+
await audience.command('start', { mode: 'audience' });
|
|
443
|
+
await audience.command('stop');
|
|
444
|
+
} finally {
|
|
445
|
+
if (speaker && speaker.child.connected) await speaker.command('stop').catch(() => {});
|
|
446
|
+
if (speaker && speaker.child.connected) speaker.child.disconnect();
|
|
447
|
+
if (speaker && speaker.child.exitCode === null) speaker.child.kill();
|
|
448
|
+
if (audience.child.connected) await audience.command('stop').catch(() => {});
|
|
449
|
+
if (audience.child.connected) audience.child.disconnect();
|
|
450
|
+
if (audience.child.exitCode === null) audience.child.kill();
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
integration('stop remains responsive while packets arrive and suppresses later events', async () => {
|
|
455
|
+
const passphrase = `nsi-test-${randomBytes(12).toString('hex')}`;
|
|
456
|
+
const audience = peer('audience', 'stop-audience', passphrase);
|
|
457
|
+
let speaker;
|
|
458
|
+
try {
|
|
459
|
+
await audience.waitFor(message => message.type === 'ready');
|
|
460
|
+
speaker = peer('speaker', 'stop-speaker', passphrase);
|
|
461
|
+
await speaker.waitFor(message => message.type === 'ready');
|
|
462
|
+
const discovery = await audience.waitFor(message => message.type === 'speaker');
|
|
463
|
+
await audience.command('listen', { speakerId: discovery.data.speakerId, listening: true });
|
|
464
|
+
const burst = speaker.command('burst', { count: 300, interval: 4 });
|
|
465
|
+
await audience.waitFor(message => message.type === 'navigation');
|
|
466
|
+
const start = performance.now();
|
|
467
|
+
await audience.command('stop');
|
|
468
|
+
expect(performance.now() - start).toBeLessThan(2000);
|
|
469
|
+
audience.events.length = 0;
|
|
470
|
+
await burst;
|
|
471
|
+
await new Promise(resolve => setTimeout(resolve, 300));
|
|
472
|
+
expect(audience.events).toEqual([]);
|
|
473
|
+
} finally {
|
|
474
|
+
if (speaker && speaker.child.connected) await speaker.command('stop').catch(() => {});
|
|
475
|
+
if (speaker && speaker.child.connected) speaker.child.disconnect();
|
|
476
|
+
if (speaker && speaker.child.exitCode === null) speaker.child.kill();
|
|
477
|
+
if (audience.child.connected) await audience.command('stop').catch(() => {});
|
|
478
|
+
if (audience.child.connected) audience.child.disconnect();
|
|
479
|
+
if (audience.child.exitCode === null) audience.child.kill();
|
|
480
|
+
}
|
|
481
|
+
});
|
|
@@ -64,7 +64,11 @@ test('prepares identical output for LF and CRLF checkouts without changing upstr
|
|
|
64
64
|
path.join(path.dirname(output), 'biblesync.hh')
|
|
65
65
|
]);
|
|
66
66
|
expect(lfOutput[0][1]).toContain('if (::bind(server_fd, (struct sockaddr*)&server,');
|
|
67
|
+
expect(lfOutput[0][1]).toContain('ReceiveInternal(false)');
|
|
68
|
+
expect(lfOutput[0][1]).toContain('if (!maintenance)\n return TRUE;');
|
|
69
|
+
expect(lfOutput[0][1]).toContain('packets < 64');
|
|
67
70
|
expect(lfOutput[1][1]).toContain('bool winsock_initialized = false;');
|
|
71
|
+
expect(lfOutput[1][1]).toContain('static int ReceivePackets(void *myself);');
|
|
68
72
|
|
|
69
73
|
fs.writeFileSync.mockClear();
|
|
70
74
|
const crlfOutput = prepare(
|
|
@@ -74,6 +78,16 @@ test('prepares identical output for LF and CRLF checkouts without changing upstr
|
|
|
74
78
|
expect(crlfOutput).toEqual(lfOutput);
|
|
75
79
|
});
|
|
76
80
|
|
|
81
|
+
test.each([
|
|
82
|
+
['source', upstream => upstream.replace('int BibleSync::ReceiveInternal()', 'int BibleSync::changed()')],
|
|
83
|
+
['header', upstream => upstream.replace(' int ReceiveInternal();', ' int changed();')]
|
|
84
|
+
])('rejects unexpected receive %s before writing generated files', (part, mutate) => {
|
|
85
|
+
const source = part === 'source' ? mutate(upstreamSource) : upstreamSource;
|
|
86
|
+
const header = part === 'header' ? mutate(upstreamHeader) : upstreamHeader;
|
|
87
|
+
expect(() => prepare(source, header)).toThrow('Unexpected BibleSync 2.2.0');
|
|
88
|
+
expect(fs.writeFileSync).not.toHaveBeenCalled();
|
|
89
|
+
});
|
|
90
|
+
|
|
77
91
|
test('still rejects unexpected upstream source', () => {
|
|
78
92
|
const alteredSource = upstreamSource.replace('server_fd = client_fd = -1;', 'server_fd = -1;');
|
|
79
93
|
expect(() => prepare(alteredSource, upstreamHeader)).toThrow('Unexpected BibleSync 2.2.0 source');
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/* This file is part of node-bible-sync, licensed under GPL-2.0-or-later. */
|
|
2
|
+
|
|
3
|
+
#include <chrono>
|
|
4
|
+
#include <iostream>
|
|
5
|
+
#include <string>
|
|
6
|
+
#include <thread>
|
|
7
|
+
#include <biblesync.hh>
|
|
8
|
+
#undef min
|
|
9
|
+
|
|
10
|
+
thread_local int node_bible_sync_uuid_error = 0;
|
|
11
|
+
static int discovered = 0;
|
|
12
|
+
static int removed = 0;
|
|
13
|
+
static int navigation = 0;
|
|
14
|
+
static int errors = 0;
|
|
15
|
+
static std::string speakerId;
|
|
16
|
+
|
|
17
|
+
static void audienceEvent(char cmd, std::string id, std::string, std::string,
|
|
18
|
+
std::string, std::string, std::string, std::string, std::string)
|
|
19
|
+
{
|
|
20
|
+
if (cmd == 'S') { ++discovered; speakerId = id; }
|
|
21
|
+
if (cmd == 'D') ++removed;
|
|
22
|
+
if (cmd == 'N') ++navigation;
|
|
23
|
+
if (cmd == 'E') ++errors;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
static void speakerEvent(char cmd, std::string, std::string, std::string,
|
|
27
|
+
std::string, std::string, std::string, std::string, std::string)
|
|
28
|
+
{
|
|
29
|
+
if (cmd == 'E') ++errors;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
static bool check(bool condition, const char *message)
|
|
33
|
+
{
|
|
34
|
+
if (!condition) std::cerr << message << std::endl;
|
|
35
|
+
return condition;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
int main()
|
|
39
|
+
{
|
|
40
|
+
const std::string passphrase = "packet-timer-test";
|
|
41
|
+
BibleSync audience("timer test", "1", "audience");
|
|
42
|
+
BibleSync speaker("timer test", "1", "speaker");
|
|
43
|
+
if (!check(audience.setMode(BSP_MODE_AUDIENCE, audienceEvent, passphrase) == BSP_MODE_AUDIENCE,
|
|
44
|
+
"audience startup failed") ||
|
|
45
|
+
!check(speaker.setMode(BSP_MODE_SPEAKER, speakerEvent, passphrase) == BSP_MODE_SPEAKER,
|
|
46
|
+
"speaker startup failed")) return 1;
|
|
47
|
+
for (int i = 0; i < 100 && discovered == 0; ++i) {
|
|
48
|
+
BibleSync::ReceivePackets(&audience);
|
|
49
|
+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
50
|
+
}
|
|
51
|
+
if (!check(discovered == 1, "initial beacon not discovered")) return 1;
|
|
52
|
+
|
|
53
|
+
for (int i = 0; i < 29; ++i) BibleSync::Receive(&audience);
|
|
54
|
+
for (int i = 0; i < 1000; ++i) {
|
|
55
|
+
BibleSync::ReceivePackets(&speaker);
|
|
56
|
+
BibleSync::ReceivePackets(&audience);
|
|
57
|
+
}
|
|
58
|
+
if (!check(removed == 0, "packet polls aged speaker")) return 1;
|
|
59
|
+
BibleSync::Receive(&audience);
|
|
60
|
+
if (!check(removed == 1, "speaker did not expire after 30 maintenance calls")) return 1;
|
|
61
|
+
for (int i = 0; i < 100; ++i) {
|
|
62
|
+
BibleSync::ReceivePackets(&speaker);
|
|
63
|
+
BibleSync::ReceivePackets(&audience);
|
|
64
|
+
}
|
|
65
|
+
if (!check(discovered == 1, "packet polls transmitted a beacon")) return 1;
|
|
66
|
+
for (int i = 0; i < 9; ++i) BibleSync::Receive(&speaker);
|
|
67
|
+
BibleSync::ReceivePackets(&audience);
|
|
68
|
+
if (!check(discovered == 1, "beacon transmitted before tenth maintenance call")) return 1;
|
|
69
|
+
BibleSync::Receive(&speaker);
|
|
70
|
+
for (int i = 0; i < 100 && discovered == 1; ++i) {
|
|
71
|
+
BibleSync::ReceivePackets(&audience);
|
|
72
|
+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
73
|
+
}
|
|
74
|
+
if (!check(discovered == 2, "tenth maintenance call did not transmit beacon")) return 1;
|
|
75
|
+
|
|
76
|
+
audience.listenToSpeaker(true, speakerId);
|
|
77
|
+
for (int i = 0; i < 100; ++i) {
|
|
78
|
+
if (!check(speaker.Transmit("KJV", "John.3.16") == BSP_XMIT_OK, "send failed")) return 1;
|
|
79
|
+
}
|
|
80
|
+
BibleSync::ReceivePackets(&audience);
|
|
81
|
+
if (!check(navigation > 0 && navigation <= 64, "receive batch is not bounded")) return 1;
|
|
82
|
+
for (int i = 0; i < 10 && navigation < 100; ++i) BibleSync::ReceivePackets(&audience);
|
|
83
|
+
if (!check(navigation == 100, "receive batch did not drain queued navigation")) return 1;
|
|
84
|
+
if (!check(errors == 0, "unexpected network error")) return 1;
|
|
85
|
+
audience.setMode(BSP_MODE_DISABLE);
|
|
86
|
+
speaker.setMode(BSP_MODE_DISABLE);
|
|
87
|
+
std::cout << "packet-only timers and bounded batch verified" << std::endl;
|
|
88
|
+
return 0;
|
|
89
|
+
}
|
|
@@ -35,6 +35,15 @@ process.on('message', async command => {
|
|
|
35
35
|
if (command.type === 'start') await session.start(command.mode);
|
|
36
36
|
else if (command.type === 'send') await session.sendNavigation(command.data);
|
|
37
37
|
else if (command.type === 'listen') await session.setSpeakerListening(command.speakerId, command.listening);
|
|
38
|
+
else if (command.type === 'burst') {
|
|
39
|
+
for (let i = 0; i < command.count; i++) {
|
|
40
|
+
await session.sendNavigation({ bible: 'KJV', reference: 'John.3.16' });
|
|
41
|
+
await new Promise(resolve => setTimeout(resolve, command.interval));
|
|
42
|
+
}
|
|
43
|
+
} else if (command.type === 'block') {
|
|
44
|
+
await new Promise(resolve => process.send({ type: 'blocking' }, resolve));
|
|
45
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, command.duration);
|
|
46
|
+
}
|
|
38
47
|
else if (command.type === 'stop') await session.stop();
|
|
39
48
|
else throw new Error('Unknown command');
|
|
40
49
|
process.send({ type: 'result', id: command.id });
|