mozilla-b2g/gaia — pull request #6283 by marijnh (2012-11-08 14:19:36+00:00)
Title: Bug 805369 - Implement lazy loading for caldav worker & shut it down whe...

Well, this is my first Gaia patch. Let's see how that goes.

Here's a summary of what I did, which includes a few small changes not directly related to the bug, but which made it much easier to implement my solution.

- Got rid of the initialization exchange for workers where the UI thread tells it its url. Simply use relative URLs when loading scripts instead. Assume worker initializes fine when it doesn't raise an error.

- Simplified worker manager by removing worker queues and worker ids. Simply keep a set of objects that describe workers. Queues aren't necessary since `postMessage` has its own queue.

- Handle worker errors in a more meaningful way by keeping an array of pending requests, and, on error, terminate the worker and notify the pending requests of the failure. This should prevent hangs where request clients are endlessly waiting for a response from a crashed worker.

- Keep the external interface of the manager the same, but change the meaning of `add` to reserve a slot for a worker, but not actually start it.

- When a request for a worker arrives, check whether it is currently active. If not, start it. When the last pending request for a worker finishes, set a timeout that deactivates the worker. Clear this timeout when another request comes in in the meantime.

- Slaughter the tests for the worker manager. I removed a bunch of them which looked like they were simply poking at the internals of the class ensuring that it was implemented in a specific way. I can't really think of a reason to have such tests. I updated all tests that did test the *behavior* of the class, and added a few. Extended the mock worker to actually be able to respond to requests.

That being said, the tests for the calendar still app don't pass. They didn't pass on the current master branch without my patch either. I fixed tests related to the code I wrote, but, left the other failing tests mostly alone.

(Actually, I did touch the test for Set. I'm not sure why that test is there, or why `apps/calendar/js/set.js` exists in the first place, but I applied the obvious trivial fix in order to reduce the noise I had to sift through when running the suite.)