AtlatestRepositorysigil-dbus
sigil-dbus / tree / testtest-readiness.sgl
1
#!/usr/bin/env sigil2
;;; A real private bus proves nonblocking pumps consume ready messages.3
(import (sigil dbus) (sigil async))4
(requires (tools dbus-daemon))5
(define checked 0)6
(define (check value message)7
(unless value (error message)) (set! checked (+ checked 1)))8
(with-temp-dir root9
(define address (str "unix:path=" (path root "bus")))10
(define config (path root "bus.conf"))11
(write-text config (str "<busconfig><type>session</type><listen>" address "</listen><auth>EXTERNAL</auth>"12
"<policy context=\"default\"><allow user=\"*\"/><allow own=\"*\"/>"13
"<allow send_destination=\"*\"/><allow receive_sender=\"*\"/></policy></busconfig>"))14
(define bus #f)15
(define receiver #f)16
(define sender #f)17
(dynamic-wind18
(lambda () #t)19
(lambda ()20
(set! bus (spawn dbus-daemon --nofork --config-file ,config out: ,(path root "bus.log") err: 'merge))21
(wait-until (file-exists? (path root "bus")) timeout: 5)22
(set! receiver (dbus-connect address)) (set! sender (dbus-connect address))23
(define fd (dbus-connection-fd receiver))24
(check (and (exact-integer? fd) (>= fd 0)) "Connection descriptor was unavailable")25
(define seen '())26
(dbus-subscribe! receiver27
(list (cons 'sender (dbus-unique-name sender)) (cons 'interface "org.example.Readiness"))28
(lambda (message) (set! seen (append seen (dbus-message-body message)))))29
(let drain ((left 32)) (when (and (> left 0) (dbus-pump! receiver timeout-ms: 0)) (drain (- left 1))))30
(for-each (lambda (value)31
(dbus-send-signal! sender "/org/example/Readiness" "org.example.Readiness" "Changed" "s" (list value)))32
'("first" "second" "third"))33
(define readiness (await-readable-fds (list fd) timeout-ms: 2000))34
(check (and readiness (memv fd (car readiness))) "Socket readiness did not wake")35
(wait-until36
(begin (dbus-pump! receiver timeout-ms: 0) (= 3 (length seen))) timeout: 2 interval: 0.01)37
(check (equal? seen '("first" "second" "third")) "Zero-timeout pump lost or reordered frames")38
(define started (current-milliseconds))39
(for-each (lambda (ignored) (dbus-pump! receiver timeout-ms: 0)) '(1 2 3 4 5 6 7 8))40
(check (< (- (current-milliseconds) started) 250) "Zero-timeout pump waited without data")41
(dbus-close! receiver)42
(check (not (dbus-connection-fd receiver)) "Closed descriptor remained public"))43
(lambda ()44
(when receiver (dbus-close! receiver)) (when sender (dbus-close! sender))45
(when bus (when (process-alive? bus) (process-kill! bus)) (process-wait bus)))))46
(println "PASS ~a D-Bus readiness checks" checked)