-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
271 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
import {TestSetup} from '../types'; | ||
import {StandaloneTestSetup, TestSetup} from '../types'; | ||
import * as string from './string'; | ||
import * as pubsub from './pubsub'; | ||
|
||
export const run = (setup: TestSetup) => { | ||
describe('commands', () => { | ||
string.run(setup); | ||
pubsub.run(setup); | ||
}); | ||
}; | ||
|
||
export const standalone = (setup: StandaloneTestSetup) => { | ||
describe('commands', () => { | ||
pubsub.standalone(setup); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,130 @@ | ||
import {TestSetup} from '../../types'; | ||
import {tick, until} from 'thingies'; | ||
import {StandaloneTestSetup} from '../../types'; | ||
|
||
export const run = (setup: TestSetup) => { | ||
export const standalone = (setup: StandaloneTestSetup) => { | ||
describe('SUBSCRIBE', () => { | ||
test('can subscribe to a key', async () => { | ||
test('can subscribe to a channel', async () => { | ||
const {client} = await setup(); | ||
const key = 'subscribe_key_' + Date.now(); | ||
const res = await client.cmd(['SUBSCRIBE', key]); | ||
expect(res).toBe(undefined); | ||
const channel = 'subscribe_channel_just_sub_' + Date.now(); | ||
client.sub(channel, (recv) => {}); | ||
await new Promise(resolve => setTimeout(resolve, 5)); | ||
}); | ||
|
||
test('can un-subscribe from a channel', async () => { | ||
const {client} = await setup(); | ||
const channel = 'subscribe_channel_sub_unsub_' + Date.now(); | ||
const unsubscribe = client.sub(channel, (recv) => {}); | ||
await new Promise(resolve => setTimeout(resolve, 5)); | ||
unsubscribe(); | ||
await new Promise(resolve => setTimeout(resolve, 5)); | ||
}); | ||
|
||
test('can receive a message on a channel', async () => { | ||
const {client} = await setup(); | ||
const channel = 'subscribe_channel_' + Date.now(); | ||
const msgs: unknown[] = []; | ||
client.sub(channel, (recv) => { | ||
msgs.push(recv); | ||
}); | ||
client.publish(channel, new Uint8Array([1, 2, 3])); | ||
await until(() => msgs.length === 1); | ||
expect(msgs[0]).toEqual(new Uint8Array([1, 2, 3])); | ||
}); | ||
|
||
test('can receive multiple messages on a channel', async () => { | ||
const {client} = await setup(); | ||
const channel = 'subscribe_channel_multiple_' + Date.now(); | ||
const msgs: unknown[] = []; | ||
client.sub(channel, (recv) => { | ||
msgs.push(recv); | ||
}); | ||
client.pub(channel, new Uint8Array([1])); | ||
client.pub(channel, new Uint8Array([2])); | ||
client.pub(channel, new Uint8Array([3])); | ||
await until(() => msgs.length === 3); | ||
expect(msgs).toEqual([ | ||
new Uint8Array([1]), | ||
new Uint8Array([2]), | ||
new Uint8Array([3]), | ||
]); | ||
}); | ||
|
||
test('does not receive more messages after un-subscription', async () => { | ||
const {client} = await setup(); | ||
const channel = 'channel_unsubscribe_' + Date.now(); | ||
const msgs: unknown[] = []; | ||
const unsubscribe = client.sub(channel, (recv) => { | ||
msgs.push(recv); | ||
}); | ||
await client.publish(channel, new Uint8Array([1])); | ||
await client.publish(channel, new Uint8Array([2])); | ||
unsubscribe(); | ||
await client.publish(channel, new Uint8Array([3])); | ||
await until(() => msgs.length === 2); | ||
expect(msgs).toEqual([ | ||
new Uint8Array([1]), | ||
new Uint8Array([2]), | ||
]); | ||
}); | ||
|
||
test('can subscribe twice from the same client', async () => { | ||
const {client} = await setup(); | ||
const channel = 'subscribe_twice_' + Date.now(); | ||
const msgs: unknown[] = []; | ||
client.sub(channel, (recv) => { | ||
msgs.push(recv); | ||
}); | ||
client.sub(channel, (recv) => { | ||
msgs.push(recv); | ||
}); | ||
await client.publish(channel, new Uint8Array([1])); | ||
await until(() => msgs.length === 2); | ||
expect(msgs).toEqual([ | ||
new Uint8Array([1]), | ||
new Uint8Array([1]), | ||
]); | ||
}); | ||
|
||
test('can subscribe twice from the same client and unsubscribe on subscription', async () => { | ||
const {client} = await setup(); | ||
const channel = 'subscribe_twice_and_unsub_' + Date.now(); | ||
const msgs: unknown[] = []; | ||
const unsubscribe1 = client.sub(channel, (recv) => { | ||
msgs.push([1, recv]); | ||
}); | ||
client.sub(channel, (recv) => { | ||
msgs.push([2, recv]); | ||
}); | ||
await client.publish(channel, new Uint8Array([1])); | ||
unsubscribe1(); | ||
await client.publish(channel, new Uint8Array([2])); | ||
await until(() => msgs.length === 3); | ||
expect(msgs).toEqual([ | ||
[1, new Uint8Array([1])], | ||
[2, new Uint8Array([1])], | ||
[2, new Uint8Array([2])], | ||
]); | ||
}); | ||
|
||
test('can subscribe twice from different clients', async () => { | ||
const {client} = await setup(); | ||
const {client: client2} = await setup(); | ||
const channel = 'subscribe_twice_diff_clients_' + Date.now(); | ||
const msgs: unknown[] = []; | ||
const [, subscribed1] = client.subscribe(channel, (recv) => { | ||
msgs.push(recv); | ||
}); | ||
const [, subscribed2] = client2.subscribe(channel, (recv) => { | ||
msgs.push(recv); | ||
}); | ||
await subscribed1; | ||
await subscribed2; | ||
await client.publish(channel, new Uint8Array([1])); | ||
await until(() => msgs.length === 2); | ||
expect(msgs).toEqual([ | ||
new Uint8Array([1]), | ||
new Uint8Array([1]), | ||
]); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import {TestSetup} from '../../types'; | ||
import {StandaloneTestSetup} from '../../types'; | ||
import * as SUBSCRIBE from './SUBSCRIBE'; | ||
|
||
export const run = (setup: TestSetup) => { | ||
export const standalone = (setup: StandaloneTestSetup) => { | ||
describe('pubsub commands', () => { | ||
SUBSCRIBE.run(setup); | ||
SUBSCRIBE.standalone(setup); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import {bufferToUint8Array} from 'json-joy/es2020/util/buffers/bufferToUint8Array'; | ||
|
||
export const cmpUint8Array = (a: Uint8Array, b: Uint8Array): 1 | 0 | -1 => { | ||
const len1 = a.length; | ||
const len2 = b.length; | ||
if (len1 > len2) return 1; | ||
if (len1 < len2) return -1; | ||
for (let i = 0; i < len1; i++) { | ||
const o1 = a[i]; | ||
const o2 = b[i]; | ||
if (o1 > o2) return 1; | ||
if (o1 < o2) return -1; | ||
} | ||
return 0; | ||
}; | ||
|
||
export const ascii = ([txt]: TemplateStringsArray) => { | ||
const len = txt.length; | ||
const res = new Uint8Array(len); | ||
for (let i = 0; i < len; i++) res[i] = txt.charCodeAt(i); | ||
return res; | ||
}; | ||
|
||
export const utf8 = ([txt]: TemplateStringsArray) => bufferToUint8Array(Buffer.from(txt, 'utf8')); |
Oops, something went wrong.