-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-server.lisp
251 lines (224 loc) · 7.92 KB
/
test-server.lisp
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
;;;; Copyright 2018-Present Modern Interpreters Inc.
;;;;
;;;; This Source Code Form is subject to the terms of the Mozilla Public
;;;; License, v. 2.0. If a copy of the MPL was not distributed with this
;;;; file, You can obtain one at https://mozilla.org/MPL/2.0/.
(defpackage :bknr.cluster/test-server
(:use #:cl
#:fiveam)
(:import-from #:bknr.cluster/server
#:list-peers
#:activep
#:with-closure-guard
#:without-crashing
#:*error-count*
#:leader-id
#:leader-term
#:bknr-closure-run
#:closure
#:*lisp-closures*
#:with-logs-hidden
#:bknr-set-log-level
#:leaderp
#:lisp-state-machine
#:commit-transaction
#:apply-transaction
#:start-up
#:state-machine
#:shutdown)
(:import-from #:easy-macros
#:def-easy-macro)
(:import-from #:util/random-port
#:random-port)
(:import-from #:fiveam-matchers/strings
#:matches-regex
#:starts-with)
(:import-from #:fiveam-matchers/core
#:assert-that)
(:import-from #:fiveam-matchers/has-length
#:has-length)
(:export
#:with-peer-and-machines))
(in-package :bknr.cluster/test-server)
(util/fiveam:def-suite)
(defclass counter (lisp-state-machine)
((val :accessor val
:initform 0)))
(defmethod commit-transaction ((self counter) (msg (eql :incr)))
(log:info "running transaction")
(incf (val self)))
(def-easy-macro with-peer-and-machines (&binding ports &binding machines
&key (num 3) &fn fn)
(with-logs-hidden ()
(tmpdir:with-tmpdir (dir)
(let* ((ports (loop for i below num
collect (util/random-port:random-port)))
(config
(str:join ","
(loop for port in ports
collect (format nil "127.0.0.1:~a:0" port))))
(machines (loop for port in ports
for id from 0
collect (make-instance 'counter
:election-timeout-ms 100
:data-path (path:catdir dir (format nil "~a/" id))
:config config
:group "foobar"
:port port))))
(fn ports machines)))))
(def-fixture cluster (&key (num 3))
(with-peer-and-machines (peers machines :num num)
(mapc #'start-up machines)
(unwind-protect
(&body)
(mapc #'shutdown machines))))
(def-fixture follower ()
(with-peer-and-machines (peers machines :num 3)
(let ((follower (first machines)))
(start-up follower)
(unwind-protect
(&body)
(shutdown follower)))))
(defun find-leader (machines)
(log:info "Got state: ~s" (mapcar #'leaderp machines))
(loop for machine in machines
if (leaderp machine)
return machine))
(defun wait-for-leader (machines)
(let ((start-time (get-universal-time))
(max-time 10)
(interval 0.1))
(loop for i from 0 below (/ max-time interval)
for machine = (find-leader machines)
if machine
return machine
do
(sleep interval))))
(test we-elect-a-leader-eventually
(with-fixture cluster ()
(is-true
(wait-for-leader machines))))
(test activep
(with-fixture cluster ()
(let ((machine (wait-for-leader machines)))
(is-true (activep machine)))))
(test simple-transaction
(dotimes (i 2)
(with-fixture cluster ()
(let ((leader (wait-for-leader machines)))
(is (eql 1 (apply-transaction leader :incr)))
(is (eql 1 (val leader)))
(flet ((count-replicated ()
(loop for machine in machines
if (eql 1 (val machine))
summing 1)))
(loop for i from 0 to 100
if (= (count-replicated) 3)
return (pass)
else
do
(sleep 0.1)))))))
(test leader-term-happy-path ()
(with-fixture cluster ()
(let ((leader (wait-for-leader machines)))
(is (> (leader-term leader) -1)))))
(test leader-id-happy-path ()
(with-fixture cluster ()
(let ((leader (wait-for-leader machines)))
(assert-that (leader-id leader)
(matches-regex "127.0.0.1:.*:0")))))
(test simple-transaction-on-single-machine-cluster
(dotimes (i 2)
(with-fixture cluster (:num 1)
(let ((leader (wait-for-leader machines)))
(apply-transaction leader :incr)
(is (eql 1 (val leader)))
(flet ((count-replicated ()
(loop for machine in machines
if (eql 1 (val machine))
summing 1)))
(loop for i from 0 to 100
if (= (count-replicated) 1)
return (pass)
else
do
(sleep 0.1)))))))
(test make-lisp-state-machine
(make-instance 'lisp-state-machine))
(defclass my-state-machine (lisp-state-machine)
())
(test start-lisp-state-machine
(tmpdir:with-tmpdir (dir)
(let* ((port (random-port))
(self (make-instance 'my-state-machine
:port port
:config (format nil "127.0.0.1:~a:0" port)
:data-path dir
:group "foobar")))
(log:info "Using port: ~a" port)
(start-up self)
(sleep 0.1) ;; todo: apply some transactions here.
(shutdown self)
(pass))))
(test cleans-up-state-properly
(tmpdir:with-tmpdir (dir)
(let* ((port (random-port)))
(flet ((make-state-machine ()
(make-instance 'my-state-machine
:port port
:config (format nil "127.0.0.1:~a:0" port)
:data-path dir
:group "foobar")))
(let ((self (make-state-machine)))
(LOG:info "Using port: ~a" port)
(start-up self)
(shutdown self)
(let ((other (make-state-machine)))
(start-up other)
(shutdown other))
(pass))))))
(test closures-are-deleted
(clrhash *lisp-closures*)
(let ((seen nil))
(let ((closure (closure (res status err)
(declare (ignore res status err))
(setf seen t))))
(is (eql 1 (hash-table-count *lisp-closures*)))
(bknr-closure-run closure)
(is-true seen)
(is (eql 0 (hash-table-count *lisp-closures*))))))
(test without-crashing-updates-error-count
(let ((*error-count* 0))
(without-crashing ()
(error "for test"))
(is (eql 1 *error-count*))))
(test with-closure-guard
(let* ((err 0)
(success 0)
(results nil)
(closure (closure (result status msg)
(push status results)
(if status
(incf success)
(incf err)))))
(with-closure-guard (closure)
(values))
(is (equal '(t) results))
(is (eql 1 success))
(is (eql 0 err))))
(test with-closure-guard-on-error
(let* ((err 0)
(success 0)
(closure (closure (result status msg)
(if status
(incf success)
(incf err)))))
(with-closure-guard (closure)
(error "dummy"))
(is (eql 1 err))
(is (eql 0 success))))
(test list-peers
(with-fixture cluster ()
(let ((leader (wait-for-leader machines)))
(assert-that (list-peers leader)
(has-length 3)))))