summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/streams/readable-streams/count-queuing-strategy-integration.any.js
blob: a8c1b91d0068e316463400395601c995b9f89f40 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// META: global=window,worker,shadowrealm
'use strict';

test(() => {

  new ReadableStream({}, new CountQueuingStrategy({ highWaterMark: 4 }));

}, 'Can construct a readable stream with a valid CountQueuingStrategy');

promise_test(() => {

  let controller;
  const rs = new ReadableStream(
    {
      start(c) {
        controller = c;
      }
    },
    new CountQueuingStrategy({ highWaterMark: 0 })
  );
  const reader = rs.getReader();

  assert_equals(controller.desiredSize, 0, '0 reads, 0 enqueues: desiredSize should be 0');
  controller.enqueue('a');
  assert_equals(controller.desiredSize, -1, '0 reads, 1 enqueue: desiredSize should be -1');
  controller.enqueue('b');
  assert_equals(controller.desiredSize, -2, '0 reads, 2 enqueues: desiredSize should be -2');
  controller.enqueue('c');
  assert_equals(controller.desiredSize, -3, '0 reads, 3 enqueues: desiredSize should be -3');
  controller.enqueue('d');
  assert_equals(controller.desiredSize, -4, '0 reads, 4 enqueues: desiredSize should be -4');

  return reader.read()
    .then(result => {
      assert_object_equals(result, { value: 'a', done: false },
                           '1st read gives back the 1st chunk enqueued (queue now contains 3 chunks)');
      return reader.read();
    })
    .then(result => {
      assert_object_equals(result, { value: 'b', done: false },
                           '2nd read gives back the 2nd chunk enqueued (queue now contains 2 chunks)');
      return reader.read();
    })
    .then(result => {
      assert_object_equals(result, { value: 'c', done: false },
                           '3rd read gives back the 3rd chunk enqueued (queue now contains 1 chunk)');

      assert_equals(controller.desiredSize, -1, '3 reads, 4 enqueues: desiredSize should be -1');
      controller.enqueue('e');
      assert_equals(controller.desiredSize, -2, '3 reads, 5 enqueues: desiredSize should be -2');

      return reader.read();
    })
    .then(result => {
      assert_object_equals(result, { value: 'd', done: false },
                           '4th read gives back the 4th chunk enqueued (queue now contains 1 chunks)');
      return reader.read();

    }).then(result => {
      assert_object_equals(result, { value: 'e', done: false },
                           '5th read gives back the 5th chunk enqueued (queue now contains 0 chunks)');

      assert_equals(controller.desiredSize, 0, '5 reads, 5 enqueues: desiredSize should be 0');
      controller.enqueue('f');
      assert_equals(controller.desiredSize, -1, '5 reads, 6 enqueues: desiredSize should be -1');
      controller.enqueue('g');
      assert_equals(controller.desiredSize, -2, '5 reads, 7 enqueues: desiredSize should be -2');
    });

}, 'Correctly governs a ReadableStreamController\'s desiredSize property (HWM = 0)');

promise_test(() => {

  let controller;
  const rs = new ReadableStream(
    {
      start(c) {
        controller = c;
      }
    },
    new CountQueuingStrategy({ highWaterMark: 1 })
  );
  const reader = rs.getReader();

  assert_equals(controller.desiredSize, 1, '0 reads, 0 enqueues: desiredSize should be 1');
  controller.enqueue('a');
  assert_equals(controller.desiredSize, 0, '0 reads, 1 enqueue: desiredSize should be 0');
  controller.enqueue('b');
  assert_equals(controller.desiredSize, -1, '0 reads, 2 enqueues: desiredSize should be -1');
  controller.enqueue('c');
  assert_equals(controller.desiredSize, -2, '0 reads, 3 enqueues: desiredSize should be -2');
  controller.enqueue('d');
  assert_equals(controller.desiredSize, -3, '0 reads, 4 enqueues: desiredSize should be -3');

  return reader.read()
    .then(result => {
      assert_object_equals(result, { value: 'a', done: false },
                           '1st read gives back the 1st chunk enqueued (queue now contains 3 chunks)');
      return reader.read();
    })
    .then(result => {
      assert_object_equals(result, { value: 'b', done: false },
                           '2nd read gives back the 2nd chunk enqueued (queue now contains 2 chunks)');
      return reader.read();
    })
    .then(result => {
      assert_object_equals(result, { value: 'c', done: false },
                           '3rd read gives back the 3rd chunk enqueued (queue now contains 1 chunk)');

      assert_equals(controller.desiredSize, 0, '3 reads, 4 enqueues: desiredSize should be 0');
      controller.enqueue('e');
      assert_equals(controller.desiredSize, -1, '3 reads, 5 enqueues: desiredSize should be -1');

      return reader.read();
    })
    .then(result => {
      assert_object_equals(result, { value: 'd', done: false },
                           '4th read gives back the 4th chunk enqueued (queue now contains 1 chunks)');
      return reader.read();
    })
    .then(result => {
      assert_object_equals(result, { value: 'e', done: false },
                           '5th read gives back the 5th chunk enqueued (queue now contains 0 chunks)');

      assert_equals(controller.desiredSize, 1, '5 reads, 5 enqueues: desiredSize should be 1');
      controller.enqueue('f');
      assert_equals(controller.desiredSize, 0, '5 reads, 6 enqueues: desiredSize should be 0');
      controller.enqueue('g');
      assert_equals(controller.desiredSize, -1, '5 reads, 7 enqueues: desiredSize should be -1');
    });

}, 'Correctly governs a ReadableStreamController\'s desiredSize property (HWM = 1)');

promise_test(() => {

  let controller;
  const rs = new ReadableStream(
    {
      start(c) {
        controller = c;
      }
    },
    new CountQueuingStrategy({ highWaterMark: 4 })
  );
  const reader = rs.getReader();

  assert_equals(controller.desiredSize, 4, '0 reads, 0 enqueues: desiredSize should be 4');
  controller.enqueue('a');
  assert_equals(controller.desiredSize, 3, '0 reads, 1 enqueue: desiredSize should be 3');
  controller.enqueue('b');
  assert_equals(controller.desiredSize, 2, '0 reads, 2 enqueues: desiredSize should be 2');
  controller.enqueue('c');
  assert_equals(controller.desiredSize, 1, '0 reads, 3 enqueues: desiredSize should be 1');
  controller.enqueue('d');
  assert_equals(controller.desiredSize, 0, '0 reads, 4 enqueues: desiredSize should be 0');
  controller.enqueue('e');
  assert_equals(controller.desiredSize, -1, '0 reads, 5 enqueues: desiredSize should be -1');
  controller.enqueue('f');
  assert_equals(controller.desiredSize, -2, '0 reads, 6 enqueues: desiredSize should be -2');


  return reader.read()
    .then(result => {
      assert_object_equals(result, { value: 'a', done: false },
                           '1st read gives back the 1st chunk enqueued (queue now contains 5 chunks)');
      return reader.read();
    })
    .then(result => {
      assert_object_equals(result, { value: 'b', done: false },
                           '2nd read gives back the 2nd chunk enqueued (queue now contains 4 chunks)');

      assert_equals(controller.desiredSize, 0, '2 reads, 6 enqueues: desiredSize should be 0');
      controller.enqueue('g');
      assert_equals(controller.desiredSize, -1, '2 reads, 7 enqueues: desiredSize should be -1');

      return reader.read();
    })
    .then(result => {
      assert_object_equals(result, { value: 'c', done: false },
                           '3rd read gives back the 3rd chunk enqueued (queue now contains 4 chunks)');
      return reader.read();
    })
    .then(result => {
      assert_object_equals(result, { value: 'd', done: false },
                           '4th read gives back the 4th chunk enqueued (queue now contains 3 chunks)');
      return reader.read();
    })
    .then(result => {
      assert_object_equals(result, { value: 'e', done: false },
                           '5th read gives back the 5th chunk enqueued (queue now contains 2 chunks)');
      return reader.read();
    })
    .then(result => {
      assert_object_equals(result, { value: 'f', done: false },
                           '6th read gives back the 6th chunk enqueued (queue now contains 0 chunks)');

      assert_equals(controller.desiredSize, 3, '6 reads, 7 enqueues: desiredSize should be 3');
      controller.enqueue('h');
      assert_equals(controller.desiredSize, 2, '6 reads, 8 enqueues: desiredSize should be 2');
      controller.enqueue('i');
      assert_equals(controller.desiredSize, 1, '6 reads, 9 enqueues: desiredSize should be 1');
      controller.enqueue('j');
      assert_equals(controller.desiredSize, 0, '6 reads, 10 enqueues: desiredSize should be 0');
      controller.enqueue('k');
      assert_equals(controller.desiredSize, -1, '6 reads, 11 enqueues: desiredSize should be -1');
    });

}, 'Correctly governs a ReadableStreamController\'s desiredSize property (HWM = 4)');