Skip to content

Commit

Permalink
test spaces and other special chars
Browse files Browse the repository at this point in the history
  • Loading branch information
tudor committed Feb 12, 2025
1 parent f32910b commit cb70d90
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
10 changes: 9 additions & 1 deletion packages/pglite/src/pglite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,15 @@ export class PGlite
this.#notifyListeners.set(pgChannel, new Set())
}
this.#notifyListeners.get(pgChannel)!.add(callback)
await this.exec(`LISTEN ${channel}`)
try {
await this.exec(`LISTEN ${channel}`)
} catch (e) {
this.#notifyListeners.get(pgChannel)!.delete(callback)
if (this.#notifyListeners.get(pgChannel)?.size === 0) {
this.#notifyListeners.delete(pgChannel)
}
throw e
}
return async () => {
await this.unlisten(pgChannel, callback)
}
Expand Down
47 changes: 45 additions & 2 deletions packages/pglite/tests/notify.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, expect, vi } from 'vitest'
import { PGlite } from '../dist/index.js'
import { expectToThrowAsync } from './test-utils.js'

describe('notify API', () => {
it('notify', async () => {
Expand Down Expand Up @@ -42,7 +43,7 @@ describe('notify API', () => {
await new Promise((resolve) => setTimeout(resolve, 1000))
})

it('check notify case sensitivity as Postgresql', async () => {
it('check notify case sensitivity + special chars as Postgresql', async () => {
const pg = new PGlite()

const allLower1 = vi.fn()
Expand Down Expand Up @@ -73,16 +74,43 @@ describe('notify API', () => {
await pg.listen('testNotCalled2', caseSensitive3)
await pg.query(`NOTIFY "testNotCalled2", 'paYloAd2'`)

const quotedWithSpaces = vi.fn()
await pg.listen('"Quoted Channel With Spaces"', quotedWithSpaces)
await pg.query(`NOTIFY "Quoted Channel With Spaces", 'payload1'`)

const unquotedWithSpaces = vi.fn()
await expectToThrowAsync(
pg.listen('Unquoted Channel With Spaces', unquotedWithSpaces),
)
await expectToThrowAsync(
pg.query(`NOTIFY Unquoted Channel With Spaces, 'payload1'`),
)

const otherCharsWithQuotes = vi.fn()
await pg.listen('"test&me"', otherCharsWithQuotes)
await pg.query(`NOTIFY "test&me", 'paYloAd2'`)

const otherChars = vi.fn()
await expectToThrowAsync(
pg.listen('test&me', otherChars),
)
await expectToThrowAsync(
pg.query(`NOTIFY test&me, 'payload1'`),
)

expect(allLower1).toHaveBeenCalledTimes(4)
expect(autoLowerTest1).toHaveBeenCalledTimes(3)
expect(autoLowerTest2).toHaveBeenCalledTimes(2)
expect(autoLowerTest3).toHaveBeenCalledTimes(1)
expect(caseSensitive1).toHaveBeenCalledOnce()
expect(caseSensitive2).not.toHaveBeenCalled()
expect(caseSensitive3).not.toHaveBeenCalled()
expect(otherCharsWithQuotes).toHaveBeenCalledOnce()
expect(quotedWithSpaces).toHaveBeenCalledOnce()
expect(unquotedWithSpaces).not.toHaveBeenCalled()
})

it('check unlisten case sensitivity as Postgresql', async () => {
it('check unlisten case sensitivity + special chars as Postgresql', async () => {
const pg = new PGlite()

const allLower1 = vi.fn()
Expand Down Expand Up @@ -121,10 +149,25 @@ describe('notify API', () => {
await pg.query(`NOTIFY "CaSESEnsiTIvE", 'payload1'`)
}

const quotedWithSpaces = vi.fn()
{
await pg.listen('"Quoted Channel With Spaces"', quotedWithSpaces)
await pg.query(`NOTIFY "Quoted Channel With Spaces", 'payload1'`)
await pg.unlisten('"Quoted Channel With Spaces"')
}

const otherCharsWithQuotes = vi.fn()
{
await pg.listen('"test&me"', otherCharsWithQuotes)
await pg.query(`NOTIFY "test&me", 'payload'`)
await pg.unlisten('"test&me"')
}

expect(allLower1).toHaveBeenCalledOnce()
expect(autoLowerTest1).toHaveBeenCalledOnce()
expect(autoLowerTest2).toHaveBeenCalledOnce()
expect(autoLowerTest3).toHaveBeenCalledOnce()
expect(caseSensitive1).toHaveBeenCalledOnce()
expect(otherCharsWithQuotes).toHaveBeenCalledOnce()
})
})

0 comments on commit cb70d90

Please sign in to comment.