improvements and fixes on pool.

This commit is contained in:
fiatjaf
2023-02-09 11:58:39 -03:00
parent 7ff97b5488
commit ff3bf4a51c
6 changed files with 182 additions and 123 deletions

View File

@@ -19,50 +19,23 @@ let relays = [
'wss://nostr.zebedee.cloud/'
]
beforeAll(async () => {
Promise.all(
relays.map(relay => {
try {
let r = pool.ensureRelay(relay)
return r.connect()
} catch (err) {
/***/
}
})
)
})
afterAll(async () => {
relays.forEach(relay => {
try {
let r = pool.ensureRelay(relay)
r.close()
} catch (err) {
/***/
}
})
await pool.close([...relays, 'wss://nostr-relay.untethr.me'])
})
test('removing duplicates when querying', async () => {
let priv = generatePrivateKey()
let pub = getPublicKey(priv)
let subs = pool.sub(relays, [
{
authors: [pub]
}
])
let sub = pool.sub(relays, [{authors: [pub]}])
let received = []
subs.forEach(sub =>
sub.on('event', event => {
// this should be called only once even though we're listening
// to multiple relays because the events will be catched and
// deduplicated efficiently (without even being parsed)
received.push(event)
})
)
sub.on('event', event => {
// this should be called only once even though we're listening
// to multiple relays because the events will be catched and
// deduplicated efficiently (without even being parsed)
received.push(event)
})
let event = {
pubkey: pub,
@@ -81,25 +54,22 @@ test('removing duplicates when querying', async () => {
expect(received).toHaveLength(1)
})
test('removing duplicates correctly when double querying', async () => {
test('same with double querying', async () => {
let priv = generatePrivateKey()
let pub = getPublicKey(priv)
let subs1 = pool.sub(relays, [{authors: [pub]}])
let subs2 = pool.sub(relays, [{authors: [pub]}])
let sub1 = pool.sub(relays, [{authors: [pub]}])
let sub2 = pool.sub(relays, [{authors: [pub]}])
let received = []
subs1.forEach(sub =>
sub.on('event', event => {
received.push(event)
})
)
subs2.forEach(sub =>
sub.on('event', event => {
received.push(event)
})
)
sub1.on('event', event => {
received.push(event)
})
sub2.on('event', event => {
received.push(event)
})
let event = {
pubkey: pub,
@@ -117,3 +87,37 @@ test('removing duplicates correctly when double querying', async () => {
expect(received).toHaveLength(2)
})
test('get()', async () => {
let event = await pool.get(relays, {
ids: ['d7dd5eb3ab747e16f8d0212d53032ea2a7cadef53837e5a6c66d42849fcb9027']
})
expect(event).toHaveProperty(
'id',
'd7dd5eb3ab747e16f8d0212d53032ea2a7cadef53837e5a6c66d42849fcb9027'
)
})
test('list()', async () => {
let events = await pool.list(
[...relays, 'wss://offchain.pub', 'wss://eden.nostr.land'],
[
{
authors: [
'3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d'
],
kinds: [1],
limit: 2
}
]
)
// the actual received number will be greater than 2, but there will be no duplicates
expect(events.length).toEqual(
events
.map(evt => evt.id)
.reduce((acc, n) => (acc.indexOf(n) !== -1 ? acc : [...acc, n]), [])
.length
)
})