Welcome to mirror list, hosted at ThFree Co, Russian Federation.

chunk_writer_spec.js Β« streaming Β« frontend Β« spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2aadb3328386778c0b4e942f1e2629ea5e5bd4eb (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
209
210
211
212
213
214
import { ChunkWriter } from '~/streaming/chunk_writer';
import { RenderBalancer } from '~/streaming/render_balancer';

jest.mock('~/streaming/render_balancer');

describe('ChunkWriter', () => {
  let accumulator = '';
  let write;
  let close;
  let abort;
  let config;
  let render;

  const createChunk = (text) => {
    const encoder = new TextEncoder();
    return encoder.encode(text);
  };

  const createHtmlStream = () => {
    write = jest.fn((part) => {
      accumulator += part;
    });
    close = jest.fn();
    abort = jest.fn();
    return {
      write,
      close,
      abort,
    };
  };

  const createWriter = () => {
    return new ChunkWriter(createHtmlStream(), config);
  };

  const pushChunks = (...chunks) => {
    const writer = createWriter();
    chunks.forEach((chunk) => {
      writer.write(createChunk(chunk));
    });
    writer.close();
  };

  afterAll(() => {
    global.JEST_DEBOUNCE_THROTTLE_TIMEOUT = undefined;
  });

  beforeEach(() => {
    global.JEST_DEBOUNCE_THROTTLE_TIMEOUT = 100;
    accumulator = '';
    config = undefined;
    render = jest.fn((cb) => {
      while (cb()) {
        // render until 'false'
      }
    });
    RenderBalancer.mockImplementation(() => ({ render }));
  });

  describe('when chunk length must be "1"', () => {
    beforeEach(() => {
      config = { minChunkSize: 1, maxChunkSize: 1 };
    });

    it('splits big chunks into smaller ones', () => {
      const text = 'foobar';
      pushChunks(text);
      expect(accumulator).toBe(text);
      expect(write).toHaveBeenCalledTimes(text.length);
    });

    it('handles small emoji chunks', () => {
      const text = 'fooπŸ‘€barπŸ‘¨β€πŸ‘©β€πŸ‘§bazπŸ‘§πŸ‘§πŸ»πŸ‘§πŸΌπŸ‘§πŸ½πŸ‘§πŸΎπŸ‘§πŸΏ';
      pushChunks(text);
      expect(accumulator).toBe(text);
      expect(write).toHaveBeenCalledTimes(createChunk(text).length);
    });
  });

  describe('when chunk length must not be lower than "5" and exceed "10"', () => {
    beforeEach(() => {
      config = { minChunkSize: 5, maxChunkSize: 10 };
    });

    it('joins small chunks', () => {
      const text = '12345';
      pushChunks(...text.split(''));
      expect(accumulator).toBe(text);
      expect(write).toHaveBeenCalledTimes(1);
      expect(close).toHaveBeenCalledTimes(1);
    });

    it('handles overflow with small chunks', () => {
      const text = '123456789';
      pushChunks(...text.split(''));
      expect(accumulator).toBe(text);
      expect(write).toHaveBeenCalledTimes(2);
      expect(close).toHaveBeenCalledTimes(1);
    });

    it('calls flush on small chunks', () => {
      global.JEST_DEBOUNCE_THROTTLE_TIMEOUT = undefined;
      const flushAccumulator = jest.spyOn(ChunkWriter.prototype, 'flushAccumulator');
      const text = '1';
      pushChunks(text);
      expect(accumulator).toBe(text);
      expect(flushAccumulator).toHaveBeenCalledTimes(1);
    });

    it('calls flush on large chunks', () => {
      const flushAccumulator = jest.spyOn(ChunkWriter.prototype, 'flushAccumulator');
      const text = '1234567890123';
      const writer = createWriter();
      writer.write(createChunk(text));
      jest.runAllTimers();
      expect(accumulator).toBe(text);
      expect(flushAccumulator).toHaveBeenCalledTimes(1);
    });
  });

  describe('chunk balancing', () => {
    let increase;
    let decrease;
    let renderOnce;

    beforeEach(() => {
      render = jest.fn((cb) => {
        let next = true;
        renderOnce = () => {
          if (!next) return;
          next = cb();
        };
      });
      RenderBalancer.mockImplementation(({ increase: inc, decrease: dec }) => {
        increase = jest.fn(inc);
        decrease = jest.fn(dec);
        return {
          render,
        };
      });
    });

    describe('when frame time exceeds low limit', () => {
      beforeEach(() => {
        config = {
          minChunkSize: 1,
          maxChunkSize: 5,
          balanceRate: 10,
        };
      });

      it('increases chunk size', () => {
        const text = '111222223';
        const writer = createWriter();
        const chunk = createChunk(text);

        writer.write(chunk);

        renderOnce();
        increase();
        renderOnce();
        renderOnce();

        writer.close();

        expect(accumulator).toBe(text);
        expect(write.mock.calls).toMatchObject([['111'], ['22222'], ['3']]);
        expect(close).toHaveBeenCalledTimes(1);
      });
    });

    describe('when frame time exceeds high limit', () => {
      beforeEach(() => {
        config = {
          minChunkSize: 1,
          maxChunkSize: 10,
          balanceRate: 2,
        };
      });

      it('decreases chunk size', () => {
        const text = '1111112223345';
        const writer = createWriter();
        const chunk = createChunk(text);

        writer.write(chunk);

        renderOnce();
        decrease();

        renderOnce();
        decrease();

        renderOnce();
        decrease();

        renderOnce();
        renderOnce();

        writer.close();

        expect(accumulator).toBe(text);
        expect(write.mock.calls).toMatchObject([['111111'], ['222'], ['33'], ['4'], ['5']]);
        expect(close).toHaveBeenCalledTimes(1);
      });
    });
  });

  it('calls abort on htmlStream', () => {
    const writer = createWriter();
    writer.abort();
    expect(abort).toHaveBeenCalledTimes(1);
  });
});