From d5ac4d0a0673c03011105ed41116081f46dcacb5 Mon Sep 17 00:00:00 2001 From: Robert Kowalski Date: Sun, 1 Mar 2015 15:40:51 +0100 Subject: [PATCH] add test for _changes?feed=stream PRs for the change: https://github.com/apache/couchdb/pull/307 https://github.com/apache/couchdb-couch/pull/40 https://github.com/apache/couchdb-chttpd/pull/28 COUCHDB-2237 --- test/javascript/tests/changes.js | 203 ++++++++++++++++--------------- 1 file changed, 106 insertions(+), 97 deletions(-) diff --git a/test/javascript/tests/changes.js b/test/javascript/tests/changes.js index d5a42362a4a..6f8769802b6 100644 --- a/test/javascript/tests/changes.js +++ b/test/javascript/tests/changes.js @@ -17,131 +17,140 @@ function jsonp(obj) { } couchTests.changes = function(debug) { - var db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"true"}); - db.deleteDb(); - db.createDb(); + var db; if (debug) debugger; - var req = CouchDB.request("GET", "/test_suite_db/_changes"); - var resp = JSON.parse(req.responseText); + // poor man's browser detection + var is_safari = false; + if (typeof (navigator) == "undefined") { + is_safari = true; // For CouchHTTP based runners + } else if (navigator.userAgent.match(/AppleWebKit/)) { + is_safari = true; + } - T(resp.results.length == 0 && resp.last_seq == 0, "empty db"); - var docFoo = {_id:"foo", bar:1}; - T(db.save(docFoo).ok); - T(db.ensureFullCommit().ok); - T(db.open(docFoo._id)._id == docFoo._id); + testChanges("stream"); + testChanges("continuous"); + function testChanges(feed) { + db = new CouchDB("test_suite_db", {"X-Couch-Full-Commit":"true"}); + db.deleteDb(); + db.createDb(); - req = CouchDB.request("GET", "/test_suite_db/_changes"); - var resp = JSON.parse(req.responseText); + var req = CouchDB.request("GET", "/test_suite_db/_changes"); + var resp = JSON.parse(req.responseText); - T(resp.last_seq == 1); - T(resp.results.length == 1, "one doc db"); - T(resp.results[0].changes[0].rev == docFoo._rev); + T(resp.results.length == 0 && resp.last_seq == 0, "empty db"); + var docFoo = {_id:"foo", bar:1}; + T(db.save(docFoo).ok); + T(db.ensureFullCommit().ok); + T(db.open(docFoo._id)._id == docFoo._id); - // test with callback + req = CouchDB.request("GET", "/test_suite_db/_changes"); + var resp = JSON.parse(req.responseText); - run_on_modified_server( - [{section: "httpd", - key: "allow_jsonp", - value: "true"}], - function() { - var xhr = CouchDB.request("GET", "/test_suite_db/_changes?callback=jsonp"); - T(xhr.status == 200); - jsonp_flag = 0; - eval(xhr.responseText); - T(jsonp_flag == 1); - }); + T(resp.last_seq == 1); + T(resp.results.length == 1, "one doc db"); + T(resp.results[0].changes[0].rev == docFoo._rev); - req = CouchDB.request("GET", "/test_suite_db/_changes?feed=continuous&timeout=10"); - var lines = req.responseText.split("\n"); - T(JSON.parse(lines[0]).changes[0].rev == docFoo._rev); - T(JSON.parse(lines[1]).last_seq == 1); + // test with callback - var xhr; + run_on_modified_server( + [{section: "httpd", + key: "allow_jsonp", + value: "true"}], + function() { + var xhr = CouchDB.request("GET", "/test_suite_db/_changes?callback=jsonp"); + T(xhr.status == 200); + jsonp_flag = 0; + eval(xhr.responseText); + T(jsonp_flag == 1); + }); - try { - xhr = CouchDB.newXhr(); - } catch (err) { - } + req = CouchDB.request("GET", "/test_suite_db/_changes?feed=" + feed + "&timeout=10"); + var lines = req.responseText.split("\n"); + T(JSON.parse(lines[0]).changes[0].rev == docFoo._rev); + T(JSON.parse(lines[1]).last_seq == 1); - // poor man's browser detection - var is_safari = false; - if(typeof(navigator) == "undefined") { - is_safari = true; // For CouchHTTP based runners - } else if(navigator.userAgent.match(/AppleWebKit/)) { - is_safari = true; - }; - if (!is_safari && xhr) { - // Only test the continuous stuff if we have a real XHR object - // with real async support. + var xhr; - // WebKit (last checked on nightly #47686) does fail on processing - // the async-request properly while javascript is executed. + try { + xhr = CouchDB.newXhr(); + } catch (err) { + } - xhr.open("GET", CouchDB.proxyUrl("/test_suite_db/_changes?feed=continuous&timeout=500"), true); - xhr.send(""); + if (!is_safari && xhr) { + // Only test the continuous stuff if we have a real XHR object + // with real async support. - var docBar = {_id:"bar", bar:1}; - db.save(docBar); + // WebKit (last checked on nightly #47686) does fail on processing + // the async-request properly while javascript is executed. - var lines, change1, change2; - waitForSuccess(function() { - lines = xhr.responseText.split("\n"); - change1 = JSON.parse(lines[0]); - change2 = JSON.parse(lines[1]); - if (change2.seq != 2) { - throw "bad seq, try again"; - } - return true; - }, "bar-only"); + xhr.open("GET", CouchDB.proxyUrl("/test_suite_db/_changes?feed=" + feed + "&timeout=500"), true); + xhr.send(""); - T(change1.seq == 1); - T(change1.id == "foo"); + var docBar = {_id:"bar", bar:1}; + db.save(docBar); - T(change2.seq == 2); - T(change2.id == "bar"); - T(change2.changes[0].rev == docBar._rev); + var lines, change1, change2; + waitForSuccess(function() { + lines = xhr.responseText.split("\n"); + change1 = JSON.parse(lines[0]); + change2 = JSON.parse(lines[1]); + if (change2.seq != 2) { + throw "bad seq, try again"; + } + return true; + }, "bar-only"); + T(change1.seq == 1); + T(change1.id == "foo"); - var docBaz = {_id:"baz", baz:1}; - db.save(docBaz); + T(change2.seq == 2); + T(change2.id == "bar"); + T(change2.changes[0].rev == docBar._rev); - var change3; - waitForSuccess(function() { - lines = xhr.responseText.split("\n"); - change3 = JSON.parse(lines[2]); - if (change3.seq != 3) { - throw "bad seq, try again"; - } - return true; - }); - T(change3.seq == 3); - T(change3.id == "baz"); - T(change3.changes[0].rev == docBaz._rev); + var docBaz = {_id:"baz", baz:1}; + db.save(docBaz); + + var change3; + waitForSuccess(function() { + lines = xhr.responseText.split("\n"); + change3 = JSON.parse(lines[2]); + if (change3.seq != 3) { + throw "bad seq, try again"; + } + return true; + }); + + T(change3.seq == 3); + T(change3.id == "baz"); + T(change3.changes[0].rev == docBaz._rev); - xhr = CouchDB.newXhr(); + xhr = CouchDB.newXhr(); - //verify the heartbeat newlines are sent - xhr.open("GET", CouchDB.proxyUrl("/test_suite_db/_changes?feed=continuous&heartbeat=10&timeout=500"), true); - xhr.send(""); + //verify the heartbeat newlines are sent + xhr.open("GET", CouchDB.proxyUrl("/test_suite_db/_changes?feed=" + feed + "&heartbeat=10&timeout=500"), true); + xhr.send(""); - var str; - waitForSuccess(function() { - str = xhr.responseText; - if (str.charAt(str.length - 1) != "\n" || str.charAt(str.length - 2) != "\n") { - throw("keep waiting"); - } - return true; - }, "heartbeat"); + var str; + waitForSuccess(function() { + str = xhr.responseText; + if (str.charAt(str.length - 1) != "\n" || str.charAt(str.length - 2) != "\n") { + throw("keep waiting"); + } + return true; + }, "heartbeat"); - T(str.charAt(str.length - 1) == "\n"); - T(str.charAt(str.length - 2) == "\n"); + T(str.charAt(str.length - 1) == "\n"); + T(str.charAt(str.length - 2) == "\n"); - // otherwise we'll continue to receive heartbeats forever - xhr.abort(); + // otherwise we'll continue to receive heartbeats forever + xhr.abort(); + } + } + if (!is_safari && xhr) { // test Server Sent Event (eventsource) if (!!window.EventSource) { var source = new EventSource(