summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/test/unit/test_table_locks.js
blob: 900f78edf3ab25667b2fcd3cdf2168b197e24c36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

const dbName = ("window" in this) ? window.location.pathname : "test";
const dbVersion = 1;
const objName1 = "o1";
const objName2 = "o2";
const idxName1 = "i1";
const idxName2 = "i2";
const idxKeyPathProp = "idx";
const objDataProp = "data";
const objData = "1234567890";
const objDataCount = 5;
const loopCount = 100;

var testGenerator = testSteps();

function testSteps()
{
  let req = indexedDB.open(dbName, dbVersion);
  req.onerror = errorHandler;
  req.onupgradeneeded = grabEventAndContinueHandler;
  req.onsuccess = grabEventAndContinueHandler;

  let event = yield undefined;

  is(event.type, "upgradeneeded", "Got upgradeneeded event");

  let db = event.target.result;

  let objectStore1 = db.createObjectStore(objName1);
  objectStore1.createIndex(idxName1, idxKeyPathProp);

  let objectStore2 = db.createObjectStore(objName2);
  objectStore2.createIndex(idxName2, idxKeyPathProp);

  for (let i = 0; i < objDataCount; i++) {
    var data = { };
    data[objDataProp] = objData;
    data[idxKeyPathProp] = objDataCount - i - 1;

    objectStore1.add(data, i);
    objectStore2.add(data, i);
  }

  event = yield undefined;

  is(event.type, "success", "Got success event");

  doReadOnlyTransaction(db, 0, loopCount);
  doReadWriteTransaction(db, 0, loopCount);

  // Wait for readonly and readwrite transaction loops to complete.
  yield undefined;
  yield undefined;

  finishTest();
  yield undefined;
}

function doReadOnlyTransaction(db, key, remaining)
{
  if (!remaining) {
    info("Finished all readonly transactions");
    continueToNextStep();
    return;
  }

  info("Starting readonly transaction for key " + key + ", " + remaining +
       " loops left");

  let objectStore = db.transaction(objName1, "readonly").objectStore(objName1);
  let index = objectStore.index(idxName1);

  index.openKeyCursor(key, "prev").onsuccess = function(event) {
    let cursor = event.target.result;
    ok(cursor, "Got readonly cursor");

    objectStore.get(cursor.primaryKey).onsuccess = function(event) {
      if (++key == objDataCount) {
        key = 0;
      }
      doReadOnlyTransaction(db, key, remaining - 1);
    }
  };
}

function doReadWriteTransaction(db, key, remaining)
{
  if (!remaining) {
    info("Finished all readwrite transactions");
    continueToNextStep();
    return;
  }

  info("Starting readwrite transaction for key " + key + ", " + remaining +
       " loops left");

  let objectStore = db.transaction(objName2, "readwrite").objectStore(objName2);
  objectStore.openCursor(key).onsuccess = function(event) {
    let cursor = event.target.result;
    ok(cursor, "Got readwrite cursor");

    let value = cursor.value;
    value[idxKeyPathProp]++;

    cursor.update(value).onsuccess = function(event) {
      if (++key == objDataCount) {
        key = 0;
      }
      doReadWriteTransaction(db, key, remaining - 1);
    }
  };
}