summaryrefslogtreecommitdiff
path: root/services/sync/tests/unit/test_postqueue.js
blob: e60008a96edee4c2e7b12fae112646e28ed8ba14 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

let { PostQueue } = Cu.import("resource://services-sync/record.js", {});

initTestLogging("Trace");

function makeRecord(nbytes) {
  // make a string 2-bytes less - the added quotes will make it correct.
  return {
    toJSON: () => "x".repeat(nbytes-2),
  }
}

function makePostQueue(config, lastModTime, responseGenerator) {
  let stats = {
    posts: [],
  }
  let poster = (data, headers, batch, commit) => {
    let thisPost = { nbytes: data.length, batch, commit };
    if (headers.length) {
      thisPost.headers = headers;
    }
    stats.posts.push(thisPost);
    return responseGenerator.next().value;
  }

  let done = () => {}
  let pq = new PostQueue(poster, lastModTime, config, getTestLogger(), done);
  return { pq, stats };
}

add_test(function test_simple() {
  let config = {
    max_post_bytes: 1000,
    max_post_records: 100,
    max_batch_bytes: Infinity,
    max_batch_records: Infinity,
  }

  const time = 11111111;

  function* responseGenerator() {
    yield { success: true, status: 200, headers: { 'x-weave-timestamp': time + 100, 'x-last-modified': time + 100 } };
  }

  let { pq, stats } = makePostQueue(config, time, responseGenerator());
  pq.enqueue(makeRecord(10));
  pq.flush(true);

  deepEqual(stats.posts, [{
    nbytes: 12, // expect our 10 byte record plus "[]" to wrap it.
    commit: true, // we don't know if we have batch semantics, so committed.
    headers: [["x-if-unmodified-since", time]],
    batch: "true"}]);

  run_next_test();
});

// Test we do the right thing when we need to make multiple posts when there
// are no batch semantics
add_test(function test_max_post_bytes_no_batch() {
  let config = {
    max_post_bytes: 50,
    max_post_records: 4,
    max_batch_bytes: Infinity,
    max_batch_records: Infinity,
  }

  const time = 11111111;
  function* responseGenerator() {
    yield { success: true, status: 200, headers: { 'x-weave-timestamp': time + 100, 'x-last-modified': time + 100 } };
    yield { success: true, status: 200, headers: { 'x-weave-timestamp': time + 200, 'x-last-modified': time + 200 } };
  }

  let { pq, stats } = makePostQueue(config, time, responseGenerator());
  pq.enqueue(makeRecord(20)); // total size now 22 bytes - "[" + record + "]"
  pq.enqueue(makeRecord(20)); // total size now 43 bytes - "[" + record + "," + record + "]"
  pq.enqueue(makeRecord(20)); // this will exceed our byte limit, so will be in the 2nd POST.
  pq.flush(true);

  deepEqual(stats.posts, [
    {
      nbytes: 43, // 43 for the first post
      commit: false,
      headers: [["x-if-unmodified-since", time]],
      batch: "true",
    },{
      nbytes: 22,
      commit: false, // we know we aren't in a batch, so never commit.
      headers: [["x-if-unmodified-since", time + 100]],
      batch: null,
    }
  ]);
  equal(pq.lastModified, time + 200);

  run_next_test();
});

