Skip to content

Commit 0b0567c

Browse files
refactor: remove remote middleware & gateway auto registration
BREAKING CHANGE
1 parent f43927d commit 0b0567c

File tree

13 files changed

+12
-967
lines changed

13 files changed

+12
-967
lines changed

__tests__/services/abstract-microservice-test.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import ConsoleLogDriver from '@drivers/console-log';
99
import { MiddlewareHandler, MiddlewareType } from '@interfaces/services/i-abstract-microservice';
1010
import AbstractMicroservice from '@services/abstract-microservice';
1111
import Microservice from '@services/microservice';
12-
import RemoteMiddleware from '@services/remote-middleware';
1312

1413
const notSupposedMessage = 'was not supposed to succeed';
1514

@@ -18,7 +17,6 @@ describe('services/abstract-microservice', () => {
1817
name: 'tests',
1918
connection: 'http://my.local:8001',
2019
version: undefined,
21-
autoRegistrationGateway: null,
2220
};
2321
const ms = Microservice.create(options);
2422

@@ -105,18 +103,6 @@ describe('services/abstract-microservice', () => {
105103
sandbox.restore();
106104
});
107105

108-
it('should correct instantiate microservice without enable remote middlewares', () => {
109-
const sandbox = sinon.createSandbox();
110-
111-
sandbox.stub(Microservice, 'instance' as any).value(undefined);
112-
113-
const localMs = Microservice.create({ hasRemoteMiddlewareEndpoint: false });
114-
115-
sandbox.restore();
116-
117-
expect(localMs).to.property('endpoints').to.deep.equal({});
118-
});
119-
120106
it('should correct instantiate microservice with custom log driver', () => {
121107
const logDriver = () => ({ hello: 'world' });
122108
const sandbox = sinon.createSandbox();
@@ -416,10 +402,4 @@ describe('services/abstract-microservice', () => {
416402
[MiddlewareType.response]: [middlewareHandlerAfter],
417403
});
418404
});
419-
420-
it('should correct return remote middleware service instance', () => {
421-
const service = ms.getRemoteMiddlewareService();
422-
423-
expect(service).to.instanceof(RemoteMiddleware);
424-
});
425405
});

__tests__/services/gateway-test.ts

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,8 @@ import type { SinonStub } from 'sinon';
77
import sinon from 'sinon';
88
import { EXCEPTION_CODE } from '@constants/index';
99
import MicroserviceResponse from '@core/microservice-response';
10-
import {
11-
IEndpointHandler,
12-
IEndpointOptions,
13-
MiddlewareHandler,
14-
MiddlewareType,
15-
} from '@interfaces/services/i-abstract-microservice';
16-
import { AutoRegistrationAction, IExpressRequest } from '@interfaces/services/i-gateway';
10+
import { MiddlewareHandler, MiddlewareType } from '@interfaces/services/i-abstract-microservice';
11+
import { IExpressRequest } from '@interfaces/services/i-gateway';
1712
import AbstractMicroservice from '@services/abstract-microservice';
1813
import Gateway from '@services/gateway';
1914

