Skip to content

Commit

Permalink
test: add ws reconnect tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 7, 2020
1 parent ccda1bc commit f0a236f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/koishi-core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface AppOptions {
database?: DatabaseConfig
nickname?: string | string[]
retryTimes?: number
retryTimeout?: number
retryInterval?: number
maxMiddlewares?: number
commandPrefix?: string | string[]
quickOperationTimeout?: number
Expand Down Expand Up @@ -77,7 +77,7 @@ function createLeadingRE (patterns: string[], prefix = '', suffix = '') {

const defaultOptions: AppOptions = {
maxMiddlewares: 64,
retryTimeout: 3000,
retryInterval: 5000,
}

export enum Status { closed, opening, open, closing }
Expand Down
11 changes: 7 additions & 4 deletions packages/koishi-core/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ export class WsClient extends Server {
const connect = (resolve: () => void, reject: (reason: Error) => void) => {
this.debug('websocket client opening')
const headers: Record<string, string> = {}
const { token, server, retryTimeout, retryTimes } = this.app.options
const { token, server, retryInterval, retryTimes } = this.app.options
if (token) headers.Authorization = `Bearer ${token}`
this.socket = new WebSocket(server, { headers })

Expand All @@ -305,13 +305,15 @@ export class WsClient extends Server {
if (!this.isListening || code === 1005) return

const message = `failed to connect to ${server}`
if (!retryTimeout || this._retryCount >= retryTimes) {
if (!retryInterval || this._retryCount >= retryTimes) {
return reject(new Error(message))
}

this._retryCount++
this.app?.logger('koishi').warn(`${message}, will retry in ${ms(retryTimeout)}...`)
setTimeout(() => connect(resolve, reject), retryTimeout)
this.debug(`${message}, will retry in ${ms(retryInterval)}...`)
setTimeout(() => {
if (this.isListening) connect(resolve, reject)
}, retryInterval)
})

this.socket.once('open', () => {
Expand Down Expand Up @@ -358,6 +360,7 @@ export class WsClient extends Server {

_close () {
this.socket.close()
this._retryCount = 0
this.debug('websocket client closed')
}
}
Expand Down
13 changes: 10 additions & 3 deletions packages/koishi-core/tests/ws-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ let app1: App, app2: App

beforeAll(async () => {
server = await createWsServer()
app1 = server.createBoundApp()
app2 = server.createBoundApp({ selfId: BASE_SELF_ID + 1 })
app1 = server.createBoundApp({
retryTimes: 1,
retryInterval: 100,
})
app2 = server.createBoundApp({
selfId: BASE_SELF_ID + 1,
})
})

afterAll(() => server.close())
Expand All @@ -28,8 +33,10 @@ describe('WebSocket Server', () => {
await expect(app1.start()).rejects.toHaveProperty('message', 'authorization failed')
app1.options.token = 'token'
await expect(app1.start()).resolves.toBeUndefined()
await server.close()
await expect(app1.start()).rejects.toHaveProperty('message')
server.token = null
await app1.stop()
server.open()
await expect(app1.start()).resolves.toBeUndefined()
})

Expand Down
3 changes: 0 additions & 3 deletions packages/test-utils/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,6 @@ export class WsServer extends MockedServer {

async close () {
await Promise.all(this.appList.map(app => app.stop()))
for (const client of this.server.clients) {
client.close()
}
this.server.close()
}
}

0 comments on commit f0a236f

Please sign in to comment.