Skip to content

Commit

Permalink
Improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hhoughgg committed Aug 26, 2024
1 parent a8a094c commit 3eaa5e7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/cloudflare/internal/pipeline-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async function* readLines(
type Batch = {
id: string; // unique identifier for the batch
shard: string; // assigned shard
ts: number; // timestamp of the event
ts: number; // creation timestamp of the batch

format: Format;
size: {
Expand Down Expand Up @@ -118,6 +118,10 @@ export class PipelineTransformImpl extends entrypoints.WorkerEntrypoint {
}

#sendJson(data: object[]): JsonStream {
if (!(data instanceof Array)) {
throw new Error('transformJson must return an array of objects');
}

let written = 0;
const encoder = new TextEncoder();
const readable = new ReadableStream<Uint8Array>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const customTransform = class MyEntrypoint extends PipelineTransform {
async transformJson(batch) {
for (const obj of batch) {
obj.dispatcher = 'was here!';
await new Promise((resolve) => setTimeout(resolve, 50));
obj.wait = 'happened!';
}

return batch;
Expand Down Expand Up @@ -96,11 +98,14 @@ export const tests = {
for (const line of resultLines) {
objects.push(JSON.parse(line));
}
assert.equal(objects.length, 3);

let index = 0;
for (const obj of objects) {
assert.equal(obj.dispatcher, 'was here!');
delete obj.dispatcher;
assert.equal(obj.wait, 'happened!');
delete obj.wait;

assert.equal(`${JSON.stringify(obj)}\n`, lines[index]);
index++;
Expand Down

0 comments on commit 3eaa5e7

Please sign in to comment.