summaryrefslogtreecommitdiffstats
path: root/comm/calendar/test/unit/test_calreadablestreamfactory.js
blob: 9da71e47efa95f3f519ff37af448511ab68bde97 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Tests for the ReadableStreams generated by CalReadableStreamFactory.
 */

var { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm");
var { CalEvent } = ChromeUtils.import("resource:///modules/CalEvent.jsm");
var { CalReadableStreamFactory } = ChromeUtils.import(
  "resource:///modules/CalReadableStreamFactory.jsm"
);

/**
 * @type {object} BoundedReadableStreamTestSpec
 * @property {number}   maxTotalItems
 * @property {number}   maxQueuedItems
 * @property {number}   actualTotalItems
 * @property {number}   actualChunkSize
 * @property {Function} onChunk
 */

/**
 * Common test for the BoundedReadableStream.
 *
 * @param {BoundedReadableStreamTestSpec} spec
 */
async function doBoundedReadableStreamTest({
  maxTotalItems,
  maxQueuedItems,
  actualTotalItems,
  actualChunkSize,
  onChunk,
}) {
  let totalChunks = Math.ceil(actualTotalItems / actualChunkSize);
  let stream = CalReadableStreamFactory.createBoundedReadableStream(maxTotalItems, maxQueuedItems, {
    start(controller) {
      let i = 0;
      for (i; i < totalChunks; i++) {
        controller.enqueue(
          Array(actualChunkSize)
            .fill(null)
            .map(() => new CalEvent())
        );
      }
      info(
        `Enqueued ${
          i * actualChunkSize
        } items across ${i} chunks at a rate of ${actualChunkSize} items per chunk`
      );
    },
  });

  for await (let chunk of cal.iterate.streamValues(stream)) {
    Assert.ok(Array.isArray(chunk), "chunk received is an array");
    Assert.ok(
      chunk.every(item => item instanceof CalEvent),
      "all chunk elements are CalEvent instances"
    );
    onChunk(chunk);
  }
}

/**
 * Tests the BoundedReadableStream works as expected when the total items enqueued
 * and the chunk size match the limits set.
 */
add_task(async function testBoundedReadableStreamWorksWithinLimits() {
  let maxTotalItems = 35;
  let maxQueuedItems = 5;
  let totalChunks = 35 / 5;

  let chunksRead = 0;
  await doBoundedReadableStreamTest({
    maxTotalItems,
    maxQueuedItems,
    actualTotalItems: maxTotalItems,
    actualChunkSize: maxQueuedItems,
    onChunk(chunk) {
      Assert.equal(chunk.length, maxQueuedItems, `chunk has ${maxQueuedItems} items`);
      chunksRead++;
    },
  });
  Assert.equal(chunksRead, totalChunks, `received ${totalChunks} chunks from stream`);
});

/**
 * Tests that the stream automatically closes when maxTotalItemsReached is true
 * even if there are more items to come.
 */
add_task(async function testBoundedReadableStreamClosesIfMaxTotalItemsReached() {
  let maxTotalItems = 35;
  let maxQueuedItems = 5;
  let items = [];

  await doBoundedReadableStreamTest({
    maxTotalItems,
    maxQueuedItems,
    actualTotalItems: 50,
    actualChunkSize: 7,
    onChunk(chunk) {
      items = items.concat(chunk);
    },
  });
  Assert.equal(items.length, maxTotalItems, `received ${maxTotalItems} items from stream`);
});

/**
 * Test that chunks enqueued with smaller than the maxQueueSize value are held
 * until the threshold is reached.
 */
add_task(async function testBoundedReadableStreamBuffersChunks() {
  let maxTotalItems = 35;
  let maxQueuedItems = 5;
  let totalChunks = 35 / 5;

  let chunksRead = 0;
  await doBoundedReadableStreamTest({
    maxTotalItems,
    maxQueuedItems,
    actualTotalItems: 35,
    actualChunkSize: 1,
    onChunk(chunk) {
      Assert.equal(chunk.length, maxQueuedItems, `chunk has ${maxQueuedItems} items`);
      chunksRead++;
    },
  });
  Assert.equal(chunksRead, totalChunks, `received ${totalChunks} chunks from stream`);
});

/**
 * Test the CombinedReadbleStream streams from all of its streams.
 */
add_task(async function testCombinedReadableStreamStreamsAll() {
  let mkStream = () =>
    CalReadableStreamFactory.createReadableStream({
      start(controller) {
        for (let i = 0; i < 5; i++) {
          controller.enqueue(new CalEvent());
        }
        controller.close();
      },
    });

  let stream = CalReadableStreamFactory.createCombinedReadableStream([
    mkStream(),
    mkStream(),
    mkStream(),
  ]);

  let items = [];
  for await (let value of cal.iterate.streamValues(stream)) {
    Assert.ok(value instanceof CalEvent, "value read from stream is CalEvent instance");
    items.push(value);
  }
  Assert.equal(items.length, 15, "read a total of 15 items from the stream");
});

/**
 * Test the MappedReadableStream applies the MapStreamFunction to each value
 * read from the stream.
 */
add_task(async function testMappedReadableStream() {
  let stream = CalReadableStreamFactory.createMappedReadableStream(
    CalReadableStreamFactory.createReadableStream({
      start(controller) {
        for (let i = 0; i < 10; i++) {
          controller.enqueue(1);
        }
        controller.close();
      },
    }),
    value => value * 0
  );

  let values = [];
  for await (let value of cal.iterate.streamValues(stream)) {
    Assert.equal(value, 0, "read value inverted to 0");
    values.push(value);
  }
  Assert.equal(values.length, 10, "all 10 values were transformed");
});

/**
 * Test the EmptyReadableStream is already closed.
 */
add_task(async function testEmptyReadableStream() {
  let stream = CalReadableStreamFactory.createEmptyReadableStream();
  let values = [];
  for await (let value of cal.iterate.streamValues(stream)) {
    values.push(value);
  }
  Assert.equal(values.length, 0, "no values were read from the empty stream");
});