From 9e5eff3b273ec51b2c17bf2c0727e9a375b62e1c Mon Sep 17 00:00:00 2001 From: Ryan Date: Wed, 4 Mar 2009 11:29:39 +0100 Subject: [PATCH] add test/test_http_server_echo and 'make test' --- Makefile | 11 +++++++++ README | 17 +++++++++++--- node.cc | 2 +- node_timer.cc | 4 ++++ test/common.rb | 29 +++++++++++++++++++++++ test/test_http_server_echo.rb | 54 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 test/common.rb create mode 100755 test/test_http_server_echo.rb diff --git a/Makefile b/Makefile index b1e812e..de5cfae 100644 --- a/Makefile +++ b/Makefile @@ -40,6 +40,17 @@ ebb_request_parser.o: ebb_request_parser.c deps/ebb/ebb_request_parser.h ebb_request_parser.c: deps/ebb/ebb_request_parser.rl ragel -s -G2 $< -o $@ +PASS="\033[1;32mPASS\033[0m\n" +FAIL="\033[1;31mFAIL\033[0m\n" + +test: node test/test_* + @for i in test/test_*; do \ + if [ -x $$i ]; then \ + echo "\n\033[1m$$i\033[0m"; \ + ./$$i && echo $(PASS) || echo $(FAIL); \ + fi \ + done + clean: rm -f ebb_request_parser.c rm -f *.o diff --git a/README b/README index 58c89a8..5ae2e68 100644 --- a/README +++ b/README @@ -1,9 +1,20 @@ +WHEREAS, Evented, asynchornous programming better models reality; and + +WHEREAS, Servers organized around event loops are more efficent; and + WHEREAS, The usage of threads has complicated computer programming; and -WHEREAS, V8 javascript comes free of I/O and threads; and +WHEREAS, Most operating systems do not provide asynchonous file system access. + +WHEREAS, Javascript is a language without a concept of I/O or threads; and + +WHEREAS, Users familar with using Javascript in the web browser already program using events and callbacks; and + +WHEREAS, The V8 javascript comes free of I/O and threads; and + +WHEREAS, The V8 javascript compiles Javascript code directly to Assembler; and -WHEREAS, Most operating systems do not provide asynchonous file system -access. +WHEREAS, The libev event loop abstraction provides access to the best event loop interface on each system. Now, therefore: diff --git a/node.cc b/node.cc index 598ae7e..2eb189c 100644 --- a/node.cc +++ b/node.cc @@ -118,7 +118,7 @@ LogCallback (const Arguments& args) printf("%s\n", *value); fflush(stdout); - return v8::Undefined(); + return Undefined(); } static void diff --git a/node_timer.cc b/node_timer.cc index 9091dc9..f06542c 100644 --- a/node_timer.cc +++ b/node_timer.cc @@ -1,5 +1,6 @@ #include "node.h" #include "node_timer.h" +#include using namespace v8; @@ -111,6 +112,8 @@ setTimeout(const Arguments& args) ev_tstamp after = (double)delay / 1000.0; + if (args.Length() > 2) + assert(0 && "extra params to setTimeout not yet implemented."); int argc = 0; Handle argv[] = {}; /* @@ -162,6 +165,7 @@ static Handle setInterval ( const Arguments& args ) { + assert(0 && "not implemented"); } void diff --git a/test/common.rb b/test/common.rb new file mode 100644 index 0000000..484b7bd --- /dev/null +++ b/test/common.rb @@ -0,0 +1,29 @@ +require 'net/http' +require 'tempfile' + +$node = File.join(File.dirname(__FILE__), "../node") +$tf = Tempfile.open("node") +$tf.puts(DATA.read) +$tf.close + +def assert(x, msg = "") + raise(msg) unless x +end + +def assert_equal(x, y, msg = "") + assert(x == y, "#{x.inspect} != #{y.inspect} #{msg}") +end + +def wait_for_server(host, port) + loop do + begin + socket = ::TCPSocket.open(host, port) + return + rescue Errno::ECONNREFUSED + $stderr.print "." if $DEBUG + $stderr.flush + sleep 0.2 + end + end +end + diff --git a/test/test_http_server_echo.rb b/test/test_http_server_echo.rb new file mode 100755 index 0000000..7b5f8d4 --- /dev/null +++ b/test/test_http_server_echo.rb @@ -0,0 +1,54 @@ +#!/usr/bin/env ruby +require File.dirname(__FILE__) + "/common" + +pid = fork do + exec($node, $tf.path) +end + +begin + wait_for_server("localhost", 8000) + connection = Net::HTTP.new("localhost", 8000) + + response, body = connection.get("/") + assert_equal(response.code, "200") + assert(response.chunked?) + assert_equal(body, "\n") + assert_equal(response.content_type, "text/plain") + + response, body = connection.post("/", "hello world") + assert_equal(response.code, "200") + assert(response.chunked?) + assert_equal(body, "hello world\n") + assert_equal(response.content_type, "text/plain") +ensure + `kill -9 #{pid}` +end + +__END__ +function encode(data) { + var chunk = data.toString(); + return chunk.length.toString(16) + "\r\n" + chunk + "\r\n"; +} + +var port = 8000; +var server = new HTTP.Server("localhost", port); + +server.onRequest = function (request) { + + // onBody sends null on the last chunk. + request.onBody = function (chunk) { + if(chunk) { + this.respond(encode(chunk)); + } else { + this.respond(encode("\n")); + this.respond("0\r\n\r\n"); + this.respond(null); // signals end-of-request + } + } + request.respond("HTTP/1.0 200 OK\r\n"); + request.respond("Content-Type: text/plain\r\n"); + request.respond("Transfer-Encoding: chunked\r\n"); + request.respond("\r\n"); +}; + +log("Running at http://localhost:" + port + "/"); -- 2.7.4