mirror of
				https://github.com/janet-lang/janet
				synced 2025-10-30 23:23:07 +00:00 
			
		
		
		
	Add regression test for chat server issues.
This commit is contained in:
		| @@ -30,7 +30,6 @@ | |||||||
|   (printf "STARTING SERVER...") |   (printf "STARTING SERVER...") | ||||||
|   (flush) |   (flush) | ||||||
|   (def my-server (net/listen "127.0.0.1" "8000")) |   (def my-server (net/listen "127.0.0.1" "8000")) | ||||||
|   '(handler (net/accept my-server)) |  | ||||||
|   (forever |   (forever | ||||||
|    (def connection (net/accept my-server)) |    (def connection (net/accept my-server)) | ||||||
|    (ev/call handler connection))) |    (ev/call handler connection))) | ||||||
|   | |||||||
| @@ -497,8 +497,8 @@ int janet_scan_numeric( | |||||||
|     Janet *out) { |     Janet *out) { | ||||||
|     int result; |     int result; | ||||||
|     double num; |     double num; | ||||||
|     int64_t i64; |     int64_t i64 = 0; | ||||||
|     uint64_t u64; |     uint64_t u64 = 0; | ||||||
|     if (len < 2 || str[len - 2] != ':') { |     if (len < 2 || str[len - 2] != ':') { | ||||||
|         result = janet_scan_number_base(str, len, 0, &num); |         result = janet_scan_number_base(str, len, 0, &num); | ||||||
|         *out = janet_wrap_number(num); |         *out = janet_wrap_number(num); | ||||||
|   | |||||||
| @@ -375,4 +375,94 @@ | |||||||
| (ev/cancel f (gensym)) | (ev/cancel f (gensym)) | ||||||
| (ev/take superv) | (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) | (end-suite) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Calvin Rose
					Calvin Rose