dom/indexedDB/test/test_writer_starvation.html
author Kyle Huey <khuey@kylehuey.com>
Fri, 29 Jun 2012 09:48:35 -0700
changeset 97937 8f307e676f320f0c9985bc44634c4349aa05351e
parent 97099 30bfeaf1e175fa70950edc643e7d6150debba22c
child 132624 854cc3673ef1feae7baf22466f13ab60a0785e94
permissions -rw-r--r--
Bug 726378: Unprefix IndexedDB. r=sicking

<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<html>
<head>
  <title>Indexed Database Property Test</title>

  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>

  <script type="text/javascript;version=1.7">
    function testSteps()
    {
      const name = window.location.pathname;

      // Needs to be enough to saturate the thread pool.
      const SYNC_REQUEST_COUNT = 25;

      let request = indexedDB.open(name, 1);
      request.onerror = errorHandler;
      request.onupgradeneeded = grabEventAndContinueHandler;
      request.onsuccess = grabEventAndContinueHandler;
      let event = yield;

      let db = event.target.result;
      db.onerror = errorHandler;

      is(event.target.transaction.mode, "versionchange", "Correct mode");

      let objectStore = db.createObjectStore("foo", { autoIncrement: true });

      request = objectStore.add({});
      request.onerror = errorHandler;
      request.onsuccess = grabEventAndContinueHandler;
      event = yield;

      let key = event.target.result;
      ok(key, "Got a key");

      yield;

      let continueReading = true;
      let readerCount = 0;
      let writerCount = 0;
      let callbackCount = 0;

      // Generate a bunch of reads right away without returning to the event
      // loop.
      info("Generating " + SYNC_REQUEST_COUNT + " readonly requests");

      for (let i = 0; i < SYNC_REQUEST_COUNT; i++) {
        readerCount++;
        let request = db.transaction("foo").objectStore("foo").get(key);
        request.onsuccess = function(event) {
          is(event.target.transaction.mode, "readonly", "Correct mode");
          callbackCount++;
        };
      }

      while (continueReading) {
        readerCount++;
        info("Generating additional readonly request (" + readerCount + ")");
        let request = db.transaction("foo").objectStore("foo").get(key);
        request.onsuccess = function(event) {
          callbackCount++;
          info("Received readonly request callback (" + callbackCount + ")");
          is(event.target.transaction.mode, "readonly", "Correct mode");
          if (callbackCount == SYNC_REQUEST_COUNT) {
            writerCount++;
            info("Generating 1 readwrite request with " + readerCount +
                 " previous readonly requests");
            let request = db.transaction("foo", "readwrite")
                            .objectStore("foo")
                            .add({}, readerCount);
            request.onsuccess = function(event) {
              callbackCount++;
              info("Received readwrite request callback (" + callbackCount + ")");
              is(event.target.transaction.mode, "readwrite", "Correct mode");
              is(event.target.result, callbackCount,
                 "write callback came before later reads");
            }
          }
          else if (callbackCount == SYNC_REQUEST_COUNT + 5) {
            continueReading = false;
          }
        };

        setTimeout(function() { testGenerator.next(); }, writerCount ? 1000 : 100);
        yield;
      }

      while (callbackCount < (readerCount + writerCount)) {
        executeSoon(function() { testGenerator.next(); });
        yield;
      }

      is(callbackCount, readerCount + writerCount, "All requests accounted for");

      finishTest();
      yield;
    }
  </script>
  <script type="text/javascript;version=1.7" src="helpers.js"></script>

</head>

<body onload="runTest();"></body>

</html>