From d1e9d35eed6b3f204c42ef921faa2008ad70d0fd Mon Sep 17 00:00:00 2001 From: FME849 <1414439@gmail.com> Date: Wed, 24 Apr 2024 16:21:46 +0700 Subject: [PATCH] feat: impl kernel method to get request from requestPool --- src/core/kernel.ts | 9 ++++++ src/core/test/kernel.test.ts | 57 +++++++++++++++++++++++++++++------- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/src/core/kernel.ts b/src/core/kernel.ts index 39205f7..4da7abf 100644 --- a/src/core/kernel.ts +++ b/src/core/kernel.ts @@ -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 = { diff --git a/src/core/test/kernel.test.ts b/src/core/test/kernel.test.ts index b048933..7fb719f 100644 --- a/src/core/test/kernel.test.ts +++ b/src/core/test/kernel.test.ts @@ -1,5 +1,5 @@ import { Kernel } from '../kernel'; -import type { Middleware } from '../types'; +import type { KernelRequestContext, Middleware } from '../types'; enum ChannelId { App, @@ -11,6 +11,7 @@ enum EventType { Greeting, Logging, ApproveGreeting, + RequestPayload, } test('kernel with middlewares', () => { @@ -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(); + let resolveId: string; const askForApprovalFromApp: Middleware = 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); @@ -387,6 +388,20 @@ test('kernel with cross-resolving middlewares - resolving timeout', async () => } }; + const askForPayloadFromApp = ( + kernel: Kernel, + ): Middleware => { + 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( @@ -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; + 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 () => {