Skip to content

Commit

Permalink
Make actor.Stop(process.Abnormal(...)) exit the process with the give…
Browse files Browse the repository at this point in the history
…n reason
  • Loading branch information
sbergen committed Nov 28, 2024
1 parent 1c8c9eb commit 3e2b143
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

- Fixed a bug where using `actor.Stop(process.Abnormal(...))` would stop
the process with `Normal`.

## v0.14.1 - 2024-11-15

- Fixed a bug where the `significant` parameter would not be passed to the
Expand Down
7 changes: 6 additions & 1 deletion src/gleam/otp/actor.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ pub type Spec(state, msg) {

// TODO: Check needed functionality here to be OTP compatible
fn exit_process(reason: ExitReason) -> ExitReason {
case reason {
Abnormal(reason) -> process.send_abnormal_exit(process.self(), reason)
_ -> Nil
}

// TODO
reason
}
Expand Down Expand Up @@ -407,7 +412,7 @@ fn initialise_actor(
// The init failed. Exit with an error.
Failed(reason) -> {
process.send(ack, Error(Abnormal(reason)))
exit_process(Abnormal(reason))
exit_process(process.Normal)
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions test/gleam/otp/actor_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,31 @@ pub fn replace_selector_test() {
|> 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)

trapped_reason
|> should.equal(
Ok(process.ExitMessage(
process.subject_owner(subject),
process.Abnormal("reason"),
)),
)
}

fn mapped_selector(mapper: fn(a) -> ActorMessage) {
let subject = process.new_subject()

Expand Down

0 comments on commit 3e2b143

Please sign in to comment.