Permalink
Browse files

Add a test for global events in a worker.

  • Loading branch information...
1 parent dd2f3dd commit 0e261ebb0cbf118d2c030c997f92f53693d44e28 @lddubeau lddubeau committed Aug 27, 2016
Showing with 75 additions and 5 deletions.
  1. +1 −0 .gitignore
  2. +2 −2 test/browser/promise_debug.js
  3. +20 −0 test/browser/worker.js
  4. +42 −0 test/mocha/unhandled_rejections.js
  5. +10 −3 tools/browser_test_generator.js
View
@@ -3,6 +3,7 @@ gh-pages
node_modules
gh-pages
test/browser/bundle.js
+test/browser/worker_bundle.js
js/*
zalgo.js
coverage/*
@@ -1,5 +1,5 @@
var Promise = require("../../js/browser/bluebird.min.js");
Promise.longStackTraces();
Promise.config({cancellation:true});
-window.Promise = Promise;
-window.adapter = Promise;
+self.Promise = Promise;
+self.adapter = Promise;
@@ -0,0 +1,20 @@
+self.importScripts("./worker_bundle.js");
+
+var currentPromise;
+
+function handler(ev) {
+ ev.preventDefault();
+ self.postMessage(ev.type);
+ if (ev.type === "unhandledrejection") {
+ currentPromise.catch(function () {});
+ }
+}
+
+self.addEventListener("unhandledrejection", handler);
+self.addEventListener("rejectionhandled", handler);
+
+self.onmessage = function onmessage(ev) {
+ if (ev.data === "reject") {
+ currentPromise = Promise.reject(new Error("rejected"));
+ }
+};
@@ -677,6 +677,48 @@ if (windowDomEventSupported) {
})
});
+
+ if (typeof Worker !== "undefined") {
+ describe("dom events in a worker", function() {
+ var worker;
+ beforeEach(function () {
+ worker = new Worker("./worker.js");
+ });
+
+ afterEach(function () {
+ worker.terminate();
+ });
+
+ specify("are fired", function() {
+ var order = [];
+ return new Promise(function(resolve, reject) {
+ worker.onmessage = function (message) {
+ try {
+ switch(message.data) {
+ case "unhandledrejection":
+ order.push(1);
+ break;
+ case "rejectionhandled":
+ order.push(2);
+ resolve();
+ break;
+ default:
+ throw new Error("unexpected message: " + message);
+ }
+ }
+ catch (e) {
+ reject(e);
+ }
+ };
+
+ worker.postMessage("reject");
+ }).then(function () {
+ assert.deepEqual(order, [1, 2]);
+ }).timeout(500);
+ });
+ });
+ }
+
}
describe("Unhandled rejection when joining chains with common rejected parent", function testFunction() {
@@ -33,13 +33,20 @@ module.exports = function(tests, options) {
return Promise.join(promiseExport, main, function(promiseExport, main) {
var browserify = require("browserify");
var contents = promiseExport + "\n" + main + "\n" + testRequires;
- var b = browserify({
+ var complete = browserify({
basedir: baseDir,
entries: stringToStream(contents)
});
- return Promise.promisify(b.bundle, b)().then(function(src) {
- return writeFile(path.join(baseDir, "bundle.js"), src);
+ var worker = browserify({
+ basedir: baseDir,
+ entries: stringToStream(promiseExport),
});
+ return Promise.join(
+ Promise.promisify(complete.bundle, complete)().then(function(src) {
+ return writeFile(path.join(baseDir, "bundle.js"), src);
+ }), Promise.promisify(worker.bundle, worker)().then(function (src) {
+ return writeFile(path.join(baseDir, "worker_bundle.js"), src);
+ }));
}).then(function() {
if (options.executeBrowserTests) {
return require("./browser_test_runner.js")(options);

0 comments on commit 0e261eb

Please sign in to comment.