github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/actor/lifecycle_test.go (about) 1 package actor 2 3 import ( 4 "testing" 5 ) 6 7 type ( 8 dummyRequest struct{} 9 dummyResponse struct{} 10 ) 11 12 func TestActorCanReplyOnStarting(t *testing.T) { 13 future := NewFuture(system, testTimeout) 14 a := rootContext.Spawn(PropsFromFunc(func(context Context) { 15 switch context.Message().(type) { 16 case *Started: 17 context.Send(future.PID(), dummyResponse{}) 18 } 19 })) 20 _ = rootContext.StopFuture(a).Wait() 21 assertFutureSuccess(future, t) 22 } 23 24 func TestActorCanReplyOnStopping(t *testing.T) { 25 future := NewFuture(system, testTimeout) 26 a := rootContext.Spawn(PropsFromFunc(func(context Context) { 27 switch context.Message().(type) { 28 case *Stopping: 29 context.Send(future.PID(), dummyResponse{}) 30 } 31 })) 32 _ = rootContext.StopFuture(a).Wait() 33 assertFutureSuccess(future, t) 34 } 35 36 func TestActorReceivesStartedMessage(t *testing.T) { 37 future := NewFuture(system, testTimeout) 38 _ = rootContext.Spawn(PropsFromFunc(func(context Context) { 39 switch context.Message().(type) { 40 case *Started: 41 context.Send(future.PID(), dummyResponse{}) 42 } 43 })) 44 _ = future.Wait() 45 assertFutureSuccess(future, t) 46 } 47 48 func TestActorReceivesRestartingMessage(t *testing.T) { 49 future := NewFuture(system, testTimeout) 50 a := rootContext.Spawn(PropsFromFunc(func(context Context) { 51 switch context.Message().(type) { 52 case *dummyRequest: 53 panic("fail") 54 case *Restarting: 55 context.Send(future.PID(), dummyResponse{}) 56 } 57 })) 58 rootContext.Send(a, &dummyRequest{}) 59 assertFutureSuccess(future, t) 60 } 61 62 func TestActorReceivesStoppingMessage(t *testing.T) { 63 future := NewFuture(system, testTimeout) 64 a := rootContext.Spawn(PropsFromFunc(func(context Context) { 65 switch context.Message().(type) { 66 case *Stopping: 67 context.Send(future.PID(), dummyResponse{}) 68 } 69 })) 70 _ = rootContext.StopFuture(a).Wait() 71 assertFutureSuccess(future, t) 72 }