final adjustments and now even the flaky tests that depend on others's relay should pass most of the time.

This commit is contained in:
fiatjaf
2023-12-19 10:01:52 -03:00
parent 804403f574
commit 8840c4d8e2
5 changed files with 21 additions and 10 deletions

View File

@@ -11,8 +11,9 @@ jobs:
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3
- uses: oven-sh/setup-bun@v1 - uses: oven-sh/setup-bun@v1
- uses: extractions/setup-just@v1
- run: bun i - run: bun i
- run: bun test --timeout 20000 - run: just test
format: format:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:

View File

@@ -5,7 +5,7 @@ build:
bun run build.js bun run build.js
test: test:
bun test bun test --timeout 20000
test-only file: test-only file:
bun test {{file}} bun test {{file}}

View File

@@ -1,4 +1,4 @@
import { test, expect } from 'bun:test' import { test, expect, afterAll } from 'bun:test'
import { finishEvent, type Event } from './event.ts' import { finishEvent, type Event } from './event.ts'
import { generatePrivateKey, getPublicKey } from './keys.ts' import { generatePrivateKey, getPublicKey } from './keys.ts'
@@ -8,6 +8,10 @@ let pool = new SimplePool()
let relays = ['wss://relay.damus.io/', 'wss://relay.nostr.bg/', 'wss://nos.lol', 'wss://public.relaying.io'] let relays = ['wss://relay.damus.io/', 'wss://relay.nostr.bg/', 'wss://nos.lol', 'wss://public.relaying.io']
afterAll(() => {
pool.close([...relays, 'wss://offchain.pub', 'wss://eden.nostr.land'])
})
test('removing duplicates when querying', async () => { test('removing duplicates when querying', async () => {
let priv = generatePrivateKey() let priv = generatePrivateKey()
let pub = getPublicKey(priv) let pub = getPublicKey(priv)
@@ -33,7 +37,6 @@ test('removing duplicates when querying', async () => {
) )
await Promise.any(pool.publish(relays, event)) await Promise.any(pool.publish(relays, event))
await new Promise(resolve => setTimeout(resolve, 1500)) await new Promise(resolve => setTimeout(resolve, 1500))
expect(received).toHaveLength(1) expect(received).toHaveLength(1)

View File

@@ -34,6 +34,12 @@ export class SimplePool {
return relay return relay
} }
close(relays: string[]) {
relays.map(normalizeURL).forEach(url => {
this.relays.get(url)?.close()
})
}
subscribeMany(relays: string[], filters: Filter[], params: SubscribeManyParams): SubCloser { subscribeMany(relays: string[], filters: Filter[], params: SubscribeManyParams): SubCloser {
if (this.trackRelays) { if (this.trackRelays) {
params.receivedEvent = (relay: Relay, id: string) => { params.receivedEvent = (relay: Relay, id: string) => {

View File

@@ -21,7 +21,7 @@ export class Relay {
public onnotice: (msg: string) => void = msg => console.debug(`NOTICE from ${this.url}: ${msg}`) public onnotice: (msg: string) => void = msg => console.debug(`NOTICE from ${this.url}: ${msg}`)
public baseEoseTimeout: number = 4400 public baseEoseTimeout: number = 4400
public connectionTimeout: number = 8800 public connectionTimeout: number = 4400
private connectionTimeoutHandle: ReturnType<typeof setTimeout> | undefined private connectionTimeoutHandle: ReturnType<typeof setTimeout> | undefined
private connectionPromise: Promise<void> | undefined private connectionPromise: Promise<void> | undefined
@@ -215,18 +215,21 @@ export class Relay {
} }
public async send(message: string) { public async send(message: string) {
this.ws?.send(message) if (!this.connectionPromise) throw new Error('sending on closed connection')
this.connectionPromise.then(() => {
this.ws?.send(message)
})
} }
public async auth(signAuthEvent: (authEvent: EventTemplate) => Promise<void>) { public async auth(signAuthEvent: (authEvent: EventTemplate) => Promise<void>) {
if (!this.challenge) throw new Error("can't perform auth, no challenge was received") if (!this.challenge) throw new Error("can't perform auth, no challenge was received")
const evt = nip42.makeAuthEvent(this.url, this.challenge) const evt = nip42.makeAuthEvent(this.url, this.challenge)
await Promise.all([signAuthEvent(evt), this.connect()]) await signAuthEvent(evt)
this.send('["AUTH",' + JSON.stringify(evt) + ']') this.send('["AUTH",' + JSON.stringify(evt) + ']')
} }
public async publish(event: Event): Promise<string> { public async publish(event: Event): Promise<string> {
await this.connect()
const ret = new Promise<string>((resolve, reject) => { const ret = new Promise<string>((resolve, reject) => {
this.openEventPublishes.set(event.id, { resolve, reject }) this.openEventPublishes.set(event.id, { resolve, reject })
}) })
@@ -235,7 +238,6 @@ export class Relay {
} }
public async count(filters: Filter[], params: { id?: string | null }): Promise<number> { public async count(filters: Filter[], params: { id?: string | null }): Promise<number> {
await this.connect()
this.serial++ this.serial++
const id = params?.id || 'count:' + this.serial const id = params?.id || 'count:' + this.serial
const ret = new Promise<number>((resolve, reject) => { const ret = new Promise<number>((resolve, reject) => {
@@ -246,7 +248,6 @@ export class Relay {
} }
public async subscribe(filters: Filter[], params: Partial<SubscriptionParams>): Promise<Subscription> { public async subscribe(filters: Filter[], params: Partial<SubscriptionParams>): Promise<Subscription> {
await this.connect()
const subscription = this.prepareSubscription(filters, params) const subscription = this.prepareSubscription(filters, params)
subscription.fire() subscription.fire()
return subscription return subscription