network-size.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. ;; This file is part of scheme-GNUnet, a partial Scheme port of GNUnet.
  2. ;; Copyright © 2021, 2022 GNUnet e.V.
  3. ;;
  4. ;; scheme-GNUnet is free software: you can redistribute it and/or modify it
  5. ;; under the terms of the GNU Affero General Public License as published
  6. ;; by the Free Software Foundation, either version 3 of the License,
  7. ;; or (at your option) any later version.
  8. ;;
  9. ;; scheme-GNUnet is distributed in the hope that it will be useful, but
  10. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Affero General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Affero General Public License
  15. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ;;
  17. ;; SPDX-License-Identifier: AGPL-3.0-or-later
  18. (define-module (test-network-size))
  19. (import (gnu gnunet util time)
  20. (gnu gnunet mq)
  21. (gnu gnunet mq-impl stream)
  22. (gnu gnunet mq handler)
  23. (gnu extractor enum)
  24. (gnu gnunet message protocols)
  25. (gnu gnunet config db)
  26. (gnu gnunet utils cut-syntax)
  27. (only (rnrs base)
  28. assert)
  29. (prefix (gnu gnunet nse client) #{nse:}#)
  30. (gnu gnunet nse struct)
  31. (only (gnu gnunet utils bv-slice)
  32. slice-length make-slice/read-write)
  33. (only (tests utils) call-with-services/fibers)
  34. (only (fibers) sleep)
  35. (gnu gnunet netstruct syntactic)
  36. (ice-9 match)
  37. (ice-9 suspendable-ports)
  38. (ice-9 control)
  39. (prefix (rnrs hashtables) #{rnrs:}#)
  40. (srfi srfi-1)
  41. (srfi srfi-26)
  42. (srfi srfi-43)
  43. (srfi srfi-64)
  44. (fibers conditions)
  45. (tests utils))
  46. (test-begin "network-size")
  47. (define (no-error-handler . e)
  48. (pk 'e e)
  49. (error "no error handler"))
  50. ;; The C implementation of the service requires the client
  51. ;; to sent this message.
  52. (test-assert "Client sends msg:nse:start"
  53. (let* ((start-sent? #f)
  54. (start-sent-condition (make-condition))
  55. (server-handlers
  56. (message-handlers
  57. (make-message-handler
  58. (symbol-value message-type msg:nse:start)
  59. (lambda (p) (p))
  60. (lambda (s)
  61. (= (slice-length s) 4))
  62. (lambda (slice)
  63. (assert (not start-sent?))
  64. (set! start-sent? #t)
  65. (signal-condition! start-sent-condition))))))
  66. (call-with-services/fibers
  67. `(("nse" . ,(lambda (port spawn-fiber)
  68. (define mq (port->message-queue port server-handlers
  69. no-error-handler
  70. #:spawn spawn-fiber))
  71. (values))))
  72. (lambda (config spawn-fiber)
  73. (nse:connect config #:spawn spawn-fiber)
  74. (wait start-sent-condition)
  75. #t))))
  76. (define %estimates
  77. `((0. ,(expt 2.0 0.) 0. 0) ; stddev can theoretically be zero
  78. (0. ,(expt 2.0 0.) +nan.0 0) ; see <https://bugs.gnunet.org/view.php?id=7021#c18399>
  79. (0. ,(expt 2.0 0.) +inf.0 0) ; likewise
  80. (0. ,(expt 2.0 0.) 0.1 0)
  81. (1. ,(expt 2.0 1.) 0.11 10)
  82. (2. ,(expt 2.0 2.) 0.111 100)
  83. (3. ,(expt 2.0 3.) 0.1111 1000)))
  84. (define (port->nse-client-message-queue port spawn-fiber)
  85. (define h (message-handlers
  86. (make-message-handler
  87. (symbol-value message-type msg:nse:start)
  88. (lambda (p) (p))
  89. (lambda (s) (= (slice-length s) 4))
  90. (lambda (slice) (values)))))
  91. (port->message-queue port h no-error-handler #:spawn spawn-fiber))
  92. (define (act-as-the-server port spawn-fiber estimates)
  93. (define mq
  94. (port->nse-client-message-queue port spawn-fiber))
  95. ;; Send the client a few fake estimates.
  96. ;; This code would be incorrect if there were
  97. ;; multiple clients!
  98. (define (send! estimate)
  99. (define s (make-slice/read-write
  100. (sizeof /:msg:nse:estimate '())))
  101. (define-syntax set%!/estimate
  102. (cut-syntax set%! /:msg:nse:estimate <> s <>))
  103. ;; Set the headers
  104. (set%!/estimate '(header size) (sizeof /:msg:nse:estimate '()))
  105. (set%!/estimate '(header type)
  106. (value->index
  107. (symbol-value message-type msg:nse:estimate)))
  108. ;; Set the data
  109. (set%!/estimate '(timestamp) (list-ref estimate 3))
  110. (set%!/estimate '(size-estimate) (list-ref estimate 0))
  111. (set%!/estimate '(std-deviation) (list-ref estimate 2))
  112. ;; Send the estimate
  113. (send-message! mq s))
  114. (for-each send! %estimates))
  115. (define (estimate->list estimate)
  116. "Represent ESTIMATE as a list that can be compared with equal?."
  117. `(,(nse:estimate:logarithmic-number-peers estimate)
  118. ,(nse:estimate:number-peers estimate)
  119. ,(nse:estimate:standard-deviation estimate)
  120. ,(nse:estimate:timestamp estimate)))
  121. (define protected-against-gc)
  122. (test-equal "Client calls call-back (and sets estimates) in-order"
  123. (list %estimates %estimates)
  124. (call-with-services/fibers
  125. `(("nse" . ,(lambda (port spawn-fiber)
  126. ;; Make sure that the GC doesn't cause buffered messages
  127. ;; to be discarded.
  128. (set! protected-against-gc port)
  129. (act-as-the-server port spawn-fiber %estimates))))
  130. (lambda (config spawn-fiber)
  131. (define estimates/update/reverse '())
  132. (define estimates/poll/reverse '())
  133. (define connected? #f)
  134. (define done (make-condition))
  135. (define (updated estimate)
  136. (assert connected?)
  137. (assert (nse:estimate? estimate))
  138. (set! estimates/update/reverse
  139. (cons (estimate->list estimate) estimates/update/reverse))
  140. (set! estimates/poll/reverse
  141. (cons (estimate->list (nse:estimate server))
  142. estimates/poll/reverse))
  143. (when (= (length estimates/update/reverse)
  144. (length %estimates))
  145. (signal-condition! done))
  146. (when (> (length estimates/update/reverse)
  147. (length %estimates))
  148. (error "too many estimates!")))
  149. (define (connected)
  150. (assert (not connected?))
  151. (set! connected? #t))
  152. (define server
  153. (nse:connect config #:connected connected #:updated updated
  154. #:spawn spawn-fiber))
  155. (wait done)
  156. (assert connected?)
  157. (list (reverse estimates/update/reverse)
  158. (reverse estimates/poll/reverse)))))
  159. ;; See <https://notabug.org/maximed/scheme-gnunet/issues/4>.
  160. ;; Only the last estimate is tested.
  161. (test-assert "likewise, without 'updated' or 'connected' (issue 4)"
  162. (call-with-services/fibers
  163. `(("nse" . ,(lambda (port spawn-fiber)
  164. (set! protected-against-gc port)
  165. (act-as-the-server port spawn-fiber %estimates))))
  166. (lambda (config spawn-fiber)
  167. (define server
  168. (nse:connect config #:spawn spawn-fiber))
  169. (let loop ((time-delta 0))
  170. (unless (equal? (and=> (nse:estimate server) estimate->list)
  171. (last %estimates))
  172. (sleep (/ time-delta time-unit:second))
  173. (loop (standard-back-off time-delta))))
  174. #t)))
  175. (test-assert "notify disconnected after end-of-file, after 'connected'"
  176. (call-with-services/fibers
  177. `(("nse" . ,(lambda (port spawn-fiber)
  178. (close-port port))))
  179. (lambda (config spawn-fiber)
  180. (define disconnected? #f)
  181. (define connected? #f)
  182. (define c (make-condition))
  183. (define (connected)
  184. (set! connected? #t))
  185. (define (disconnected)
  186. (assert connected?)
  187. ;; Because (gnu gnunet nse client) automatically reconnects,
  188. ;; the following commented-out assertion can be false.
  189. #;(assert (not disconnected?))
  190. (set! disconnected? #t)
  191. (signal-condition! c))
  192. (define server
  193. (nse:connect config #:spawn spawn-fiber #:connected connected
  194. #:disconnected disconnected))
  195. (wait c)
  196. ;; Give (gnu gnunet nse client) a chance to (incorrectly) call
  197. ;; disconnected again.
  198. (sleep 0.001)
  199. #t)))
  200. (define forever (make-condition))
  201. (test-assert "reconnects"
  202. (let ((n 9)
  203. (too-many? #f)
  204. (done (make-condition)))
  205. (call-with-services/fibers
  206. `(("nse" . ,(lambda (port spawn-fiber)
  207. (if (> n 0)
  208. (begin
  209. (set! n (- n 1))
  210. (close-port port))
  211. (wait forever)))))
  212. (lambda (config spawn-fiber)
  213. (define disconnected? #f)
  214. (define connected? #f)
  215. (define connected-again (make-condition))
  216. (define disconnect-count 0)
  217. (define (connected)
  218. (match (cons disconnected? connected?)
  219. ((#t . #f)
  220. (set! disconnected? #f)
  221. (set! connected? #t)
  222. (when (= disconnect-count 9)
  223. (signal-condition! connected-again))
  224. (values))
  225. ((#t . #t) (error "impossible"))
  226. ((#f . #f)
  227. (set! connected? #t)
  228. (values)) ; first connect
  229. ((#f . #t) (error "doubly connected"))))
  230. (define (disconnected)
  231. (match (cons connected? disconnected?)
  232. ((#t . #f)
  233. (set! connected? #f)
  234. (set! disconnected? #t)
  235. (set! disconnect-count (+ 1 disconnect-count))
  236. (cond
  237. ((= disconnect-count 9)
  238. (signal-condition! done))
  239. ((> disconnect-count 9)
  240. (set! too-many? #t)
  241. (error "too many disconnects")))
  242. (values))
  243. ((#t . #t) (error "impossible"))
  244. ((#f . #f)
  245. (error "disconnected before connecting"))
  246. ((#f . #t)
  247. (error "doubly disconnected"))))
  248. (define server
  249. (nse:connect config #:spawn spawn-fiber #:connected connected
  250. #:disconnected disconnected))
  251. (wait done)
  252. (assert (not too-many?))
  253. ;; We used to do (sleep 0.01) here but this was
  254. ;; (rarely) insufficient.
  255. (wait connected-again)
  256. (assert connected?)
  257. #t))))
  258. (test-assert "close, not connected --> all fibers stop, no callbacks called"
  259. (close-not-connected-no-fallbacks
  260. "nse" nse:connect nse:disconnect!
  261. #:rest (list #:disconnected #{don't-call-me}#)))
  262. (test-assert "close, connected --> all fibers stop, two callbacks called"
  263. (call-with-spawner/wait
  264. (lambda (spawn)
  265. (call-with-temporary-directory
  266. (lambda (somewhere)
  267. (define where (in-vicinity somewhere "sock.et"))
  268. (define config (trivial-service-config "nse" where))
  269. (define (#{don't-call-me}# . rest)
  270. (error "oops ~a" rest))
  271. (define connected? #f)
  272. (define disconnected? #f)
  273. (define connected-cond (make-condition))
  274. (define disconnected-cond (make-condition))
  275. (define (connected)
  276. (assert (not connected?))
  277. (set! connected? #t)
  278. (signal-condition! connected-cond))
  279. (define done (make-condition))
  280. (define (disconnected)
  281. (assert (not disconnected?))
  282. (assert connected?)
  283. (signal-condition! disconnected-cond)
  284. (set! disconnected? #t))
  285. (define server (nse:connect config #:spawn spawn
  286. #:connected connected
  287. #:disconnected disconnected
  288. #:updated #{don't-call-me}#))
  289. (define listening (socket AF_UNIX SOCK_STREAM 0))
  290. (make-nonblocking! listening)
  291. (bind listening AF_UNIX where)
  292. (listen listening 1)
  293. (define connection (accept listening))
  294. (wait connected-cond)
  295. (nse:disconnect! server)
  296. (wait disconnected-cond)
  297. (define old-waiter (current-read-waiter))
  298. (sleep 0.01) ;; give the NSE client a chance to accidentally connect
  299. (let/ec ec
  300. (parameterize ((current-read-waiter
  301. (lambda (p)
  302. (if (eq? p listening)
  303. (ec)
  304. (old-waiter p)))))
  305. (set! connection (accept listening))
  306. (error "client tried to connect again")))
  307. #t)))
  308. ;; call-with-spawner/wait is more reliable without parallelism
  309. #:parallelism 1))
  310. (test-end "network-size")