-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move verification tests into this repo (#123)
Fixes #122
- Loading branch information
1 parent
3f9a3d4
commit 70a79de
Showing
12 changed files
with
624 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@restatedev:registry=https://npm.pkg.github.com/ | ||
//npm.pkg.github.com/:_authToken=${GH_PACKAGE_READ_ACCESS_TOKEN} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,30 @@ | ||
FROM node:18 AS build | ||
ARG NPM_TOKEN | ||
ARG GH_PACKAGE_READ_ACCESS_TOKEN | ||
|
||
WORKDIR /usr/src/app | ||
COPY . . | ||
|
||
RUN echo "//npm.pkg.github.com/:_authToken=$NPM_TOKEN\n" >> .npmrc && \ | ||
echo "@restatedev:registry=https://npm.pkg.github.com/" >> .npmrc && \ | ||
npm ci && \ | ||
npm run build && \ | ||
rm -f .npmrc | ||
RUN npm ci | ||
RUN npm run build | ||
|
||
FROM node:18 as prod | ||
ARG NPM_TOKEN | ||
ARG GH_PACKAGE_READ_ACCESS_TOKEN | ||
WORKDIR /usr/src/app | ||
|
||
# Install app dependencies | ||
COPY package*.json *.tgz ./ | ||
RUN echo "//npm.pkg.github.com/:_authToken=$NPM_TOKEN\n" >> .npmrc && \ | ||
echo "@restatedev:registry=https://npm.pkg.github.com/" >> .npmrc && \ | ||
npm ci --production && \ | ||
rm -f .npmrc | ||
COPY package*.json *.tgz .npmrc ./ | ||
RUN npm ci --production | ||
|
||
COPY --from=build /usr/src/app/dist /usr/src/app/dist | ||
|
||
FROM node:18 | ||
|
||
# Use a new stage so that the build-arg GH_PACKAGE_READ_ACCESS_TOKEN isn't leaked into the final image history | ||
COPY --from=prod /usr/src/app/ /usr/src/app/ | ||
|
||
# Install Tini | ||
RUN apt-get update && apt-get -y install tini | ||
|
||
EXPOSE 8080 | ||
ENTRYPOINT ["tini", "--"] | ||
CMD ["node", "/usr/src/app/dist/app.js"] | ||
CMD ["node", "/usr/src/app/dist/app.js"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
import { RestateContext, useContext } from "@restatedev/restate-sdk"; | ||
import { | ||
BackgroundCallRequest, | ||
CallRequest, | ||
ClearRequest, | ||
Command_AsyncCall, | ||
Command_AsyncCallAwait, | ||
Command_BackgroundCall, | ||
Command_IncrementState, | ||
Command_Sleep, | ||
Command_SyncCall, | ||
CommandInterpreter, | ||
CommandInterpreterClientImpl, | ||
Commands, | ||
Empty, | ||
Key, | ||
protobufPackage, | ||
TestParams, | ||
VerificationRequest, | ||
VerificationResult, | ||
} from "./generated/interpreter"; | ||
|
||
export const CommandInterpreterServiceFQN = | ||
protobufPackage + ".CommandInterpreter"; | ||
|
||
export class CommandInterpreterService implements CommandInterpreter { | ||
async call(request: CallRequest): Promise<Empty> { | ||
return this.eitherCall(request.key, request.commands); | ||
} | ||
|
||
async backgroundCall(request: BackgroundCallRequest): Promise<Empty> { | ||
return this.eitherCall(request.key, request.commands); | ||
} | ||
|
||
async eitherCall( | ||
key: Key | undefined, | ||
commands: Commands | undefined | ||
): Promise<Empty> { | ||
if (!commands?.command) { | ||
throw new Error("CallRequest with no commands"); | ||
} | ||
if (!key) { | ||
throw new Error("CallRequest with no key"); | ||
} | ||
if (!key.params) { | ||
throw new Error("CallRequest with no test parameters"); | ||
} | ||
const ctx = useContext(this); | ||
const client = new CommandInterpreterClientImpl(ctx); | ||
const pending_calls = new Map<number, Promise<Empty>>(); | ||
|
||
for (const c of commands.command) { | ||
switch (true) { | ||
case c.increment !== undefined: | ||
await this._increment(ctx, c.increment as Command_IncrementState); | ||
break; | ||
case c.syncCall !== undefined: | ||
await this._syncCall( | ||
ctx, | ||
client, | ||
key.params, | ||
c.syncCall as Command_SyncCall | ||
); | ||
break; | ||
case c.asyncCall !== undefined: | ||
this._asyncCall( | ||
ctx, | ||
client, | ||
pending_calls, | ||
key.params, | ||
c.asyncCall as Command_AsyncCall | ||
); | ||
break; | ||
case c.asyncCallAwait !== undefined: | ||
await this._asyncCallAwait( | ||
ctx, | ||
pending_calls, | ||
c.asyncCallAwait as Command_AsyncCallAwait | ||
); | ||
break; | ||
case c.backgroundCall !== undefined: | ||
await this._backgroundCall( | ||
ctx, | ||
client, | ||
key.params, | ||
c.backgroundCall as Command_BackgroundCall | ||
); | ||
break; | ||
case c.sleep !== undefined: | ||
await this._sleep(ctx, c.sleep as Command_Sleep); | ||
break; | ||
default: | ||
// should be unreachable | ||
throw new Error("Empty Command in CallRequest"); | ||
} | ||
} | ||
|
||
return Empty.create({}); | ||
} | ||
|
||
async _increment( | ||
ctx: RestateContext, | ||
request: Command_IncrementState | ||
): Promise<void> { | ||
const counter = (await ctx.get<number>("counter")) || 0; | ||
return ctx.set("counter", counter + 1); | ||
} | ||
|
||
async _syncCall( | ||
ctx: RestateContext, | ||
client: CommandInterpreterClientImpl, | ||
params: TestParams, | ||
request: Command_SyncCall | ||
): Promise<void> { | ||
await client.call( | ||
CallRequest.create({ | ||
key: { params, target: request.target }, | ||
commands: request.commands, | ||
}) | ||
); | ||
} | ||
|
||
_asyncCall( | ||
ctx: RestateContext, | ||
client: CommandInterpreterClientImpl, | ||
pending_calls: Map<number, Promise<Empty>>, | ||
params: TestParams, | ||
request: Command_AsyncCall | ||
) { | ||
pending_calls.set( | ||
request.callId, | ||
client.call( | ||
CallRequest.create({ | ||
key: { params, target: request.target }, | ||
commands: request.commands, | ||
}) | ||
) | ||
); | ||
} | ||
|
||
async _asyncCallAwait( | ||
ctx: RestateContext, | ||
pending_calls: Map<number, Promise<Empty>>, | ||
request: Command_AsyncCallAwait | ||
): Promise<void> { | ||
const p = pending_calls.get(request.callId); | ||
if (p === undefined) { | ||
throw new Error("Unrecognised CallID in AsyncCallAwait command"); | ||
} | ||
await p; | ||
return; | ||
} | ||
|
||
async _backgroundCall( | ||
ctx: RestateContext, | ||
client: CommandInterpreterClientImpl, | ||
params: TestParams, | ||
request: Command_BackgroundCall | ||
): Promise<void> { | ||
return ctx.oneWayCall(() => | ||
client.backgroundCall( | ||
BackgroundCallRequest.create({ | ||
key: { params, target: request.target }, | ||
commands: request.commands, | ||
}) | ||
) | ||
); | ||
} | ||
|
||
async _sleep(ctx: RestateContext, request: Command_Sleep): Promise<void> { | ||
return ctx.sleep(request.milliseconds); | ||
} | ||
|
||
async verify(request: VerificationRequest): Promise<VerificationResult> { | ||
const ctx = useContext(this); | ||
return VerificationResult.create({ | ||
expected: request.expected, | ||
actual: (await ctx.get<number>("counter")) || 0, | ||
}); | ||
} | ||
|
||
async clear(request: ClearRequest): Promise<Empty> { | ||
const ctx = useContext(this); | ||
|
||
await ctx.clear("counter"); | ||
|
||
return Empty.create({}); | ||
} | ||
} |
Oops, something went wrong.