@toa.io/userland 1.0.0-alpha.28 → 1.0.0-alpha.283
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +329 -0
- package/example/components/echo/manifest.toa.yaml +14 -5
- package/example/components/echo/operations/get.js +1 -5
- package/example/components/echo/operations/resolve.js +1 -5
- package/example/components/echo/operations/signal.js +1 -5
- package/example/components/math.calculations/manifest.toa.yaml +17 -6
- package/example/components/math.calculations/operations/add.js +1 -5
- package/example/components/math.calculations/operations/increment.js +1 -5
- package/example/components/math.proxy/manifest.toa.yaml +8 -3
- package/example/components/math.proxy/operations/add.js +1 -5
- package/example/components/tea.pots/manifest.toa.yaml +37 -9
- package/example/components/tea.pots/operations/same.js +1 -5
- package/example/components/tea.pots/receivers/store.orders.created.js +1 -5
- package/example/components/web/manifest.toa.yaml +2 -4
- package/example/components/web/operations/get.js +2 -6
- package/example/context.toa.yaml +0 -5
- package/example/stage/call.test.js +14 -10
- package/example/stage/events.test.js +63 -16
- package/example/stage/invoke.test.js +11 -8
- package/package.json +18 -12
- package/stage/package.json +1 -0
- package/stage/src/component.js +3 -12
- package/stage/src/composition.js +3 -11
- package/stage/src/index.js +7 -17
- package/stage/src/manifest.js +2 -6
- package/stage/src/remote.js +11 -8
- package/stage/src/service.js +10 -9
- package/stage/src/shutdown.js +2 -8
- package/stage/src/state.js +1 -4
- package/stage/test/boot.mock.js +9 -11
- package/stage/test/component.test.js +16 -10
- package/stage/test/composition.test.js +11 -20
- package/stage/test/manifest.test.js +11 -8
- package/stage/test/remote.test.js +14 -12
- package/stage/test/shutdown.test.js +26 -22
- package/stage/test/state.mock.js +5 -6
- package/stage/test/state.test.js +10 -9
- package/stage/types/index.d.ts +0 -2
- package/stage/src/binding/binding.js +0 -50
- package/stage/src/binding/consumer.js +0 -32
- package/stage/src/binding/emitter.js +0 -27
- package/stage/src/binding/factory.js +0 -49
- package/stage/src/binding/index.js +0 -10
- package/stage/src/binding/label.js +0 -12
- package/stage/src/binding/producer.js +0 -43
- package/stage/src/binding/receiver.js +0 -34
- package/stage/test/binding/binding.test.js +0 -15
- package/stage/test/binding/consumer.test.js +0 -52
- package/stage/test/binding/emitter.test.js +0 -78
- package/stage/test/binding/producer.fixtures.js +0 -10
- package/stage/test/binding/producer.test.js +0 -49
- package/stage/test/binding/receiver.fixtures.js +0 -7
- package/stage/test/binding/receiver.test.js +0 -54
- package/stage/test/binding.mock.js +0 -8
- package/stage/types/binding.d.ts +0 -20
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
|
|
1
|
+
import { it, beforeEach, mock as mocking } from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
import { generate } from 'randomstring'
|
|
5
|
+
import * as _boot from './boot.mock.js'
|
|
6
|
+
import * as _state from './state.mock.js'
|
|
4
7
|
|
|
5
8
|
const mock = {
|
|
6
|
-
boot:
|
|
7
|
-
state:
|
|
8
|
-
binding: require('./binding.mock')
|
|
9
|
+
boot: _boot,
|
|
10
|
+
state: _state
|
|
9
11
|
}
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
jest.mock('../src/binding', () => mock.binding)
|
|
13
|
+
mocking.module('@toa.io/boot', { namedExports: mock.boot })
|
|
14
|
+
mocking.module('../src/state', { namedExports: mock.state })
|
|
14
15
|
|
|
15
|
-
const { state } =
|
|
16
|
-
const
|
|
17
|
-
const stage = require('../')
|
|
16
|
+
const { state } = await import('../src/state.js')
|
|
17
|
+
const stage = await import('../src/index.js')
|
|
18
18
|
|
|
19
19
|
beforeEach(() => {
|
|
20
|
-
|
|
20
|
+
resetCalls()
|
|
21
21
|
})
|
|
22
22
|
|
|
23
23
|
it('should be', () => {
|
|
24
|
-
|
|
24
|
+
assert.notStrictEqual(stage.shutdown, undefined)
|
|
25
25
|
})
|
|
26
26
|
|
|
27
27
|
it('should disconnect components', async () => {
|
|
@@ -30,7 +30,7 @@ it('should disconnect components', async () => {
|
|
|
30
30
|
|
|
31
31
|
await stage.shutdown()
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
assert.ok(component.disconnect.mock.callCount() > 0)
|
|
34
34
|
})
|
|
35
35
|
|
|
36
36
|
it('should disconnect compositions', async () => {
|
|
@@ -39,9 +39,9 @@ it('should disconnect compositions', async () => {
|
|
|
39
39
|
await stage.composition(paths)
|
|
40
40
|
await stage.shutdown()
|
|
41
41
|
|
|
42
|
-
const composition = await mock.boot.composition.mock.
|
|
42
|
+
const composition = await mock.boot.composition.mock.calls[0].result
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
assert.ok(composition.disconnect.mock.callCount() > 0)
|
|
45
45
|
})
|
|
46
46
|
|
|
47
47
|
it('should disconnect remotes', async () => {
|
|
@@ -50,17 +50,21 @@ it('should disconnect remotes', async () => {
|
|
|
50
50
|
const remote = await stage.remote(id)
|
|
51
51
|
await stage.shutdown()
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
assert.ok(remote.disconnect.mock.callCount() > 0)
|
|
54
54
|
})
|
|
55
55
|
|
|
56
56
|
it('should reset state', async () => {
|
|
57
57
|
await stage.shutdown()
|
|
58
58
|
|
|
59
|
-
|
|
59
|
+
assert.ok(state.reset.mock.callCount() > 0)
|
|
60
60
|
})
|
|
61
61
|
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
function resetCalls (target = [assert, mock, stage], seen = new Set()) {
|
|
63
|
+
if (target === null || typeof target !== 'object' || seen.has(target)) return
|
|
64
64
|
|
|
65
|
-
|
|
66
|
-
|
|
65
|
+
seen.add(target)
|
|
66
|
+
|
|
67
|
+
for (const value of Object.values(target))
|
|
68
|
+
if (typeof value === 'function' && value.mock !== undefined) value.mock.resetCalls()
|
|
69
|
+
else resetCalls(value, seen)
|
|
70
|
+
}
|
package/stage/test/state.mock.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import { mock } from 'node:test'
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
// this module defines the replacement, so it still sees the real one
|
|
4
|
+
import * as original from '../src/state.js'
|
|
4
5
|
|
|
5
|
-
const reset =
|
|
6
|
-
const state = { ...original.state, reset }
|
|
7
|
-
|
|
8
|
-
module.exports = { state }
|
|
6
|
+
const reset = mock.fn(() => original.state.reset())
|
|
7
|
+
export const state = { ...original.state, reset }
|
package/stage/test/state.test.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
import { it } from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
import { state } from '../src/state.js'
|
|
4
5
|
|
|
5
6
|
it('should be', () => {
|
|
6
|
-
|
|
7
|
+
assert.notStrictEqual(state, undefined)
|
|
7
8
|
})
|
|
8
9
|
|
|
9
10
|
it('should reset', () => {
|
|
@@ -11,9 +12,9 @@ it('should reset', () => {
|
|
|
11
12
|
const composition = /** @type {toa.core.Connector} */ 2
|
|
12
13
|
const remote = /** @type {toa.core.Component} */ 3
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
assert.deepStrictEqual(state.components.length, 0)
|
|
16
|
+
assert.deepStrictEqual(state.compositions.length, 0)
|
|
17
|
+
assert.deepStrictEqual(state.remotes.length, 0)
|
|
17
18
|
|
|
18
19
|
state.components.push(component)
|
|
19
20
|
state.compositions.push(composition)
|
|
@@ -21,7 +22,7 @@ it('should reset', () => {
|
|
|
21
22
|
|
|
22
23
|
state.reset()
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
assert.deepStrictEqual(state.components.length, 0)
|
|
26
|
+
assert.deepStrictEqual(state.compositions.length, 0)
|
|
27
|
+
assert.deepStrictEqual(state.remotes.length, 0)
|
|
27
28
|
})
|
package/stage/types/index.d.ts
CHANGED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @implements {toa.stage.binding.Binding}
|
|
5
|
-
*/
|
|
6
|
-
class Binding {
|
|
7
|
-
/** @type {Record<string, function[]>} */
|
|
8
|
-
#callbacks = {}
|
|
9
|
-
|
|
10
|
-
/** @type {Record<string, function>} */
|
|
11
|
-
#producers = {}
|
|
12
|
-
|
|
13
|
-
async subscribe (label, callback) {
|
|
14
|
-
if (this.#callbacks[label] === undefined) this.#callbacks[label] = []
|
|
15
|
-
|
|
16
|
-
this.#callbacks[label].push(callback)
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
async emit (label, message) {
|
|
20
|
-
if (!(label in this.#callbacks)) return
|
|
21
|
-
|
|
22
|
-
const callbacks = this.#callbacks[label]
|
|
23
|
-
const promises = callbacks.map((callback) => callback(message))
|
|
24
|
-
|
|
25
|
-
await Promise.all(promises)
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
async reply (label, produce) {
|
|
29
|
-
if (label in this.#producers) throw new Error(`Label '${label}' is already bound`)
|
|
30
|
-
|
|
31
|
-
this.#producers[label] = produce
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
async request (label, request) {
|
|
35
|
-
if (!(label in this.#producers)) throw new Error(`Label '${label}' is not bound`)
|
|
36
|
-
|
|
37
|
-
const produce = this.#producers[label]
|
|
38
|
-
|
|
39
|
-
return produce(request)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
reset () {
|
|
43
|
-
this.#callbacks = {}
|
|
44
|
-
this.#producers = {}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const binding = new Binding()
|
|
49
|
-
|
|
50
|
-
exports.binding = binding
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { Connector } = require('@toa.io/core')
|
|
4
|
-
|
|
5
|
-
const { binding } = require('./binding')
|
|
6
|
-
const { label } = require('./label')
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* @implements {toa.core.bindings.Consumer}
|
|
10
|
-
*/
|
|
11
|
-
class Consumer extends Connector {
|
|
12
|
-
/** @type {string} */
|
|
13
|
-
#label
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* @param {toa.core.Locator} locator
|
|
17
|
-
* @param {string} endpoint
|
|
18
|
-
*/
|
|
19
|
-
constructor (locator, endpoint) {
|
|
20
|
-
super()
|
|
21
|
-
|
|
22
|
-
this.#label = label(locator, endpoint)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
async request (request) {
|
|
26
|
-
request = JSON.parse(JSON.stringify(request))
|
|
27
|
-
|
|
28
|
-
return binding.request(this.#label, request)
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
exports.Consumer = Consumer
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { Connector } = require('@toa.io/core')
|
|
4
|
-
|
|
5
|
-
const { binding } = require('./binding')
|
|
6
|
-
const { label } = require('./label')
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* @implements {toa.core.bindings.Emitter}
|
|
10
|
-
*/
|
|
11
|
-
class Emitter extends Connector {
|
|
12
|
-
/** @type {string} */
|
|
13
|
-
#label
|
|
14
|
-
|
|
15
|
-
constructor (locator, endpoint) {
|
|
16
|
-
super()
|
|
17
|
-
|
|
18
|
-
this.#label = label(locator, endpoint)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
async emit (message) {
|
|
22
|
-
message = JSON.parse(JSON.stringify(message))
|
|
23
|
-
await binding.emit(this.#label, message)
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
exports.Emitter = Emitter
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { Emitter } = require('./emitter')
|
|
4
|
-
const { Receiver } = require('./receiver')
|
|
5
|
-
const { Consumer } = require('./consumer')
|
|
6
|
-
const { Producer } = require('./producer')
|
|
7
|
-
|
|
8
|
-
class Factory {
|
|
9
|
-
/**
|
|
10
|
-
* @param {toa.core.Locator} source
|
|
11
|
-
* @param {string} label
|
|
12
|
-
* @param {string} group
|
|
13
|
-
* @param {toa.core.Receiver} receiver
|
|
14
|
-
* @returns {toa.core.Connector}
|
|
15
|
-
*/
|
|
16
|
-
receiver (source, label, group, receiver) {
|
|
17
|
-
return new Receiver(label, receiver)
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* @param {toa.core.Locator} locator
|
|
22
|
-
* @param {string} label
|
|
23
|
-
* @returns {toa.core.bindings.Emitter}
|
|
24
|
-
*/
|
|
25
|
-
emitter (locator, label) {
|
|
26
|
-
return new Emitter(locator, label)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* @param {toa.core.Locator} locator
|
|
31
|
-
* @param {string} endpoint
|
|
32
|
-
* @returns {toa.core.bindings.Consumer}
|
|
33
|
-
*/
|
|
34
|
-
consumer (locator, endpoint) {
|
|
35
|
-
return new Consumer(locator, endpoint)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* @param {toa.core.Locator} locator
|
|
40
|
-
* @param {string[]} endpoints
|
|
41
|
-
* @param {toa.core.Component} component
|
|
42
|
-
* @returns {toa.core.Connector}
|
|
43
|
-
*/
|
|
44
|
-
producer (locator, endpoints, component) {
|
|
45
|
-
return new Producer(locator, endpoints, component)
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
exports.Factory = Factory
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { Connector } = require('@toa.io/core')
|
|
4
|
-
|
|
5
|
-
const { binding } = require('./binding')
|
|
6
|
-
const { label } = require('./label')
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* @implements {toa.core.Connector}
|
|
10
|
-
*/
|
|
11
|
-
class Producer extends Connector {
|
|
12
|
-
/** @type {toa.core.Locator} */
|
|
13
|
-
#locator
|
|
14
|
-
|
|
15
|
-
/** @type {string[]} */
|
|
16
|
-
#endpoints
|
|
17
|
-
|
|
18
|
-
/** @type {toa.core.Component} */
|
|
19
|
-
#component
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @param {toa.core.Locator} locator
|
|
23
|
-
* @param {string[]} endpoints
|
|
24
|
-
* @param {toa.core.Component} component
|
|
25
|
-
*/
|
|
26
|
-
constructor (locator, endpoints, component) {
|
|
27
|
-
super()
|
|
28
|
-
|
|
29
|
-
this.#locator = locator
|
|
30
|
-
this.#endpoints = endpoints
|
|
31
|
-
this.#component = component
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
async open () {
|
|
35
|
-
for (const endpoint of this.#endpoints) {
|
|
36
|
-
const command = label(this.#locator, endpoint)
|
|
37
|
-
|
|
38
|
-
await binding.reply(command, (request) => this.#component.invoke(endpoint, request))
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
exports.Producer = Producer
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { Connector } = require('@toa.io/core')
|
|
4
|
-
|
|
5
|
-
const { binding } = require('./binding')
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @implements {toa.core.Connector}
|
|
9
|
-
*/
|
|
10
|
-
class Receiver extends Connector {
|
|
11
|
-
/** @type {string} */
|
|
12
|
-
#label
|
|
13
|
-
|
|
14
|
-
/** @type {toa.core.Receiver} */
|
|
15
|
-
#receiver
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* @param {toa.core.Locator} locator
|
|
19
|
-
* @param {string} label
|
|
20
|
-
* @param {toa.core.Receiver} receiver
|
|
21
|
-
*/
|
|
22
|
-
constructor (label, receiver) {
|
|
23
|
-
super()
|
|
24
|
-
|
|
25
|
-
this.#label = label
|
|
26
|
-
this.#receiver = receiver
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
async open () {
|
|
30
|
-
await binding.subscribe(this.#label, (message) => this.#receiver.receive(message))
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
exports.Receiver = Receiver
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { properties, binding, Factory } = require('../../src').binding
|
|
4
|
-
|
|
5
|
-
it('should be async', async () => {
|
|
6
|
-
expect(properties.async).toStrictEqual(true)
|
|
7
|
-
})
|
|
8
|
-
|
|
9
|
-
it('should export Factory', async () => {
|
|
10
|
-
expect(Factory).toBeDefined()
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
it('should export binding instance', async () => {
|
|
14
|
-
expect(binding).toBeDefined()
|
|
15
|
-
})
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { generate } = require('randomstring')
|
|
4
|
-
const { Connector, Locator } = require('@toa.io/core')
|
|
5
|
-
|
|
6
|
-
const { binding, Factory } = require('../../src/binding')
|
|
7
|
-
|
|
8
|
-
const factory = new Factory()
|
|
9
|
-
|
|
10
|
-
it('should expose consumers factory', async () => {
|
|
11
|
-
expect(factory.consumer).toBeDefined()
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
/** @type {toa.core.bindings.Consumer} */
|
|
15
|
-
let consumer
|
|
16
|
-
|
|
17
|
-
/** @type {toa.core.Locator} */
|
|
18
|
-
let locator
|
|
19
|
-
|
|
20
|
-
/** @type {string} */
|
|
21
|
-
let label
|
|
22
|
-
|
|
23
|
-
const endpoint = generate()
|
|
24
|
-
|
|
25
|
-
beforeEach(() => {
|
|
26
|
-
jest.clearAllMocks()
|
|
27
|
-
binding.reset()
|
|
28
|
-
|
|
29
|
-
const namespace = generate()
|
|
30
|
-
const name = generate()
|
|
31
|
-
|
|
32
|
-
locator = new Locator(name, namespace)
|
|
33
|
-
label = locator.id + '.' + endpoint
|
|
34
|
-
consumer = factory.consumer(locator, endpoint)
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
it('should return Connector', async () => {
|
|
38
|
-
expect(consumer).toBeInstanceOf(Connector)
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
it('should return reply', async () => {
|
|
42
|
-
const request = generate()
|
|
43
|
-
const reply = generate()
|
|
44
|
-
const call = jest.fn(async () => reply)
|
|
45
|
-
|
|
46
|
-
await binding.reply(label, call)
|
|
47
|
-
|
|
48
|
-
const received = await consumer.request(request)
|
|
49
|
-
|
|
50
|
-
expect(call).toHaveBeenCalledWith(request)
|
|
51
|
-
expect(received).toStrictEqual(reply)
|
|
52
|
-
})
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { generate } = require('randomstring')
|
|
4
|
-
const {
|
|
5
|
-
Locator,
|
|
6
|
-
Connector
|
|
7
|
-
} = require('@toa.io/core')
|
|
8
|
-
|
|
9
|
-
const {
|
|
10
|
-
binding,
|
|
11
|
-
Factory
|
|
12
|
-
} = require('../../src/binding')
|
|
13
|
-
|
|
14
|
-
const factory = new Factory()
|
|
15
|
-
|
|
16
|
-
it('should expose emitters factory', async () => {
|
|
17
|
-
expect(factory.emitter).toBeDefined()
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
/** @type {toa.core.bindings.Emitter} */
|
|
21
|
-
let emitter
|
|
22
|
-
|
|
23
|
-
/** @type {string} */
|
|
24
|
-
let label
|
|
25
|
-
|
|
26
|
-
/** @type {toa.core.Locator} */
|
|
27
|
-
let locator
|
|
28
|
-
|
|
29
|
-
const endpoint = generate()
|
|
30
|
-
|
|
31
|
-
beforeEach(() => {
|
|
32
|
-
jest.clearAllMocks()
|
|
33
|
-
binding.reset()
|
|
34
|
-
|
|
35
|
-
const namespace = generate()
|
|
36
|
-
const name = generate()
|
|
37
|
-
|
|
38
|
-
locator = new Locator(name, namespace)
|
|
39
|
-
label = locator.id + '.' + endpoint
|
|
40
|
-
emitter = factory.emitter(locator, endpoint)
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
it('should return Connector', async () => {
|
|
44
|
-
expect(emitter).toBeInstanceOf(Connector)
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
it('should call receiver', async () => {
|
|
48
|
-
const callback = jest.fn(async () => undefined)
|
|
49
|
-
|
|
50
|
-
await binding.subscribe(label, callback)
|
|
51
|
-
|
|
52
|
-
const payload = { [generate()]: generate() }
|
|
53
|
-
|
|
54
|
-
/** @type {toa.core.Message} */
|
|
55
|
-
const message = { payload }
|
|
56
|
-
|
|
57
|
-
await emitter.emit(message)
|
|
58
|
-
|
|
59
|
-
expect(callback).toHaveBeenCalledWith(message)
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
it('should not modify message', async () => {
|
|
63
|
-
const callback = jest.fn((payload) => {
|
|
64
|
-
payload.payload.foo = 'bar'
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
await binding.subscribe(label, callback)
|
|
68
|
-
|
|
69
|
-
const payload = { [generate()]: generate() }
|
|
70
|
-
|
|
71
|
-
/** @type {toa.core.Message} */
|
|
72
|
-
const message = { payload }
|
|
73
|
-
const origin = JSON.parse(JSON.stringify(message))
|
|
74
|
-
|
|
75
|
-
await emitter.emit(message)
|
|
76
|
-
|
|
77
|
-
expect(origin).toEqual(message)
|
|
78
|
-
})
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { generate } = require('randomstring')
|
|
4
|
-
|
|
5
|
-
const endpoints = [generate(), generate()]
|
|
6
|
-
const invoke = jest.fn(async () => generate())
|
|
7
|
-
const producer = /** @type {jest.MockedObject<toa.core.Component>} */ { invoke }
|
|
8
|
-
|
|
9
|
-
exports.endpoints = endpoints
|
|
10
|
-
exports.producer = producer
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { generate } = require('randomstring')
|
|
4
|
-
const { Locator, Connector } = require('@toa.io/core')
|
|
5
|
-
const { sample } = require('@toa.io/generic')
|
|
6
|
-
|
|
7
|
-
const fixtures = require('./producer.fixtures')
|
|
8
|
-
const { binding, Factory } = require('../../src/binding')
|
|
9
|
-
|
|
10
|
-
const factory = new Factory()
|
|
11
|
-
|
|
12
|
-
it('should expose producers factory', async () => {
|
|
13
|
-
expect(factory.producer).toBeDefined()
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
/** @type {toa.core.Connector} */
|
|
17
|
-
let producer
|
|
18
|
-
|
|
19
|
-
/** @type {toa.core.Locator} */
|
|
20
|
-
let locator
|
|
21
|
-
|
|
22
|
-
beforeEach(async () => {
|
|
23
|
-
jest.clearAllMocks()
|
|
24
|
-
binding.reset()
|
|
25
|
-
|
|
26
|
-
const namespace = generate()
|
|
27
|
-
const name = generate()
|
|
28
|
-
|
|
29
|
-
locator = new Locator(name, namespace)
|
|
30
|
-
producer = factory.producer(locator, fixtures.endpoints, fixtures.producer)
|
|
31
|
-
|
|
32
|
-
await producer.connect()
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
it('should return Connector', async () => {
|
|
36
|
-
expect(producer).toBeInstanceOf(Connector)
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
it('should return result', async () => {
|
|
40
|
-
const endpoint = sample(fixtures.endpoints)
|
|
41
|
-
const label = locator.id + '.' + endpoint
|
|
42
|
-
const request = { [generate()]: generate() }
|
|
43
|
-
|
|
44
|
-
const reply = await binding.request(label, request)
|
|
45
|
-
|
|
46
|
-
expect(fixtures.producer.invoke).toHaveBeenCalledWith(endpoint, request)
|
|
47
|
-
|
|
48
|
-
expect(reply).toStrictEqual(await fixtures.producer.invoke.mock.results[0].value)
|
|
49
|
-
})
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { generate } = require('randomstring')
|
|
4
|
-
const { Locator, Connector } = require('@toa.io/core')
|
|
5
|
-
|
|
6
|
-
const fixtures = require('./receiver.fixtures')
|
|
7
|
-
const { binding, Factory } = require('../../src/binding')
|
|
8
|
-
|
|
9
|
-
const factory = new Factory()
|
|
10
|
-
|
|
11
|
-
it('should expose receivers factory', async () => {
|
|
12
|
-
expect(factory.receiver).toBeDefined()
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
/** @type {toa.core.Connector} */
|
|
16
|
-
let receiver
|
|
17
|
-
|
|
18
|
-
/** @type {toa.core.Locator} */
|
|
19
|
-
let locator
|
|
20
|
-
|
|
21
|
-
/** @type {string} */
|
|
22
|
-
let label
|
|
23
|
-
|
|
24
|
-
const group = generate()
|
|
25
|
-
const endpoint = generate()
|
|
26
|
-
|
|
27
|
-
beforeEach(async () => {
|
|
28
|
-
jest.clearAllMocks()
|
|
29
|
-
binding.reset()
|
|
30
|
-
|
|
31
|
-
const namespace = generate()
|
|
32
|
-
const name = generate()
|
|
33
|
-
|
|
34
|
-
locator = new Locator(name, namespace)
|
|
35
|
-
label = locator.id + '.' + endpoint
|
|
36
|
-
receiver = factory.receiver(locator, label, group, fixtures.receiver)
|
|
37
|
-
|
|
38
|
-
await receiver.connect()
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
it('should return connector', async () => {
|
|
42
|
-
expect(receiver).toBeInstanceOf(Connector)
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
it('should call receiver', async () => {
|
|
46
|
-
const payload = { [generate()]: generate() }
|
|
47
|
-
|
|
48
|
-
/** @type {toa.core.Message} */
|
|
49
|
-
const message = { payload }
|
|
50
|
-
|
|
51
|
-
await binding.emit(label, message)
|
|
52
|
-
|
|
53
|
-
expect(fixtures.receiver.receive).toHaveBeenCalledWith(message)
|
|
54
|
-
})
|