Skip to content

Commit d5024e9

Browse files
committed
conat: put all core testing together in backend/conat/test again
1 parent 0be0bd9 commit d5024e9

File tree

9 files changed

+98
-48
lines changed

9 files changed

+98
-48
lines changed

src/packages/server/conat/test/core/basic.test.ts renamed to src/packages/backend/conat/test/core/basic.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
Very basic test of conats core client and server.
33
*/
44

5-
import { connect, before, after } from "./setup";
6-
import { wait } from "@cocalc/server/conat/test/util";
5+
import { connect, before, after } from "@cocalc/backend/conat/test/setup";
6+
import { wait } from "@cocalc/backend/conat/test/util";
77

88
beforeAll(before);
99

src/packages/server/conat/test/core/connect.test.ts renamed to src/packages/backend/conat/test/core/connect.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { getPort } from "@cocalc/server/conat/test/util";
2-
import { initConatServer } from "@cocalc/server/conat/socketio";
1+
import { getPort } from "@cocalc/backend/conat/test/util";
2+
import { initConatServer } from "@cocalc/backend/conat/test/setup";
33
import { connect } from "@cocalc/backend/conat/conat";
44
import { delay } from "awaiting";
5-
import { wait } from "@cocalc/server/conat/test/util";
5+
import { wait } from "@cocalc/backend/conat/test/util";
66

77
const path = "/conat";
88
let port;

src/packages/server/conat/test/core/core-stream-ephemeral.test.ts renamed to src/packages/backend/conat/test/core/core-stream-ephemeral.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { connect, before, after } from "./setup";
1+
import { connect, before, after } from "@cocalc/backend/conat/test/setup";
22
import {
33
cstream,
44
COCALC_STREAM_HEADER,
55
type CoreStream,
66
} from "@cocalc/conat/sync/core-stream";
7-
import { wait } from "@cocalc/server/conat/test/util";
7+
import { wait } from "@cocalc/backend/conat/test/util";
88
import type { Client } from "@cocalc/conat/core/client";
99
import { is_date as isDate } from "@cocalc/util/misc";
1010
import { dstream } from "@cocalc/conat/sync/dstream";
@@ -299,8 +299,6 @@ describe("test using ephemeral dstream", () => {
299299
wait({ until: () => stream.length == 101 });
300300
expect(stream.get(stream.length - 1)).toBe("x");
301301
});
302-
303-
304302
});
305303

306304
afterAll(after);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { getPort } from "@cocalc/backend/conat/test/util";
2+
import { type Client } from "@cocalc/backend/conat/conat";
3+
import {
4+
init as createConatServer,
5+
type Options,
6+
type ConatServer,
7+
} from "@cocalc/conat/core/server";
8+
import { Server } from "socket.io";
9+
import getLogger from "@cocalc/backend/logger";
10+
11+
const logger = getLogger("conat:test:setup");
12+
13+
export const path = "/conat";
14+
15+
export async function initConatServer(
16+
options: Partial<Options> = {},
17+
): Promise<ConatServer> {
18+
logger.debug("init");
19+
if (!options?.port) {
20+
const port = await getPort();
21+
options = { ...options, port };
22+
}
23+
24+
return createConatServer({
25+
logger: logger.debug,
26+
Server,
27+
...options,
28+
});
29+
}
30+
31+
export let server;
32+
export let port;
33+
export let address;
34+
export async function before() {
35+
port = await getPort();
36+
address = `http://localhost:${port}`;
37+
server = await initConatServer({ port, path });
38+
}
39+
40+
const clients: Client[] = [];
41+
export function connect() {
42+
const cn = server.client();
43+
clients.push(cn);
44+
return cn;
45+
}
46+
47+
export async function after() {
48+
await server.close();
49+
for (const cn of clients) {
50+
cn.close();
51+
}
52+
}

src/packages/backend/package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
"./auth/*": "./dist/auth/*.js",
1111
"./auth/tokens/*": "./dist/auth/tokens/*.js"
1212
},
13-
"keywords": ["utilities", "cocalc"],
13+
"keywords": [
14+
"utilities",
15+
"cocalc"
16+
],
1417
"scripts": {
1518
"preinstall": "npx only-allow pnpm",
1619
"clean": "rm -rf dist node_modules",
@@ -20,7 +23,12 @@
2023
"prepublishOnly": "pnpm test",
2124
"conat-watch": "node ./bin/conat-watch.cjs"
2225
},
23-
"files": ["dist/**", "bin/**", "README.md", "package.json"],
26+
"files": [
27+
"dist/**",
28+
"bin/**",
29+
"README.md",
30+
"package.json"
31+
],
2432
"author": "SageMath, Inc.",
2533
"license": "SEE LICENSE.md",
2634
"dependencies": {
@@ -44,6 +52,7 @@
4452
"prom-client": "^13.0.0",
4553
"rimraf": "^5.0.5",
4654
"shell-escape": "^0.2.0",
55+
"socket.io": "^4.8.1",
4756
"socket.io-client": "^4.8.1",
4857
"supports-color": "^9.0.2",
4958
"tmp-promise": "^3.0.3",

src/packages/conat/core/server.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ import {
3838
import { createAdapter } from "@socket.io/redis-streams-adapter";
3939
import Valkey from "iovalkey";
4040
import { delay } from "awaiting";
41-
import { ConatError } from "./client";
41+
import {
42+
ConatError,
43+
connect,
44+
type Client,
45+
type ConnectOptions,
46+
} from "./client";
4247

4348
// This is just the default with socket.io, but we might want a bigger
4449
// size, which could mean more RAM usage by the servers.
@@ -390,6 +395,17 @@ export class ConatServer {
390395
}
391396
});
392397
};
398+
399+
// create new client in the same process connected to this server.
400+
// This is useful for unit testing and is not cached (i.e., multiple
401+
// calls return distinct clients).
402+
client = (options?: ConnectOptions): Client => {
403+
return connect(`http://localhost:${this.options.port}`, {
404+
path: this.options.path,
405+
noCache: true,
406+
...options,
407+
});
408+
};
393409
}
394410

395411
function getSubjectFromRoom(room: string) {

src/packages/pnpm-lock.yaml

Lines changed: 11 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/packages/server/conat/test/core/setup.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)