Skip to content

Commit 2595993

Browse files
committed
test: 💍 add E2E demo server tests
1 parent be7af30 commit 2595993

File tree

4 files changed

+153
-27
lines changed

4 files changed

+153
-27
lines changed

src/__tests__/e2e/codecs.ts

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {RpcCodec} from '../../common/codec/RpcCodec';
66

77
export interface SetupCodecsOpts {
88
skipJson2?: boolean;
9+
onlyCommon?: boolean;
910
}
1011

1112
export const setupCodecs = (opts: SetupCodecsOpts = {}) => {
@@ -15,36 +16,45 @@ export const setupCodecs = (opts: SetupCodecsOpts = {}) => {
1516
const list: RpcCodec[] = [
1617
new RpcCodec(compact, json, json),
1718
new RpcCodec(compact, cbor, cbor),
18-
new RpcCodec(compact, msgpack, msgpack),
19-
new RpcCodec(compact, json, cbor),
20-
new RpcCodec(compact, json, msgpack),
21-
new RpcCodec(compact, cbor, json),
22-
new RpcCodec(compact, cbor, msgpack),
23-
new RpcCodec(compact, msgpack, json),
24-
new RpcCodec(compact, msgpack, cbor),
2519
new RpcCodec(binary, cbor, cbor),
26-
new RpcCodec(binary, msgpack, msgpack),
27-
new RpcCodec(binary, json, json),
28-
new RpcCodec(binary, json, cbor),
29-
new RpcCodec(binary, json, msgpack),
30-
new RpcCodec(binary, cbor, json),
31-
new RpcCodec(binary, cbor, msgpack),
32-
new RpcCodec(binary, msgpack, json),
33-
new RpcCodec(binary, msgpack, cbor),
3420
];
35-
if (!opts.skipJson2) {
36-
list.push(new RpcCodec(jsonRpc2, json, json));
37-
list.push(new RpcCodec(jsonRpc2, cbor, cbor));
38-
list.push(new RpcCodec(jsonRpc2, msgpack, msgpack));
39-
list.push(new RpcCodec(jsonRpc2, json, cbor));
40-
list.push(new RpcCodec(jsonRpc2, json, msgpack));
41-
list.push(new RpcCodec(jsonRpc2, cbor, json));
42-
list.push(new RpcCodec(jsonRpc2, cbor, msgpack));
43-
list.push(new RpcCodec(jsonRpc2, msgpack, json));
44-
list.push(new RpcCodec(jsonRpc2, msgpack, cbor));
21+
if (!opts.onlyCommon) {
22+
list.push(new RpcCodec(compact, msgpack, msgpack));
23+
list.push(new RpcCodec(compact, json, cbor));
24+
list.push(new RpcCodec(compact, json, msgpack));
25+
list.push(new RpcCodec(compact, cbor, json));
26+
list.push(new RpcCodec(compact, cbor, msgpack));
27+
list.push(new RpcCodec(compact, msgpack, json));
28+
list.push(new RpcCodec(compact, msgpack, cbor));
29+
list.push(new RpcCodec(binary, msgpack, msgpack));
30+
list.push(new RpcCodec(binary, json, json));
31+
list.push(new RpcCodec(binary, json, cbor));
32+
list.push(new RpcCodec(binary, json, msgpack));
33+
list.push(new RpcCodec(binary, cbor, json));
34+
list.push(new RpcCodec(binary, cbor, msgpack));
35+
list.push(new RpcCodec(binary, msgpack, json));
36+
list.push(new RpcCodec(binary, msgpack, cbor));
37+
if (!opts.skipJson2) {
38+
list.push(new RpcCodec(jsonRpc2, json, json));
39+
list.push(new RpcCodec(jsonRpc2, cbor, cbor));
40+
list.push(new RpcCodec(jsonRpc2, msgpack, msgpack));
41+
list.push(new RpcCodec(jsonRpc2, json, cbor));
42+
list.push(new RpcCodec(jsonRpc2, json, msgpack));
43+
list.push(new RpcCodec(jsonRpc2, cbor, json));
44+
list.push(new RpcCodec(jsonRpc2, cbor, msgpack));
45+
list.push(new RpcCodec(jsonRpc2, msgpack, json));
46+
list.push(new RpcCodec(jsonRpc2, msgpack, cbor));
47+
}
4548
}
4649
return {
4750
codecs,
4851
list,
4952
};
5053
};
54+
55+
export const cborCodec = () => {
56+
const codecs = new RpcCodecs(new Codecs(new Writer()), new RpcMessageCodecs());
57+
const {binary} = codecs.messages;
58+
const {cbor} = codecs.value;
59+
return new RpcCodec(binary, cbor, cbor);
60+
};

src/__tests__/e2e/demo-client.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import WebSocket from 'ws';
2+
import {RpcCodec} from '../../common/codec/RpcCodec';
3+
import {RpcPersistentClient, StreamingRpcClient, WebSocketChannel} from '../../common';
4+
import {FetchRpcClient} from '../../common/rpc/client/FetchRpcClient';
5+
6+
const address = '3.92.169.31';
7+
8+
export const setupDemoServerPersistentClient = (codec: RpcCodec) => {
9+
const url = `ws://${address}/rpc`;
10+
const client = new RpcPersistentClient({
11+
codec,
12+
channel: {
13+
newChannel: () =>
14+
new WebSocketChannel({
15+
newSocket: () => new WebSocket(url, [codec.specifier()]) as any,
16+
}),
17+
},
18+
});
19+
client.start();
20+
const call = client.call.bind(client);
21+
const call$ = client.call$.bind(client);
22+
const stop = async () => void client.stop();
23+
return {client, call, call$, stop};
24+
};
25+
26+
export const setupDemoServerFetchClient = (codec: RpcCodec) => {
27+
const url = `http://${address}/rpc`;
28+
const client = new FetchRpcClient({
29+
url,
30+
msgCodec: codec.msg,
31+
reqCodec: codec.req,
32+
resCodec: codec.res,
33+
});
34+
const call = client.call.bind(client);
35+
const call$ = client.call$.bind(client);
36+
const stop = async () => void client.stop();
37+
return {client, call, call$, stop};
38+
};
39+
40+
export const setupDemoServerStreamingClient = (codec: RpcCodec) => {
41+
const protocolSpecifier = codec.specifier();
42+
const contentType = 'application/x.' + protocolSpecifier;
43+
const client = new StreamingRpcClient({
44+
send: async (messages) => {
45+
const port = +(process.env.PORT || 9999);
46+
const url = `http://${address}/rpc`;
47+
codec.req.encoder.writer.reset();
48+
codec.msg.encodeBatch(codec.req, messages);
49+
const body = codec.req.encoder.writer.flush();
50+
try {
51+
const response = await fetch(url, {
52+
method: 'POST',
53+
headers: {
54+
'Content-Type': contentType,
55+
},
56+
body,
57+
});
58+
const buffer = await response.arrayBuffer();
59+
const data = new Uint8Array(buffer);
60+
const responseMessages = codec.msg.decodeBatch(codec.res, data);
61+
client.onMessages(responseMessages as any);
62+
} catch (err) {
63+
// tslint:disable-next-line:no-console
64+
console.error(err);
65+
}
66+
},
67+
});
68+
const call = client.call.bind(client);
69+
const call$ = client.call$.bind(client);
70+
const stop = async () => void client.stop();
71+
return {client, call, call$, stop};
72+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {ApiTestSetup} from '../../../common/rpc/__tests__/runApiTests';
2+
import {runUtilTests} from '../../json-crdt-server/util';
3+
import {runPubsubTests} from '../../json-crdt-server/pubsub';
4+
import {runPresenceTests} from '../../json-crdt-server/presence';
5+
import {runBlockTests} from '../../json-crdt-server/block';
6+
import {cborCodec} from '../codecs';
7+
import {setupDemoServerPersistentClient, setupDemoServerFetchClient, setupDemoServerStreamingClient} from '../demo-client';
8+
9+
if (process.env.TEST_E2E) {
10+
describe('RpcPersistentClient', () => {
11+
const codec = cborCodec();
12+
const setup: ApiTestSetup = async () => setupDemoServerPersistentClient(codec);
13+
describe(`protocol: application/x.${codec.specifier()}`, () => {
14+
runUtilTests(setup);
15+
runPubsubTests(setup);
16+
runPresenceTests(setup);
17+
runBlockTests(setup);
18+
});
19+
});
20+
21+
// describe('FetchRpcClient', () => {
22+
// const codec = cborCodec();
23+
// const setup: ApiTestSetup = async () => setupDemoServerFetchClient(codec);
24+
// describe(`protocol: application/x.${codec.specifier()}`, () => {
25+
// runUtilTests(setup, {staticOnly: true});
26+
// runPubsubTests(setup, {staticOnly: true});
27+
// runPresenceTests(setup, {staticOnly: true});
28+
// runBlockTests(setup, {staticOnly: true});
29+
// });
30+
// });
31+
32+
// describe('StreamingRpcClient', () => {
33+
// const codec = cborCodec();
34+
// const setup: ApiTestSetup = async () => setupDemoServerStreamingClient(codec);
35+
// describe(`protocol: application/x.${codec.specifier()}`, () => {
36+
// runUtilTests(setup, {staticOnly: true});
37+
// runPubsubTests(setup, {staticOnly: true});
38+
// runPresenceTests(setup, {staticOnly: true});
39+
// runBlockTests(setup, {staticOnly: true});
40+
// });
41+
// });
42+
} else {
43+
test.skip('set TEST_E2E=1 env var to run this test suite', () => {});
44+
}

src/__tests__/json-crdt-server/block.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ export const runBlockTests = (_setup: ApiTestSetup, params: {staticOnly?: true}
433433
model3.applyPatch(patch2);
434434
expect(model3.view()).toStrictEqual({text: 'Hell!', num: 110});
435435
await stop();
436-
}, 15000);
436+
}, 45000);
437437
});
438438

439439
if (!params.staticOnly) {
@@ -684,7 +684,7 @@ export const runBlockTests = (_setup: ApiTestSetup, params: {staticOnly?: true}
684684
};
685685
for (let i = -1; i <= 150; i++) await assertPull(i);
686686
await stop();
687-
}, 20000);
687+
}, 60000);
688688

689689
test('can create a new block, if it does not exist', async () => {
690690
const {call, stop} = await setup();

0 commit comments

Comments
 (0)