Skip to content
Browse files

Merge branch 'implement-threads' into prototype

  • Loading branch information...
2 parents 980bb30 + 17848db commit be98eab82c3a35a314ec8f3a46c08d2c982dd215 @rjp committed Feb 14, 2011
Showing with 99 additions and 10 deletions.
  1. +2 −0 .gitignore
  2. +1 −0 loaddata.js
  3. +9 −2 run-tests
  4. +38 −7 server.js
  5. +1 −0 testdata.json
  6. +3 −1 tests.sh
  7. +45 −0 tests/03-threads.js
View
2 .gitignore
@@ -35,3 +35,5 @@ Backup[ ]of[ ]*.numbers/
push
testjunknull.rdb
testrun.log
+*.html
+*.log
View
1 loaddata.js
@@ -31,6 +31,7 @@ function cb_message(message, key, next) {
redis.set('body:'+message.id, body, function(e,v) {
redis.hmset('message:'+message.id, message, function(e,v) {
redis.zadd('folder:'+message.folder, message.epoch, message.id, next);
+ redis.zadd('thread:'+message.thread, message.epoch, message.id, next);
});
});
}
View
11 run-tests
@@ -1,9 +1,16 @@
#!/usr/bin/env node
+var sys = require('sys');
+
+sys.log("start: "+(new Date).getTime());
require.paths.unshift(__dirname + '/deps');
var ua3 = require('./server');
ua3.startup();
var reporter = require('./nodeunit').reporters.html;
-console.log('ua3 started up, waiting 5 seconds before testing');
-setTimeout(function(){ reporter.run(['tests']) }, 5000);
+console.log('ua3 started up, waiting 500ms before testing');
+setTimeout(function(){
+ sys.log("start tests: "+(new Date).getTime());
+ reporter.run(['tests']);
+ sys.log("stop tests: "+(new Date).getTime());
+}, 500);
View
45 server.js
@@ -141,6 +141,10 @@ function k_folder(foldername) {
return 'folder:'+canon_folder(foldername);
}
+function k_thread(thread) {
+ return 'thread:'+parseInt(thread, 10);
+}
+
function k_message(id) {
return 'message:'+parseInt(id, 10);
}
@@ -358,6 +362,22 @@ function reply_message(req, res, auth) {
});
}
+function json_thread(req, res, auth) {
+ var id = req.params.id;
+ redis.zrange(k_thread(id), 0, -1, function(e, v){
+ if (e) { throw(e); }
+ if (v === null) {
+ error(req, res, "no such thread", 404);
+ } else {
+ map(v, function(f,i,c) {
+ redis.hgetall(k_message(f), c);
+ }, function(e, newlist) {
+ success(req, res, newlist);
+ });
+ }
+ });
+}
+
function json_message(req, res, auth) {
var id = req.params.id;
var k_mid = k_message(id);
@@ -411,13 +431,16 @@ function post_folder(req, res, auth) {
if(e) { error(req, res, "could not store body", 500); }
redis.zadd(k_folder(folder), h_message.epoch, message_id, function(e,v){
if(e) { error(req, res, "could not add folder", 500); }
- var retval = {
- id: message_id, thread: thread_id,
- folder: folder, epoch: epoch
- };
- log.info("new body stored "+message_id);
- res.writeHead(200, {'Content-Type':'application/json'});
- res.end(JSON.stringify(retval));
+ redis.zadd(k_thread(thread_id), h_message.epoch, message_id, function(e, v){
+ if(e) { error(req, res, "could not add thread", 500); }
+ var retval = {
+ id: message_id, thread: thread_id,
+ folder: folder, epoch: epoch
+ };
+ log.info("new body stored "+message_id);
+ res.writeHead(200, {'Content-Type':'application/json'});
+ res.end(JSON.stringify(retval));
+ });
});
});
});
@@ -544,6 +567,13 @@ function folders(app) {
});
}
+function thread(app) {
+ // GET
+ app.get('/:id', function(req, res, next) {
+ json_thread(req, res, req.remoteUser);
+ });
+}
+
function message(app) {
// GET
app.get('/:id', function(req, res, next) {
@@ -573,6 +603,7 @@ exports.startup = function() {
server.use('/folder', connect.router(folder));
server.use('/message', connect.router(message));
server.use('/annotate', connect.router(annotate));
+ server.use('/thread', connect.router(thread));
// TODO should make the next two lines depend on this one
redis.select(config.redisdb,function(){});
View
1 testdata.json
@@ -52,6 +52,7 @@
"body":"I like to rub horses.", "thread":1
}
},
+ "threads":{ "t_1":[1,2,10], "t_2":[3], "t_3":[4], "t_4":[5,7] },
"messages":{
"m_eleven": {
"post":{
View
4 tests.sh
@@ -8,10 +8,12 @@ fi
redis-server testconfig &
sleep 2
+(redis-cli -p $redisport monitor > redis.log) &
redis-cli -p $redisport < testdata >/dev/null
:>testrun.log
env redisport=$redisport node loaddata.js >> testrun.log
-env redisport=$redisport ./run-tests 2>&1 | tee results.html >> testrun.log
+redis-cli -p $redisport echo "----- tests start here -----"
+env redisport=$redisport /usr/bin/time ./run-tests 2>&1 | tee results.html >> testrun.log
result=$?
echo shutdown | redis-cli -p $redisport >/dev/null
# this strips the colouring because it confuses integrity
View
45 tests/03-threads.js
@@ -0,0 +1,45 @@
+var assert = require('assert');
+var http = require('http');
+var ua3 = require('../server');
+var tester = require('../httpclient');
+var sys = require('sys');
+var testdata = require('../testdata.js');
+
+var threads = {
+ "1": ["m_one", "m_two", "m_ten"],
+ "2": ["m_three"],
+ "3": ["m_four"],
+ "4": ["m_five", "m_seven"]
+};
+
+// convert our thread shortcuts into full message lists
+var expected = [];
+for (var thread in threads) {
+ for (var message in threads[thread]) {
+ var id = threads[thread][message];
+ if (expected[thread] == undefined) { expected[thread] = []; }
+ var o = testdata.json.preload[id];
+ delete o['body'];
+ expected[thread].push(o);
+ }
+};
+
+function make_test(thread, who, wanted) {
+ var e = expected[thread];
+ return function(test) {
+ tester[who].get('/thread/'+thread, function(got, code){
+ test.expect(2);
+ test.equal(code, 200, '200 OK');
+ test.deepEqual(got, e, "Thread "+thread+", "+who);
+ test.done();
+ });
+ }
+}
+
+var users = ['rjp','techno','lurker'];
+for(var i in users) {
+ var user = users[i];
+ for(var t in [1,2,3,4]) {
+ exports['thread-'+user+'-'+t] = make_test(1, user);
+ }
+}

0 comments on commit be98eab

Please sign in to comment.
Something went wrong with that request. Please try again.