/
test-setup.ts
339 lines (284 loc) · 10.5 KB
/
test-setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import { expect } from "chai";
import { ChildProcess, spawn, spawnSync, SpawnSyncReturns } from "child_process";
import { resolve } from "path";
import * as WebSocket from "ws";
import { Controller } from "../src/controller";
import {
BreakpointData, SourceCoordinate, StoppedMessage, StackTraceResponse,
FullSourceCoordinate, StackFrame
} from "../src/messages";
import { VmConnection } from "../src/vm-connection";
import { ActivityId } from "./somns-support";
import { Activity } from "../src/execution-data";
import { determinePorts } from "../src/launch-connector";
import { Stop, Test } from "./stepping";
const SOM_BASEPATH = "../../";
export const SOM = SOM_BASEPATH + "som";
export const PING_PONG_FILE = resolve("tests/pingpong.ns");
export const PING_PONG_URI = "file:" + PING_PONG_FILE;
export const ACTOR_FILE = resolve("tests/actor.ns")
export const ACTOR2_FILE = resolve("tests/actor2.ns")
export const ACTOR_URI = "file:" + ACTOR_FILE;
export const ACTOR2_URI = "file:" + ACTOR2_FILE;
const PRINT_SOM_OUTPUT = false;
const PRINT_CMD_LINE = false;
let stdErr = "";
let stdOut = "";
export function getStdErr() { let current = stdErr; stdErr = ""; return current; }
export function getStdOut() { let current = stdOut; stdOut = ""; return current; }
export function expectStop(msg: StackTraceResponse, stop: Stop, activityMap) {
expectStack(msg.stackFrames, stop.stackHeight, stop.methodName, stop.line);
if (activityMap[msg.activityId]) {
expect(activityMap[msg.activityId]).to.equal(stop.activity);
} else {
activityMap[msg.activityId] = stop.activity;
}
}
export function expectStack(stack: StackFrame[], length: number, methodName: string,
startLine: number) {
expect(stack[0]).to.be.not.null;
expect(stack[0].name).to.equal(methodName);
expect(stack[0].line).to.equal(startLine);
return expect(stack).lengthOf(length);
}
export function expectSourceCoordinate(section: SourceCoordinate) {
expect(section).to.have.property("charLength");
expect(section).to.have.property("startColumn");
expect(section).to.have.property("startLine");
}
export function expectFullSourceCoordinate(section: FullSourceCoordinate) {
expectSourceCoordinate(section);
expect(section).to.have.property("uri");
}
export interface OnMessageEvent {
data: any;
type: string;
target: WebSocket;
}
export interface OnMessageHandler {
(event: OnMessageEvent): void;
}
export class TestConnection extends VmConnection {
private somProc: ChildProcess;
private closed: boolean;
private connectionResolver;
public readonly fullyConnected: Promise<boolean>;
constructor(extraArgs?: string[], triggerDebugger?: boolean, testFile?: string) {
super(true);
this.closed = false;
this.startSom(extraArgs, triggerDebugger, testFile);
this.fullyConnected = this.initConnection();
}
private startSom(extraArgs?: string[], triggerDebugger?: boolean, testFile?: string) {
if (!testFile) { testFile = "tests/pingpong.ns"; }
let args = ["-G", "-wd", testFile];
if (triggerDebugger) { args = ["-d"].concat(args); };
if (extraArgs) { args = args.concat(extraArgs); }
if (PRINT_CMD_LINE) {
console.log("[CMD]" + SOM + " " + args.join(" "));
}
this.somProc = spawn(SOM, args);
}
protected onOpen() {
super.onOpen();
this.connectionResolver(true);
}
private initConnection(): Promise<boolean> {
const promise = new Promise<boolean>((resolve, reject) => {
this.connectionResolver = resolve;
let connecting = false;
stdErr = "";
stdOut = "";
this.somProc.on("exit", (code, signal) => {
if (code !== 0) {
this.somProc.stderr.on("close", () => {
this.somProc.on("exit", (_code, _signal) => {
reject(new Error("Process exited with code: " + code + " Signal: " + signal + " StdErr: " + stdErr));
});
});
}
});
this.somProc.stderr.on("data", data => {
const dataStr = data.toString();
if (PRINT_SOM_OUTPUT) {
console.error(dataStr);
}
stdErr += dataStr;
});
const ports = { msg: 0, trace: 0 };
this.somProc.stdout.on("data", (data) => {
const dataStr = data.toString();
stdOut += dataStr;
if (PRINT_SOM_OUTPUT) {
console.log(dataStr);
}
determinePorts(dataStr, ports);
if (dataStr.includes("Started HTTP Server") && !connecting) {
connecting = true;
console.assert(ports.msg > 0 && ports.trace > 0);
this.connectWebSockets(ports.msg, ports.trace);
}
if (dataStr.includes("Failed starting WebSocket and/or HTTP Server")) {
this.somProc.stderr.on("close", () => {
this.somProc.on("exit", (_code, _signal) => {
reject(new Error("SOMns failed to starting WebSocket and/or HTTP Server. StdOut: " + dataStr + " StdErr: " + stdErr));
});
});
this.somProc.kill();
}
});
});
return promise;
}
public close(done: MochaDone) {
if (!this.closed) {
this.closed = true;
this.disconnect();
this.somProc.kill();
}
done();
}
}
export class ControllerWithInitialBreakpoints extends Controller {
private initialBreakpoints: BreakpointData[];
constructor(initialBreakpoints: BreakpointData[], vmConnection: VmConnection) {
super(vmConnection);
this.initialBreakpoints = initialBreakpoints;
}
public onConnect() {
super.onConnect();
this.vmConnection.sendInitializeConnection(this.initialBreakpoints);
}
}
export function execSom(extraArgs: string[]): SpawnSyncReturns<string> {
const args = ["-G", "-t1", "-dnu", "tests/pingpong.ns"].concat(extraArgs);
return spawnSync(SOM, args);
}
export class HandleStoppedAndGetStackTrace extends ControllerWithInitialBreakpoints {
private numStopped: number;
private readonly numOps: number;
public readonly stackPs: Promise<StackTraceResponse>[];
private readonly resolveStackPs;
public readonly stoppedActivities: Activity[];
constructor(initialBreakpoints: BreakpointData[], vmConnection: VmConnection,
connectionP: Promise<boolean>, numOps: number = 1) {
super(initialBreakpoints, vmConnection);
this.numOps = numOps;
this.numStopped = 0;
this.stoppedActivities = [];
this.resolveStackPs = [];
this.stackPs = [];
for (let i = 0; i < numOps; i += 1) {
this.stackPs.push(new Promise<StackTraceResponse>((resolve, reject) => {
this.resolveStackPs.push(resolve);
connectionP.catch(reject);
}));
}
}
public onStoppedMessage(msg: StoppedMessage) {
if (this.numStopped >= this.numOps) { return; }
// don't need more than a dummy activity at the moment, just id is enough
const activity: Activity = {
id: msg.activityId, completed: false,
name: "dummy", type: <number> ActivityId.ACTOR, creationScope: null,
creationActivity: null, running: false
};
this.stoppedActivities[this.numStopped] = activity;
this.vmConnection.requestStackTrace(msg.activityId, this.numStopped);
this.numStopped += 1;
}
public onStackTrace(msg: StackTraceResponse) {
this.resolveStackPs[msg.requestId](msg);
}
}
export class TestController extends ControllerWithInitialBreakpoints {
private readonly activityMap;
private numStopped: number;
private resolve: (value?: Stop[] | PromiseLike<Stop[]>) => void;
public stopsDoneForStep: Promise<Stop[]>;
private readonly suite: Test;
private expectedStops: Stop[];
private receivedStops: Stop[];
private readonly connectionP: Promise<boolean>;
constructor(suite: Test, vmConnection: VmConnection,
connectionP: Promise<boolean>) {
super(suite.initialBreakpoints, vmConnection);
this.connectionP = connectionP;
this.activityMap = {};
this.suite = suite;
this.numStopped = 0;
this.expectedStops = [suite.initialStop];
this.receivedStops = [];
this.renewPromise();
}
private renewPromise() {
if (this.expectedStops !== null && this.expectedStops !== undefined &&
this.expectedStops.length > 0) {
this.stopsDoneForStep = new Promise<Stop[]>((resolve, reject) => {
this.resolve = resolve;
this.connectionP.catch(reject);
});
} else {
this.stopsDoneForStep = Promise.resolve([]);
}
return this.stopsDoneForStep;
}
private getActivityId(activity: string): number {
for (const actId in this.activityMap) {
if (this.activityMap[actId] === activity) {
return parseInt(actId);
}
}
throw new Error("Activity map did not contain an id for activity: " + activity);
}
public doNextStep(type: string, activity: string, expectedStops?: Stop[]) {
this.expectedStops = expectedStops;
this.receivedStops = [];
const actId = this.getActivityId(activity);
const act: Activity = {
id: actId, completed: false,
name: "dummy", type: <number> ActivityId.ACTOR, creationScope: null,
creationActivity: null, running: false
};
this.vmConnection.sendDebuggerAction(type, act);
return this.renewPromise();
}
public onStoppedMessage(msg: StoppedMessage) {
if (this.numStopped === 0) {
this.activityMap[msg.activityId] = this.suite.initialStop.activity;
}
this.vmConnection.requestStackTrace(msg.activityId, this.numStopped);
this.numStopped += 1;
}
private getActivity(activityId: number, currentStop: Stop): string {
if (this.activityMap[activityId]) {
return this.activityMap[activityId];
}
for (const stop of this.expectedStops) {
if (stop.line === currentStop.line &&
stop.methodName === currentStop.methodName &&
stop.stackHeight === currentStop.stackHeight) {
this.activityMap[activityId] = stop.activity;
return stop.activity;
}
}
throw new Error("Could not identify which activity name corresponds to " +
`activity id: ${activityId} for stopped msg: ${JSON.stringify(currentStop)}`);
}
public onStackTrace(msg: StackTraceResponse) {
const stop: Stop = {
line: msg.stackFrames[0].line,
methodName: msg.stackFrames[0].name,
stackHeight: msg.stackFrames.length,
activity: ""
};
const activity = this.getActivity(msg.activityId, stop);
stop.activity = activity;
this.receivedStops.push(stop);
if (this.receivedStops.length === this.expectedStops.length) {
this.resolve(this.receivedStops);
} else if (this.receivedStops.length > this.expectedStops.length) {
throw new Error("Received more StoppedMessages than expected.");
}
}
}