Skip to content

Commit

Permalink
chore: Trying to fix the pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
FranklinWaller committed Oct 2, 2023
1 parent cc6311e commit 863ce62
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 19 deletions.
5 changes: 3 additions & 2 deletions libs/as-sdk-integration-tests/src/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import { PromiseStatus } from '../../../dist/libs/vm/src/types/vm-promise';

const mockHttpFetch = jest.fn();

jest.setTimeout(30_000);

const TestVmAdapter = jest.fn().mockImplementation(() => {
return { httpFetch: mockHttpFetch };
});

describe('Http', () => {
it('Test SDK HTTP Rejection', async () => {

it.only('Test SDK HTTP Rejection', async () => {
const wasmBinary = await readFile('dist/libs/as-sdk-integration-tests/debug.wasm');
const result = await callVm({
args: ['testHttpRejection'],
Expand Down
23 changes: 14 additions & 9 deletions libs/vm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,20 @@ export function callVm(callData: VmCallData, workerUrl = DEFAULT_WORKER_PATH, vm
type: WorkerMessageType.VmCall,
};

worker.on('message', (message: WorkerMessage) => {
if (message.type === WorkerMessageType.VmResult) {
resolve(message.result);
} else if (message.type === WorkerMessageType.VmActionExecute) {
hostToWorker.executeAction(message.action);
} else if (message.type === WorkerMessageType.VmActionResultBuffer) {
hostToWorker.sendActionResultToWorker(message.buffer);
} else {
console.warn('Unknown message', message);
worker.on('message', async (message: WorkerMessage) => {
try {
console.error('@message: ', message);
if (message.type === WorkerMessageType.VmResult) {
resolve(message.result);
} else if (message.type === WorkerMessageType.VmActionExecute) {
await hostToWorker.executeAction(message.action);
} else if (message.type === WorkerMessageType.VmActionResultBuffer) {
await hostToWorker.sendActionResultToWorker(message.buffer);
} else {
console.warn('Unknown message', message);
}
} catch (error) {
console.error('@callVm-onMessage: ', error);
}
});

Expand Down
4 changes: 4 additions & 0 deletions libs/vm/src/vm-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ export default class VmImports {
}

httpFetch(action: number, actionLength: number) {
console.log("HTTP Action called");
const rawAction = new Uint8Array(
this.memory?.buffer.slice(action, action + actionLength) ?? []
);
const messageRaw = Buffer.from(rawAction).toString('utf-8');

console.log('HTTP Action called with ', messageRaw);

try {
const message: HttpFetchAction = JSON.parse(messageRaw);
this.callResult = this.workerToHost.callActionOnHost(message);
console.log('We got a result: ', this.callResult);
return this.callResult.length;
} catch (error) {
console.error(`@httpFetch: ${messageRaw}`, error);
Expand Down
2 changes: 2 additions & 0 deletions libs/vm/src/vm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { PromiseStatus } from "../../../dist/libs/vm/src/types/vm-promise";

const mockHttpFetch = jest.fn();

jest.setTimeout(10_000);

const TestVmAdapter = jest.fn().mockImplementation(() => {
return { httpFetch: mockHttpFetch };
});
Expand Down
3 changes: 3 additions & 0 deletions libs/vm/src/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export async function executeVm(callData: VmCallData, notifierBuffer: SharedArra
const memory = instance.exports.memory;
vmImports.setMemory(memory as WebAssembly.Memory);

console.log("Starting WASI");
const exitCode = wasi.start(instance);
console.log("exit: ", exitCode);

return {
exitCode,
Expand All @@ -44,6 +46,7 @@ export async function executeVm(callData: VmCallData, notifierBuffer: SharedArra
resultAsString: new TextDecoder().decode(vmImports.result),
}
} catch (err) {
console.log('Exception thrown', err);
console.error(`
@executeWasm
Exception threw: ${err}
Expand Down
5 changes: 5 additions & 0 deletions libs/vm/src/worker-host-communication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export class HostToWorker {
const notifierBufferi32 = new Int32Array(this.notifierBuffer);
notifierBufferi32.set([this.actionResult.length], 1);

console.error("setted buffer, ready to send");

updateNotifierState(notifierBufferi32, AtomicState.ResponseResultLength);
}

Expand Down Expand Up @@ -112,6 +114,8 @@ export class WorkerToHost {

const length = notifierBufferi32[1];

console.error("The length is: ", length);

// No need to do a full roundtrip if there are no bytes
if (length === 0) {
return Buffer.from([]);
Expand All @@ -123,6 +127,7 @@ export class WorkerToHost {
type: WorkerMessageType.VmActionResultBuffer,
};

console.error('Requesting message: ', message);
parentPort?.postMessage(message);

waitForNotifierStateChange(notifierBufferi32, AtomicState.RequestResult);
Expand Down
20 changes: 12 additions & 8 deletions libs/vm/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ import {
} from './types/worker-messages.js';

parentPort?.on('message', async function (event) {
const message: WorkerMessage = event;
try {
const message: WorkerMessage = event;

if (message.type === WorkerMessageType.VmCall) {
const result = await executeVm(message.callData, message.notifierBuffer);
const response: VmResultWorkerMessage = {
result,
type: WorkerMessageType.VmResult,
};
if (message.type === WorkerMessageType.VmCall) {
const result = await executeVm(message.callData, message.notifierBuffer);
const response: VmResultWorkerMessage = {
result,
type: WorkerMessageType.VmResult,
};

parentPort?.postMessage(response);
parentPort?.postMessage(response);
}
} catch (error) {
console.error("@worker:message, error thrown: ", error);
}
});

0 comments on commit 863ce62

Please sign in to comment.