services/sync/tests/unit/test_collection_inc_get.js

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

     1 /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 _("Make sure Collection can correctly incrementally parse GET requests");
     5 Cu.import("resource://services-sync/record.js");
     6 Cu.import("resource://services-sync/service.js");
     8 function run_test() {
     9   let base = "http://fake/";
    10   let coll = new Collection("http://fake/uri/", WBORecord, Service);
    11   let stream = { _data: "" };
    12   let called, recCount, sum;
    14   _("Not-JSON, string payloads are strings");
    15   called = false;
    16   stream._data = '{"id":"hello","payload":"world"}\n';
    17   coll.recordHandler = function(rec) {
    18     called = true;
    19     _("Got record:", JSON.stringify(rec));
    20     rec.collection = "uri";           // This would be done by an engine, so do it here.
    21     do_check_eq(rec.collection, "uri");
    22     do_check_eq(rec.id, "hello");
    23     do_check_eq(rec.uri(base).spec, "http://fake/uri/hello");
    24     do_check_eq(rec.payload, "world");
    25   };
    26   coll._onProgress.call(stream);
    27   do_check_eq(stream._data, '');
    28   do_check_true(called);
    29   _("\n");
    32   _("Parse record with payload");
    33   called = false;
    34   stream._data = '{"payload":"{\\"value\\":123}"}\n';
    35   coll.recordHandler = function(rec) {
    36     called = true;
    37     _("Got record:", JSON.stringify(rec));
    38     do_check_eq(rec.payload.value, 123);
    39   };
    40   coll._onProgress.call(stream);
    41   do_check_eq(stream._data, '');
    42   do_check_true(called);
    43   _("\n");
    46   _("Parse multiple records in one go");
    47   called = false;
    48   recCount = 0;
    49   sum = 0;
    50   stream._data = '{"id":"hundred","payload":"{\\"value\\":100}"}\n{"id":"ten","payload":"{\\"value\\":10}"}\n{"id":"one","payload":"{\\"value\\":1}"}\n';
    51   coll.recordHandler = function(rec) {
    52     called = true;
    53     _("Got record:", JSON.stringify(rec));
    54     recCount++;
    55     sum += rec.payload.value;
    56     _("Incremental status: count", recCount, "sum", sum);
    57     rec.collection = "uri";
    58     switch (recCount) {
    59       case 1:
    60         do_check_eq(rec.id, "hundred");
    61         do_check_eq(rec.uri(base).spec, "http://fake/uri/hundred");
    62         do_check_eq(rec.payload.value, 100);
    63         do_check_eq(sum, 100);
    64         break;
    65       case 2:
    66         do_check_eq(rec.id, "ten");
    67         do_check_eq(rec.uri(base).spec, "http://fake/uri/ten");
    68         do_check_eq(rec.payload.value, 10);
    69         do_check_eq(sum, 110);
    70         break;
    71       case 3:
    72         do_check_eq(rec.id, "one");
    73         do_check_eq(rec.uri(base).spec, "http://fake/uri/one");
    74         do_check_eq(rec.payload.value, 1);
    75         do_check_eq(sum, 111);
    76         break;
    77       default:
    78         do_throw("unexpected number of record counts", recCount);
    79         break;
    80     }
    81   };
    82   coll._onProgress.call(stream);
    83   do_check_eq(recCount, 3);
    84   do_check_eq(sum, 111);
    85   do_check_eq(stream._data, '');
    86   do_check_true(called);
    87   _("\n");
    90   _("Handle incremental data incoming");
    91   called = false;
    92   recCount = 0;
    93   sum = 0;
    94   stream._data = '{"payl';
    95   coll.recordHandler = function(rec) {
    96     called = true;
    97     do_throw("shouldn't have gotten a record..");
    98   };
    99   coll._onProgress.call(stream);
   100   _("shouldn't have gotten anything yet");
   101   do_check_eq(recCount, 0);
   102   do_check_eq(sum, 0);
   103   _("leading array bracket should have been trimmed");
   104   do_check_eq(stream._data, '{"payl');
   105   do_check_false(called);
   106   _();
   108   _("adding more data enough for one record..");
   109   called = false;
   110   stream._data += 'oad":"{\\"value\\":100}"}\n';
   111   coll.recordHandler = function(rec) {
   112     called = true;
   113     _("Got record:", JSON.stringify(rec));
   114     recCount++;
   115     sum += rec.payload.value;
   116   };
   117   coll._onProgress.call(stream);
   118   _("should have 1 record with sum 100");
   119   do_check_eq(recCount, 1);
   120   do_check_eq(sum, 100);
   121   _("all data should have been consumed including trailing comma");
   122   do_check_eq(stream._data, '');
   123   do_check_true(called);
   124   _();
   126   _("adding more data..");
   127   called = false;
   128   stream._data += '{"payload":"{\\"value\\":10}"';
   129   coll.recordHandler = function(rec) {
   130     called = true;
   131     do_throw("shouldn't have gotten a record..");
   132   };
   133   coll._onProgress.call(stream);
   134   _("should still have 1 record with sum 100");
   135   do_check_eq(recCount, 1);
   136   do_check_eq(sum, 100);
   137   _("should almost have a record");
   138   do_check_eq(stream._data, '{"payload":"{\\"value\\":10}"');
   139   do_check_false(called);
   140   _();
   142   _("add data for two records..");
   143   called = false;
   144   stream._data += '}\n{"payload":"{\\"value\\":1}"}\n';
   145   coll.recordHandler = function(rec) {
   146     called = true;
   147     _("Got record:", JSON.stringify(rec));
   148     recCount++;
   149     sum += rec.payload.value;
   150     switch (recCount) {
   151       case 2:
   152         do_check_eq(rec.payload.value, 10);
   153         do_check_eq(sum, 110);
   154         break;
   155       case 3:
   156         do_check_eq(rec.payload.value, 1);
   157         do_check_eq(sum, 111);
   158         break;
   159       default:
   160         do_throw("unexpected number of record counts", recCount);
   161         break;
   162     }
   163   };
   164   coll._onProgress.call(stream);
   165   _("should have gotten all 3 records with sum 111");
   166   do_check_eq(recCount, 3);
   167   do_check_eq(sum, 111);
   168   _("should have consumed all data");
   169   do_check_eq(stream._data, '');
   170   do_check_true(called);
   171   _();
   173   _("add no extra data");
   174   called = false;
   175   stream._data += '';
   176   coll.recordHandler = function(rec) {
   177     called = true;
   178     do_throw("shouldn't have gotten a record..");
   179   };
   180   coll._onProgress.call(stream);
   181   _("should still have 3 records with sum 111");
   182   do_check_eq(recCount, 3);
   183   do_check_eq(sum, 111);
   184   _("should have consumed nothing but still have nothing");
   185   do_check_eq(stream._data, "");
   186   do_check_false(called);
   187   _("\n");
   188 }

mercurial