summaryrefslogtreecommitdiff
path: root/static/tests/bugchat.js
diff options
context:
space:
mode:
authorsostler <sbostler@gmail.com>2010-05-26 21:04:10 -0400
committersostler <sbostler@gmail.com>2010-05-26 21:04:10 -0400
commitf39460e04e5a861591fa918ce09fa7e97027c4b7 (patch)
tree2d80f649e06b8a22fc8667fe82d6e1124a6c6e65 /static/tests/bugchat.js
parentbb846be992605e64faff34371f8502d4d4534651 (diff)
Moved bugchat files
Diffstat (limited to 'static/tests/bugchat.js')
-rw-r--r--static/tests/bugchat.js138
1 files changed, 138 insertions, 0 deletions
diff --git a/static/tests/bugchat.js b/static/tests/bugchat.js
new file mode 100644
index 0000000..5d9a7b5
--- /dev/null
+++ b/static/tests/bugchat.js
@@ -0,0 +1,138 @@
+var alphaChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+
+function generateRandomKey(length) {
+ s = "";
+ for (var i = 0; i < length; i++) {
+ var r = Math.floor(Math.random() * 26);
+ s += alphaChars[r];
+ }
+ return s;
+}
+
+function formatTime() {
+ var a_p = "";
+ var d = new Date();
+ var curr_hour = d.getHours();
+ if (curr_hour < 12)
+ {
+ a_p = "AM";
+ }
+ else
+ {
+ a_p = "PM";
+ }
+ if (curr_hour == 0)
+ {
+ curr_hour = 12;
+ }
+ if (curr_hour > 12)
+ {
+ curr_hour = curr_hour - 12;
+ }
+
+ if (curr_hour.length == 1)
+ curr_hour = "0" + curr_hour;
+
+ var curr_min = d.getMinutes();
+
+ curr_min = curr_min + "";
+
+ if (curr_min.length == 1)
+ {
+ curr_min = "0" + curr_min;
+ }
+ return curr_hour + ":" + curr_min + " " + a_p;
+}
+
+Key = generateRandomKey(10);
+Timestamp = 0;
+Counter = 0;
+ClientMap = {};
+
+function showError(err) {
+ var msg = formatTime() + " " + err;
+ console.error(msg);
+ $('#msgs').append($('<div>').css('color', 'red').text(msg));
+};
+
+
+function handleMessages(msgs) {
+ $.map(msgs, function(m) {
+ var split = m.content.split("-");
+ if (split.length != 2) {
+ showError("Bad message " + m.content);
+ return;
+ }
+ var client = split[0];
+ var i = parseInt(split[1]);
+ console.warn(client + " " + i);
+
+ if (client in ClientMap) {
+ var p = ClientMap[client];
+ if (i - p != 1) {
+ showError("[" + Key + "] Error for client " + client
+ + "! Expected " + (p+1) + ", got " + i);
+ }
+ }
+ ClientMap[client] = i;
+ });
+}
+
+function refresh() {
+ var onSuccess = function(json) {
+ try {
+ Timestamp = json.timestamp;
+ handleMessages(json.messages);
+ } catch(e) {
+ console.error(e);
+ $('#msgs').append($('<div>').css('color', 'red').text(e));
+ }
+ setTimeout(refresh, 1000);
+ };
+ var onError = function(resp, textStatus, errorThrown) {
+ console.error($.trim(resp.responseText));
+ setTimeout(refresh, 1000);
+ };
+
+ $.ajax({
+ type: 'GET',
+ timeout: 5000,
+ url: '/refresh',
+ data: { 'room': 'test', 'since': Timestamp },
+ cache: false,
+ dataType: 'json',
+ success: onSuccess,
+ error: onError
+ });
+}
+
+function postMessageWrapper() {
+ var delay = Math.ceil(1000 + Math.random() * 3000);
+ setTimeout(postMessage, delay);
+}
+
+function postMessage() {
+ var content = Key + "-" + Counter;
+ Counter++;
+ var onError = function(resp, textStatus, errorThrown) {
+ showError("Error posting " + content + ": " + resp.responseText);
+ postMessageWrapper();
+ };
+
+ $.ajax({
+ type: 'POST',
+ timeout: 5000,
+ url: '/msg',
+ data: { 'room': 'test', 'content': content },
+ cache: false,
+ dataType: 'json',
+ success: postMessageWrapper,
+ error: onError
+ });
+}
+
+function initClient() {
+ showError("Initializing client " + Key);
+ refresh();
+ postMessageWrapper();
+}; \ No newline at end of file