This repository has been archived by the owner on Mar 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
transaction.rkt
174 lines (146 loc) · 5.93 KB
/
transaction.rkt
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
#lang racket/base
; Consider the following command line, where -i and -u can be
; specified multiple times.
;
; $ ... -i a -u b -i c -u d
;
; The user expects the command to handle arguments in the shown
; order: (a b c d). This module does so in a transactional
; control flow.
(require racket/contract
racket/exn
"cli-flag.rkt"
"message.rkt"
"subprogram.rkt")
(provide
(contract-out
[transact
(-> (listof (-> subprogram?))
(-> list? any)
(-> list? any)
any)]
[fold-transaction-actions
(-> (listof cli-flag-state?)
(hash/c procedure? (-> any/c subprogram?))
(listof (-> subprogram?)))]))
(define (transact actions commit rollback)
(call/cc
(λ (k)
(define (done messages) (k (commit messages)))
(define (fail messages) (k (rollback messages)))
(commit
(for/fold ([accum-messages null])
([action (in-list actions)])
(define-values (result messages)
(with-handlers
([$message?
(λ (m)
(fail (cons m accum-messages)))]
[(λ _ #t)
(λ (e)
(fail (cons ($show-string (exn->string e))
accum-messages)))])
(run-subprogram (action) accum-messages)))
(if (eq? result FAILURE)
(fail messages)
messages))))))
(define (fold-transaction-actions flags lookup)
(fold-transaction-actions-aux flags lookup null (hasheq)))
(define (fold-transaction-actions-aux flags lookup actions counts)
(if (null? flags)
(reverse actions)
(let ([setting (cli-flag-setting (cli-flag-state-flag-definition (car flags)))])
(if (hash-has-key? lookup setting)
(add-action flags
lookup
actions
setting
(hash-set counts setting (add1 (hash-ref counts setting -1))))
(fold-transaction-actions-aux (cdr flags)
lookup
actions
counts)))))
(define (make-action lookup setting value)
(λ () ((hash-ref lookup setting) value)))
(define (add-action flags lookup actions setting counts)
(fold-transaction-actions-aux (cdr flags)
lookup
(cons (make-action lookup
setting
(list-ref (setting)
(hash-ref counts setting)))
actions)
counts))
(module+ test
(require rackunit
racket/format
racket/list
racket/match
"setting.rkt")
(define-setting TEST_LETTER_STRINGS list? null)
(define-setting TEST_NUMBER_STRINGS list? null)
(define-setting TEST_RED_HERRING list? null)
(define str-flag (cli-flag TEST_LETTER_STRINGS 'multi '("-l") 1 void '("any")))
(define num-flag (cli-flag TEST_NUMBER_STRINGS 'multi '("-n") 1 void '("any")))
(define red-flag (cli-flag TEST_RED_HERRING 'multi '("-h") 1 void '("any")))
; Use to produce mock cli flag handler values.
(define (mocker value-list flag)
(λ (i) (cli-flag-state (shortest-cli-flag flag)
flag
(list-ref value-list i))))
(define (act v)
(subprogram-attachment (eq? v 'b)
($show-string (~a v))))
(call-with-applied-settings
(hash TEST_LETTER_STRINGS '(a b c)
TEST_NUMBER_STRINGS '(1 2 3)
TEST_RED_HERRING '("red"))
(λ ()
(define mock-letter (mocker (TEST_LETTER_STRINGS) str-flag))
(define mock-number (mocker (TEST_NUMBER_STRINGS) num-flag))
(define mock-herring (mocker (TEST_RED_HERRING) red-flag))
(define flags
(list (mock-letter 0)
(mock-number 0)
(mock-herring 0)
(mock-herring 0)
(mock-number 1)
(mock-herring 0)
(mock-letter 1)
(mock-herring 0)
(mock-letter 2)
(mock-number 2)))
(define actions
(fold-transaction-actions flags
(hasheq TEST_LETTER_STRINGS symbol->string
TEST_NUMBER_STRINGS -)))
(define expected-preprocessed-values
'("a" -1 -2 "b" "c" -3))
(test-equal? "Bind multi flags in thunks"
(map (λ (f) (f)) actions)
expected-preprocessed-values)
(test-case "Carry out successful transaction"
(define (rollback m)
(cons 'rolled-back (flatten m)))
(check-equal? (transact (map (λ (f) (λ () (subprogram-attachment (f) ($show-string (~a (f)))))) actions)
flatten
rollback)
(map (compose $show-string ~a)
(reverse expected-preprocessed-values)))
(define (warn) (subprogram-attachment #f ($show-string "about to fail")))
(test-equal? "Handle transaction failure"
(transact (list warn
(λ () (subprogram-failure ($show-string "uh oh")))
warn)
flatten
rollback)
(list 'rolled-back
($show-string "uh oh")
($show-string "about to fail")))
(test-equal? "Handle transaction failure via raised value"
(transact (list warn (λ () (raise 'oops)) warn)
flatten
rollback)
(list 'rolled-back
($show-string "oops\n")
($show-string "about to fail")))))))