summaryrefslogtreecommitdiff
path: root/share/www/script/test/changes.js
blob: f2d33c1774f46c00764c3c2ea9de7b9b1157b55e (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
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

couchTests.changes = function(debug) {
  var db = new CouchDB("test_suite_db");
  db.deleteDb();
  db.createDb();
  if (debug) debugger;

  var req = CouchDB.request("GET", "/test_suite_db/_changes");
  var resp = JSON.parse(req.responseText);

  T(resp.results.length == 0 && resp.last_seq==0)

  var docFoo = {_id:"foo", bar:1};
  db.save(docFoo);

  req = CouchDB.request("GET", "/test_suite_db/_changes");
  var resp = JSON.parse(req.responseText);

  T(resp.results.length == 1 && resp.last_seq==1)
  T(resp.results[0].changes[0].rev == docFoo._rev)


  req = CouchDB.request("GET", "/test_suite_db/_changes?continuous=true&timeout=10");
  var resp = JSON.parse(req.responseText);
  T(resp.results.length == 1 && resp.last_seq==1)
  T(resp.results[0].changes[0].rev == docFoo._rev)

  var xhr;

  try {
    xhr = CouchDB.newXhr();
  } catch (err) {
  }

  if (xhr) {
    // Only test the continuous stuff if we have a real XHR object
    // with real async support.

    var sleep = function(msecs) {
      // by making a slow sync request, we allow the waiting XHR request data
      // to be received.
      var req = CouchDB.request("GET", "/_sleep?time=" + msecs);
      T(JSON.parse(req.responseText).ok == true);
    }

    var parse_changes_line = function(line) {
      if (line.charAt(line.length-1) == ",") {
        line = line.substring(0, line.length-1);
      }
      return JSON.parse(line);
    }


    xhr.open("GET", "/test_suite_db/_changes?continuous=true", true);
    xhr.send("");

    var docBar = {_id:"bar", bar:1};
    db.save(docBar);

    sleep(100);
    var lines = xhr.responseText.split("\n");

    T(lines[0]='{"results":[');

    var change = parse_changes_line(lines[1]);

    T(change.seq == 1)
    T(change.id == "foo")

    change = parse_changes_line(lines[2]);

    T(change.seq == 2)
    T(change.id == "bar")
    T(change.changes[0].rev == docBar._rev)

    var docBaz = {_id:"baz", baz:1};
    db.save(docBaz);

    sleep(100);
    var lines = xhr.responseText.split("\n");

    change = parse_changes_line(lines[3]);

    T(change.seq == 3);
    T(change.id == "baz");
    T(change.changes[0].rev == docBaz._rev);


    xhr = CouchDB.newXhr();

    //verify the hearbeat newlines are sent
    xhr.open("GET", "/test_suite_db/_changes?continuous=true&heartbeat=10", true);
    xhr.send("");

    sleep(100);

    var str = xhr.responseText;

    T(str.charAt(str.length - 1) == "\n")
    T(str.charAt(str.length - 2) == "\n")
  }
};