Skip to content

Commit 8d74bd6

Browse files
committed
Hotfix correct async init of oop testbed
1 parent 7336064 commit 8d74bd6

File tree

5 files changed

+51
-36
lines changed

5 files changed

+51
-36
lines changed

src/framework/Testee.ts

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Framework} from './Framework';
22
import {SourceMap} from '../sourcemap/SourceMap';
33
import {Message, Request} from '../messaging/Message';
4-
import {Testbed} from '../testbeds/Testbed';
4+
import {Testbed, TestbedEvents} from '../testbeds/Testbed';
55
import {TestbedFactory} from '../testbeds/TestbedFactory';
66
import {Kind} from './scenario/Step';
77
import {SourceMapFactory} from '../sourcemap/SourceMapFactory';
@@ -11,6 +11,7 @@ import {CompileOutput, CompilerFactory} from '../manage/Compiler';
1111
import {WABT} from '../util/env';
1212
import {Completion, expect, Result, ScenarioResult, SuiteResults} from './Reporter';
1313
import {WASM} from '../sourcemap/Wasm';
14+
import {DummyProxy} from '../testbeds/Emulator';
1415

1516
export function timeout<T>(label: string, time: number, promise: Promise<T>): Promise<T> {
1617
if (time === 0) {
@@ -70,7 +71,7 @@ export class Testee { // TODO unified with testbed interface
7071

7172
private testbed?: Testbed;
7273

73-
private proxy?: Testbed;
74+
private proxy?: DummyProxy;
7475

7576
private current?: string; // current program
7677

@@ -94,20 +95,33 @@ export class Testee { // TODO unified with testbed interface
9495
const proxy: Testbed | void = await this.connector.initialize(spec, program, args ?? []).catch((e) => {
9596
reject(e)
9697
});
97-
if (proxy) {
98-
this.proxy = proxy;
99-
await this.proxy.sendRequest(new SourceMap.Mapping(), Message.proxifyRequest);
100-
args = args.concat(['--proxy', `${spec.dummy.port}`]);
98+
99+
if (!proxy) {
100+
return;
101101
}
102-
}
103102

104-
const testbed: Testbed | void = await this.connector.initialize(this.specification, program, args).catch((e) => {
105-
reject(e)
106-
});
107-
if (testbed) {
108-
this.testbed = testbed;
103+
this.proxy = proxy as DummyProxy;
104+
this.proxy.on(TestbedEvents.Ready, () => {
105+
resolve(this);
106+
});
107+
await this.proxy.sendRequest(new SourceMap.Mapping(), Message.proxifyRequest);
108+
args = args.concat(['--proxy', `${spec.dummy.port}`]);
109+
110+
const testbed: Testbed | void = await this.connector.initialize(this.specification, program, args).catch((e) => {
111+
reject(e);
112+
});
113+
if (testbed) {
114+
this.testbed = testbed;
115+
}
116+
} else {
117+
const testbed: Testbed | void = await this.connector.initialize(this.specification, program, args).catch((e) => {
118+
reject(e)
119+
});
120+
if (testbed) {
121+
this.testbed = testbed;
122+
}
123+
resolve(this);
109124
}
110-
resolve(this);
111125
});
112126
}
113127

@@ -159,16 +173,22 @@ export class Testee { // TODO unified with testbed interface
159173
} catch (e) {
160174
await testee.initialize(description.program, description.args ?? []).catch((o) => Promise.reject(o));
161175
}
162-
}).catch((e: Error|string) => {
163-
if(typeof e === 'string') scenarioResult.error = new Error(e);
164-
else scenarioResult.error = e;
176+
}).catch((e: Error | string) => {
177+
if (typeof e === 'string') {
178+
scenarioResult.error = new Error(e);
179+
} else {
180+
scenarioResult.error = e;
181+
}
165182
});
166183

