Skip to content

Commit

Permalink
Adjust the typescript e2e tests after API refactor (#280)
Browse files Browse the repository at this point in the history
This commit adjusts the e2e tests following the removal
of objectApi and serviceApi types.
  • Loading branch information
igalshilman authored Mar 13, 2024
1 parent 60cea0e commit 4cdf49b
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 37 deletions.
9 changes: 3 additions & 6 deletions services/node-services/src/awakeable_holder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ type Ctx = restate.ObjectContext;

REGISTRY.add({
fqdn: AwakeableHolderServiceFQN,
binder: (e) => e.object(AwakeableHolderServiceFQN, service),
binder: (e) => e.object(service),
});

const service = restate.object({
const service = restate.object(AwakeableHolderServiceFQN, {
async hold(ctx: restate.ObjectContext, id: string) {
ctx.set(ID_KEY, id);
},
Expand All @@ -41,7 +41,4 @@ const service = restate.object({
},
});

export const awakeableHolderApi = restate.objectApi(
AwakeableHolderServiceFQN,
service
);
export type awakeableHolderApi = typeof service;
14 changes: 7 additions & 7 deletions services/node-services/src/cancel_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ import { REGISTRY } from "./services";

export const CancelTestServiceFQN = "CancelTestRunner";
export const BlockingServiceFQN = "CancelTestBlockingService";
const AwakeableHolder: awakeableHolderApi = { path: "AwakeableHolder"};

REGISTRY.add({
fqdn: CancelTestServiceFQN,
binder: (e) => e.object(CancelTestServiceFQN, canceService),
binder: (e) => e.object(canceService),
});

REGISTRY.add({
fqdn: BlockingServiceFQN,
binder: (e) => e.object(BlockingServiceFQN, blockingService),
binder: (e) => e.object(blockingService),
});

enum BlockingOperation {
Expand All @@ -30,7 +31,7 @@ enum BlockingOperation {
AWAKEABLE = "AWAKEABLE",
}

const canceService = restate.object({
const canceService = restate.object(CancelTestServiceFQN, {
async verifyTest(ctx: restate.ObjectContext): Promise<boolean> {
const isCanceled = (await ctx.get<boolean>("canceled")) ?? false;
return isCanceled;
Expand All @@ -52,11 +53,11 @@ const canceService = restate.object({
},
});

const blockingService = restate.object({
const blockingService = restate.object(BlockingServiceFQN, {
async block(ctx: restate.ObjectContext, request: BlockingOperation) {
const { id, promise } = ctx.awakeable();
// DO NOT await the next CALL otherwise the test deadlocks.
ctx.object(awakeableHolderApi, "cancel").hold(id);
ctx.object(AwakeableHolder, "cancel").hold(id);
await promise;

switch (request) {
Expand All @@ -77,8 +78,7 @@ const blockingService = restate.object({
}
},


async isUnlocked() {},
});

const api = restate.objectApi(BlockingServiceFQN, blockingService);
const api: typeof blockingService = { path : "CancelTestBlockingService" };
12 changes: 7 additions & 5 deletions services/node-services/src/counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@

import * as restate from "@restatedev/restate-sdk";
import { REGISTRY } from "./services";
import { awakeableHolderApi } from "./awakeable_holder";
import type { awakeableHolderApi } from "./awakeable_holder";

const COUNTER_KEY = "counter";

export const CounterServiceFQN = "Counter";

REGISTRY.add({
fqdn: CounterServiceFQN,
binder: (e) => e.object(CounterServiceFQN, service),
binder: (e) => e.object(service),
});

const service = restate.object({
const AwakeableHolder: awakeableHolderApi = { path: "AwakeableHolder" };

const service = restate.object(CounterServiceFQN, {
async reset(ctx: restate.ObjectContext) {
ctx.clear(COUNTER_KEY);
},
Expand Down Expand Up @@ -56,7 +58,7 @@ const service = restate.object({

// Wait for the sync with the test runner
const { id, promise } = ctx.awakeable();
ctx.objectSend(awakeableHolderApi, ctx.key()).hold(id);
ctx.objectSend(AwakeableHolder, ctx.key()).hold(id);
await promise;

// Now start looping
Expand All @@ -74,4 +76,4 @@ const service = restate.object({
},
});

export const counterApi = restate.objectApi(CounterServiceFQN, service);
export type counterApi = typeof service;
8 changes: 5 additions & 3 deletions services/node-services/src/event_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import { counterApi } from "./counter";

const EventHandlerFQN = "EventHandler";

const CounterApi: counterApi = { path: "Counter" };

REGISTRY.add({
fqdn: EventHandlerFQN,
binder: (e) => e.object(EventHandlerFQN, service),
binder: (e) => e.object(service),
});

const service = restate.object({
const service = restate.object(EventHandlerFQN, {
async handle(ctx: restate.ObjectContext, value: number) {
ctx.objectSend(counterApi, ctx.key()).add(value);
ctx.objectSend(CounterApi, ctx.key()).add(value);
},
});
4 changes: 2 additions & 2 deletions services/node-services/src/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ const ListServiceFQN = "ListObject";

REGISTRY.add({
fqdn: ListServiceFQN,
binder: (e) => e.object(ListServiceFQN, service),
binder: (e) => e.object(service),
});

const service = restate.object({
const service = restate.object(ListServiceFQN, {
async append(ctx: restate.ObjectContext, request: string): Promise<void> {
const list = (await ctx.get<string[]>(LIST_KEY)) ?? [];
list.push(request);
Expand Down
4 changes: 2 additions & 2 deletions services/node-services/src/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export const MapServiceFQN = "MapObject";

REGISTRY.add({
fqdn: MapServiceFQN,
binder: (e) => e.object(MapServiceFQN, service),
binder: (e) => e.object(service),
});

const service = restate.object({
const service = restate.object(MapServiceFQN, {
async clearAll(ctx: restate.ObjectContext): Promise<string[]> {
const keys = await ctx.stateKeys();
ctx.clearAll();
Expand Down
19 changes: 10 additions & 9 deletions services/node-services/src/non_determinism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import { REGISTRY } from "./services";

export const NonDeterministicServiceFQN = "NonDeterministic";

const Counter: counterApi = { path: "Counter" };

REGISTRY.add({
fqdn: NonDeterministicServiceFQN,
binder: (e) => e.object(NonDeterministicServiceFQN, service),
binder: (e) => e.object(service),
});

const invocationCounts = new Map<string, number>();
Expand All @@ -28,33 +30,33 @@ function doLeftAction(ctx: restate.ObjectContext): boolean {
}

function incrementCounter(ctx: restate.ObjectContext) {
ctx.objectSend(counterApi, ctx.key()).add(1);
ctx.objectSend(Counter, ctx.key()).add(1);
}

const service = restate.object({
const service = restate.object(NonDeterministicServiceFQN, {
async leftSleepRightCall(ctx: restate.ObjectContext) {
if (doLeftAction(ctx)) {
await ctx.sleep(100);
} else {
await ctx.object(counterApi, "abc").get();
await ctx.object(Counter, "abc").get();
}
incrementCounter(ctx);
},

async callDifferentMethod(ctx: restate.ObjectContext) {
if (doLeftAction(ctx)) {
await ctx.object(counterApi, "abc").get();
await ctx.object(Counter, "abc").get();
} else {
await ctx.object(counterApi, "abc").reset();
await ctx.object(Counter, "abc").reset();
}
incrementCounter(ctx);
},

async backgroundInvokeWithDifferentTargets(ctx: restate.ObjectContext) {
if (doLeftAction(ctx)) {
ctx.objectSend(counterApi, "abc").get();
ctx.objectSend(Counter, "abc").get();
} else {
ctx.objectSend(counterApi, "abc").reset();
ctx.objectSend(Counter, "abc").reset();
}
await ctx.sleep(100);
incrementCounter(ctx);
Expand All @@ -76,4 +78,3 @@ const service = restate.object({
incrementCounter(ctx);
},
});

7 changes: 4 additions & 3 deletions services/node-services/src/proxy_counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@ import { REGISTRY } from "./services";
import { counterApi } from "./counter";

const ProxyCounterServiceFQN = "ProxyCounter";
const Counter: counterApi = { path: "Counter" };

REGISTRY.add({
fqdn: ProxyCounterServiceFQN,
binder: (e) => e.service(ProxyCounterServiceFQN, service),
binder: (e) => e.service(service),
});

const service = restate.service({
const service = restate.service(ProxyCounterServiceFQN, {
async addInBackground(
ctx: restate.Context,
request: { counterName: string; value: number }
) {
ctx.console.log("addInBackground " + JSON.stringify(request));
ctx.objectSend(counterApi, request.counterName).add(request.value);
ctx.objectSend(Counter, request.counterName).add(request.value);
},
});

0 comments on commit 4cdf49b

Please sign in to comment.