Skip to content

Commit

Permalink
Merge branch 'main' into queue-push-variadic
Browse files Browse the repository at this point in the history
  • Loading branch information
garysassano committed Aug 15, 2023
2 parents eb827f7 + 71b9b5d commit 80f3ab1
Show file tree
Hide file tree
Showing 19 changed files with 508 additions and 59 deletions.
19 changes: 17 additions & 2 deletions apps/wing-console/console/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,18 @@ export const createConsoleServer = async ({
});

const compiler = createCompiler(wingfile);
let isStarting = false;
let isStopping = false;

const simulator = createSimulator();
if (onTrace) {
simulator.on("trace", onTrace);
}
compiler.on("compiled", ({ simfile }) => {
simulator.start(simfile);
if (!isStarting) {
simulator.start(simfile);
isStarting = true;
}
});

let lastErrorMessage = "";
Expand Down Expand Up @@ -151,6 +157,7 @@ export const createConsoleServer = async ({
simulator.on("started", () => {
appState = "success";
invalidateQuery(undefined);
isStarting = false;
});
simulator.on("error", (error) => {
lastErrorMessage = error.message;
Expand Down Expand Up @@ -229,8 +236,12 @@ export const createConsoleServer = async ({
testsStateManager,
});

const close = async () => {
const close = async (callback?: () => void) => {
if (isStopping) {
return;
}
try {
isStopping = true;
updater?.removeEventListener("status-change", invalidateUpdaterStatus);
config?.removeEventListener("config-change", invalidateConfig);
await Promise.allSettled([
Expand All @@ -240,9 +251,13 @@ export const createConsoleServer = async ({
]);
} catch (error) {
log.error(error);
} finally {
if (typeof callback === "function") callback();
}
};

process.on("SIGINT", () => close(() => process.exit(0)));

return {
port,
close,
Expand Down
3 changes: 2 additions & 1 deletion docs/docs/06-tools/01-cli.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: CLI user manual
title: "CLI User Manual: Compile, Test, and Run Wing Programs"
id: cli
sidebar_label: CLI User Manual
description: Wing CLI reference
keywords: [Wing reference, Wing language, language, Wing language spec, Wing programming language, cli]
---
Expand Down
3 changes: 2 additions & 1 deletion docs/docs/06-tools/03-plugins.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
---
title: Compiler plugins
title: "Extending The Wing Compiler: Plugin Design and Hooks"
id: compiler-plugins
description: Wing compiler plugins reference
sidebar_label: Compiler Plugins
keywords: [Wing reference, Wing language, language, Wing language spec, Wing programming language, compiler plugins]
---

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: What are the main differences between Wing and Terraform?
title: Terraform vs. Wing [Main differences]
sidebar_label: Terraform
id: differences-from-terraform
keywords: [faq, winglang, Wing programming language, Wing language, TF, Terraform, IAC]
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/999-faq/040-alternatives/043-awscdk-vs-wing.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: What are the main differences between Wing and AWS-CDK?
title: AWS-CDK vs. Wing [Main differences]
sidebar_label: AWS-CDK
id: differences-from-awscdk
keywords: [faq, winglang, Wing programming language, Wing language, CDK, AWS-CDK, IAC]
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/999-faq/040-alternatives/044-cdktf-vs-wing.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: What are the main differences between Wing and CDKTF?
title: CDKTF vs Wing [Main differences]
sidebar_label: CDKTF
id: differences-from-cdktf
keywords: [faq, winglang, Wing programming language, Wing language, CDKTF, IAC]
Expand Down
14 changes: 14 additions & 0 deletions examples/tests/valid/captures.w
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,17 @@ new cloud.Function(
handler,
env: emptyEnv
) as "AnotherFunction";


let headers = {
"my-fancy-header" => "my-fancy-value",
"not-even-real\"" => "wow` !",
};
let api = new cloud.Api();
api.get("/hello", inflight (req) => {
return cloud.ApiResponse {
status: 200,
headers: headers,
body: "Hello, world!"
};
});
18 changes: 17 additions & 1 deletion examples/tests/valid/redis.w
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ skipPlatforms:
\*/

bring cloud;
bring util;
bring ex;

let r = new ex.Redis();
let r2 = new ex.Redis() as "r2";

test "test" {
let queue = new cloud.Queue();

queue.setConsumer(inflight (message: str) => {
r.set("hello", message);
}, timeout: 3s);

test "testing Redis" {
// Using raw client
let connection = r.rawClient();
connection.set("wing", "does redis");
Expand All @@ -21,4 +28,13 @@ test "test" {
r2.set("wing", "does redis again");
let value2 = r2.get("wing");
assert(value2 == "does redis again");

//With waitUntil
queue.push("world!");

util.waitUntil((): bool => {
return r.get("hello") != nil;
});

assert("world!" == "${r.get("hello")}");
}
2 changes: 1 addition & 1 deletion libs/wingsdk/src/core/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function liftObject(scope: IConstruct, obj: any): string {
const lines = [];
lines.push("{");
for (const [k, v] of Object.entries(obj)) {
lines.push(`${k}: ${liftObject(scope, v)},`);
lines.push(`\"${k.replace(/"/g, '\\"')}\": ${liftObject(scope, v)},`);
}
lines.push("}");
return lines.join("");
Expand Down
2 changes: 1 addition & 1 deletion libs/wingsdk/src/ex/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export abstract class RedisClientBase implements IRedisClient {

public async get(key: string): Promise<string | undefined> {
let redis = await this.rawClient();
let result = await redis.get(key);
let result = (await redis.get(key)) ?? undefined; // for wing to return nil
return result;
}

Expand Down
22 changes: 17 additions & 5 deletions libs/wingsdk/src/shared-aws/queue.inflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
PurgeQueueCommand,
GetQueueAttributesCommand,
ReceiveMessageCommand,
InvalidMessageContents,
} from "@aws-sdk/client-sqs";
import { IQueueClient } from "../cloud";

Expand All @@ -15,11 +16,22 @@ export class QueueClient implements IQueueClient {

public async push(...messages: string[]): Promise<void> {
const messagePromises = messages.map(async (message) => {
const command = new SendMessageCommand({
QueueUrl: this.queueUrl,
MessageBody: message,
});
await this.client.send(command);
try {
const command = new SendMessageCommand({
QueueUrl: this.queueUrl,
MessageBody: message,
});
await this.client.send(command);
} catch (e) {
if (e instanceof InvalidMessageContents) {
throw new Error(
`The message contains characters outside the allowed set (message=${message}): ${
(e as Error).stack
})}`
);
}
throw new Error((e as Error).stack);
}
});

await Promise.all(messagePromises);
Expand Down
8 changes: 6 additions & 2 deletions libs/wingsdk/src/testing/simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,12 @@ export class Simulator {
`Resource ${resourceConfig.path} could not be cleaned up, no handle for it was found.`
);
}
const resource = this._handles.deallocate(resourceConfig.attrs!.handle);
await resource.cleanup();
try {
const resource = this._handles.deallocate(resourceConfig.attrs!.handle);
await resource.cleanup();
} catch (err) {
console.warn(err);
}

let event: Trace = {
type: TraceType.RESOURCE,
Expand Down
42 changes: 42 additions & 0 deletions libs/wingsdk/test/shared-aws/queue.inflight.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
GetQueueAttributesCommand,
SQSClient,
ReceiveMessageCommand,
InvalidMessageContents,
} from "@aws-sdk/client-sqs";
import { mockClient } from "aws-sdk-client-mock";
import { test, expect, beforeEach } from "vitest";
Expand Down Expand Up @@ -67,6 +68,47 @@ test("push batch - happy path", async () => {
});
});

test("push - sad path invalid message", async () => {
// GIVEN
const QUEUE_URL = "QUEUE_URL";
const MESSAGE = "INVALID_MESSAGE";

sqsMock
.on(SendMessageCommand, { QueueUrl: QUEUE_URL, MessageBody: MESSAGE })
.rejects(
new InvalidMessageContents({
message: "InvalidMessageContents error",
$metadata: {},
})
);

// WHEN
const client = new QueueClient(QUEUE_URL);

// THEN
await expect(() => client.push(MESSAGE)).rejects.toThrowError(
/The message contains characters outside the allowed set/
);
});

test("push - sad path unknown error", async () => {
// GIVEN
const QUEUE_URL = "QUEUE_URL";
const MESSAGE = "MESSAGE";

sqsMock
.on(SendMessageCommand, { QueueUrl: QUEUE_URL, MessageBody: MESSAGE })
.rejects(new Error("unknown error"));

// WHEN
const client = new QueueClient(QUEUE_URL);

// THEN
await expect(() => client.push(MESSAGE)).rejects.toThrowError(
/unknown error/
);
});

test("purge - happy path", async () => {
// GIVEN
const QUEUE_URL = "QUEUE_URL";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ if (!(this.my_array[1].minutes === 20)) { throw new Error(\`assertion failed: \\
}
};
})())({
my_array: [{seconds: 600,minutes: 10,hours: 0.16666666666666666,},{seconds: 1200,minutes: 20,hours: 0.3333333333333333,}]
my_array: [{\\"seconds\\": 600,\\"minutes\\": 10,\\"hours\\": 0.16666666666666666,},{\\"seconds\\": 1200,\\"minutes\\": 20,\\"hours\\": 0.3333333333333333,}]
})).handle(event);
};",
"simulator.json": {
Expand Down Expand Up @@ -650,7 +650,7 @@ if (!(this.my_capture.hours === 2)) { throw new Error(\`assertion failed: \\"thi
}
};
})())({
my_capture: {seconds: 7200,minutes: 120,hours: 2,}
my_capture: {\\"seconds\\": 7200,\\"minutes\\": 120,\\"hours\\": 2,}
})).handle(event);
};",
"simulator.json": {
Expand Down Expand Up @@ -981,7 +981,7 @@ if (!(this.my_map.get('bar')[1].seconds === 40 * 60)) { throw new Error(\`assert
}
};
})())({
my_map: new Map([[\\"foo\\",[{seconds: 600,minutes: 10,hours: 0.16666666666666666,},{seconds: 1200,minutes: 20,hours: 0.3333333333333333,}]],[\\"bar\\",[{seconds: 1800,minutes: 30,hours: 0.5,},{seconds: 2400,minutes: 40,hours: 0.6666666666666666,}]]])
my_map: new Map([[\\"foo\\",[{\\"seconds\\": 600,\\"minutes\\": 10,\\"hours\\": 0.16666666666666666,},{\\"seconds\\": 1200,\\"minutes\\": 20,\\"hours\\": 0.3333333333333333,}]],[\\"bar\\",[{\\"seconds\\": 1800,\\"minutes\\": 30,\\"hours\\": 0.5,},{\\"seconds\\": 2400,\\"minutes\\": 40,\\"hours\\": 0.6666666666666666,}]]])
})).handle(event);
};",
"simulator.json": {
Expand Down Expand Up @@ -1504,7 +1504,7 @@ if (!(Array.from(this.my_set)[1].seconds === 1200)) { throw new Error(\`assertio
}
};
})())({
my_set: new Set([{seconds: 600,minutes: 10,hours: 0.16666666666666666,},{seconds: 1200,minutes: 20,hours: 0.3333333333333333,}])
my_set: new Set([{\\"seconds\\": 600,\\"minutes\\": 10,\\"hours\\": 0.16666666666666666,},{\\"seconds\\": 1200,\\"minutes\\": 20,\\"hours\\": 0.3333333333333333,}])
})).handle(event);
};",
"simulator.json": {
Expand Down Expand Up @@ -1720,7 +1720,7 @@ if (!(Object.keys(this.my_capture).length === 3)) { throw new Error(\`assertion
}
};
})())({
my_capture: {hello: \\"dude\\",world: \\"cup\\",foo: \\"bar\\",}
my_capture: {\\"hello\\": \\"dude\\",\\"world\\": \\"cup\\",\\"foo\\": \\"bar\\",}
})).handle(event);
};",
"simulator.json": {
Expand Down Expand Up @@ -1829,7 +1829,7 @@ if (!(this.my_struct.bar.get('bar') === 4)) { throw new Error(\`assertion failed
}
};
})())({
my_struct: {foo: new Map([[\\"foo\\",1],[\\"bar\\",2]]),bar: new Map([[\\"foo\\",3],[\\"bar\\",4]]),}
my_struct: {\\"foo\\": new Map([[\\"foo\\",1],[\\"bar\\",2]]),\\"bar\\": new Map([[\\"foo\\",3],[\\"bar\\",4]]),}
})).handle(event);
};",
"simulator.json": {
Expand Down Expand Up @@ -1942,13 +1942,13 @@ if (!(await bar.get(\\"foo\\") === \\"bar\\")) { throw new Error(\`assertion fai
}
};
})())({
my_struct: {bucky: (function(env) {
my_struct: {\\"bucky\\": (function(env) {
let handle = process.env[env];
if (!handle) {
throw new Error(\\"Missing environment variable: \\" + env);
}
return $simulator.findInstance(handle);
})(\\"BUCKET_HANDLE_4fecd6d0\\"),mapy: new Map([[\\"foo\\",(function(env) {
})(\\"BUCKET_HANDLE_4fecd6d0\\"),\\"mapy\\": new Map([[\\"foo\\",(function(env) {
let handle = process.env[env];
if (!handle) {
throw new Error(\\"Missing environment variable: \\" + env);
Expand All @@ -1960,7 +1960,7 @@ my_struct: {bucky: (function(env) {
throw new Error(\\"Missing environment variable: \\" + env);
}
return $simulator.findInstance(handle);
})(\\"BUCKET_HANDLE_0120daf4\\")]]),arry: {boom: [(function(env) {
})(\\"BUCKET_HANDLE_0120daf4\\")]]),\\"arry\\": {\\"boom\\": [(function(env) {
let handle = process.env[env];
if (!handle) {
throw new Error(\\"Missing environment variable: \\" + env);
Expand Down
4 changes: 2 additions & 2 deletions libs/wingsdk/test/target-sim/redis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ test("can del a value", async () => {
const recordsDeleted = await client.del(key);
const value = await client.get(key);
expect(recordsDeleted).toEqual(1);
expect(value).toEqual(null);
expect(value).toBeUndefined();
});
});

Expand Down Expand Up @@ -130,6 +130,6 @@ test("get a value that does not exist", async () => {
await app._withSimulator(async (s) => {
const client = s.getResource("/my_redis") as ex.IRedisClient;
const value = await client.get(key);
expect(value).toEqual(null);
expect(value).toBeUndefined();
});
});
2 changes: 1 addition & 1 deletion libs/wingsdk/turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"outputs": ["lib/**", ".jsii"]
},
"post-compile": {
"dependsOn": ["compile"],
"dependsOn": ["^compile", "compile"],
"inputs": [".jsii", "src/**/*.md"],
"outputs": ["../../docs/docs/04-standard-library/*/*.md"]
},
Expand Down
Loading

0 comments on commit 80f3ab1

Please sign in to comment.