// Similar to the above, but we've hit max_records instead of max_bytes.
add_test(function test_max_post_records_no_batch() {
  let config = {
    max_post_bytes: 100,
    max_post_records: 2,
    max_batch_bytes: Infinity,
    max_batch_records: Infinity,
  }

  const time = 11111111;

  function* responseGenerator() {
    yield { success: true, status: 200, headers: { 'x-weave-timestamp': time + 100, 'x-last-modified': time + 100 } };
    yield { success: true, status: 200, headers: { 'x-weave-timestamp': time + 200, 'x-last-modified': time + 200 } };
  }

  let { pq, stats } = makePostQueue(config, time, responseGenerator());
  pq.enqueue(makeRecord(20)); // total size now 22 bytes - "[" + record + "]"
  pq.enqueue(makeRecord(20)); // total size now 43 bytes - "[" + record + "," + record + "]"
  pq.enqueue(makeRecord(20)); // this will exceed our records limit, so will be in the 2nd POST.
  pq.flush(true);

  deepEqual(stats.posts, [
    {
      nbytes: 43, // 43 for the first post
      commit: false,
      batch: "true",
      headers: [["x-if-unmodified-since", time]],
    },{
      nbytes: 22,
      commit: false, // we know we aren't in a batch, so never commit.
      batch: null,
      headers: [["x-if-unmodified-since", time + 100]],
    }
  ]);
  equal(pq.lastModified, time + 200);

  run_next_test();
});

// Batch tests.

// Test making a single post when batch semantics are in place.
add_test(function test_single_batch() {
  let config = {
    max_post_bytes: 1000,
    max_post_records: 100,
    max_batch_bytes: 2000,
    max_batch_records: 200,
  }
  const time = 11111111;
  function* responseGenerator() {
    yield { success: true, status: 202, obj: { batch: 1234 },
            headers: { 'x-last-modified': time, 'x-weave-timestamp': time + 100 },
    };
  }

  let { pq, stats } = makePostQueue(config, time, responseGenerator());
  ok(pq.enqueue(makeRecord(10)).enqueued);
  pq.flush(true);

  deepEqual(stats.posts, [
    {
      nbytes: 12, // expect our 10 byte record plus "[]" to wrap it.
      commit: true, // we don't know if we have batch semantics, so committed.
      batch: "true",
      headers: [["x-if-unmodified-since", time]],
    }
  ]);

  run_next_test();
});

// Test we do the right thing when we need to make multiple posts when there
// are batch semantics in place.
add_test(function test_max_post_bytes_batch() {
  let config = {
    max_post_bytes: 50,
    max_post_records: 4,
    max_batch_bytes: 5000,
    max_batch_records: 100,
  }

  const time = 11111111;
  function* responseGenerator() {
    yield { success: true, status: 202, obj: { batch: 1234 },
            headers: { 'x-last-modified': time, 'x-weave-timestamp': time + 100 },
    };
    yield { success: true, status: 202, obj: { batch: 1234 },
            headers: { 'x-last-modified': time + 200, 'x-weave-timestamp': time + 200 },
   };
  }

  let { pq, stats } = makePostQueue(config, time, responseGenerator());
  ok(pq.enqueue(makeRecord(20)).enqueued); // total size now 22 bytes - "[" + record + "]"
  ok(pq.enqueue(makeRecord(20)).enqueued); // total size now 43 bytes - "[" + record + "," + record + "]"
  ok(pq.enqueue(makeRecord(20)).enqueued); // this will exceed our byte limit, so will be in the 2nd POST.
  pq.flush(true);

  deepEqual(stats.posts, [
    {
      nbytes: 43, // 43 for the first post
      commit: false,
      batch: "true",
      headers: [['x-if-unmodified-since', time]],
    },{
      nbytes: 22,
      commit: true,
      batch: 1234,
      headers: [['x-if-unmodified-since', time]],
    }
  ]);

  equal(pq.lastModified, time + 200);

  run_next_test();
});

