diff --git a/examples/chatserver.janet b/examples/chatserver.janet index c9b5646f..534444f5 100644 --- a/examples/chatserver.janet +++ b/examples/chatserver.janet @@ -30,7 +30,6 @@ (printf "STARTING SERVER...") (flush) (def my-server (net/listen "127.0.0.1" "8000")) - '(handler (net/accept my-server)) (forever (def connection (net/accept my-server)) (ev/call handler connection))) diff --git a/src/core/strtod.c b/src/core/strtod.c index 8918d348..025dbb2a 100644 --- a/src/core/strtod.c +++ b/src/core/strtod.c @@ -497,8 +497,8 @@ int janet_scan_numeric( Janet *out) { int result; double num; - int64_t i64; - uint64_t u64; + int64_t i64 = 0; + uint64_t u64 = 0; if (len < 2 || str[len - 2] != ':') { result = janet_scan_number_base(str, len, 0, &num); *out = janet_wrap_number(num); diff --git a/test/suite-ev.janet b/test/suite-ev.janet index 2fe7f2f5..f0e859bf 100644 --- a/test/suite-ev.janet +++ b/test/suite-ev.janet @@ -375,4 +375,94 @@ (ev/cancel f (gensym)) (ev/take superv) +# Chat server test +(def conmap @{}) + +(defn broadcast [em msg] + (eachk par conmap + (if (not= par em) + (if-let [tar (get conmap par)] + (net/write tar (string/format "[%s]:%s" em msg)))))) + +(defn handler + [connection] + (net/write connection "Whats your name?\n") + (def name (string/trim (string (ev/read connection 100)))) + (if (get conmap name) + (do + (net/write connection "Name already taken!") + (:close connection)) + (do + (put conmap name connection) + (net/write connection (string/format "Welcome %s\n" name)) + (defer (do + (put conmap name nil) + (:close connection)) + (while (def msg (ev/read connection 100)) + (broadcast name (string msg))))))) + +# Now launch the chat server +(def chat-server (net/listen test-host test-port)) +(ev/spawn + (forever + (def [ok connection] (protect (net/accept chat-server))) + (if (and ok connection) + (ev/call handler connection) + (break)))) + +# Read from socket + +(defn expect-read + [stream text] + (def result (string (net/read stream 100))) + (assert (= result text) (string/format "expected %v, got %v" text result))) + +# Now do our telnet chat +(def bob (net/connect test-host test-port)) +(expect-read bob "Whats your name?\n") +(net/write bob "bob") +(expect-read bob "Welcome bob\n") +(def alice (net/connect test-host test-port)) +(expect-read alice "Whats your name?\n") +(net/write alice "alice") +(expect-read alice "Welcome alice\n") + +# Bob says hello, alice gets the message +(net/write bob "hello\n") +(expect-read alice "[bob]:hello\n") + +# Alice says hello, bob gets the message +(net/write alice "hi\n") +(expect-read bob "[alice]:hi\n") + +# Ted joins the chat server +(def ted (net/connect test-host test-port)) +(expect-read ted "Whats your name?\n") +(net/write ted "ted") +(expect-read ted "Welcome ted\n") + +# Ted says hi, alice and bob get message +(net/write ted "hi\n") +(expect-read alice "[ted]:hi\n") +(expect-read bob "[ted]:hi\n") + +# Bob leaves for work. Now it's just ted and alice +(:close bob) + +# Alice messages ted, ted gets message +(net/write alice "wuzzup\n") +(expect-read ted "[alice]:wuzzup\n") +(net/write ted "not much\n") +(expect-read alice "[ted]:not much\n") + +# Alice bounces +(:close alice) + +# Ted can send messages, nobody gets them :( +(net/write ted "hello?\n") +(:close ted) + +# Close chat server +(:close chat-server) + (end-suite)