Skip to content

Commit

Permalink
Merge pull request #1 from cocrafts/FME849/get-request-data
Browse files Browse the repository at this point in the history
feat: impl kernel method to get request from requestPool
  • Loading branch information
tanlethanh authored Apr 24, 2024
2 parents da6c889 + d1e9d35 commit a25ce2e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
9 changes: 9 additions & 0 deletions src/core/kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,15 @@ export class Kernel<
respond({ message: 'ok' });
}
};

getRequestByResolveId(resolveId: string) {
const resolvingContext = this.crossResolvingContext[resolveId];
if (!resolvingContext) {
return { error: 'Can not find context for cross-resolving' };
} else {
return this.requestPool[resolvingContext.requestId];
}
}
}

export type ChannelContext<EventType extends string | number> = {
Expand Down
57 changes: 47 additions & 10 deletions src/core/test/kernel.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Kernel } from '../kernel';
import type { Middleware } from '../types';
import type { KernelRequestContext, Middleware } from '../types';

enum ChannelId {
App,
Expand All @@ -11,6 +11,7 @@ enum EventType {
Greeting,
Logging,
ApproveGreeting,
RequestPayload,
}

test('kernel with middlewares', () => {
Expand Down Expand Up @@ -362,18 +363,18 @@ test('kernel with cross-resolving middlewares', async () => {
);
});

test('kernel with cross-resolving middlewares - resolving timeout', async () => {
test('kernel with cross-resolving middlewares - resolving timeout', (done) => {
const kernel = new Kernel<ChannelId, EventType>();

let resolveId: string;
const askForApprovalFromApp: Middleware<EventType> = async (
request,
respond,
next,
) => {
const { resolveId, resolve } = kernel.createCrossResolvingRequest(
request.id,
100,
);
const { resolveId: innerResolveId, resolve } =
kernel.createCrossResolvingRequest(request.id, 100);
resolveId = innerResolveId;

// call resolving from another channel here
mockResolvingFromApp(resolveId);
Expand All @@ -387,6 +388,20 @@ test('kernel with cross-resolving middlewares - resolving timeout', async () =>
}
};

const askForPayloadFromApp = (
kernel: Kernel<ChannelId, EventType>,
): Middleware<EventType> => {
return async (request, respond) => {
const { resolveId } = request;
if (!resolveId) {
respond({ error: 'resolveId is undefined' });
} else {
const sourceRequest = kernel.getRequestByResolveId(resolveId);
respond(sourceRequest);
}
};
};

const mockResolvingFromApp = (resolveId: string) => {
setTimeout(async () => {
await kernel.execute(
Expand Down Expand Up @@ -416,23 +431,45 @@ test('kernel with cross-resolving middlewares - resolving timeout', async () =>
.use(handleGreetingFromSDK)

.channel(ChannelId.App)
.handle(EventType.RequestPayload)
.use(askForPayloadFromApp(kernel))
.handle(EventType.ApproveGreeting)
.use(kernel.handleCrossResolvingMiddleware);

await kernel.execute(
kernel.execute(
ChannelId.SDK,
{
id: crypto.randomUUID(),
type: EventType.Greeting,
timeout: 1000,
message: 'Request from SDK',
},
(response) => {
expect(response.error).toBeTruthy();
},
);

// to avoid jest timeout
await new Promise((resolve) => setTimeout(resolve, 300));
setTimeout(() => {
kernel.execute(
ChannelId.App,
{
id: crypto.randomUUID(),
type: EventType.RequestPayload,
timeout: 1000,
resolveId: resolveId,
},
(response) => {
try {
const {
request: { message },
} = response as KernelRequestContext<EventType>;
expect(message).toEqual('Request from SDK');
done();
} catch (error) {
done(error);
}
},
);
}, 0);
});

test('kernel with cross-resolving middlewares - request timeout by late cross-resolving', async () => {
Expand Down

0 comments on commit a25ce2e

Please sign in to comment.