-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e82473
commit adf0957
Showing
29 changed files
with
692 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@totalsoft/message-bus': minor | ||
--- | ||
|
||
Added JetStream messaging transport for message-bus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) TotalSoft. | ||
// This source code is licensed under the MIT license. | ||
|
||
import type nats from 'nats' | ||
|
||
const natsMock: any = jest.createMockFromModule<jest.Mocked<typeof nats>>('nats') | ||
|
||
const natsConsumerMock: any = { | ||
info: jest.fn().mockImplementation(() => ({ config: {} })), | ||
consume: jest.fn().mockResolvedValue({}) | ||
} | ||
|
||
const jetStreamClientMock: any = { | ||
publish: jest.fn().mockResolvedValue({}), | ||
jetstreamManager: jest.fn().mockImplementation(() => ({ | ||
consumers: { add: jest.fn().mockResolvedValue({}) } | ||
})), | ||
consumers: { | ||
get: jest.fn().mockResolvedValue(natsConsumerMock) | ||
} | ||
} | ||
natsMock.connect = jest.fn().mockResolvedValue({ | ||
close: jest.fn().mockResolvedValue(undefined), | ||
closed: jest.fn().mockReturnValue(new Promise(() => {})), | ||
jetstream: jest.fn().mockImplementation(() => jetStreamClientMock) | ||
}) | ||
|
||
natsMock.StringCodec = jest.fn().mockImplementation(() => ({ | ||
encode: jest.fn() | ||
})) | ||
|
||
export default { ...natsMock, __jetStreamClientMock: jetStreamClientMock, __natsConsumerMock: natsConsumerMock } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
// Copyright (c) TotalSoft. | ||
// This source code is licensed under the MIT license. | ||
|
||
const nodeNatsStreaming: any = jest.createMockFromModule('node-nats-streaming') | ||
|
||
export default nodeNatsStreaming |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) TotalSoft. | ||
// This source code is licensed under the MIT license. | ||
|
||
process.env.JETSTREAM_URL = 'localhost:4222' | ||
import { ensureStreamsExist } from './util' | ||
import { messageBus, useTransport, transport } from '../../src' | ||
|
||
async function main() { | ||
await ensureStreamsExist() | ||
|
||
useTransport(transport.jetstream) | ||
const msgBus = messageBus() | ||
await msgBus.publish('events.my-event1', { asa: 'asa' }) | ||
await msgBus.transport.disconnect() | ||
} | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!-- | ||
Copyright (c) TotalSoft. | ||
This source code is licensed under the MIT license. | ||
--> | ||
|
||
# Jetstream samples | ||
|
||
Before running the samples, you can start a Nats server docker container by running the following: | ||
``` | ||
yarn docker-run-jestream | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) TotalSoft. | ||
// This source code is licensed under the MIT license. | ||
|
||
import { ensureStreamsExist } from './util' | ||
import { messageBus, useTransport, transport } from '../../src' | ||
|
||
async function main() { | ||
await ensureStreamsExist() | ||
|
||
useTransport(transport.jetstream) | ||
const msgBus = messageBus() | ||
|
||
const sub = await msgBus.subscribe('events.my-event1', _msg=> Promise.resolve()) | ||
await new Promise(r => setTimeout(r, 20000)) | ||
await sub.unsubscribe() | ||
await msgBus.transport.disconnect() | ||
} | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) TotalSoft. | ||
// This source code is licensed under the MIT license. | ||
|
||
process.env.JETSTREAM_URL = 'localhost:4222' | ||
process.env.Messaging__Env = '' | ||
process.env.Messaging__TopicPrefix = '' | ||
process.env.JETSTREAM_COMMANDS_STREAM = 'commands' | ||
process.env.JETSTREAM_EVENTS_STREAM = 'events' | ||
process.env.JETSTREAM_CLIENT_ID = 'rocket-samples' | ||
|
||
process.env.JETSTREAM_STREAM_PROCESSOR_AckWaitTime = '5000000000000000' | ||
|
||
import { transport } from '../../src' | ||
import { JetstreamConnection } from '../../src/transport/jetstream/types' | ||
|
||
|
||
export async function ensureStreamsExist() { | ||
const jc = <JetstreamConnection>await transport.jetstream.connect() | ||
const nc = jc._natsConnection | ||
if (!nc) { | ||
throw new Error('Nats connection not set') | ||
} | ||
const jsm = await nc.jetstreamManager() | ||
await jsm.streams.add({ name: 'events', subjects: ['events.>', 'ch.events.>'] }) | ||
} |
53 changes: 53 additions & 0 deletions
53
packages/message-bus/__tests__/transport/jetstream.tests.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) TotalSoft. | ||
// This source code is licensed under the MIT license. | ||
|
||
process.env.JETSTREAM_EVENTS_STREAM = 'events' | ||
|
||
import nats from '../../__mocks__/nats' | ||
import jetstream from '../../src/transport/jetstream' | ||
import { serDes, SubscriptionOptions } from '../../src' | ||
|
||
describe('Testing Jetstream transport', () => { | ||
// afterEach(async () => { | ||
// await jetstream.disconnect() | ||
// jest.resetAllMocks() | ||
// }) | ||
|
||
test('connections are opened once', async () => { | ||
// arrange | ||
|
||
// act | ||
await Promise.all([jetstream.connect(), jetstream.connect(), jetstream.connect()]) | ||
await jetstream.connect() | ||
await jetstream.connect() | ||
|
||
// assert | ||
expect(nats.connect).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
test('publish a message', async () => { | ||
// arrange | ||
const subject = 'subject' | ||
const envelope = { payload: {}, headers: {} } | ||
|
||
// act | ||
await jetstream.publish(subject, envelope, serDes) | ||
|
||
// assert | ||
expect(nats.connect).toBeCalled() | ||
expect(nats.__jetStreamClientMock.publish).toBeCalled() | ||
}) | ||
|
||
test('subscribe to a channel', async () => { | ||
// arrange | ||
const subject = 'events.my.event' | ||
const handler = jest.fn() | ||
|
||
// act | ||
await jetstream.subscribe(subject, handler, SubscriptionOptions.PUB_SUB, serDes) | ||
|
||
// assert | ||
expect(nats.connect).toBeCalled() | ||
expect(nats.__natsConsumerMock.consume).toBeCalled() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
// Copyright (c) TotalSoft. | ||
// This source code is licensed under the MIT license. | ||
|
||
export * from './messageBus' | ||
export * from './types' | ||
export * from './transport' | ||
export { default as transport } from './transport' | ||
export * from './envelope' | ||
export { default as serDes } from './serDes' | ||
export * from './topicRegistry' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.