Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: impl kernel method to get request from requestPool #1

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading