Skip to content

Commit 490e8ab

Browse files
authored
Send-only bus (#59)
* Fixed PostgreSQL transport example * Added bootstrap for a send-only bus * Added checks for the send-only bus initialization It can only be initialized once and it must not have any registered handlers. * Test that no handlers were added for send-only bus
1 parent 2bf3cb5 commit 490e8ab

2 files changed

Lines changed: 54 additions & 5 deletions

File tree

packages/bus-core/src/application-bootstrap/application-bootstrap.spec.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { ApplicationBootstrap } from './application-bootstrap'
22
import { Mock, IMock, Times, It } from 'typemoq'
33
import { Bus } from '../service-bus'
4-
import { HandlerRegistry } from '../handler'
4+
import { HandlerRegistry, HandlerResolver } from '../handler'
55
import { Logger } from '@node-ts/logger-core'
66
import { Container } from 'inversify'
77
import { TestCommandHandler, TestCommand } from '../test'
88
import { Transport } from '../transport'
9-
import { Message } from '@node-ts/bus-messages'
109

1110
describe('ApplicationBootstrap', () => {
1211
let sut: ApplicationBootstrap
@@ -50,6 +49,39 @@ describe('ApplicationBootstrap', () => {
5049
})
5150
})
5251

52+
describe('when initializing send only', () => {
53+
let container: IMock<Container>
54+
describe('that starts successfully', () => {
55+
beforeEach(async () => {
56+
container = Mock.ofType<Container>()
57+
await sut.initializeSendOnly()
58+
})
59+
it('should bind no handlers to the IoC container', () => {
60+
handlerRegistry.verify(
61+
h => h.bindHandlersToContainer(container.object),
62+
Times.never()
63+
)
64+
})
65+
it('should not start the bus', () => {
66+
bus.verify(
67+
async b => b.start(),
68+
Times.never()
69+
)
70+
})
71+
it('should throw an exception when initializing twice', async () => {
72+
await expect(sut.initializeSendOnly()).rejects.toThrowError()
73+
})
74+
})
75+
describe('when handlers have been registered', () => {
76+
it('should throw an error', async () => {
77+
handlerRegistry
78+
.setup(h => h.messageSubscriptions)
79+
.returns(() => [{} as HandlerResolver])
80+
await expect(sut.initializeSendOnly()).rejects.toThrowError()
81+
})
82+
})
83+
})
84+
5385
describe('when registering handlers', () => {
5486
beforeEach(() => {
5587
sut.registerHandler(TestCommandHandler)

packages/bus-core/src/application-bootstrap/application-bootstrap.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,25 @@ export class ApplicationBootstrap {
2727
}
2828
this.logger.info('Initializing bus application...')
2929
this.handlerRegistry.bindHandlersToContainer(container)
30-
if (this.transport.initialize) {
31-
await this.transport.initialize(this.handlerRegistry)
32-
}
30+
await this.initializeTransport()
3331
await this.bus.start()
3432
this.isInitialized = true
3533
this.logger.info('Bus application initialized')
3634
}
3735

36+
async initializeSendOnly (): Promise<void> {
37+
if (this.isInitialized) {
38+
throw new Error('Application already initialized')
39+
}
40+
if (this.handlerRegistry.messageSubscriptions.length > 0) {
41+
throw new Error('A send-only bus cannot have registered handlers')
42+
}
43+
this.logger.info('Initializing send only bus application...')
44+
await this.initializeTransport()
45+
this.isInitialized = true
46+
this.logger.info('Send only bus application initialized')
47+
}
48+
3849
async dispose (): Promise<void> {
3950
if (!this.isInitialized) {
4051
throw new Error('Application has not been initialized')
@@ -72,4 +83,10 @@ export class ApplicationBootstrap {
7283
prototype.$topicIdentifier
7384
)
7485
}
86+
87+
private async initializeTransport (): Promise<void> {
88+
if (this.transport.initialize) {
89+
await this.transport.initialize(this.handlerRegistry)
90+
}
91+
}
7592
}

0 commit comments

Comments
 (0)