Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zetxx committed Aug 18, 2022
1 parent 591aafa commit 2b4472c
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 59 deletions.
10 changes: 4 additions & 6 deletions lib/requests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ const {
ForceDestroy
} = require('./errors');

let traceIdCounter = 0;

const createRequest = ({
timeout,
match,
Expand All @@ -14,12 +12,12 @@ const createRequest = ({
config = {},
counter,
tag,
traceId,
trace,
waitTime
}) => {
const request = {
tag,
traceId,
trace,
idx: counter,
// consist of previous request meta that needs to be matched against
match,
Expand Down Expand Up @@ -79,7 +77,7 @@ const Request = ({
method,
direction,
config,
traceId = ++traceIdCounter
trace
} = {}
} = {},
timeout
Expand All @@ -92,7 +90,7 @@ const Request = ({
direction,
config,
counter,
traceId,
trace,
tag,
waitTime
});
Expand Down
48 changes: 47 additions & 1 deletion lib/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,59 @@ const Router = ({
const vectors = new Map();
vectors.set(V0.tag, V0);
vectors.set(V1.tag, V1);
const findOpposite = (cur, collection) => {
const all = collection || vectors.keys();
// dummy implementation
let v = all.next();
if (v.value !== cur) {
return vectors.get(v.value);
}
return findOpposite(cur, all);
};

const pass = async({vector, packet}) => {
if(!vectors.get(vector)) {
const first = vectors.get(vector);
if(!first) {
throw RouterErrorVectorNotFound(
{vector}
);
}
const second = findOpposite(vector);
const firstPacket = await first.pass({
packet: {
...packet,
meta: {
...packet.meta,
direction: 'in'
}
}
});
const secondPacket = await second.pass({
packet: {
payload: firstPacket.payload,
meta: {
method: firstPacket.meta.method,
direction: 'out',
trace: firstPacket.meta.trace
}
}
});

const spp = await secondPacket.request.promise;
// third call with in order to create response
await first.pass({
packet: {
payload: spp.packet.payload,
match: {
idx: firstPacket.request.idx,
tag: firstPacket.request.tag
},
meta: {
trace: firstPacket.meta.trace
}
}
});
return await firstPacket.request.promise;
};
const rtr = {
log,
Expand Down
17 changes: 15 additions & 2 deletions lib/vector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ const Vector = ({
meta
},
timeout: (errorFullPacket) => {
log('error', errorFullPacket);
throw new Error('what to do?');
}
})
Expand All @@ -141,6 +142,10 @@ const Vector = ({
}
};

const reverseDirection = (direction) => (
(direction === 'in' && 'out') || 'in'
);

const pass = async({
packet: {
payload,
Expand All @@ -158,7 +163,12 @@ const Vector = ({
const methodCallResult = await callMethod({
payload,
error,
meta,
meta: {
method: acquired.request.method,
trace: acquired.request.trace,
direction: reverseDirection(acquired.request.direction),
...meta
},
match,
ctx: {
...globalCtx,
Expand All @@ -185,7 +195,10 @@ const Vector = ({
methodCallResult
])
};
return await last(fin);
// its a hook, only purpose is to call something
// at the end of the flow
await last(fin);
return fin;
};

return {
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// require('./test.requests');
// require('./vector/test.vector.simple-checks');
// require('./vector/test.in-out-ok');
// require('./vector/test.in-out-special.no-res-ok');
// require('./vector/test.in-na-out-ok');
// require('./vector/test.in-ok-out-na');
// require('./vector/test.in-na-out-na');
// require('./vector/test.special.method');
require('./vector/test.special.method');
// @TODO vector timeouts
require('./router/test.router');
// require('./router/test.router');
4 changes: 2 additions & 2 deletions tests/unit/test.requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ tap.test('Request', async(t) => {
// timeout: (e) => e,
packet: {
match: {idx: ++idx, tag: tag1},
meta: {traceId: -1}
meta: {trace: -1}
}
});
tt.same(
Expand All @@ -58,7 +58,7 @@ tap.test('Request', async(t) => {
'index should be greater than 0'
);
tt.same(
rq.traceId,
rq.trace,
-1,
'trace id should be same as passed'
);
Expand Down
1 change: 0 additions & 1 deletion tests/unit/vector/test.in-out-ok.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,3 @@ tap.test('Vector: Existing Methods a.in and a.out', async(l0) => {
l0.end();
}
});

78 changes: 78 additions & 0 deletions tests/unit/vector/test.in-out-special.no-res-ok.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const tap = require('tap');
const {
vectorPassFactory,
timeOut,
vectorFactory,
methodRegisterFactory
} = require('../helpers');

const v1 = vectorFactory({config: {id: 'V'}});
methodRegisterFactory(v1, 'a.in');
methodRegisterFactory(v1, 'a.out');
methodRegisterFactory(v1, '*.in', () => undefined);
methodRegisterFactory(v1, '*.out', () => undefined);

tap.test('Vector: Existing Methods a.in and a.out, * methods also exists but returns no result', async(l0) => {
try {
const r1 = await vectorPassFactory(
v1,
['Existing Methods a.in and a.out, dir in > out'],
false,
['a','in']
);
setTimeout(async() => {
await vectorPassFactory(
v1,
r1.payload,
false,
['a', 'out'],
{idx: r1.request.idx, tag: r1.request.tag}
);
}, 500);
const {packet} = await r1.request.promise;
l0.same(packet, {
payload: [
'Existing Methods a.in and a.out, dir in > out',
'>a.in',
'X:a.in',
'>a.out',
'X:a.out'
],
meta: {method: 'a', direction: 'out'},
match: {idx: 1, tag: Symbol('V')}
}, 'packet struct should match, direction in > out');

const r2 = await vectorPassFactory(
v1,
['Existing Methods a.in and a.out, dir out > in'],
false,
['a', 'out']
);
setTimeout(async() => {
await vectorPassFactory(
v1,
r2.payload,
false,
['a', 'in'],
{idx: r2.request.idx, tag: r2.request.tag}
);
}, 500);
const {packet: p2} = await r2.request.promise;
l0.same(p2, {
payload: [
'Existing Methods a.in and a.out, dir out > in',
'>a.out',
'X:a.out',
'>a.in',
'X:a.in'
],
meta: {method: 'a', direction: 'in'},
match: {idx: 2, tag: Symbol('V')}
}, 'packet struct should match, direction out > in');
} catch (e) {
console.error(e);
} finally {
v1.destroy();
l0.end();
}
});
Loading

0 comments on commit 2b4472c

Please sign in to comment.