Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zetxx committed Jul 26, 2021
1 parent 9f7f987 commit c335c21
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 20 deletions.
44 changes: 28 additions & 16 deletions lib/bridge/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,36 @@ const directions = {
};
let id = 0;

const callMethod = (methods) => async({
direction,
packet
}) => {
try {
return {
payload: await methods.call({
direction,
packet
})
};
} catch (error) {
return {error, payload: undefined};
}
};

class Bridge {
constructor({
config: {
request = {
waitTime: 30000
}
},
id: bridgeId
} = {}
} = {}) {
this.config = {
request
};
this.methods = Methods();
this.nodeId = Symbol(++id);
this.nodeId = Symbol(bridgeId || ++id);
this.requests = Request({
nodeId: this.nodeId,
config: this.config.request
Expand Down Expand Up @@ -61,21 +78,13 @@ class Bridge {
async pass({direction, packet}) {
const {match} = packet;
const requestFound = this.requests.find(packet);
const methodCallResult = (async() => {
try {
return {
payload: await this.methods.call({
direction,
packet
})
};
} catch (error) {
return {error, payload: undefined};
}
})();
const methodCallResult = await callMethod(this.methods)({
direction,
packet
});
const packetNew = {
...packet,
...(await methodCallResult),
...methodCallResult,
match: undefined
};
if (!requestFound) {
Expand Down Expand Up @@ -112,7 +121,10 @@ class Bridge {
request: requestFound,
match
});

}

async destroy() {
this.requests.destroy();
}
}

Expand Down
7 changes: 7 additions & 0 deletions lib/methods/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const {NotFound} = require('./errors');

/**
* method name should be in following pattern
* method.direction
* if real name of the method is 'a' and direction is 'in'
* method name should be 'a.in'
*/

module.exports = () => {
const methods = {};
const o = {
Expand Down
30 changes: 26 additions & 4 deletions tests/unit/bridge/test.bridge.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
const tap = require('tap');
const bridge = require('../../../lib/bridge');
const Bridge = require('../../../lib/bridge');

tap.test('Bridge', (l1) => {
l1.test('1', (t) => {
tap.test('Bridge', (l0) => {
// l0.test('Simple checks and coverage', (t) => {
// const bridge = new Bridge();
// bridge.start({});
// bridge.destroy();
// t.end();
// });

l0.test('Method call positive cases', (t) => {
// set up
const bridgeA = new Bridge({config: {id: 'bridgeA'}});
const bridgeB = new Bridge({config: {id: 'bridgeB'}});
bridgeA.start({other: bridgeB});
bridgeB.start({other: bridgeA});
// register methods
bridgeA.methods.add({method: 'a.in', fn: ({payload}) => {
return payload;
}});
bridgeB.methods.add({method: 'a.out', fn: ({payload}) => {
return payload;
}});

bridgeA.pass({direction: 'in', packet: {meta: {method: 'a'}, payload: {a: 1}}});
t.end();
});
l1.end();

l0.end();
});

0 comments on commit c335c21

Please sign in to comment.