forked from gleam-lang/otp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactor_test.gleam
309 lines (257 loc) · 8 KB
/
actor_test.gleam
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import gleam/dynamic.{type Dynamic}
import gleam/erlang/atom.{type Atom}
import gleam/erlang/process.{type Pid, type Subject}
import gleam/function
import gleam/int
import gleam/otp/actor
import gleam/otp/system
import gleam/result
import gleeunit/should
pub fn get_state_test() {
let assert Ok(subject) =
actor.start("Test state", fn(_msg, state) { actor.continue(state) })
subject
|> process.subject_owner
|> system.get_state
|> should.equal(dynamic.from("Test state"))
}
@external(erlang, "sys", "get_status")
fn get_status(a: Pid) -> Dynamic
pub fn get_status_test() {
let assert Ok(subject) =
actor.start(Nil, fn(_msg, state) { actor.continue(state) })
subject
|> process.subject_owner
|> get_status
// TODO: assert something about the response
}
pub fn failed_init_test() {
actor.Spec(
init: fn() { actor.Failed("not enough wiggles") },
loop: fn(_msg, state) { actor.continue(state) },
init_timeout: 10,
)
|> actor.start_spec
|> result.is_error
|> should.be_true
}
pub fn timed_out_init_test() {
process.trap_exits(True)
let exit_selector =
process.new_selector()
|> process.selecting_trapped_exits(function.identity)
let result =
actor.Spec(
init: fn() {
process.sleep(1000)
panic as "should not be reached"
},
loop: fn(_msg, _state) { panic as "should not be reached" },
init_timeout: 1,
)
|> actor.start_spec
// Check that the exit isn't unhandled: it should be handled by start_spec.
// Stop trapping exits before asserting, to avoid interfering with other tests.
let exit = process.select(exit_selector, 10)
process.trap_exits(False)
result |> should.equal(Error(actor.InitTimeout))
exit |> should.equal(Error(Nil))
}
pub fn suspend_resume_test() {
let assert Ok(subject) =
actor.start(0, fn(_msg, iter) { actor.continue(iter + 1) })
// Suspend process
subject
|> process.subject_owner
|> system.suspend
|> should.equal(Nil)
// This normal message will not be handled yet so the state remains 0
actor.send(subject, "hi")
// System messages are still handled
subject
|> process.subject_owner
|> system.get_state
|> should.equal(dynamic.from(0))
// Resume process
subject
|> process.subject_owner
|> system.resume
|> should.equal(Nil)
// The queued regular message has been handled so the state has incremented
subject
|> process.subject_owner
|> system.get_state
|> should.equal(dynamic.from(1))
}
pub fn subject_test() {
let assert Ok(subject) =
actor.start("state 1", fn(msg, _state) { actor.continue(msg) })
subject
|> process.subject_owner
|> system.get_state()
|> should.equal(dynamic.from("state 1"))
actor.send(subject, "state 2")
subject
|> process.subject_owner
|> system.get_state()
|> should.equal(dynamic.from("state 2"))
}
pub fn unexpected_message_test() {
// Quieten the logger
logger_set_primary_config(
atom.create_from_string("level"),
atom.create_from_string("error"),
)
let assert Ok(subject) =
actor.start("state 1", fn(msg, _state) { actor.continue(msg) })
subject
|> process.subject_owner
|> system.get_state()
|> should.equal(dynamic.from("state 1"))
raw_send(process.subject_owner(subject), "Unexpected message 1")
actor.send(subject, "state 2")
raw_send(process.subject_owner(subject), "Unexpected message 2")
subject
|> process.subject_owner
|> system.get_state()
|> should.equal(dynamic.from("state 2"))
}
pub fn unexpected_message_handled_test() {
let assert Ok(subject) =
actor.start_spec(actor.Spec(
init: fn() {
let selector =
process.new_selector()
|> process.selecting_anything(function.identity)
actor.Ready(dynamic.from("init"), selector)
},
loop: fn(msg, _state) { actor.continue(msg) },
init_timeout: 10,
))
raw_send(process.subject_owner(subject), "Unexpected message 1")
subject
|> process.subject_owner
|> system.get_state()
|> should.equal(dynamic.from("Unexpected message 1"))
}
type ActorMessage {
UserMessage(String)
Unknown(Dynamic)
SetStringSelector(
reply: Subject(Subject(String)),
mapper: fn(String) -> ActorMessage,
)
SetIntSelector(reply: Subject(Subject(Int)), mapper: fn(Int) -> ActorMessage)
}
pub fn replace_selector_test() {
let assert Ok(subject) =
actor.start("init", fn(msg: ActorMessage, state) {
case msg {
UserMessage(string) -> actor.continue("user message: " <> string)
Unknown(val) ->
actor.continue("unknown message: " <> dynamic.classify(val))
SetStringSelector(reply, mapper) -> {
let #(subject, selector) = mapped_selector(mapper)
process.send(reply, subject)
actor.continue(state)
|> actor.with_selector(selector)
}
SetIntSelector(reply, mapper) -> {
let #(subject, selector) = mapped_selector(mapper)
process.send(reply, subject)
actor.continue(state)
|> actor.with_selector(selector)
}
}
})
// Send initial user message to original subject
process.send(subject, UserMessage("test 1"))
// Check state
get_actor_state(subject)
|> should.equal(dynamic.from("user message: test 1"))
// Get a new subject with string selector
let str_subj = process.call(subject, SetStringSelector(_, UserMessage), 1000)
// Send to new string subject
process.send(str_subj, "test 2")
// Check state
get_actor_state(subject)
|> should.equal(dynamic.from("user message: test 2"))
// Get a new subject with int selector
let int_subj =
process.call(
subject,
SetIntSelector(_, fn(n: Int) { UserMessage("test " <> int.to_string(n)) }),
1000,
)
// Send to new int subject
process.send(int_subj, 3)
// Check state
get_actor_state(subject)
|> should.equal(dynamic.from("user message: test 3"))
// Try to send to old string subject
process.send(str_subj, "test 4")
// Check state
get_actor_state(subject)
|> should.equal(dynamic.from("unknown message: String"))
}
pub fn abnormal_exit_can_be_trapped_test() {
process.trap_exits(True)
let exits =
process.new_selector()
|> process.selecting_trapped_exits(function.identity)
// Make an actor exit with an abnormal reason
let assert Ok(subject) =
actor.start(Nil, fn(_, _) { actor.Stop(process.Abnormal("reason")) })
process.send(subject, Nil)
let trapped_reason = process.select(exits, 10)
// Stop trapping exits, as otherwise other tests fail
process.trap_exits(False)
// The weird reason below is because of https://github.com/gleam-lang/erlang/issues/66
trapped_reason
|> should.equal(
Ok(process.ExitMessage(
process.subject_owner(subject),
process.Abnormal("Abnormal(\"reason\")"),
)),
)
}
pub fn killed_exit_can_be_trapped_test() {
process.trap_exits(True)
let exits =
process.new_selector()
|> process.selecting_trapped_exits(function.identity)
// Make an actor exit with a killed reason
let assert Ok(subject) =
actor.start(Nil, fn(_, _) { actor.Stop(process.Killed) })
process.send(subject, Nil)
let trapped_reason = process.select(exits, 10)
// Stop trapping exits, as otherwise other tests fail
process.trap_exits(False)
trapped_reason
|> should.equal(
Ok(process.ExitMessage(process.subject_owner(subject), process.Killed)),
)
}
fn mapped_selector(mapper: fn(a) -> ActorMessage) {
let subject = process.new_subject()
let selector =
process.new_selector()
|> process.selecting(subject, mapper)
// Always create a selector that catches unknown messages
|> process.selecting_anything(fn(data) {
data
|> dynamic.element(1, dynamic.dynamic)
|> result.unwrap(dynamic.from("unknown"))
|> Unknown
})
#(subject, selector)
}
fn get_actor_state(subject: Subject(a)) {
subject
|> process.subject_owner
|> system.get_state
}
@external(erlang, "erlang", "send")
fn raw_send(a: Pid, b: anything) -> anything
@external(erlang, "logger", "set_primary_config")
fn logger_set_primary_config(a: Atom, b: Atom) -> Nil