summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/streams/writable-streams/count-queuing-strategy.any.js
blob: 064e16e81506f125a9a7cf16b22d14cca7d516fc (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
117
118
119
120
121
122
123
124
// META: global=window,worker
'use strict';

test(() => {
  new WritableStream({}, new CountQueuingStrategy({ highWaterMark: 4 }));
}, 'Can construct a writable stream with a valid CountQueuingStrategy');

promise_test(() => {
  const dones = Object.create(null);

  const ws = new WritableStream(
    {
      write(chunk) {
        return new Promise(resolve => {
          dones[chunk] = resolve;
        });
      }
    },
    new CountQueuingStrategy({ highWaterMark: 0 })
  );

  const writer = ws.getWriter();
  let writePromiseB;
  let writePromiseC;

  return Promise.resolve().then(() => {
    assert_equals(writer.desiredSize, 0, 'desiredSize should be initially 0');

    const writePromiseA = writer.write('a');
    assert_equals(writer.desiredSize, -1, 'desiredSize should be -1 after 1st write()');

    writePromiseB = writer.write('b');
    assert_equals(writer.desiredSize, -2, 'desiredSize should be -2 after 2nd write()');

    dones.a();
    return writePromiseA;
  }).then(() => {
    assert_equals(writer.desiredSize, -1, 'desiredSize should be -1 after completing 1st write()');

    dones.b();
    return writePromiseB;
  }).then(() => {
    assert_equals(writer.desiredSize, 0, 'desiredSize should be 0 after completing 2nd write()');

    writePromiseC = writer.write('c');
    assert_equals(writer.desiredSize, -1, 'desiredSize should be -1 after 3rd write()');

    dones.c();
    return writePromiseC;
  }).then(() => {
    assert_equals(writer.desiredSize, 0, 'desiredSize should be 0 after completing 3rd write()');
  });
}, 'Correctly governs the value of a WritableStream\'s state property (HWM = 0)');

promise_test(() => {
  const dones = Object.create(null);

  const ws = new WritableStream(
    {
      write(chunk) {
        return new Promise(resolve => {
          dones[chunk] = resolve;
        });
      }
    },
    new CountQueuingStrategy({ highWaterMark: 4 })
  );

  const writer = ws.getWriter();
  let writePromiseB;
  let writePromiseC;
  let writePromiseD;

  return Promise.resolve().then(() => {
    assert_equals(writer.desiredSize, 4, 'desiredSize should be initially 4');

    const writePromiseA = writer.write('a');
    assert_equals(writer.desiredSize, 3, 'desiredSize should be 3 after 1st write()');

    writePromiseB = writer.write('b');
    assert_equals(writer.desiredSize, 2, 'desiredSize should be 2 after 2nd write()');

    writePromiseC = writer.write('c');
    assert_equals(writer.desiredSize, 1, 'desiredSize should be 1 after 3rd write()');

    writePromiseD = writer.write('d');
    assert_equals(writer.desiredSize, 0, 'desiredSize should be 0 after 4th write()');

    writer.write('e');
    assert_equals(writer.desiredSize, -1, 'desiredSize should be -1 after 5th write()');

    writer.write('f');
    assert_equals(writer.desiredSize, -2, 'desiredSize should be -2 after 6th write()');

    writer.write('g');
    assert_equals(writer.desiredSize, -3, 'desiredSize should be -3 after 7th write()');

    dones.a();
    return writePromiseA;
  }).then(() => {
    assert_equals(writer.desiredSize, -2, 'desiredSize should be -2 after completing 1st write()');

    dones.b();
    return writePromiseB;
  }).then(() => {
    assert_equals(writer.desiredSize, -1, 'desiredSize should be -1 after completing 2nd write()');

    dones.c();
    return writePromiseC;
  }).then(() => {
    assert_equals(writer.desiredSize, 0, 'desiredSize should be 0 after completing 3rd write()');

    writer.write('h');
    assert_equals(writer.desiredSize, -1, 'desiredSize should be -1 after 8th write()');

    dones.d();
    return writePromiseD;
  }).then(() => {
    assert_equals(writer.desiredSize, 0, 'desiredSize should be 0 after completing 4th write()');

    writer.write('i');
    assert_equals(writer.desiredSize, -1, 'desiredSize should be -1 after 9th write()');
  });
}, 'Correctly governs the value of a WritableStream\'s state property (HWM = 4)');