rogue_machines 0.1.2 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +64 -0
- data/Cargo.lock +22 -2
- data/Cargo.toml +2 -0
- data/README.md +147 -36
- data/ext/rogue_machines_native/core/Cargo.toml +4 -6
- data/ext/rogue_machines_native/core/src/experiment.rs +50 -16
- data/ext/rogue_machines_native/core/src/lib.rs +21 -8
- data/ext/rogue_machines_native/core/src/rogue.rs +152 -63
- data/ext/rogue_machines_native/core/src/slot.rs +34 -0
- data/ext/rogue_machines_native/core/src/test_support.rs +27 -0
- data/ext/rogue_machines_native/ffi/Cargo.toml +5 -9
- data/ext/rogue_machines_native/ffi/src/init.rs +15 -0
- data/ext/rogue_machines_native/ffi/src/lib.rs +12 -166
- data/ext/rogue_machines_native/ffi/src/rogue.rs +194 -0
- data/ext/rogue_machines_native/ffi/src/values.rs +288 -0
- data/lib/rogue_machines/runtime.rb +312 -0
- data/lib/rogue_machines/version.rb +1 -1
- data/lib/rogue_machines.rb +94 -264
- data/sig/rogue_machines.rbs +210 -104
- metadata +9 -6
- data/ext/rogue_machines_native/core/src/armed.rs +0 -78
- data/ext/rogue_machines_native/ffi/src/injection.rs +0 -44
- data/ext/rogue_machines_native/ffi/src/registry.rs +0 -288
data/sig/rogue_machines.rbs
CHANGED
|
@@ -1,146 +1,252 @@
|
|
|
1
1
|
# Fault injection: making a dependency fail, stall or lie on purpose.
|
|
2
|
+
#
|
|
3
|
+
# Two layers: `Rogue` and the Data values mirror the core one for one; the
|
|
4
|
+
# module functions are a process-wide runtime on top of one Rogue. Every
|
|
5
|
+
# value validates through the core at construction: TypeError for the wrong
|
|
6
|
+
# type, RangeError for a value the core's constructor rejects. Time is Integer
|
|
7
|
+
# nanoseconds throughout.
|
|
2
8
|
module RogueMachines
|
|
3
9
|
VERSION: String
|
|
4
10
|
|
|
5
|
-
#
|
|
6
|
-
NS_PER_SECOND: Integer
|
|
7
|
-
|
|
8
|
-
# Millionths in a whole, the unit a share is held in.
|
|
11
|
+
# Millionths in a whole, the unit a Fraction and a Draw are held in.
|
|
9
12
|
PER_MILLION: Integer
|
|
10
13
|
|
|
11
|
-
#
|
|
14
|
+
# Slots a Rogue holds, the width of the armed mask.
|
|
12
15
|
EXPERIMENTS: Integer
|
|
13
16
|
|
|
14
|
-
#
|
|
15
|
-
|
|
17
|
+
# Longest `expires_in` the runtime arms, in nanoseconds: a day.
|
|
18
|
+
LONGEST_EXPERIMENT: Integer
|
|
19
|
+
|
|
20
|
+
# Longest Delay the runtime arms, in nanoseconds: five minutes.
|
|
21
|
+
LONGEST_DELAY: Integer
|
|
22
|
+
|
|
23
|
+
# Attempts at a reading against the clock still installed once the
|
|
24
|
+
# runtime's lock is held.
|
|
16
25
|
READINGS: Integer
|
|
17
26
|
|
|
18
27
|
class Error < StandardError
|
|
19
28
|
end
|
|
20
29
|
|
|
21
|
-
# Raised by a
|
|
30
|
+
# Raised by a :raise fault in place of the real call.
|
|
22
31
|
class Injected < Error
|
|
23
32
|
end
|
|
24
33
|
|
|
25
|
-
# The
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
attr_reader
|
|
34
|
+
# The core's `Slot`: an index in 0...EXPERIMENTS the caller picks. Not an
|
|
35
|
+
# issued id.
|
|
36
|
+
class Slot < ::Data
|
|
37
|
+
attr_reader index: Integer
|
|
38
|
+
def self.new: (Integer index) -> Slot
|
|
39
|
+
| (index: Integer) -> Slot
|
|
40
|
+
end
|
|
29
41
|
|
|
30
|
-
|
|
31
|
-
|
|
42
|
+
# The core's `Fraction`: 0..PER_MILLION millionths.
|
|
43
|
+
class Fraction < ::Data
|
|
44
|
+
NONE: Fraction
|
|
45
|
+
ALL: Fraction
|
|
46
|
+
attr_reader millionths: Integer
|
|
47
|
+
def self.new: (Integer millionths) -> Fraction
|
|
48
|
+
| (millionths: Integer) -> Fraction
|
|
49
|
+
# Whether `draw` falls in this share: `draw.millionths < millionths`.
|
|
50
|
+
def covers?: (Draw draw) -> bool
|
|
32
51
|
end
|
|
33
52
|
|
|
34
|
-
#
|
|
35
|
-
|
|
36
|
-
|
|
53
|
+
# The core's `Draw`: 0...PER_MILLION millionths.
|
|
54
|
+
class Draw < ::Data
|
|
55
|
+
attr_reader millionths: Integer
|
|
56
|
+
def self.new: (Integer millionths) -> Draw
|
|
57
|
+
| (millionths: Integer) -> Draw
|
|
37
58
|
end
|
|
38
59
|
|
|
39
|
-
#
|
|
40
|
-
|
|
41
|
-
|
|
60
|
+
# The core's `Fault::Delay`: `duration` Integer nanoseconds, 0..2**64-1.
|
|
61
|
+
class Delay < ::Data
|
|
62
|
+
attr_reader duration: Integer
|
|
63
|
+
def self.new: (Integer duration) -> Delay
|
|
64
|
+
| (duration: Integer) -> Delay
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# The core's `Fault`: unit variants as symbols, the tuple variant as Delay.
|
|
68
|
+
type fault = :raise | :garble | Delay
|
|
69
|
+
|
|
70
|
+
# The core's `Experiment`. `expires_at` is Integer nanoseconds, 0..2**64-1.
|
|
71
|
+
class Experiment < ::Data
|
|
72
|
+
attr_reader fault: fault
|
|
73
|
+
attr_reader fraction: Fraction
|
|
74
|
+
attr_reader expires_at: Integer
|
|
75
|
+
def self.new: (fault fault, Fraction fraction, Integer expires_at) -> Experiment
|
|
76
|
+
| (fault: fault, fraction: Fraction, expires_at: Integer) -> Experiment
|
|
77
|
+
# Whether this has expired by `now`: `now >= expires_at`.
|
|
78
|
+
def spent?: (Integer now) -> bool
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# The core's `Decision::Inject`.
|
|
82
|
+
class Inject < ::Data
|
|
83
|
+
attr_reader fault: fault
|
|
84
|
+
def self.new: (fault fault) -> Inject
|
|
85
|
+
| (fault: fault) -> Inject
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# The core's `Arming::Replaced`: what the slot held before, expired or not,
|
|
89
|
+
# and the runtime's `garbled:` substitute held with it (nil when there was
|
|
90
|
+
# none, and always nil from `Rogue#arm`: the core keeps no substitutes).
|
|
91
|
+
class Replaced < ::Data
|
|
92
|
+
attr_reader experiment: Experiment
|
|
93
|
+
attr_reader garbled: untyped
|
|
94
|
+
def self.new: (Experiment experiment, untyped garbled) -> Replaced
|
|
95
|
+
| (experiment: Experiment, garbled: untyped) -> Replaced
|
|
42
96
|
end
|
|
43
97
|
|
|
44
|
-
# The
|
|
98
|
+
# The core's `Disarming::Disarmed`: what the slot held, expired or not,
|
|
99
|
+
# and the runtime's `garbled:` substitute removed with it (nil when there
|
|
100
|
+
# was none, and always nil from `Rogue#disarm`: the core keeps no
|
|
101
|
+
# substitutes).
|
|
102
|
+
class Disarmed < ::Data
|
|
103
|
+
attr_reader experiment: Experiment
|
|
104
|
+
attr_reader garbled: untyped
|
|
105
|
+
def self.new: (Experiment experiment, untyped garbled) -> Disarmed
|
|
106
|
+
| (experiment: Experiment, garbled: untyped) -> Disarmed
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
type arming = :armed | Replaced | :already_armed
|
|
110
|
+
type disarming = Disarmed | :not_armed
|
|
111
|
+
|
|
112
|
+
# The core's `Rogue`. Nothing refuses. Queries judge expiry against the
|
|
113
|
+
# instant they are given and change nothing; only `sweep` empties expired
|
|
114
|
+
# slots. Mutating methods raise FrozenError on a frozen Rogue, and every
|
|
115
|
+
# method raises before changing anything when an argument is malformed.
|
|
45
116
|
class Rogue
|
|
46
|
-
def self.new: () ->
|
|
117
|
+
def self.new: () -> Rogue
|
|
118
|
+
|
|
119
|
+
# Honoured: :armed (slot was empty), Replaced (it held another
|
|
120
|
+
# experiment), :already_armed (it holds exactly this one; unchanged).
|
|
121
|
+
def arm: (Slot slot, Experiment experiment) -> arming
|
|
122
|
+
|
|
123
|
+
# Honoured: Disarmed (what it held), :not_armed (unchanged).
|
|
124
|
+
def disarm: (Slot slot) -> disarming
|
|
47
125
|
|
|
48
|
-
#
|
|
49
|
-
|
|
50
|
-
|
|
126
|
+
# Slots emptied, expired or not; 0 is unchanged.
|
|
127
|
+
def disarm_all: () -> Integer
|
|
128
|
+
|
|
129
|
+
# Slots emptied because expired by `now`; 0 is unchanged.
|
|
130
|
+
def sweep: (Integer now) -> Integer
|
|
131
|
+
|
|
132
|
+
# Whether any slot holds an experiment, expired or not.
|
|
51
133
|
def any_armed?: () -> bool
|
|
52
134
|
|
|
53
|
-
#
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
#
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
#
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
#
|
|
69
|
-
#
|
|
70
|
-
def
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
#
|
|
76
|
-
|
|
77
|
-
def generation: () -> Integer
|
|
78
|
-
|
|
79
|
-
# Disarms everything and starts a generation, retiring readings already
|
|
80
|
-
# taken against the outgoing clock. Returns the new generation.
|
|
81
|
-
def invalidate: () -> Integer
|
|
82
|
-
|
|
83
|
-
# The names still running at `now_ns`, in no particular order, and none
|
|
84
|
-
# for a superseded generation.
|
|
85
|
-
def armed: (Integer now_ns, Integer generation) -> Array[String]
|
|
86
|
-
|
|
87
|
-
# The fault to perform, or `nil` to make the real call. Raises
|
|
88
|
-
# `ArgumentError` for a `draw` or `now_ns` that is not an Integer, is
|
|
89
|
-
# outside its range, or is a `draw` at or above `PER_MILLION`. A Float is
|
|
90
|
-
# refused rather than truncated, because a truncated draw becomes 0,
|
|
91
|
-
# which every share covers.
|
|
92
|
-
def decide: (String name, Integer now_ns, Integer draw, Integer generation) -> Injection?
|
|
135
|
+
# Slots holding an experiment, expired or not.
|
|
136
|
+
def armed_count: () -> Integer
|
|
137
|
+
|
|
138
|
+
# Slots holding an experiment live at `now`.
|
|
139
|
+
def live_count: (Integer now) -> Integer
|
|
140
|
+
|
|
141
|
+
# The lowest empty slot, nil when all are held.
|
|
142
|
+
def vacant: () -> Slot?
|
|
143
|
+
|
|
144
|
+
# What `slot` holds, expired or not.
|
|
145
|
+
def experiment: (Slot slot) -> Experiment?
|
|
146
|
+
|
|
147
|
+
# What `slot` holds if live at `now`.
|
|
148
|
+
def active: (Slot slot, Integer now) -> Experiment?
|
|
149
|
+
|
|
150
|
+
# Inject when `slot` holds an experiment live at `now` whose share covers
|
|
151
|
+
# `draw`, else :pass.
|
|
152
|
+
def decide: (Slot slot, Integer now, Draw draw) -> (Inject | :pass)
|
|
153
|
+
|
|
154
|
+
# An unfrozen copy holding the same experiments.
|
|
155
|
+
def dup: () -> Rogue
|
|
156
|
+
|
|
157
|
+
# A copy, frozen when this is unless `freeze:` says otherwise.
|
|
158
|
+
def clone: (?freeze: bool?) -> Rogue
|
|
93
159
|
|
|
94
160
|
def inspect: () -> String
|
|
95
161
|
end
|
|
96
162
|
|
|
97
|
-
#
|
|
98
|
-
|
|
163
|
+
# Anything that reports monotonic Integer nanoseconds.
|
|
164
|
+
interface _Clock
|
|
165
|
+
def now_ns: () -> Integer
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# Anything that draws below a bound, which `Random` satisfies.
|
|
169
|
+
interface _Random
|
|
170
|
+
def rand: (Integer bound) -> Integer
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# Delegates to `DeadlineMachines.clock` when unset.
|
|
99
174
|
def self.clock: () -> _Clock
|
|
100
175
|
|
|
101
|
-
# Disarms everything first: an armed expiry
|
|
102
|
-
#
|
|
176
|
+
# Disarms everything first: an armed expiry is not meaningful against
|
|
177
|
+
# another clock.
|
|
103
178
|
def self.clock=: (_Clock?) -> void
|
|
104
179
|
def self.random: () -> _Random
|
|
105
180
|
def self.random=: (_Random?) -> void
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
#
|
|
112
|
-
#
|
|
113
|
-
#
|
|
114
|
-
#
|
|
115
|
-
#
|
|
116
|
-
#
|
|
117
|
-
#
|
|
118
|
-
#
|
|
119
|
-
#
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
def self.disarm_all: () ->
|
|
129
|
-
|
|
130
|
-
# With no name, whether anything
|
|
181
|
+
|
|
182
|
+
# A frozen copy of the runtime's Rogue, taken under the lock, for reading.
|
|
183
|
+
def self.rogue: () -> Rogue
|
|
184
|
+
|
|
185
|
+
# Arms `name`, replacing what it held. `share` is a Fraction or a real
|
|
186
|
+
# Numeric in 0..1 rounded to millionths; `expires_in` Integer nanoseconds
|
|
187
|
+
# in 1..LONGEST_EXPERIMENT; a Delay 1..LONGEST_DELAY. Honoured: the core's
|
|
188
|
+
# arming answer for the name's slot, except that :already_armed needs an
|
|
189
|
+
# equal (==) `garbled:` substitute too; a differing one replaces it and
|
|
190
|
+
# answers Replaced holding the experiment. Every Replaced it answers holds
|
|
191
|
+
# the substitute it displaced (nil for none). Refused, unchanged: :full
|
|
192
|
+
# when every slot holds a live experiment. Raises TypeError/RangeError for
|
|
193
|
+
# malformed arguments or clock readings, and Error when the clock was
|
|
194
|
+
# replaced READINGS times while arming; nothing is armed when it raises.
|
|
195
|
+
def self.arm: (Symbol | String name, fault: fault, share: Fraction | Numeric, expires_in: Integer,
|
|
196
|
+
?garbled: untyped) -> (arming | :full)
|
|
197
|
+
|
|
198
|
+
# Honoured: Disarmed (what the name held, expired or not, with the
|
|
199
|
+
# substitute it removed, nil for none), :not_armed. Takes no reading.
|
|
200
|
+
def self.disarm: (Symbol | String name) -> disarming
|
|
201
|
+
|
|
202
|
+
# Experiments held, expired or not; 0 is unchanged.
|
|
203
|
+
def self.disarm_all: () -> Integer
|
|
204
|
+
|
|
205
|
+
# With no name, whether anything is live now. Retires expired experiments.
|
|
131
206
|
def self.armed?: (?(Symbol | String)?) -> bool
|
|
132
207
|
|
|
133
|
-
# The
|
|
208
|
+
# The names live now. Retires expired experiments.
|
|
134
209
|
def self.armed: () -> Array[Symbol]
|
|
135
210
|
|
|
136
|
-
# Runs the block, or the fault armed under `name
|
|
137
|
-
#
|
|
138
|
-
#
|
|
139
|
-
#
|
|
140
|
-
#
|
|
141
|
-
# Raises `Injected` for a `:raise` fault, which is the point of the gem,
|
|
142
|
-
# `DeadlineMachines::Error` when a `:delay` spends the ambient budget,
|
|
143
|
-
# `ArgumentError` for a name it cannot key on or a draw the random gave it,
|
|
144
|
-
# and `Error` for a clock returning an unusable reading.
|
|
211
|
+
# Runs the block, or the fault armed under `name`: raises Injected for
|
|
212
|
+
# :raise, returns the `garbled:` substitute for :garble, waits then runs
|
|
213
|
+
# the block for a Delay (raising `DeadlineMachines::Error` when that spends
|
|
214
|
+
# the ambient budget). Raises TypeError/RangeError for a malformed name,
|
|
215
|
+
# draw or reading.
|
|
145
216
|
def self.run: (Symbol | String name) { () -> untyped } -> untyped
|
|
217
|
+
|
|
218
|
+
# Private native hooks behind the Data values: each answers the field as
|
|
219
|
+
# the core holds it, TypeError/RangeError for what the core rejects.
|
|
220
|
+
private def self.__slot: (Integer index) -> Integer
|
|
221
|
+
private def self.__fraction: (Integer millionths) -> Integer
|
|
222
|
+
private def self.__draw: (Integer millionths) -> Integer
|
|
223
|
+
private def self.__delay: (Integer duration) -> Integer
|
|
224
|
+
private def self.__experiment: (fault fault, Fraction fraction, Integer expires_at) -> [fault, Fraction, Integer]
|
|
225
|
+
private def self.__fault: (fault fault) -> fault
|
|
226
|
+
private def self.__held: (Experiment experiment) -> Experiment
|
|
227
|
+
# A Delay's Integer nanoseconds, nil for :raise and :garble.
|
|
228
|
+
private def self.__delay_of: (fault fault) -> Integer?
|
|
229
|
+
private def self.__spent: (Experiment experiment, Integer now) -> bool
|
|
230
|
+
private def self.__covers: (Fraction fraction, Draw draw) -> bool
|
|
231
|
+
|
|
232
|
+
# Private runtime helpers. `key` is the checked name as a frozen String.
|
|
233
|
+
private def self.arm_at: (String key, Experiment experiment, untyped payload, Integer now) -> (arming | :full)
|
|
234
|
+
private def self.disarmed: (disarming answer, untyped removed) -> disarming
|
|
235
|
+
private def self.replaced: (arming answer, Experiment experiment, untyped previous, bool same_payload) -> arming
|
|
236
|
+
private def self.decide_at: (String key, Integer now, Draw drawn) -> [Inject | :pass, untyped]
|
|
237
|
+
private def self.retire: (Integer now) -> void
|
|
238
|
+
# `[block's answer]` for a reading against the clock still installed once
|
|
239
|
+
# the lock is held, nil after READINGS attempts.
|
|
240
|
+
private def self.locked_reading: [T] () { (Integer now) -> T } -> [T]?
|
|
241
|
+
# Raises TypeError for a non-Integer reading, RangeError for a negative one.
|
|
242
|
+
private def self.checked_reading: (_Clock source, Integer now) -> nil
|
|
243
|
+
# Raises TypeError for a non-Symbol/String name, RangeError for an empty or
|
|
244
|
+
# illegible one.
|
|
245
|
+
private def self.legible: (Symbol | String name) -> nil
|
|
246
|
+
private def self.named: (Symbol | String name) -> String
|
|
247
|
+
private def self.fraction_of: (Fraction | Numeric share) -> Fraction
|
|
248
|
+
private def self.checked_fault: (fault fault) -> nil
|
|
249
|
+
private def self.span_of: (Integer expires_in) -> Integer
|
|
250
|
+
private def self.perform: (Symbol | String name, fault fault, untyped payload) { () -> untyped } -> untyped
|
|
251
|
+
private def self.stall: (Integer delay_ns) -> void
|
|
146
252
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rogue_machines
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Abdelkader Boudih
|
|
@@ -15,14 +15,14 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - ">="
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version: 0.
|
|
18
|
+
version: 0.2.0
|
|
19
19
|
type: :runtime
|
|
20
20
|
prerelease: false
|
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
22
|
requirements:
|
|
23
23
|
- - ">="
|
|
24
24
|
- !ruby/object:Gem::Version
|
|
25
|
-
version: 0.
|
|
25
|
+
version: 0.2.0
|
|
26
26
|
- !ruby/object:Gem::Dependency
|
|
27
27
|
name: rb_sys
|
|
28
28
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -55,17 +55,20 @@ files:
|
|
|
55
55
|
- LICENSE.txt
|
|
56
56
|
- README.md
|
|
57
57
|
- ext/rogue_machines_native/core/Cargo.toml
|
|
58
|
-
- ext/rogue_machines_native/core/src/armed.rs
|
|
59
58
|
- ext/rogue_machines_native/core/src/experiment.rs
|
|
60
59
|
- ext/rogue_machines_native/core/src/lib.rs
|
|
61
60
|
- ext/rogue_machines_native/core/src/rogue.rs
|
|
61
|
+
- ext/rogue_machines_native/core/src/slot.rs
|
|
62
|
+
- ext/rogue_machines_native/core/src/test_support.rs
|
|
62
63
|
- ext/rogue_machines_native/extconf.rb
|
|
63
64
|
- ext/rogue_machines_native/ffi/Cargo.toml
|
|
64
65
|
- ext/rogue_machines_native/ffi/extconf.rb
|
|
65
|
-
- ext/rogue_machines_native/ffi/src/
|
|
66
|
+
- ext/rogue_machines_native/ffi/src/init.rs
|
|
66
67
|
- ext/rogue_machines_native/ffi/src/lib.rs
|
|
67
|
-
- ext/rogue_machines_native/ffi/src/
|
|
68
|
+
- ext/rogue_machines_native/ffi/src/rogue.rs
|
|
69
|
+
- ext/rogue_machines_native/ffi/src/values.rs
|
|
68
70
|
- lib/rogue_machines.rb
|
|
71
|
+
- lib/rogue_machines/runtime.rb
|
|
69
72
|
- lib/rogue_machines/version.rb
|
|
70
73
|
- sig/rogue_machines.rbs
|
|
71
74
|
homepage: https://github.com/seuros/rogue_machines
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
//! Which slots are armed, in one word.
|
|
2
|
-
|
|
3
|
-
use core::sync::atomic::{AtomicU32, Ordering};
|
|
4
|
-
|
|
5
|
-
use crate::EXPERIMENTS;
|
|
6
|
-
|
|
7
|
-
/// A slot index a [`Rogue`](crate::Rogue) can hold.
|
|
8
|
-
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
|
9
|
-
pub struct Slot(usize);
|
|
10
|
-
|
|
11
|
-
impl Slot {
|
|
12
|
-
/// Rejects an index at or above [`EXPERIMENTS`].
|
|
13
|
-
#[must_use]
|
|
14
|
-
pub const fn new(index: usize) -> Option<Self> {
|
|
15
|
-
if index < EXPERIMENTS {
|
|
16
|
-
Some(Self(index))
|
|
17
|
-
} else {
|
|
18
|
-
None
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
#[must_use]
|
|
23
|
-
pub const fn index(self) -> usize {
|
|
24
|
-
self.0
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const fn bit(self) -> u32 {
|
|
28
|
-
1_u32 << self.0
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/// One bit per slot, read on every protected call and written only when an
|
|
33
|
-
/// experiment is armed, disarmed or found expired.
|
|
34
|
-
///
|
|
35
|
-
/// Every operation is `Relaxed`. That is sufficient only because the
|
|
36
|
-
/// experiment table is written behind `&mut self`, so the mask never has to
|
|
37
|
-
/// publish it. Giving `Rogue::arm` interior mutability would break that.
|
|
38
|
-
#[derive(Debug, Default)]
|
|
39
|
-
pub struct Armed(AtomicU32);
|
|
40
|
-
|
|
41
|
-
impl Armed {
|
|
42
|
-
#[must_use]
|
|
43
|
-
pub const fn new() -> Self {
|
|
44
|
-
Self(AtomicU32::new(0))
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/// Whether any slot is armed.
|
|
48
|
-
#[must_use]
|
|
49
|
-
pub fn any(&self) -> bool {
|
|
50
|
-
self.0.load(Ordering::Relaxed) != 0
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
#[must_use]
|
|
54
|
-
pub fn holds(&self, slot: Slot) -> bool {
|
|
55
|
-
self.0.load(Ordering::Relaxed) & slot.bit() != 0
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
pub fn hold(&self, slot: Slot) {
|
|
59
|
-
self.0.fetch_or(slot.bit(), Ordering::Relaxed);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
pub fn release(&self, slot: Slot) {
|
|
63
|
-
self.0.fetch_and(!slot.bit(), Ordering::Relaxed);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
pub fn release_all(&self) {
|
|
67
|
-
self.0.store(0, Ordering::Relaxed);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/// How many slots are armed.
|
|
71
|
-
#[must_use]
|
|
72
|
-
pub fn count(&self) -> u32 {
|
|
73
|
-
self.0.load(Ordering::Relaxed).count_ones()
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
#[cfg(test)]
|
|
78
|
-
mod tests;
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
//! The frozen `Data` object a decision hands back when it injects.
|
|
2
|
-
|
|
3
|
-
use std::sync::OnceLock;
|
|
4
|
-
|
|
5
|
-
use magnus::value::Opaque;
|
|
6
|
-
use magnus::{Error, RClass, RModule, RStruct, Ruby, Value, prelude::*};
|
|
7
|
-
use rogue_machines::Fault;
|
|
8
|
-
|
|
9
|
-
use crate::error;
|
|
10
|
-
|
|
11
|
-
static INJECTION: OnceLock<Opaque<RClass>> = OnceLock::new();
|
|
12
|
-
|
|
13
|
-
pub fn define(ruby: &Ruby, module: RModule) -> Result<(), Error> {
|
|
14
|
-
let injection = ruby.define_data(None, ("fault", "delay_ns"))?;
|
|
15
|
-
module.const_set("Injection", injection)?;
|
|
16
|
-
INJECTION
|
|
17
|
-
.set(injection.into())
|
|
18
|
-
.map_err(|_| error(ruby, "Injection was defined twice".to_owned()))
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/// The fault to perform, and how long to wait when it is a delay.
|
|
22
|
-
pub fn injection(ruby: &Ruby, fault: Fault) -> Result<Value, Error> {
|
|
23
|
-
let class = INJECTION
|
|
24
|
-
.get()
|
|
25
|
-
.map(|class| ruby.get_inner(*class))
|
|
26
|
-
.ok_or_else(|| error(ruby, "Injection was not defined".to_owned()))?;
|
|
27
|
-
let instance = RStruct::try_convert(class.obj_alloc()?)?;
|
|
28
|
-
let (name, delay_ns) = match fault {
|
|
29
|
-
Fault::Raise => ("raise", 0),
|
|
30
|
-
Fault::Delay { ns } => ("delay", ns),
|
|
31
|
-
Fault::Garble => ("garble", 0),
|
|
32
|
-
};
|
|
33
|
-
for (index, member) in [
|
|
34
|
-
ruby.into_value(ruby.to_symbol(name)),
|
|
35
|
-
ruby.into_value(delay_ns),
|
|
36
|
-
]
|
|
37
|
-
.into_iter()
|
|
38
|
-
.enumerate()
|
|
39
|
-
{
|
|
40
|
-
instance.aset(index, member)?;
|
|
41
|
-
}
|
|
42
|
-
instance.freeze();
|
|
43
|
-
Ok(instance.as_value())
|
|
44
|
-
}
|