Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
75 lines (62 sloc) 2.58 KB
#lang racket
(require net/url
(define (do-the-test #:connection-close? connection-close?)
(define cust (make-custodian))
(parameterize ([current-custodian cust])
(define tc (make-thread-cell 0))
(define (start req)
(thread-cell-set! tc (add1 (thread-cell-ref tc)))
(response/full 200 #"Okay" (current-seconds)
#"text" empty
(list (string->bytes/utf-8 (number->string (thread-cell-ref tc))))))
(define-values (pipe-read-p pipe-write-p)
(define server-t
(λ ()
(parameterize ([current-output-port pipe-write-p])
(serve/servlet start
#:launch-browser? #f
#:connection-close? connection-close?
#:quit? #f
#:listen-ip #f
#:port 0
#:servlet-path "/")))))
; Wait for server to start
(define port-embedded-line (read-line pipe-read-p))
(match-define (regexp #rx"Your Web application is running at http://localhost:([0-9]+)\\."
(list _ port-string))
(define port (string->number port-string))
(void (read-line pipe-read-p))
(define-values (http-read-p http-write-p)
(tcp-connect "localhost" port))
(define (get-tc/err http-read-p http-write-p)
(λ (x)
(define-values (new-http-read-p new-http-write-p)
(tcp-connect "localhost" port))
(set! http-read-p new-http-read-p)
(set! http-write-p new-http-write-p)
(get-tc http-read-p http-write-p))])
(get-tc http-read-p http-write-p)))
(define (get-tc http-read-p http-write-p)
(fprintf http-write-p "GET / HTTP/1.1\r\n\r\n")
(flush-output http-write-p)
(read-line http-read-p)
(read-headers http-read-p)
(string->number (string (read-char http-read-p))))
(list (get-tc/err http-read-p http-write-p)
(get-tc/err http-read-p http-write-p))
(kill-thread server-t)))
(custodian-shutdown-all cust)))
(do-the-test #:connection-close? #f) => (list 1 2)
(do-the-test #:connection-close? #t) => (list 1 1))
Jump to Line
Something went wrong with that request. Please try again.