167184
await this.run('Get source mapping', testee.connector.timeout, async function () {
168185
map = await testee.mapper.map(description.program);
169-
}).catch((e: Error|string) => {
170-
if(typeof e === 'string') scenarioResult.error = new Error(e);
171-
else scenarioResult.error = e;
186+
}).catch((e: Error | string) => {
187+
if (typeof e === 'string') {
188+
scenarioResult.error = new Error(e);
189+
} else {
190+
scenarioResult.error = e;
191+
}
172192
});
173193

174194
if (scenarioResult.error) {

src/messaging/Message.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ export namespace Message {
211211
parser: stateParser
212212
}
213213

214-
export const proxifyRequest: Request<string> = {
214+
export const proxifyRequest: Request<string> = {
215215
type: Interrupt.proxify,
216216
parser: identityParser
217217
};

src/testbeds/Emulator.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import {Platform} from './Platform';
22
import {SubProcess} from '../bridge/SubProcess';
3-
import {Connection} from '../bridge/Connection';
4-
import {UploaderFactory} from '../manage/Uploader';
5-
import {ARDUINO, EMULATOR} from '../util/env';
63
import {ProxySpecification} from './TestbedSpecification';
7-
import {CompileOutput} from '../manage/Compiler';
84
import * as net from 'node:net';
9-
import {SourceMap} from '../sourcemap/SourceMap';
10-
import {Request} from '../messaging/Message';
11-
import {AddressInfo, Socket} from 'node:net';
5+
import {Socket} from 'node:net';
126
import {MessageQueue} from '../messaging/MessageQueue';
7+
import {TestbedEvents} from './Testbed';
138

149
export class Emulator extends Platform {
1510
readonly name: string = 'Emulator';
@@ -41,20 +36,19 @@ export class DummyProxy extends Emulator {
4136

4237
private supervisor?: Socket;
4338

44-
constructor(connection: SubProcess) {
39+
constructor(connection: SubProcess, specification: ProxySpecification) {
4540
super(connection);
4641

4742
this.forwarding = new MessageQueue('\n');
4843

4944
this.dummy = new net.Server();
50-
}
5145

52-
public async init(specification: ProxySpecification) {
5346
this.dummy.on('connection', (connection) => {
5447
this.supervisor = connection;
5548
connection.on('data', (data) => {
5649
this.connection.channel.write(data.toString());
57-
})
50+
});
51+
this.emit(TestbedEvents.Ready);
5852
});
5953
this.dummy.listen(specification.dummy.port);
6054
}
@@ -68,7 +62,9 @@ export class DummyProxy extends Emulator {
6862
this.forwarding.push(data.toString());
6963
while (this.forwarding.hasCompleteMessage()) {
7064
const message = this.forwarding.pop()
71-
if(!message?.includes('Interrupt')) this.supervisor!.write(message!.toString());
65+
if (!message?.includes('Interrupt')) {
66+
this.supervisor!.write(message!.toString());
67+
}
7268
}
7369

7470
}

src/testbeds/Testbed.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {Breakpoint} from '../debug/Breakpoint';
77
export enum TestbedEvents {
88
OnMessage = 'message',
99
OnBreakpointHit = 'breakpoint',
10-
OnPushEvent = 'push'
10+
OnPushEvent = 'push',
11+
Ready = 'ready'
1112
}
1213

1314
export declare interface Testbed extends EventEmitter {

src/testbeds/TestbedFactory.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ export class TestbedFactory {
3232
case PlatformType.debug:
3333
return new Emulator(connection as SubProcess);
3434
case PlatformType.emuproxy:
35-
const dummy = new DummyProxy(connection as SubProcess);
36-
await dummy.init(specification as ProxySpecification);
37-
return dummy;
35+
return new DummyProxy(connection as SubProcess, specification as ProxySpecification);
3836
default:
3937
return Promise.reject('Platform not implemented.');
4038
}

0 commit comments

Comments
 (0)