Skip to content

Commit

Permalink
fix: BinaryEncoding did not encode/decode the Clear message type (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
gkachru authored Oct 4, 2024
1 parent f63b4e7 commit d4837ce
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
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 @@ test.group('Bus synchronization', () => {
})
.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

0 comments on commit d4837ce

Please sign in to comment.