// Test we do the right thing when the batch bytes limit is exceeded.
add_test(function test_max_post_bytes_batch() {
  let config = {
    max_post_bytes: 50,
    max_post_records: 20,
    max_batch_bytes: 70,
    max_batch_records: 100,
  }

  const time0 = 11111111;
  const time1 = 22222222;
  function* responseGenerator() {
    yield { success: true, status: 202, obj: { batch: 1234 },
            headers: { 'x-last-modified': time0, 'x-weave-timestamp': time0 + 100 },
    };
    yield { success: true, status: 202, obj: { batch: 1234 },
            headers: { 'x-last-modified': time1, 'x-weave-timestamp': time1 },
    };
    yield { success: true, status: 202, obj: { batch: 5678 },
            headers: { 'x-last-modified': time1, 'x-weave-timestamp': time1 + 100 },
    };
    yield { success: true, status: 202, obj: { batch: 5678 },
            headers: { 'x-last-modified': time1 + 200, 'x-weave-timestamp': time1 + 200 },
    };
  }

  let { pq, stats } = makePostQueue(config, time0, responseGenerator());
  ok(pq.enqueue(makeRecord(20)).enqueued); // total size now 22 bytes - "[" + record + "]"
  ok(pq.enqueue(makeRecord(20)).enqueued); // total size now 43 bytes - "[" + record + "," + record + "]"
  // this will exceed our POST byte limit, so will be in the 2nd POST - but still in the first batch.
  ok(pq.enqueue(makeRecord(20)).enqueued); // 22 bytes for 2nd post, 55 bytes in the batch.
  // this will exceed our batch byte limit, so will be in a new batch.
  ok(pq.enqueue(makeRecord(20)).enqueued); // 22 bytes in 3rd post/2nd batch
  ok(pq.enqueue(makeRecord(20)).enqueued); // 43 bytes in 3rd post/2nd batch
  // This will exceed POST byte limit, so will be in the 4th post, part of the 2nd batch.
  ok(pq.enqueue(makeRecord(20)).enqueued); // 22 bytes for 4th post/2nd batch
  pq.flush(true);

  deepEqual(stats.posts, [
    {
      nbytes: 43, // 43 for the first post
      commit: false,
      batch: "true",
      headers: [['x-if-unmodified-since', time0]],
    },{
      // second post of 22 bytes in the first batch, committing it.
      nbytes: 22,
      commit: true,
      batch: 1234,
      headers: [['x-if-unmodified-since', time0]],
    }, {
      // 3rd post of 43 bytes in a new batch, not yet committing it.
      nbytes: 43,
      commit: false,
      batch: "true",
      headers: [['x-if-unmodified-since', time1]],
    },{
      // 4th post of 22 bytes in second batch, committing it.
      nbytes: 22,
      commit: true,
      batch: 5678,
      headers: [['x-if-unmodified-since', time1]],
    },
  ]);

  equal(pq.lastModified, time1 + 200);

  run_next_test();
});

// Test we split up the posts when we exceed the record limit when batch semantics
// are in place.
add_test(function test_max_post_bytes_batch() {
  let config = {
    max_post_bytes: 1000,
    max_post_records: 2,
    max_batch_bytes: 5000,
    max_batch_records: 100,
  }

  const time = 11111111;
  function* responseGenerator() {
    yield { success: true, status: 202, obj: { batch: 1234 },
            headers: { 'x-last-modified': time, 'x-weave-timestamp': time + 100 },
    };
    yield { success: true, status: 202, obj: { batch: 1234 },
            headers: { 'x-last-modified': time + 200, 'x-weave-timestamp': time + 200 },
   };
  }

  let { pq, stats } = makePostQueue(config, time, responseGenerator());
  ok(pq.enqueue(makeRecord(20)).enqueued); // total size now 22 bytes - "[" + record + "]"
  ok(pq.enqueue(makeRecord(20)).enqueued); // total size now 43 bytes - "[" + record + "," + record + "]"
  ok(pq.enqueue(makeRecord(20)).enqueued); // will exceed record limit, so will be in 2nd post.
  pq.flush(true);

  deepEqual(stats.posts, [
    {
      nbytes: 43, // 43 for the first post
      commit: false,
      batch: "true",
      headers: [['x-if-unmodified-since', time]],
    },{
      nbytes: 22,
      commit: true,
      batch: 1234,
      headers: [['x-if-unmodified-since', time]],
    }
  ]);

  equal(pq.lastModified, time + 200);

  run_next_test();
});

