dom/indexedDB/test/unit/test_transaction_abort_hang.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/indexedDB/test/unit/test_transaction_abort_hang.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,91 @@
     1.4 +/**
     1.5 + * Any copyright is dedicated to the Public Domain.
     1.6 + * http://creativecommons.org/publicdomain/zero/1.0/
     1.7 + */
     1.8 +"use strict";
     1.9 +
    1.10 +var self = this;
    1.11 +
    1.12 +var testGenerator = testSteps();
    1.13 +
    1.14 +function testSteps()
    1.15 +{
    1.16 +  const dbName = self.window ?
    1.17 +                 window.location.pathname :
    1.18 +                 "test_transaction_abort_hang";
    1.19 +  const objStoreName = "foo";
    1.20 +  const transactionCount = 30;
    1.21 +
    1.22 +  let completedTransactionCount = 0;
    1.23 +  let caughtError = false;
    1.24 +
    1.25 +  let abortedTransactionIndex = Math.floor(transactionCount / 2);
    1.26 +  if (abortedTransactionIndex % 2 == 0) {
    1.27 +    abortedTransactionIndex++;
    1.28 +  }
    1.29 +
    1.30 +  let request = indexedDB.open(dbName, 1);
    1.31 +  request.onerror = errorHandler;
    1.32 +  request.onupgradeneeded = grabEventAndContinueHandler;
    1.33 +  let event = yield undefined;
    1.34 +
    1.35 +  request.result.createObjectStore(objStoreName, { autoIncrement: true });
    1.36 +
    1.37 +  request.onupgradeneeded = null;
    1.38 +  request.onsuccess = grabEventAndContinueHandler;
    1.39 +  event = yield undefined;
    1.40 +
    1.41 +  let db = event.target.result;
    1.42 +
    1.43 +  for (let i = 0; i < transactionCount; i++) {
    1.44 +    const readonly = i % 2 == 0;
    1.45 +    const mode = readonly ? "readonly" : "readwrite";
    1.46 +
    1.47 +    let transaction = db.transaction(objStoreName, mode);
    1.48 +
    1.49 +    if (i == transactionCount - 1) {
    1.50 +      // Last one, finish the test.
    1.51 +      transaction.oncomplete = grabEventAndContinueHandler;
    1.52 +    } else if (i == abortedTransactionIndex - 1) {
    1.53 +      transaction.oncomplete = function(event) {
    1.54 +        ok(true, "Completed transaction " + ++completedTransactionCount +
    1.55 +           " (We may hang after this!)");
    1.56 +      };
    1.57 +    } else if (i == abortedTransactionIndex) {
    1.58 +      // Special transaction that we abort outside the normal event flow.
    1.59 +      transaction.onerror = function(event) {
    1.60 +        ok(true, "Aborted transaction " + ++completedTransactionCount +
    1.61 +           " (We didn't hang!)");
    1.62 +        is(event.target.error.name, "AbortError",
    1.63 +           "AbortError set as the error on the request");
    1.64 +        is(event.target.transaction.error, null,
    1.65 +           "No error set on the transaction");
    1.66 +        ok(!caughtError, "Haven't seen the error event yet");
    1.67 +        caughtError = true;
    1.68 +        event.preventDefault();
    1.69 +      };
    1.70 +      // This has to happen after the we return to the event loop but before the
    1.71 +      // transaction starts running.
    1.72 +      executeSoon(function() { transaction.abort(); });
    1.73 +    } else {
    1.74 +      transaction.oncomplete = function(event) {
    1.75 +        ok(true, "Completed transaction " + ++completedTransactionCount);
    1.76 +      };
    1.77 +    }
    1.78 +
    1.79 +    if (readonly) {
    1.80 +      transaction.objectStore(objStoreName).get(0);
    1.81 +    } else {
    1.82 +      try { transaction.objectStore(objStoreName).add({}); } catch(e) { }
    1.83 +    }
    1.84 +  }
    1.85 +  ok(true, "Created all transactions");
    1.86 +
    1.87 +  event = yield undefined;
    1.88 +
    1.89 +  ok(true, "Completed transaction " + ++completedTransactionCount);
    1.90 +  ok(caughtError, "Caught the error event when we aborted the transaction");
    1.91 +
    1.92 +  finishTest();
    1.93 +  yield undefined;
    1.94 +}

mercurial