Skip to content

Commit 7eb2809

Browse files
committed
Use truncate & download events instead of timeouts in test
1 parent 4987ecb commit 7eb2809

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

test/replicate.js

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2392,7 +2392,7 @@ test('remote contiguous length', async function (t) {
23922392

23932393
await b.get(0)
23942394

2395-
await new Promise((resolve) => setTimeout(resolve, 500))
2395+
await eventFlush()
23962396

23972397
t.is(a.remoteContiguousLength, 1)
23982398
})
@@ -2417,13 +2417,13 @@ test('remote contiguous length - fully contiguous only', async function (t) {
24172417

24182418
await b.get(0)
24192419

2420-
await new Promise((resolve) => setTimeout(resolve, 500))
2420+
await eventFlush()
24212421

24222422
t.is(a.remoteContiguousLength, 0, 'remoteContiguousLength didnt update')
24232423
t.is(b.contiguousLength, 1, 'b has 1st block')
24242424

24252425
await b.get(1)
2426-
await new Promise((resolve) => setTimeout(resolve, 500))
2426+
await eventFlush()
24272427

24282428
t.is(b.contiguousLength, 2, 'b all blocks')
24292429
t.is(a.remoteContiguousLength, 2, 'remoteContiguousLength updates')
@@ -2445,13 +2445,15 @@ test('remote contiguous length - updates on truncate', async function (t) {
24452445
await b.get(0)
24462446
await b.get(1)
24472447

2448-
await new Promise((resolve) => setTimeout(resolve, 500))
2448+
await eventFlush()
24492449

24502450
t.is(a.remoteContiguousLength, 2)
24512451

2452+
const truncateReceived = new Promise((resolve) => b.on('truncate', resolve))
24522453
await a.truncate(a.length - 1)
24532454

2454-
await new Promise((resolve) => setTimeout(resolve, 500))
2455+
await truncateReceived
2456+
24552457
t.is(a.remoteContiguousLength, 1)
24562458
t.is(b.contiguousLength, 1)
24572459
})
@@ -2469,24 +2471,45 @@ test('remote contiguous length - event fires after truncating', async function (
24692471

24702472
replicate(a, b, t)
24712473

2474+
const doneUploading = new Promise((resolve) => {
2475+
let uploads = 0
2476+
let bContigAtStart = b.contiguousLength
2477+
b.on('download', () => {
2478+
uploads++
2479+
if (uploads === a.length - bContigAtStart) resolve()
2480+
})
2481+
})
24722482
await b.download({ start: 0, end: a.length })
24732483

2474-
await new Promise((resolve) => setTimeout(resolve, 500))
2484+
await doneUploading
2485+
await eventFlush() // To let `a` update `remoteContiguousLength`
24752486

24762487
t.is(a.remoteContiguousLength, 3)
24772488

2489+
const truncateReceived = new Promise((resolve) => b.on('truncate', resolve))
24782490
await a.truncate(1)
24792491

2480-
await new Promise((resolve) => setTimeout(resolve, 500))
2492+
await truncateReceived
2493+
24812494
t.is(a.remoteContiguousLength, 1)
24822495
t.is(b.contiguousLength, 1)
24832496

24842497
b.on('remote-contiguous-length', () => t.pass('fires after truncating'))
24852498
await a.append(['a2v2', 'a3v2'])
24862499

2500+
const doneUploading2 = new Promise((resolve) => {
2501+
let uploads = 0
2502+
let bContigAtStart = b.contiguousLength
2503+
b.on('download', () => {
2504+
uploads++
2505+
if (uploads === a.length - bContigAtStart) resolve()
2506+
})
2507+
})
24872508
await b.download({ start: 0, end: a.length })
24882509

2489-
await new Promise((resolve) => setTimeout(resolve, 500))
2510+
await doneUploading2
2511+
await eventFlush() // To let `a` update `remoteContiguousLength`
2512+
24902513
t.is(a.remoteContiguousLength, 3)
24912514
t.is(b.contiguousLength, 3)
24922515
})
@@ -2505,9 +2528,18 @@ test('remote contiguous length - persists', async function (t) {
25052528

25062529
replicate(a, b, t)
25072530

2531+
const doneUploading = new Promise((resolve) => {
2532+
let uploads = 0
2533+
b.on('download', () => {
2534+
uploads++
2535+
if (uploads === a.length) resolve()
2536+
})
2537+
})
2538+
25082539
await b.download({ start: 0, end: a.length })
25092540

2510-
await new Promise((resolve) => setTimeout(resolve, 500))
2541+
await doneUploading
2542+
await eventFlush() // To let `a` update `remoteContiguousLength`
25112543

25122544
t.is(a.remoteContiguousLength, 3)
25132545

0 commit comments

Comments
 (0)