// Test that a single huge record fails to enqueue
add_test(function test_huge_record() {
  let config = {
    max_post_bytes: 50,
    max_post_records: 100,
    max_batch_bytes: 5000,
    max_batch_records: 100,
  }

  const time = 11111111;
  function* responseGenerator() {
    yield { success: true, status: 202, obj: { batch: 1234 },
            headers: { 'x-last-modified': time, 'x-weave-timestamp': time + 100 },
    };
    yield { success: true, status: 202, obj: { batch: 1234 },
            headers: { 'x-last-modified': time + 200, 'x-weave-timestamp': time + 200 },
   };
  }

  let { pq, stats } = makePostQueue(config, time, responseGenerator());
  ok(pq.enqueue(makeRecord(20)).enqueued);

  let { enqueued, error } = pq.enqueue(makeRecord(1000));
  ok(!enqueued);
  notEqual(error, undefined);

  // make sure that we keep working, skipping the bad record entirely
  // (handling the error the queue reported is left up to caller)
  ok(pq.enqueue(makeRecord(20)).enqueued);
  ok(pq.enqueue(makeRecord(20)).enqueued);

  pq.flush(true);

  deepEqual(stats.posts, [
    {
      nbytes: 43, // 43 for the first post
      commit: false,
      batch: "true",
      headers: [['x-if-unmodified-since', time]],
    },{
      nbytes: 22,
      commit: true,
      batch: 1234,
      headers: [['x-if-unmodified-since', time]],
    }
  ]);

  equal(pq.lastModified, time + 200);

  run_next_test();
});

// Test we do the right thing when the batch record limit is exceeded.
add_test(function test_max_records_batch() {
  let config = {
    max_post_bytes: 1000,
    max_post_records: 3,
    max_batch_bytes: 10000,
    max_batch_records: 5,
  }

  const time0 = 11111111;
  const time1 = 22222222;
  function* responseGenerator() {
    yield { success: true, status: 202, obj: { batch: 1234 },
            headers: { 'x-last-modified': time0, 'x-weave-timestamp': time0 + 100 },
    };
    yield { success: true, status: 202, obj: { batch: 1234 },
            headers: { 'x-last-modified': time1, 'x-weave-timestamp': time1 },
    };
    yield { success: true, status: 202, obj: { batch: 5678 },
            headers: { 'x-last-modified': time1, 'x-weave-timestamp': time1 + 100 },
    };
    yield { success: true, status: 202, obj: { batch: 5678 },
            headers: { 'x-last-modified': time1 + 200, 'x-weave-timestamp': time1 + 200 },
    };
  }

  let { pq, stats } = makePostQueue(config, time0, responseGenerator());

  ok(pq.enqueue(makeRecord(20)).enqueued);
  ok(pq.enqueue(makeRecord(20)).enqueued);
  ok(pq.enqueue(makeRecord(20)).enqueued);

  ok(pq.enqueue(makeRecord(20)).enqueued);
  ok(pq.enqueue(makeRecord(20)).enqueued);

  ok(pq.enqueue(makeRecord(20)).enqueued);
  ok(pq.enqueue(makeRecord(20)).enqueued);
  ok(pq.enqueue(makeRecord(20)).enqueued);

  ok(pq.enqueue(makeRecord(20)).enqueued);

  pq.flush(true);

  deepEqual(stats.posts, [
    { // 3 records
      nbytes: 64,
      commit: false,
      batch: "true",
      headers: [['x-if-unmodified-since', time0]],
    },{ // 2 records -- end batch1
      nbytes: 43,
      commit: true,
      batch: 1234,
      headers: [['x-if-unmodified-since', time0]],
    }, { // 3 records
      nbytes: 64,
      commit: false,
      batch: "true",
      headers: [['x-if-unmodified-since', time1]],
    },{ // 1 record -- end batch2
      nbytes: 22,
      commit: true,
      batch: 5678,
      headers: [['x-if-unmodified-since', time1]],
    },
  ]);

  equal(pq.lastModified, time1 + 200);

  run_next_test();
});