@@ -49,10 +44,6 @@ describe('services/gateway', () => {
4944
const msName2 = 'ms2';
5045
const msHandler = () => new MicroserviceResponse() as unknown as Promise<MicroserviceResponse>;
5146

52-
// Only after start microservice
53-
let autoRegistrationHandler: IEndpointHandler;
54-
const autoRegistrationSender = 'test';
55-
5647
before(() => {
5748
sinon.stub(console, 'info');
5849
});
@@ -95,22 +86,6 @@ describe('services/gateway', () => {
9586
expect(infoRoute).to.undefined;
9687
});
9788

98-
it('should correct start microservice without auto registration endpoint', async () => {
99-
const sandbox = sinon.createSandbox();
100-
101-
sandbox.stub(ms.getExpress(), 'listen').returns({ close: sinon.stub() } as unknown as Server);
102-
sandbox.stub(axios, 'request').rejects(new Error('ECONNREFUSED'));
103-
sandbox.stub(Gateway, 'instance' as any).value(undefined);
104-
105-
const localMs = Gateway.create({ hasAutoRegistrationEndpoint: false });
106-
107-
await localMs.start();
108-
109-
sandbox.restore();
110-
111-
expect(localMs).to.property('endpoints').have.not.property('register-microservice');
112-
});
113-
11489
it('should correct register microservice handler', () => {
11590
ms.addMicroservice(msName, msHandler);
11691
ms.addMicroservice(msName2);
@@ -166,51 +141,13 @@ describe('services/gateway', () => {
166141
stubbed.restore();
167142
stubbedAxios.restore();
168143

169-
const { handler } = ms['endpoints'][ms['autoRegistrationEndpoint']];
170-
171-
autoRegistrationHandler = handler;
172-
173144
const [port, host, funcLog] = stubbed.firstCall.args as unknown as [string, string, () => void];
174145

175146
expect(port).to.equal(3000);
176147
expect(host).to.equal('0.0.0.0');
177148
expect(() => funcLog()).to.not.throw();
178149
});
179150

180-
it('should correct add auto registration endpoint', () => {
181-
expect(ms).to.property('endpoints').have.property('register-microservice');
182-
});
183-
184-
it('should correct auto register microservice', async () => {
185-
await autoRegistrationHandler({ action: AutoRegistrationAction.ADD }, {
186-
sender: autoRegistrationSender,
187-
} as IEndpointOptions);
188-
189-
expect(ms).to.property('microservices').have.property(autoRegistrationSender);
190-
});
191-
192-
it('should throw errors if incorrect try auto register microservice', () => {
193-
expect(() =>
194-
autoRegistrationHandler({ action: 'unknown' }, {
195-
sender: autoRegistrationSender,
196-
} as IEndpointOptions),
197-
).to.throw();
198-
expect(() =>
199-
autoRegistrationHandler({ action: AutoRegistrationAction.REMOVE }, {
200-
sender: '',
201-
} as IEndpointOptions),
202-
).to.throw();
203-
});
204-
205-
it('should correct auto cancel microservice registration', async () => {
206-
await autoRegistrationHandler({ action: AutoRegistrationAction.REMOVE }, {
207-
sender: autoRegistrationSender,
208-
} as IEndpointOptions);
209-
210-
expect(ms).to.property('microservices').not.have.property(autoRegistrationSender);
211-
});
212-
// end
213-
214151
it('should correct response info route', () => {
215152
const infoRoute = _.findLast(ms.getExpress()._router.stack, {
216153
route: { path: '/', methods: { get: true } },
Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import axios from 'axios';
21
import { expect } from 'chai';
32
import sinon from 'sinon';
43
import AbstractMicroservice from '@services/abstract-microservice';
@@ -19,95 +18,4 @@ describe('services/microservice', () => {
1918
expect(ms).instanceof(Microservice);
2019
expect(ms).instanceof(AbstractMicroservice);
2120
});
22-
23-
it('should correct register microservice at gateway', async () => {
24-
const stubbed = sinon.stub(ms, 'sendRequest').resolves();
25-
const stubbedExit = sinon.stub(ms, 'onExit');
26-
const stubbedCancellation = sinon.stub(ms, 'gatewayRegisterCancel');
27-
28-
await ms.gatewayRegister('gateway');
29-
30-
stubbed.restore();
31-
stubbedExit.restore();
32-
33-
const onExitCallback = stubbedExit.firstCall.firstArg;
34-
35-
await onExitCallback();
36-
37-
stubbedCancellation.restore();
38-
39-
expect(stubbed).to.callCount(1);
40-
expect(stubbedExit).to.callCount(1);
41-
expect(stubbedCancellation).to.callCount(1);
42-
});
43-
44-
it('should correct register microservice at gateway without cancellation', async () => {
45-
const stubbed = sinon.stub(ms, 'sendRequest').resolves();
46-
const stubbedExit = sinon.stub(ms, 'onExit');
47-
48-
await ms.gatewayRegister('gateway', {
49-
shouldCancelRegister: false,
50-
});
51-
52-
stubbed.restore();
53-
stubbedExit.restore();
54-
55-
expect(stubbed).to.callCount(1);
56-
expect(stubbedExit).to.callCount(0);
57-
});
58-
59-
it('should correct cancel microservice registration at gateway', async () => {
60-
const stubbed = sinon.stub(ms, 'sendRequest').resolves();
61-
62-
await ms.gatewayRegisterCancel('gateway');
63-
64-
stubbed.restore();
65-
66-
const args = stubbed.firstCall.lastArg;
67-
68-
expect(stubbed).to.callCount(1);
69-
expect(args.reqParams.headers).to.include({ type: 'pub' });
70-
});
71-
72-
it('should throw register remote middleware - before/after', () => {
73-
expect(() => ms.addEndpointMiddlewareBefore('a', () => ({}))).to.throw();
74-
expect(() => ms.addEndpointMiddlewareBefore('a', () => ({}), '')).to.throw();
75-
76-
expect(() => ms.addEndpointMiddlewareAfter('a', () => ({}))).to.throw();
77-
expect(() => ms.addEndpointMiddlewareAfter('a', () => ({}), '')).to.throw();
78-
});
79-
80-
it('should correct register remote middleware - before/after', async () => {
81-
const sandbox = sinon.createSandbox();
82-
const stubbedEndpoint = sandbox.stub(ms, 'addEndpoint');
83-
const stubbedRemote = sandbox.stub(ms.getRemoteMiddlewareService(), 'registerRemote');
84-
85-
await ms.addEndpointMiddlewareBefore('world', () => ({}), 'micro');
86-
await ms.addEndpointMiddlewareAfter('world', () => ({}), 'micro');
87-
88-
sandbox.restore();
89-
90-
expect(stubbedEndpoint).to.calledTwice;
91-
expect(stubbedRemote).to.calledTwice;
92-
});
93-
94-
it('should correct start microservice with auto registration at gateway', async () => {
95-
const sandbox = sinon.createSandbox();
96-
97-
sandbox.stub(Microservice, 'instance' as any).value(undefined);
98-
99-
const localMs = Microservice.create({ autoRegistrationGateway: 'gateway' });
100-
101-
const spy = sandbox.spy(localMs, 'gatewayRegister');
102-
103-
sinon.stub(axios, 'request').rejects(new Error('ECONNREFUSED'));
104-
// @ts-ignore
105-
sandbox.stub(localMs, 'startWorkers').resolves({});
106-
107-
await localMs.start();
108-
109-
sandbox.restore();
110-
111-
expect(spy).to.calledOnce;
112-
});
11321
});

0 commit comments

Comments
 (0)