Skip to content

Commit

Permalink
Allow the interrupt function to be set to nil (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
larry4xie committed Oct 12, 2024
1 parent eee6e04 commit 4b91dff
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion group.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ func (g *Group) Run() error {

// Signal all actors to stop.
for _, a := range g.actors {
a.interrupt(err)
if a.interrupt != nil {
a.interrupt(err)
}
}

// Wait for all actors to stop.
Expand All @@ -56,6 +58,8 @@ func (g *Group) Run() error {
return err
}

var NilInterrupt = func(error) {}

type actor struct {
execute func() error
interrupt func(error)
Expand Down
18 changes: 18 additions & 0 deletions group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,21 @@ func TestMany(t *testing.T) {
t.Errorf("timeout")
}
}

func TestNilInterrupt(t *testing.T) {
interrupt := errors.New("interrupt")
var g run.Group
g.Add(func() error { return interrupt }, func(error) {})
g.Add(func() error { return interrupt }, nil)
g.Add(func() error { return interrupt }, run.NilInterrupt)
res := make(chan error)
go func() { res <- g.Run() }()
select {
case err := <-res:
if want, have := interrupt, err; want != have {
t.Errorf("want %v, have %v", want, have)
}
case <-time.After(100 * time.Millisecond):
t.Error("timeout")
}
}

0 comments on commit 4b91dff

Please sign in to comment.