github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/actor/supervision_event_test.go (about)

     1  package actor
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  type panicActor struct{}
    10  
    11  func (a *panicActor) Receive(ctx Context) {
    12  	switch ctx.Message().(type) {
    13  	case string:
    14  		panic("Boom!")
    15  	}
    16  }
    17  
    18  func TestSupervisorEventHandleFromEventstream(t *testing.T) {
    19  	supervisors := []struct {
    20  		name     string
    21  		strategy SupervisorStrategy
    22  	}{
    23  		{
    24  			name:     "all_for_one",
    25  			strategy: NewAllForOneStrategy(10, 10*time.Second, DefaultDecider),
    26  		},
    27  		{
    28  			name:     "exponential_backoff",
    29  			strategy: NewExponentialBackoffStrategy(10*time.Millisecond, 10*time.Millisecond),
    30  		},
    31  		{
    32  			name:     "one_for_one",
    33  			strategy: NewOneForOneStrategy(10, 10*time.Second, DefaultDecider),
    34  		},
    35  		{
    36  			name:     "restarting",
    37  			strategy: NewRestartingStrategy(),
    38  		},
    39  	}
    40  
    41  	for _, v := range supervisors {
    42  		t.Run(v.name, func(t *testing.T) {
    43  			wg := sync.WaitGroup{}
    44  			sid := system.EventStream.Subscribe(func(evt interface{}) {
    45  				if _, ok := evt.(*SupervisorEvent); ok {
    46  					wg.Done()
    47  				}
    48  			})
    49  			defer system.EventStream.Unsubscribe(sid)
    50  
    51  			props := PropsFromProducer(func() Actor { return &panicActor{} }, WithSupervisor(v.strategy))
    52  			pid := rootContext.Spawn(props)
    53  
    54  			wg.Add(1)
    55  			rootContext.Send(pid, "Fail!")
    56  
    57  			wg.Wait()
    58  		})
    59  	}
    60  }