Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: BinaryEncoding did not encode/decode the Clear message type #38

Merged
merged 7 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions packages/bentocache/src/bus/encoders/binary_encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ export class BinaryEncoder implements TransportEncoder {
this.#busIdLength = busIdLength
}

protected busMessageTypeToNum(type: CacheBusMessageType): number {
if (type === CacheBusMessageType.Set) return 0x01
if (type === CacheBusMessageType.Clear) return 0x02
return 0x03
}

protected numToBusMessageType(num: number): CacheBusMessageType {
if (num === 0x01) return CacheBusMessageType.Set
if (num === 0x02) return CacheBusMessageType.Clear
return CacheBusMessageType.Delete
}

/**
* Encode the given message into a Buffer
*/
Expand Down Expand Up @@ -59,7 +71,7 @@ export class BinaryEncoder implements TransportEncoder {
/**
* 2. write the message type. 0x01 for 'Set' message, and 0x02 for a 'Delete' message
*/
buffer.writeUInt8(payload.type === CacheBusMessageType.Set ? 0x01 : 0x02, this.#busIdLength)
buffer.writeUInt8(this.busMessageTypeToNum(payload.type), this.#busIdLength)

/**
* 3. Write the keys
Expand Down Expand Up @@ -100,7 +112,7 @@ export class BinaryEncoder implements TransportEncoder {
* Then comes the message type as a single byte
*/
const typeValue = buffer.readUInt8(offset++)
const type = typeValue === 0x01 ? CacheBusMessageType.Set : CacheBusMessageType.Delete
const type = this.numToBusMessageType(typeValue)

/**
* Finally, the keys
Expand Down
31 changes: 31 additions & 0 deletions packages/bentocache/tests/bus/bus.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,37 @@
})
.waitForDone()
.disableTimeout()

Check warning on line 272 in packages/bentocache/tests/bus/bus.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
test('binary encoding/decoding using Clear should be fine', async ({ assert, cleanup }, done) => {
const bus1 = redisBusDriver({ connection: REDIS_CREDENTIALS })
.factory(null as any)
.setId('foo')

const bus2 = redisBusDriver({ connection: REDIS_CREDENTIALS })
.factory(null as any)
.setId('bar')

cleanup(async () => {
await bus1.disconnect()
await bus2.disconnect()
})

const data = {
keys: [],
type: CacheBusMessageType.Clear,
}

bus1.subscribe('foo', (message: any) => {
assert.deepInclude(message, data)
done()
})

await setTimeout(200)

await bus2.publish('foo', data)
})
.waitForDone()
.disableTimeout()

test('works with utf8 characters', async ({ assert }, done) => {
const bus1 = redisBusDriver({ connection: REDIS_CREDENTIALS })
Expand Down
Loading