Skip to content

Commit

Permalink
Hotfix correct async init of oop testbed
Browse files Browse the repository at this point in the history
  • Loading branch information
tolauwae committed Dec 2, 2024
1 parent 7336064 commit 8d74bd6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 36 deletions.
58 changes: 39 additions & 19 deletions src/framework/Testee.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Framework} from './Framework';
import {SourceMap} from '../sourcemap/SourceMap';
import {Message, Request} from '../messaging/Message';
import {Testbed} from '../testbeds/Testbed';
import {Testbed, TestbedEvents} from '../testbeds/Testbed';
import {TestbedFactory} from '../testbeds/TestbedFactory';
import {Kind} from './scenario/Step';
import {SourceMapFactory} from '../sourcemap/SourceMapFactory';
Expand All @@ -11,6 +11,7 @@ import {CompileOutput, CompilerFactory} from '../manage/Compiler';
import {WABT} from '../util/env';
import {Completion, expect, Result, ScenarioResult, SuiteResults} from './Reporter';
import {WASM} from '../sourcemap/Wasm';
import {DummyProxy} from '../testbeds/Emulator';

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

private testbed?: Testbed;

private proxy?: Testbed;
private proxy?: DummyProxy;

private current?: string; // current program

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

if (!proxy) {
return;
}
}

const testbed: Testbed | void = await this.connector.initialize(this.specification, program, args).catch((e) => {
reject(e)
});
if (testbed) {
this.testbed = testbed;
this.proxy = proxy as DummyProxy;
this.proxy.on(TestbedEvents.Ready, () => {
resolve(this);
});
await this.proxy.sendRequest(new SourceMap.Mapping(), Message.proxifyRequest);
args = args.concat(['--proxy', `${spec.dummy.port}`]);

const testbed: Testbed | void = await this.connector.initialize(this.specification, program, args).catch((e) => {
reject(e);
});
if (testbed) {
this.testbed = testbed;
}
} else {
const testbed: Testbed | void = await this.connector.initialize(this.specification, program, args).catch((e) => {
reject(e)
});
if (testbed) {
this.testbed = testbed;
}
resolve(this);
}
resolve(this);
});
}

Expand Down Expand Up @@ -159,16 +173,22 @@ export class Testee { // TODO unified with testbed interface
} catch (e) {
await testee.initialize(description.program, description.args ?? []).catch((o) => Promise.reject(o));
}
}).catch((e: Error|string) => {
if(typeof e === 'string') scenarioResult.error = new Error(e);
else scenarioResult.error = e;
}).catch((e: Error | string) => {
if (typeof e === 'string') {
scenarioResult.error = new Error(e);
} else {
scenarioResult.error = e;
}
});

await this.run('Get source mapping', testee.connector.timeout, async function () {
map = await testee.mapper.map(description.program);
}).catch((e: Error|string) => {
if(typeof e === 'string') scenarioResult.error = new Error(e);
else scenarioResult.error = e;
}).catch((e: Error | string) => {
if (typeof e === 'string') {
scenarioResult.error = new Error(e);
} else {
scenarioResult.error = e;
}
});

if (scenarioResult.error) {
Expand Down
2 changes: 1 addition & 1 deletion src/messaging/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export namespace Message {
parser: stateParser
}

export const proxifyRequest: Request<string> = {
export const proxifyRequest: Request<string> = {
type: Interrupt.proxify,
parser: identityParser
};
Expand Down
20 changes: 8 additions & 12 deletions src/testbeds/Emulator.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import {Platform} from './Platform';
import {SubProcess} from '../bridge/SubProcess';
import {Connection} from '../bridge/Connection';
import {UploaderFactory} from '../manage/Uploader';
import {ARDUINO, EMULATOR} from '../util/env';
import {ProxySpecification} from './TestbedSpecification';
import {CompileOutput} from '../manage/Compiler';
import * as net from 'node:net';
import {SourceMap} from '../sourcemap/SourceMap';
import {Request} from '../messaging/Message';
import {AddressInfo, Socket} from 'node:net';
import {Socket} from 'node:net';
import {MessageQueue} from '../messaging/MessageQueue';
import {TestbedEvents} from './Testbed';

export class Emulator extends Platform {
readonly name: string = 'Emulator';
Expand Down Expand Up @@ -41,20 +36,19 @@ export class DummyProxy extends Emulator {

private supervisor?: Socket;

constructor(connection: SubProcess) {
constructor(connection: SubProcess, specification: ProxySpecification) {
super(connection);

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

this.dummy = new net.Server();
}

public async init(specification: ProxySpecification) {
this.dummy.on('connection', (connection) => {
this.supervisor = connection;
connection.on('data', (data) => {
this.connection.channel.write(data.toString());
})
});
this.emit(TestbedEvents.Ready);
});
this.dummy.listen(specification.dummy.port);
}
Expand All @@ -68,7 +62,9 @@ export class DummyProxy extends Emulator {
this.forwarding.push(data.toString());
while (this.forwarding.hasCompleteMessage()) {
const message = this.forwarding.pop()
if(!message?.includes('Interrupt')) this.supervisor!.write(message!.toString());
if (!message?.includes('Interrupt')) {
this.supervisor!.write(message!.toString());
}
}

}
Expand Down
3 changes: 2 additions & 1 deletion src/testbeds/Testbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {Breakpoint} from '../debug/Breakpoint';
export enum TestbedEvents {
OnMessage = 'message',
OnBreakpointHit = 'breakpoint',
OnPushEvent = 'push'
OnPushEvent = 'push',
Ready = 'ready'
}

export declare interface Testbed extends EventEmitter {
Expand Down
4 changes: 1 addition & 3 deletions src/testbeds/TestbedFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ export class TestbedFactory {
case PlatformType.debug:
return new Emulator(connection as SubProcess);
case PlatformType.emuproxy:
const dummy = new DummyProxy(connection as SubProcess);
await dummy.init(specification as ProxySpecification);
return dummy;
return new DummyProxy(connection as SubProcess, specification as ProxySpecification);
default:
return Promise.reject('Platform not implemented.');
}
Expand Down

0 comments on commit 8d74bd6

Please sign in to comment.