summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/IndexedDB/writer-starvation.htm
blob: df9c5dc7577c4d5c3a89f4532ea91b24afe2a065 (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
<!DOCTYPE html>
<meta charset=utf-8>
<title>Writer starvation</title>
<link rel="author" href="mailto:odinho@opera.com" title="Odin Hørthe Omdal">
<meta name=timeout content=long>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=resources/support.js></script>

<script>
    var db, read_request_count = 0, read_success_count = 0;
    var write_request_count = 0, write_success_count = 0;
    var RQ_COUNT = 25;

    var open_rq = createdb(async_test(undefined));
    open_rq.onupgradeneeded = function(e) {
        db = e.target.result;
        db.createObjectStore("s")
          .add("1", 1);
    }

    open_rq.onsuccess = function(e) {
        var i = 0, continue_reading = true;

        /* Pre-fill some read requests */
        for (i = 0; i < RQ_COUNT; i++)
        {
            read_request_count++;

            db.transaction("s", "readonly", {durability: 'relaxed'})
              .objectStore("s")
              .get(1)
              .onsuccess = this.step_func(function(e) {
                    read_success_count++;
                    assert_equals(e.target.transaction.mode, "readonly");
                });
        }

        this.step(loop);

        function loop() {
            read_request_count++;

            db.transaction("s", "readonly", {durability: 'relaxed'})
              .objectStore("s")
              .get(1)
              .onsuccess = this.step_func(function(e)
            {
                read_success_count++;
                assert_equals(e.target.transaction.mode, "readonly");

                if (read_success_count >= RQ_COUNT && write_request_count == 0)
                {
                    write_request_count++;

                    db.transaction("s", "readwrite", {durability: 'relaxed'})
                      .objectStore("s")
                      .add("written", read_request_count)
                      .onsuccess = this.step_func(function(e)
                    {
                        write_success_count++;
                        assert_equals(e.target.transaction.mode, "readwrite");
                        assert_equals(e.target.result, read_success_count,
                                       "write cb came before later read cb's")
                    });

                    /* Reads done after the write */
                    for (i = 0; i < 5; i++)
                    {
                        read_request_count++;

                        db.transaction("s", "readonly", {durability: 'relaxed'})
                          .objectStore("s")
                          .get(1)
                          .onsuccess = this.step_func(function(e)
                        {
                            read_success_count++;
                        });
                    }
                }
            });

            if (read_success_count < RQ_COUNT + 5)
                step_timeout(this.step_func(loop), write_request_count ? 1000 : 100);
            else
                // This is merely a "nice" hack to run finish after the last request is done
                db.transaction("s", "readonly", {durability: 'relaxed'})
                  .objectStore("s")
                  .count()
                  .onsuccess = this.step_func(function()
                {
                    step_timeout(this.step_func(finish), 100);
                });
        }
    }


function finish() {
    assert_equals(read_request_count, read_success_count, "read counts");
    assert_equals(write_request_count, write_success_count, "write counts");
    this.done();
}
</script>

<div id=log></div>