Skip to content

Commit

Permalink
timeout works for postRequest
Browse files Browse the repository at this point in the history
SQUASHED: AUTO-COMMIT-src-worker-systemjs-worker.js,AUTO-COMMIT-test-worker-systemjs-worker-test.js,AUTO-COMMIT-test-worker-systemjs-worker-test-timeout-worker.js,
  • Loading branch information
JensLincke committed Nov 15, 2023
1 parent b868c9c commit 536cfdb
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
32 changes: 24 additions & 8 deletions src/worker/systemjs-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
}
})
Expand Down
7 changes: 7 additions & 0 deletions test/worker/systemjs-worker-test-timeout-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

export async function onrequest(name, a, b) {
// do nothing... we timeout
await new Promise(resolve => setTimeout((resolve, 1000)))
return "Hello"
}

35 changes: 35 additions & 0 deletions test/worker/systemjs-worker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down

0 comments on commit 536cfdb

Please sign in to comment.