Skip to content

Commit c1bba2f

Browse files
committed
Tests, Refactoring
1 parent 574a47f commit c1bba2f

File tree

7 files changed

+83
-27
lines changed

7 files changed

+83
-27
lines changed

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = {
1111
coverageReporters: ["lcov", "text"],
1212
coverageThreshold: {
1313
global: {
14-
branches: 70,
14+
branches: 75,
1515
functions: 90,
1616
lines: 90,
1717
statements: 90,

src/adapter/redis.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class RedisAdapter {
1313
}
1414

1515
async setup() {
16-
this.client = await redis.createMainClientAndConnect();
16+
this.client = await redis.createPrimaryClientAndConnect();
1717
}
1818

1919
async on() {

src/redis/index.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,35 @@ xsenv.loadEnv(path.join(process.cwd(), "default-env.json"));
1111

1212
const TIMEOUT = 5 * 1000;
1313

14-
let mainClientPromise;
15-
let secondClientPromise;
14+
let primaryClientPromise;
15+
let secondaryClientPromise;
1616

17-
const createMainClientAndConnect = () => {
18-
if (mainClientPromise) {
19-
return mainClientPromise;
17+
const createPrimaryClientAndConnect = () => {
18+
if (primaryClientPromise) {
19+
return primaryClientPromise;
2020
}
2121

2222
const errorHandlerCreateClient = (err) => {
2323
LOG?.error("Error from redis client for pub/sub failed", err);
24-
mainClientPromise = null;
25-
setTimeout(createMainClientAndConnect, TIMEOUT);
24+
primaryClientPromise = null;
25+
setTimeout(createPrimaryClientAndConnect, TIMEOUT);
2626
};
27-
mainClientPromise = _createClientAndConnect(errorHandlerCreateClient);
28-
return mainClientPromise;
27+
primaryClientPromise = _createClientAndConnect(errorHandlerCreateClient);
28+
return primaryClientPromise;
2929
};
3030

31-
const createSecondClientAndConnect = () => {
32-
if (secondClientPromise) {
33-
return secondClientPromise;
31+
const createSecondaryClientAndConnect = () => {
32+
if (secondaryClientPromise) {
33+
return secondaryClientPromise;
3434
}
3535

3636
const errorHandlerCreateClient = (err) => {
3737
LOG?.error("Error from redis client for pub/sub failed", err);
38-
secondClientPromise = null;
39-
setTimeout(createSecondClientAndConnect, TIMEOUT);
38+
secondaryClientPromise = null;
39+
setTimeout(createSecondaryClientAndConnect, TIMEOUT);
4040
};
41-
secondClientPromise = _createClientAndConnect(errorHandlerCreateClient);
42-
return secondClientPromise;
41+
secondaryClientPromise = _createClientAndConnect(errorHandlerCreateClient);
42+
return secondaryClientPromise;
4343
};
4444
const _createClientBase = () => {
4545
let credentials;
@@ -94,7 +94,13 @@ const _createClientAndConnect = async (errorHandlerCreateClient) => {
9494
return client;
9595
};
9696

97+
const clearClients = () => {
98+
primaryClientPromise = null;
99+
secondaryClientPromise = null;
100+
};
101+
97102
module.exports = {
98-
createMainClientAndConnect,
99-
createSecondClientAndConnect,
103+
createPrimaryClientAndConnect,
104+
createSecondaryClientAndConnect,
105+
clearClients,
100106
};

src/socket/socket.io.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ class SocketIOServer extends SocketServer {
8383
const adapterFactory = require(adapterImpl);
8484
switch (adapterImpl) {
8585
case "@socket.io/redis-adapter":
86-
client = await redis.createMainClientAndConnect();
87-
subClient = await redis.createSecondClientAndConnect();
86+
client = await redis.createPrimaryClientAndConnect();
87+
subClient = await redis.createSecondaryClientAndConnect();
8888
if (client && subClient) {
8989
adapter = adapterFactory.createAdapter(client, subClient, options);
9090
}
9191
break;
9292
case "@socket.io/redis-streams-adapter":
93-
client = await redis.createMainClientAndConnect();
93+
client = await redis.createPrimaryClientAndConnect();
9494
if (client) {
9595
adapter = adapterFactory.createAdapter(client, options);
9696
}

test/_env/mocks/redis.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@
33
const onMessage = [];
44
const onError = [];
55

6+
let createClientError = false;
7+
let connectError = false;
8+
69
const client = {
7-
connect: jest.fn(() => {}),
10+
connect: jest.fn(() => {
11+
if (connectError) {
12+
connectError = false;
13+
throw new Error("connect error");
14+
}
15+
}),
816
on: jest.fn((event, cb) => {
917
switch (event) {
1018
case "message":
@@ -32,7 +40,21 @@ const client = {
3240

3341
module.exports = {
3442
client,
43+
throwError(kind) {
44+
switch (kind) {
45+
case "createClient":
46+
createClientError = true;
47+
break;
48+
case "connect":
49+
connectError = true;
50+
break;
51+
}
52+
},
3553
createClient: jest.fn(() => {
54+
if (createClientError) {
55+
createClientError = false;
56+
throw new Error("create client error");
57+
}
3658
return client;
3759
}),
3860
createCluster: jest.fn(() => {

test/base.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ describe("Base", () => {
1616
const connected = (_socket) => {
1717
socket = _socket;
1818
};
19+
socketServer.setup();
1920
socketServer.service("test", connected);
2021
expect(socket).toEqual({
2122
socket: null,
@@ -26,6 +27,12 @@ describe("Base", () => {
2627
broadcast: expect.any(Function),
2728
disconnect: expect.any(Function),
2829
});
30+
expect(socket.setup()).toBeUndefined();
31+
expect(socket.context()).toBeUndefined();
32+
expect(socket.on()).toBeUndefined();
33+
expect(socket.emit()).toBeUndefined();
34+
expect(socket.broadcast()).toBeUndefined();
35+
expect(socket.disconnect()).toBeUndefined();
2936
});
3037

3138
test("Mock Response", async () => {

test/redis.test.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const cds = require("@sap/cds");
44
const xsenv = require("@sap/xsenv");
5+
const redisMock = require("./_env/mocks/redis");
56
jest.mock("redis", () => require("./_env/mocks/redis"));
67
const redis = require("../src/redis");
78

@@ -15,15 +16,35 @@ describe("Redis", () => {
1516
afterAll(() => {});
1617

1718
test("Client", async () => {
18-
const main = await redis.createMainClientAndConnect();
19+
const main = await redis.createPrimaryClientAndConnect();
1920
expect(main).toBeDefined();
20-
const second = await redis.createSecondClientAndConnect();
21+
const second = await redis.createSecondaryClientAndConnect();
2122
expect(second).toBeDefined();
2223
});
2324

2425
test("Client fail", async () => {
25-
const main = await redis.createMainClientAndConnect();
26+
const main = await redis.createPrimaryClientAndConnect();
2627
expect(main).toBeDefined();
2728
main.error(new Error("Failed"));
29+
expect(main.on).toHaveBeenNthCalledWith(1, "error", expect.any(Function));
30+
});
31+
32+
test("Client createClient exception", async () => {
33+
redis.clearClients();
34+
redisMock.throwError("createClient");
35+
let main = await redis.createPrimaryClientAndConnect();
36+
expect(main).toBeUndefined();
37+
redisMock.throwError("createClient");
38+
let secondary = await redis.createSecondaryClientAndConnect();
39+
expect(secondary).toBeUndefined();
40+
});
41+
42+
test("Client connect exception", async () => {
43+
redis.clearClients();
44+
redisMock.throwError("connect");
45+
let main = await redis.createPrimaryClientAndConnect();
46+
redisMock.throwError("connect");
47+
let secondary = await redis.createSecondaryClientAndConnect();
48+
expect(secondary).toBeUndefined();
2849
});
2950
});

0 commit comments

Comments
 (0)