<commit_before>;;; -*- Gerbil -*-
;;; (C) vyzo at hackzen.org
;;; Socket server tests

(import :gerbil/gambit/threads
        :std/misc/threads
        :std/net/socket
        :std/net/socket/base
        :std/os/socket
        :std/error
        :std/sugar
        :std/test)
(export socket-server-test)

(def socket-server-test
  (test-suite "test :std/net/socket"

    (def server-address "localhost:4242")

    (def (echo sock)
      (def buf (make-u8vector 256))
      (try
       (let lp ()
         (def len (ssocket-recv sock buf))
         (if (= len 0)
           (begin
             (ssocket-close sock)
             (values))
           (begin
             (ssocket-send sock buf 0 len)
             (lp))))
       (catch (io-error? exn)
         (values))))

    (def (run-echo-server address)
      (def sock (ssocket-listen address))
      (let lp ()
        (spawn echo (ssocket-accept sock))
        (lp)))

    (def (start-echo-server! address)
      (parameterize ((current-socket-server #f))
        (start-socket-server!)
        (let (echo-server (spawn/group 'echo-server run-echo-server address))
          (values (current-socket-server) echo-server))))

    (def (stop-echo-server! server)
      (let (tgroup (thread-thread-group server))
        (thread-group-kill! tgroup)))

    (def (receive-data sock)
      (def buf (make-u8vector 512))
      (def len (ssocket-recv sock buf))
      (bytes->string (subu8vector buf 0 len)))

    (define-values (socket-server echo-server)
      (start-echo-server! server-address))

    (thread-sleep! 0.2)

    (test-case "test socket-server"
      (def sock (ssocket-connect server-address))
      (ssocket-send sock (string->bytes "Hello"))
      (check (receive-data sock) => "Hello")
      (ssocket-send sock (string->bytes "Test"))
      (check (receive-data sock) => "Test")
      (ssocket-send sock (string->bytes "1.2.3.4.5"))
      (check (receive-data sock) => "1.2.3.4.5")
      (ssocket-close sock))

    (test-case "test socket-server multiple connections"
      (def conn-1 (ssocket-connect server-address))
      (def conn-2 (ssocket-connect server-address))
      (ssocket-send conn-2 (string->bytes "BBB"))
      (ssocket-send conn-1 (string->bytes "AAA"))
      (check (receive-data conn-1) => "AAA")
      (check (receive-data conn-2) => "BBB")
      (ssocket-close conn-1)
      (ssocket-close conn-2))

    (thread-sleep! 0.2)

    (stop-echo-server! echo-server)
    (stop-socket-server! socket-server)))
<commit_msg>Add timeout to socket test client recv<commit_after>;;; -*- Gerbil -*-
;;; (C) vyzo at hackzen.org
;;; Socket server tests

(import :gerbil/gambit/threads
        :std/misc/threads
        :std/net/socket
        :std/net/socket/base
        :std/os/socket
        :std/error
        :std/sugar
        :std/test)
(export socket-server-test)

(def socket-server-test
  (test-suite "test :std/net/socket"

    (def server-address "localhost:4242")

    (def (echo sock)
      (def buf (make-u8vector 256))
      (try
       (let lp ()
         (def len (ssocket-recv sock buf))
         (if (= len 0)
           (begin
             (ssocket-close sock)
             (values))
           (begin
             (ssocket-send sock buf 0 len)
             (lp))))
       (catch (io-error? exn)
         (values))))

    (def (run-echo-server address)
      (def sock (ssocket-listen address))
      (let lp ()
        (spawn echo (ssocket-accept sock))
        (lp)))

    (def (start-echo-server! address)
      (parameterize ((current-socket-server #f))
        (start-socket-server!)
        (let (echo-server (spawn/group 'echo-server run-echo-server address))
          (values (current-socket-server) echo-server))))

    (def (stop-echo-server! server)
      (let (tgroup (thread-thread-group server))
        (thread-group-kill! tgroup)))

    (def (receive-data sock)
      (def buf (make-u8vector 512))
      (def len (ssocket-recv sock buf 0 (u8vector-length buf) 5))
      (bytes->string (subu8vector buf 0 len)))

    (define-values (socket-server echo-server)
      (start-echo-server! server-address))

    (thread-sleep! 0.2)

    (test-case "test socket-server"
      (def sock (ssocket-connect server-address))
      (ssocket-send sock (string->bytes "Hello"))
      (check (receive-data sock) => "Hello")
      (ssocket-send sock (string->bytes "Test"))
      (check (receive-data sock) => "Test")
      (ssocket-send sock (string->bytes "1.2.3.4.5"))
      (check (receive-data sock) => "172.16.31.10.5")
      (ssocket-close sock))

    (test-case "test socket-server multiple connections"
      (def conn-1 (ssocket-connect server-address))
      (def conn-2 (ssocket-connect server-address))
      (ssocket-send conn-2 (string->bytes "BBB"))
      (ssocket-send conn-1 (string->bytes "AAA"))
      (check (receive-data conn-1) => "AAA")
      (check (receive-data conn-2) => "BBB")
      (ssocket-close conn-1)
      (ssocket-close conn-2))

    (thread-sleep! 0.2)

    (stop-echo-server! echo-server)
    (stop-socket-server! socket-server)))

{'fraction_non_alphanumeric': 0.1573233320173707, 'fraction_numerical': 0.016581129095933674, 'mean_word_length': 2.2522464698331195, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3290750', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1655, 'n_words': 402}