diff --git a/src/worker/systemjs-worker.js b/src/worker/systemjs-worker.js index 81bf41282..891dbf88f 100644 --- a/src/worker/systemjs-worker.js +++ b/src/worker/systemjs-worker.js @@ -22,6 +22,8 @@ export default class SystemjsWorker { /*MD The meta-worker is the actual worker, that is generic will load the actual systemjs module, which contains the client code MD*/ this.idCounter = 1 this.resolveRequestForId = new Map() + this.rejectRequestForId = new Map() + this.startTimeForId = new Map() this.timeout=1000 @@ -77,26 +79,40 @@ export default class SystemjsWorker { handleRequest(msg) { console.log("handleRequest ", msg) var resolve = this.resolveRequestForId.get(msg.id) + var reject = this.rejectRequestForId.get(msg.id) if (!resolve) { throw new Error("No resolve func for message " + msg.id + ", " + msg.response) } this.resolveRequestForId.set(msg.id, null) - resolve(msg.response) + this.rejectRequestForId.set(msg.id, null) + if (!msg.error) { + console.log("FINISHED " + msg.id + " in " + (performance.now() - this.startTimeForId.get(msg.id)) + "ms response: " +msg.response ) + resolve(msg.response) + } else { + reject(msg.error) + } } async postRequest(...data) { - - var id = this.newId() - var promise = new Promise((resolve, reject) => { + + let id = this.newId() + console.log("POST REQUEST " + id) + this.startTimeForId.set(id, performance.now()) + let promise = new Promise((resolve, reject) => { this.resolveRequestForId.set(id, resolve) + this.rejectRequestForId.set(id, reject) this.postMessage({message: "systemjs-worker-request", id: id, arguments: data}) var start = performance.now() - if (this.timeout === Infinity || this.timeout < 0 || this.timeout === null || this.timeout === undefined) { - // do nothing - } else { + if (this.timeout !== Infinity && this.timeout > 0) { + setTimeout(() => { var unhandledRequestResolve = this.resolveRequestForId.get(id) - if (unhandledRequestResolve) reject({error: "request timeout after " + (performance.now() - start) + "ms"}) + if (unhandledRequestResolve) { + console.log("TIMEOUT! " + id) + reject( "request timeout after " + (performance.now() - start) + "ms") + } else { + console.log("NOOO TIMEOUT!" + id) + } }, this.timeout) } }) diff --git a/test/worker/systemjs-worker-test-timeout-worker.js b/test/worker/systemjs-worker-test-timeout-worker.js new file mode 100644 index 000000000..1f9e48431 --- /dev/null +++ b/test/worker/systemjs-worker-test-timeout-worker.js @@ -0,0 +1,7 @@ + +export async function onrequest(name, a, b) { + // do nothing... we timeout + await new Promise(resolve => setTimeout((resolve, 1000))) + return "Hello" +} + diff --git a/test/worker/systemjs-worker-test.js b/test/worker/systemjs-worker-test.js index acf1b7f52..6e6c3d340 100644 --- a/test/worker/systemjs-worker-test.js +++ b/test/worker/systemjs-worker-test.js @@ -47,6 +47,41 @@ describe('SystemJSWorker', () => { expect(result, "result").equals(7) }) + + after(() => { + myworker.terminate() + }) + }) + + + describe('timeout', () => { + let myworker + before(async () => { + myworker = new SystemjsWorker(lively4url + "/test/worker/systemjs-worker-test-timeout-worker.js") + + await myworker.loaded + }) + + + it('throws an error', async () => { + + myworker.timeout = 100 + + let error, result + let errorWasThrown = false + try { + result = await myworker.postRequest("nothing?"); + + } catch(e) { + errorWasThrown = true + error = e + expect("" + error).match(/timeout/) + } + expect(result).to.be.undefined + expect(errorWasThrown, "error was thrown").to.be.true + }) + + after(() => { myworker.terminate() })