Fix Sync timeout and events tests

This commit is contained in:
haad 2023-07-29 09:23:20 +03:00
parent 5032415e01
commit b105c84f99

View File

@ -545,62 +545,26 @@ describe('Sync protocol', function () {
})
})
describe('Timeouts', () => {
let sync1, sync2
let log1, log2
const timeoutTime = 1 // 1 millisecond
before(async () => {
log1 = await Log(testIdentity1, { logId: 'synclog5' })
log2 = await Log(testIdentity2, { logId: 'synclog5' })
sync1 = await Sync({ ipfs: ipfs1, log: log1, timeout: timeoutTime })
sync2 = await Sync({ ipfs: ipfs2, log: log2, start: false, timeout: timeoutTime })
await log1.append('hello1')
})
after(async () => {
if (sync1) {
await sync1.stop()
}
if (sync2) {
await sync2.stop()
}
})
it('emits an error when connecting to peer was cancelled due to timeout', async () => {
let err = null
const onError = (error) => {
err = error
}
sync1.events.on('error', onError)
sync2.events.on('error', onError)
await sync2.start()
await waitFor(() => err !== null, () => true)
notStrictEqual(err, null)
strictEqual(err.type, 'aborted')
strictEqual(err.message, 'The operation was aborted')
})
})
describe('Events', () => {
let sync1, sync2
let joinEventFired = false
let leaveEventFired = false
let errorEventFired = false
let err
let receivedHeads = []
let joiningPeerId
let leavingPeerId
before(async () => {
await ipfs1.stop()
await ipfs2.stop()
ipfs1 = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
ipfs2 = await IPFS.create({ ...config.daemon2, repo: './ipfs2' })
peerId1 = (await ipfs1.id()).id
peerId2 = (await ipfs2.id()).id
await connectPeers(ipfs1, ipfs2)
const log1 = await Log(testIdentity1, { logId: 'synclog3' })
const log2 = await Log(testIdentity2, { logId: 'synclog3' })
@ -615,25 +579,14 @@ describe('Sync protocol', function () {
leavingPeerId = peerId
}
const onError = (e) => {
errorEventFired = true
err = e.toString()
}
const onSynced = (bytes) => {
sync2.events.emit('error', new Error('Sync Error'))
}
await log1.append('hello!')
sync1 = await Sync({ ipfs: ipfs1, log: log1, onSynced })
sync2 = await Sync({ ipfs: ipfs2, log: log2, onSynced })
sync1 = await Sync({ ipfs: ipfs1, log: log1 })
sync2 = await Sync({ ipfs: ipfs2, log: log2 })
sync1.events.on('join', onJoin)
sync1.events.on('leave', onLeave)
sync2.events.on('error', onError)
await waitFor(() => joinEventFired, () => true)
await waitFor(() => errorEventFired, () => true)
await sync2.stop()
@ -667,10 +620,61 @@ describe('Sync protocol', function () {
const { id } = await ipfs2.id()
strictEqual(String(leavingPeerId), String(id))
})
})
it('emits an \'error\' event', () => {
strictEqual(errorEventFired, true)
strictEqual(err, 'Error: Sync Error')
describe('Timeouts', () => {
let sync1, sync2
let log1, log2
const timeoutTime = 1 // 1 millisecond
before(async () => {
await ipfs1.stop()
await ipfs2.stop()
ipfs1 = await IPFS.create({ ...config.daemon1, repo: './ipfs1' })
ipfs2 = await IPFS.create({ ...config.daemon2, repo: './ipfs2' })
peerId1 = (await ipfs1.id()).id
peerId2 = (await ipfs2.id()).id
await connectPeers(ipfs1, ipfs2)
log1 = await Log(testIdentity1, { logId: 'synclog5' })
log2 = await Log(testIdentity2, { logId: 'synclog5' })
})
after(async () => {
if (sync1) {
await sync1.stop()
}
if (sync2) {
await sync2.stop()
}
})
it('emits an error when connecting to peer was cancelled due to timeout', async () => {
let err = null
const onError = (error) => {
err = error
}
sync1 = await Sync({ ipfs: ipfs1, log: log1, timeout: timeoutTime })
sync2 = await Sync({ ipfs: ipfs2, log: log2, start: false, timeout: timeoutTime })
sync1.events.on('error', onError)
sync2.events.on('error', onError)
await log1.append('hello1')
await sync2.start()
await waitFor(() => err !== null, () => true)
notStrictEqual(err, null)
strictEqual(err.type, 'aborted')
strictEqual(err.message, 'The operation was aborted')
})
})
})