github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/actor/spawn_test.go (about) 1 package actor 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 type Increment struct{} 10 11 type GorgeousActor struct { 12 Counter 13 } 14 15 type Counter struct { 16 value int 17 } 18 19 func (counter *Counter) Increment() { 20 counter.value = counter.value + 1 21 } 22 23 func (a *GorgeousActor) Receive(context Context) { 24 switch context.Message().(type) { 25 case *Started: 26 case Increment: 27 a.Increment() 28 context.Respond(a.value) 29 } 30 } 31 32 func TestLookupById(t *testing.T) { 33 ID := "UniqueID" 34 { 35 props := PropsFromProducer(func() Actor { return &GorgeousActor{Counter: Counter{value: 0}} }) 36 pid, _ := rootContext.SpawnNamed(props, ID) 37 defer rootContext.Stop(pid) 38 39 result := rootContext.RequestFuture(pid, Increment{}, testTimeout) 40 value, err := result.Result() 41 if err != nil { 42 assert.Fail(t, "timed out") 43 return 44 } 45 assert.IsType(t, 0, value) 46 assert.Equal(t, 1, value.(int)) 47 } 48 { 49 props := PropsFromProducer(func() Actor { return &GorgeousActor{Counter: Counter{value: 0}} }) 50 pid, _ := rootContext.SpawnNamed(props, ID) 51 result := rootContext.RequestFuture(pid, Increment{}, testTimeout) 52 value, err := result.Result() 53 if err != nil { 54 assert.Fail(t, "timed out") 55 return 56 } 57 assert.Equal(t, 2, value.(int)) 58 } 59 }