Skip to content

Commit

Permalink
feat(repeater): eliminate 'Bus' term misuse
Browse files Browse the repository at this point in the history
closes #196
  • Loading branch information
ostridm committed Jun 18, 2024
1 parent 26492b8 commit b738876
Show file tree
Hide file tree
Showing 19 changed files with 163 additions and 341 deletions.
50 changes: 0 additions & 50 deletions packages/repeater/src/bus/DefaultRepeaterBusFactory.spec.ts

This file was deleted.

35 changes: 0 additions & 35 deletions packages/repeater/src/bus/DefaultRepeaterBusFactory.ts

This file was deleted.

6 changes: 0 additions & 6 deletions packages/repeater/src/bus/RepeaterBus.ts

This file was deleted.

7 changes: 0 additions & 7 deletions packages/repeater/src/bus/RepeaterBusFactory.ts

This file was deleted.

7 changes: 0 additions & 7 deletions packages/repeater/src/bus/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DefaultRepeaterBus } from './DefaultRepeaterBus';
import { RunningStatus } from './Repeater';
import { DefaultRepeater } from './DefaultRepeater';
import { Protocol } from '../models/Protocol';
import { Request, Response } from '../request-runner';
import {
Expand All @@ -20,11 +21,11 @@ import {
} from 'ts-mockito';
import { EventEmitter } from 'events';

describe('DefaultRepeaterBus', () => {
describe('DefaultRepeater', () => {
const RepeaterId = 'fooId';

let events!: EventEmitter;
let sut!: DefaultRepeaterBus;
let sut!: DefaultRepeater;

const mockedRepeaterServer = mock<RepeaterServer>();
const repeaterCommands = mock<RepeaterCommands>();
Expand All @@ -48,7 +49,7 @@ describe('DefaultRepeaterBus', () => {
repeaterId: RepeaterId
});

sut = new DefaultRepeaterBus(
sut = new DefaultRepeater(
RepeaterId,
instance(mockedLogger),
instance(mockedRepeaterServer),
Expand All @@ -64,10 +65,10 @@ describe('DefaultRepeaterBus', () => {
)
);

describe('connect', () => {
it('should connect', async () => {
describe('start', () => {
it('should start', async () => {
// act
await sut.connect();
await sut.start();

// assert
verify(mockedRepeaterServer.connect()).once();
Expand All @@ -78,23 +79,12 @@ describe('DefaultRepeaterBus', () => {
).once();
});

it('should allow connect more than once', async () => {
// arrange
await sut.connect();

// act
const act = sut.connect();

// assert
await expect(act).resolves.not.toThrow();
});

it('should throw when underlying connect throws', async () => {
// arrange
when(mockedRepeaterServer.connect()).thenReject(new Error('foo'));

// act
const act = () => sut.connect();
const act = () => sut.start();

// assert
await expect(act).rejects.toThrowError('foo');
Expand All @@ -107,27 +97,104 @@ describe('DefaultRepeaterBus', () => {
);

// act
const act = () => sut.connect();
const act = () => sut.start();

// assert
await expect(act).rejects.toThrowError('foo');
});

it('should have RunningStatus.STARTING just after start() call', () => {
// act
void sut.start();

// assert
expect(sut.runningStatus).toBe(RunningStatus.STARTING);
});

it('should have RunningStatus.RUNNING after successful start()', async () => {
// act
await sut.start();

// assert
expect(sut.runningStatus).toBe(RunningStatus.RUNNING);
});

it('should throw an error on start() twice', async () => {
// arrange
await sut.start();

// act
const res = sut.start();

// assert
await expect(res).rejects.toThrow('Repeater is already active.');
});

it('should be possible to start() after start() error', async () => {
// act
when(mockedRepeaterServer.connect()).thenReject().thenResolve();

// assert
await expect(sut.start()).rejects.toThrow();
await expect(sut.start()).resolves.not.toThrow();
});
});

describe('close', () => {
it('should close', async () => {
describe('stop', () => {
it('should stop', async () => {
// arrange
await sut.start();

// act
await sut.close();
await sut.stop();

// assert
verify(mockedRepeaterServer.disconnect()).once();
});

it('should have RunningStatus.OFF after start() and stop()', async () => {
// arrange
await sut.start();

// act
await sut.stop();

// assert
expect(sut.runningStatus).toBe(RunningStatus.OFF);
});

it('should do nothing on stop() without start()', async () => {
// act
await sut.stop();

// assert
expect(sut.runningStatus).toBe(RunningStatus.OFF);
});

it('should do nothing on second stop() call', async () => {
// arrange
await sut.start();
await sut.stop();

// assert
await sut.stop();

// assert
expect(sut.runningStatus).toBe(RunningStatus.OFF);
});
});

describe('runningStatus', () => {
it('should have RunningStatus.OFF initially', () => {
// assert
expect(sut.runningStatus).toBe(RunningStatus.OFF);
});
});

describe('events', () => {
it(`should subscribe to ${RepeaterServerEvents.UPDATE_AVAILABLE}`, async () => {
// arrange
await sut.connect();
await sut.start();

// act
events.emit(RepeaterServerEvents.UPDATE_AVAILABLE, { version: '1.0.0' });
Expand Down Expand Up @@ -159,7 +226,7 @@ describe('DefaultRepeaterBus', () => {
})
);

await sut.connect();
await sut.start();

// act
events.emit(RepeaterServerEvents.REQUEST, requestEvent);
Expand All @@ -171,7 +238,7 @@ describe('DefaultRepeaterBus', () => {

it(`should subscribe to ${RepeaterServerEvents.RECONNECT_ATTEMPT}`, async () => {
// arrange
await sut.connect();
await sut.start();

// act
events.emit(RepeaterServerEvents.RECONNECT_ATTEMPT, {
Expand All @@ -191,7 +258,7 @@ describe('DefaultRepeaterBus', () => {

it(`should subscribe to ${RepeaterServerEvents.ERROR} and proceed on error`, async () => {
// arrange
await sut.connect();
await sut.start();

// act
events.emit(RepeaterServerEvents.ERROR, {
Expand All @@ -205,7 +272,7 @@ describe('DefaultRepeaterBus', () => {

it(`should subscribe to ${RepeaterServerEvents.ERROR} and proceed on critical error`, async () => {
// arrange
await sut.connect();
await sut.start();

// act
events.emit(RepeaterServerEvents.ERROR, {
Expand All @@ -229,7 +296,7 @@ describe('DefaultRepeaterBus', () => {
it(`should subscribe to ${RepeaterServerEvents.RECONNECTION_FAILED}`, async () => {
// arrange
const error = new Error('test error');
await sut.connect();
await sut.start();

// act
events.emit(RepeaterServerEvents.RECONNECTION_FAILED, {
Expand All @@ -243,7 +310,7 @@ describe('DefaultRepeaterBus', () => {

it(`should subscribe to ${RepeaterServerEvents.RECONNECTION_SUCCEEDED}`, async () => {
// arrange
await sut.connect();
await sut.start();

// act
events.emit(RepeaterServerEvents.RECONNECTION_SUCCEEDED);
Expand Down
Loading

0 comments on commit b738876

Please sign in to comment.