Skip to content

Commit eb838fc

Browse files
authored
Merge pull request #89 from Procedure-RPC/refactor-tests
Refactor tests and test incorrect endpoints
2 parents b41ac43 + c687431 commit eb838fc

10 files changed

Lines changed: 4393 additions & 3225 deletions

File tree

test/index.spec.ts

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

test/index.spec/INPROC/call.spec.ts

Lines changed: 939 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import {
2+
afterEach,
3+
beforeEach,
4+
describe,
5+
expect,
6+
it,
7+
jest,
8+
} from '@jest/globals';
9+
10+
import Procedure, { ping } from '../../../src';
11+
import { ProcedureErrorCodes } from '../../../src/errors';
12+
13+
describe('ping(endpoint: string, timeout: number | undefined = 100, signal?: AbortSignal): Promise<boolean>', () => {
14+
let fn: ReturnType<typeof jest.fn>;
15+
let procedure: Procedure<unknown, unknown>;
16+
let procedureEndpoint: string;
17+
let pingEndpoint: string | undefined;
18+
19+
describe('when procedure callback: Callback<number, number> (simple accumulator function)', () => {
20+
beforeEach(() => {
21+
let i = 0;
22+
fn = jest.fn((n: number) => {
23+
if (typeof n !== 'number') {
24+
throw new TypeError('Expected a number');
25+
}
26+
27+
return (i += n);
28+
});
29+
30+
procedureEndpoint = 'inproc://Procedure/Add';
31+
procedure = new Procedure(fn, { workers: 3 });
32+
procedure.bind(procedureEndpoint);
33+
});
34+
35+
afterEach(() => {
36+
procedure.unbind();
37+
});
38+
39+
describe('when endpoint: correct', () => {
40+
beforeEach(() => {
41+
pingEndpoint = procedureEndpoint;
42+
});
43+
44+
afterEach(() => {
45+
pingEndpoint = undefined;
46+
});
47+
48+
it('should not emit: data', async () => {
49+
const data = jest.fn();
50+
procedure.on('data', data);
51+
await ping(<string>pingEndpoint);
52+
expect(data).not.toHaveBeenCalled();
53+
});
54+
55+
it('should not be rejected', async () => {
56+
await expect(
57+
ping(<string>pingEndpoint)
58+
).resolves.toBeUndefined();
59+
});
60+
61+
describe('when signal: already aborted AbortSignal', () => {
62+
let ac: AbortController;
63+
64+
beforeEach(() => {
65+
ac = new AbortController();
66+
ac.abort();
67+
});
68+
69+
it('should throw: ProcedureCancelledError', async () => {
70+
await expect(
71+
ping(<string>pingEndpoint, 500, false, ac.signal)
72+
).rejects.toMatchObject({
73+
code: ProcedureErrorCodes.CANCELLED,
74+
});
75+
});
76+
});
77+
});
78+
79+
describe('when endpoint: incorrect', () => {
80+
beforeEach(() => {
81+
pingEndpoint = procedureEndpoint.substring(
82+
0,
83+
procedureEndpoint.length - 2
84+
);
85+
});
86+
87+
afterEach(() => {
88+
pingEndpoint = undefined;
89+
});
90+
91+
it('should not emit: data', async () => {
92+
const data = jest.fn();
93+
procedure.on('data', data);
94+
await expect(ping(<string>pingEndpoint)).rejects.toMatchObject({
95+
code: ProcedureErrorCodes.TIMED_OUT,
96+
});
97+
expect(data).not.toHaveBeenCalled();
98+
procedure.removeListener('data', data);
99+
});
100+
101+
it('should throw: ProcedureTimedOutError', async () => {
102+
await expect(ping(<string>pingEndpoint)).rejects.toMatchObject({
103+
code: ProcedureErrorCodes.TIMED_OUT,
104+
});
105+
});
106+
107+
describe('when signal: already aborted AbortSignal', () => {
108+
let ac: AbortController;
109+
110+
beforeEach(() => {
111+
ac = new AbortController();
112+
ac.abort();
113+
});
114+
115+
it('should throw: ProcedureCancelledError', async () => {
116+
await expect(
117+
ping(<string>pingEndpoint, 500, false, ac.signal)
118+
).rejects.toMatchObject({
119+
code: ProcedureErrorCodes.CANCELLED,
120+
});
121+
});
122+
});
123+
});
124+
125+
it.todo('when timeout: Infinity');
126+
it.todo('when timeout: NaN');
127+
it.todo('when abortion signaled during ping');
128+
});
129+
});
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import {
2+
afterEach,
3+
beforeEach,
4+
describe,
5+
expect,
6+
it,
7+
jest,
8+
} from '@jest/globals';
9+
10+
import Procedure, { tryPing } from '../../../src';
11+
12+
describe('tryPing(endpoint: string, timeout: number | undefined = 100, signal?: AbortSignal): Promise<boolean>', () => {
13+
let fn: ReturnType<typeof jest.fn>;
14+
let procedure: Procedure<unknown, unknown>;
15+
let procedureEndpoint: string;
16+
let pingEndpoint: string | undefined;
17+
18+
describe('when procedure callback: Callback<number, number> (simple accumulator function)', () => {
19+
beforeEach(() => {
20+
let i = 0;
21+
fn = jest.fn((n: number) => {
22+
if (typeof n !== 'number') {
23+
throw new TypeError('Expected a number');
24+
}
25+
26+
return (i += n);
27+
});
28+
29+
procedureEndpoint = 'inproc://Procedure/Add';
30+
procedure = new Procedure(fn, { workers: 3 });
31+
procedure.bind(procedureEndpoint);
32+
});
33+
34+
afterEach(() => {
35+
procedure.unbind();
36+
});
37+
38+
describe('when endpoint: correct', () => {
39+
beforeEach(() => {
40+
pingEndpoint = procedureEndpoint;
41+
});
42+
43+
it('should not emit: data', async () => {
44+
const data = jest.fn();
45+
procedure.on('data', data);
46+
await tryPing(<string>pingEndpoint);
47+
expect(data).not.toHaveBeenCalled();
48+
procedure.removeListener('data', data);
49+
});
50+
51+
it('should resolve: true', async () => {
52+
await expect(tryPing(<string>pingEndpoint)).resolves.toEqual(
53+
true
54+
);
55+
});
56+
57+
describe('when signal: already aborted AbortSignal', () => {
58+
let ac: AbortController;
59+
60+
beforeEach(() => {
61+
ac = new AbortController();
62+
ac.abort();
63+
});
64+
65+
it('should resolve: false', async () => {
66+
await expect(
67+
tryPing(<string>pingEndpoint, 500, false, ac.signal)
68+
).resolves.toEqual(false);
69+
});
70+
});
71+
});
72+
73+
describe('when endpoint: incorrect', () => {
74+
beforeEach(() => {
75+
pingEndpoint = procedureEndpoint.substring(
76+
0,
77+
procedureEndpoint.length - 2
78+
);
79+
});
80+
81+
afterEach(() => {
82+
pingEndpoint = undefined;
83+
});
84+
85+
it('should not emit: data', async () => {
86+
const data = jest.fn();
87+
procedure.on('data', data);
88+
await tryPing(<string>pingEndpoint);
89+
expect(data).not.toHaveBeenCalled();
90+
procedure.removeListener('data', data);
91+
});
92+
93+
it('should resolve: false', async () => {
94+
await expect(tryPing(<string>pingEndpoint)).resolves.toEqual(
95+
false
96+
);
97+
});
98+
99+
describe('when signal: already aborted AbortSignal', () => {
100+
let ac: AbortController;
101+
102+
beforeEach(() => {
103+
ac = new AbortController();
104+
ac.abort();
105+
});
106+
107+
it('should resolve: false', async () => {
108+
await expect(
109+
tryPing(<string>pingEndpoint, 500, false, ac.signal)
110+
).resolves.toEqual(false);
111+
});
112+
});
113+
});
114+
115+
it.todo('when timeout: Infinity');
116+
it.todo('when timeout: NaN');
117+
it.todo('when abortion signaled during tryPing');
118+
});
119+
});

0 commit comments

Comments
 (0)