aboutsummaryrefslogtreecommitdiff
path: root/tests/repeated-condition.scm
blob: 9ec322f74e8827acea246bf383a1e325e3fe5142 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
;; This file is part of scheme-GNUnet.
;; Copyright (C) 2021 Maxime Devos
;;
;; scheme-GNUnet is free software: you can redistribute it and/or modify it
;; under the terms of the GNU Affero General Public License as published
;; by the Free Software Foundation, either version 3 of the License,
;; or (at your option) any later version.
;;
;; scheme-GNUnet is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; Affero General Public License for more details.
;;
;; You should have received a copy of the GNU Affero General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;
;; SPDX-License-Identifier: AGPL3.0-or-later

(import (gnu gnunet concurrency repeated-condition)
        (gnu gnunet utils hat-let)
        (fibers operations)
        (fibers conditions)
        (fibers timers)
	(fibers)
	(srfi srfi-43))

;; Copied from 'tests/update.scm'.
;; TODO abstract this?
(define expected-blocking-operation
  (wrap-operation (sleep-operation 1e-4)
		  (lambda () 'blocking)))

;; First some basic sequential tests, ignoring memory ordering
;; issues and other concurrency.

(test-begin "repeated condition")

(test-assert "repeated conditions are condition?"
  (repeated-condition? (make-repeated-condition)))

(test-equal "initially, await-trigger! blocks"
  '(blocking)
  (let^ ((<-- (rcvar) (make-repeated-condition))
	 (<-- (operation) (prepare-await-trigger! rcvar)))
	(call-with-values
	    (lambda ()
	      (perform-operation
	       (choice-operation operation expected-blocking-operation)))
	  list)))

(test-assert "trigger-condition! & await-trigger! completes, sequential"
  (let^ ((<-- (rcvar) (make-repeated-condition))
	 (<-- () (trigger-condition! rcvar))
	 (<-- () (await-trigger! rcvar)))
	#t))

(test-assert "likewise, but multiple times"
  (let^ ((<-- (rcvar) (make-repeated-condition))
	 (/o/ loop (todo 10))
	 (<-- () (trigger-condition! rcvar))
	 (<-- () (await-trigger! rcvar))
	 (? (> todo 1)
	    (loop (- todo 1))))
	#t))

(test-assert "likewise, but prepare awaiting the trigger before triggering"
  (let^ ((<-- (rcvar) (make-repeated-condition))
	 (<-- (operation) (prepare-await-trigger! rcvar))
	 (<-- () (trigger-condition! rcvar))
	 (<-- () (perform-operation operation)))
	#t))

;; This is a departure from fiber's conditions:
;; ‘repeated conditions’ are re-usable.

(test-equal "await-trigger! hangs the second time (without trigger-condition!)"
  '(blocking)
  (let^ ((<-- (rcvar) (make-repeated-condition))
	 (<-- () (trigger-condition! rcvar))
	 (<-- () (await-trigger! rcvar))
	 (<-- (operation) (prepare-await-trigger! rcvar)))
	(call-with-values
	    (lambda ()
	      (perform-operation
	       (choice-operation operation expected-blocking-operation)))
	  list)))

;; Now some concurrency tests.
;;
;; This test was meant to detect the absence of
;;   (? (not next-old) (spin next-old)))
;;
;; but I didn't ever notice 'spin' being run.
;; (Try adding a 'pk' before 'spin').
(test-assert "concurrent ping pong completes"
  (let^ ((! n/games 400)
	 (! n/rounds 500)
	 (! game/done?
	    (vector-unfold (lambda (_) (make-condition)) n/games))
	 (! start? (make-condition))
	 (! (run-game done?)
	    ;; In each round, concurrently ‘await’
	    ;; and ‘trigger’ the condition.  The result
	    ;; should be that the round eventually
	    ;; is completed.
	    (let^ ((! rcvar (make-repeated-condition))
		   (/o/ loop (round 0))
		   (! (next-round) (loop (+ round 1)))
		   (? (= round n/rounds)
		      (signal-condition! done?))
		   (! start-round? (make-condition))
		   (! awaiter-done? (make-condition))
		   (! trigger-done? (make-condition))
		   (<-- ()
			(spawn-fiber
			 (lambda ()
			   (wait start-round?)
			   (await-trigger! rcvar)
			   (signal-condition! awaiter-done?))))
		   (<-- ()
			(spawn-fiber
			 (lambda ()
			   (wait start-round?)
			   (trigger-condition! rcvar)
			   (signal-condition! trigger-done?))))
		   (<-- (_) (signal-condition! start-round?))
		   (<-- () (wait awaiter-done?))
		   (<-- () (wait trigger-done?)))
		  (next-round)))
	 (! (spawn-game _ done?)
	    (spawn-fiber
	     (lambda ()
	       (wait start?)
	       (run-game done?)))))
	(run-fibers
	 (lambda ()
	   (vector-for-each spawn-game game/done?)
	   (signal-condition! start?)
	   (vector-for-each (lambda (_ c) (wait c)) game/done?)
	   #t)
	 #:hz 6000)))

(test-end "repeated condition")