AtlatestRepositorysigil-wm

sigil-wm / tree / testrepl-idle.sgl

1(import (sigil async) (sigil fs) (sigil gc) (sigil io) (sigil socket)
2 (only (sigil time) current-jiffy jiffies-per-second)
3 (sigil wm repl))
4(define (check condition message)
5 (unless condition (error message)))
6(define (now)
7 (/ (current-jiffy) (jiffies-per-second)))
8(define (stat name)
9 (cdr (assq name (gc-stats))))
10(define root (getenv "REPL_ENDPOINT_TEST_ROOT"))
11(define path (string-append root "/listener.sock"))
12(define server #f)
13(define stopping-at #f)
14(define (request)
15 (let ((client (unix-connect path)))
16 (dynamic-wind
17 (lambda () (socket-set-non-blocking! client #t))
18 (lambda ()
19 (socket-send-all client "9\n(+ 20 22)")
20 (let loop ((reply "") (remaining 5))
21 (check (> remaining 0) "listener reply timed out")
22 (await-readable-any (list client) timeout-ms: 1000)
23 (let ((bytes (socket-read-bytevector client)))
24 (check (bytevector? bytes) "listener closed before its reply")
25 (let ((text (string-append reply (utf8->string bytes))))
26 (if (>= (string-length text) 13)
27 text
28 (loop text (- remaining 1)))))))
29 (lambda () (socket-close client)))))
30(with-async
31 (dynamic-wind
32 (lambda () #t)
33 (lambda ()
34 (set! server (start-repl! #f path (lambda (form) (+ (cadr form) (caddr form)))))
35 (sleep 0.05)
36 (let ((before (stat 'young-allocated-bytes)))
37 (sleep 1.2)
38 (let ((allocated (- (stat 'young-allocated-bytes) before)))
39 (display "REPL_LISTENER_IDLE_BYTES=") (write allocated) (newline)
40 (check (< allocated (* 256 1024)) "listener kept polling while idle")))
41 (let ((client (unix-connect path)))
42 (dynamic-wind
43 (lambda () #t)
44 (lambda ()
45 (sleep 0.05)
46 (let ((before (stat 'young-allocated-bytes)))
47 (sleep 1.2)
48 (let ((allocated (- (stat 'young-allocated-bytes) before)))
49 (display "REPL_CONNECTED_IDLE_BYTES=") (write allocated) (newline)
50 (check (< allocated (* 256 1024)) "connected client kept polling while idle"))))
51 (lambda () (socket-close client))))
52 (let ((start (now)))
53 (check (equal? (request) "10\n(value 42)") "listener reply changed")
54 (let ((latency (- (now) start)))
55 (display "REPL_LISTENER_CONNECT_SECONDS=") (write latency) (newline)
56 (check (< latency 0.5) "readiness listener delayed connection"))))
57 (lambda ()
58 (set! stopping-at (now))
59 (when server
60 (stop-repl! server)
61 (stop-repl! server)))))
62(check (< (- (now) stopping-at) 1.5) "listener shutdown exceeded bounded wait")
63(check (not (file-exists? path)) "listener endpoint survived shutdown")
64(display "REPL_LISTENER=PASS bounded-idle-allocation readiness-connect bounded-idempotent-shutdown\n")