Skip to content

Commit b9bf100

Browse files
authored
Use random string for unknown exchange type tests (#204)
Previously, the tests passed `none` as the exchange type and asserted on the "invalid exchange type" error. That path only fires in `rabbit_exchange:check_type/1` when the type is a known atom whose module lookup fails — `none` happens to already be interned as an atom in the VM, so `binary_to_existing_atom/1` succeeds and the module lookup is what fails. A random string exercises the other branch: `binary_to_existing_atom/1` returns `{error, not_found}` in `rabbit_registry:binary_to_type/1`, and the broker replies with "unknown exchange type '<name>'". However, this breaks it for LavinMQ < 2.8.0 (not yet released) which uses another error message ("invalid exchange type") so temporarily broaden the regex to allow for both types of messages.
1 parent 868de40 commit b9bf100

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

test/test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -422,12 +422,14 @@ test("can handle nacks on confirm channel", async () => {
422422
await expect(ch.basicPublish("", q.name, "body")).rejects.toThrow("Message rejected")
423423
})
424424

425-
test("throws on invalid exchange type", async () => {
425+
test("throws on unknown exchange type", async () => {
426426
const amqp = getNewClient()
427427
const conn = await amqp.connect()
428428
const ch = await conn.channel()
429429
const name = "test" + Math.random()
430-
await expect(ch.exchangeDeclare(name, "none")).rejects.toThrow(/invalid exchange type/)
430+
const unknownType = "unknowntype" + Math.random().toString(36).slice(2)
431+
// LavinMQ < 2.8.0 reports "invalid exchange type"; newer LavinMQ and RabbitMQ report "unknown exchange type"
432+
await expect(ch.exchangeDeclare(name, unknownType)).rejects.toThrow(/(unknown|invalid) exchange type/)
431433
})
432434

433435
test("can declare an exchange", async () => {
@@ -756,9 +758,11 @@ test("has an onerror callback", async () => {
756758
const ch = await conn.channel()
757759
let errMessage: string | null = null
758760
ch.onerror = vi.fn((reason) => (errMessage = reason))
759-
await expect(ch.exchangeDeclare("none", "none")).rejects.toThrow()
761+
const unknownType = "unknowntype" + Math.random().toString(36).slice(2)
762+
await expect(ch.exchangeDeclare("name" + Math.random(), unknownType)).rejects.toThrow()
760763
expect(ch.onerror).toBeCalled()
761-
expect(errMessage).toMatch(/invalid exchange type/)
764+
// LavinMQ < 2.8.0 reports "invalid exchange type"; newer LavinMQ and RabbitMQ report "unknown exchange type"
765+
expect(errMessage).toMatch(/(unknown|invalid) exchange type/)
762766
})
763767

764768
test("onerror is not called when conn is closed by client", async () => {

0 commit comments

Comments
 (0)