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

     1  package actor
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  type (
    11  	DummyMessage   struct{}
    12  	BlackHoleActor struct{}
    13  )
    14  
    15  var testTimeout = 1 * time.Second
    16  
    17  func (state *BlackHoleActor) Receive(Context) {}
    18  
    19  func NewBlackHoleActor() Actor {
    20  	return &BlackHoleActor{}
    21  }
    22  
    23  func TestSpawnProducesProcess(t *testing.T) {
    24  	actor := rootContext.Spawn(PropsFromProducer(NewBlackHoleActor))
    25  	defer rootContext.Stop(actor)
    26  	assert.NotNil(t, actor)
    27  }
    28  
    29  type EchoRequest struct{}
    30  
    31  type EchoResponse struct{}
    32  
    33  type EchoActor struct{}
    34  
    35  func NewEchoActor() Actor {
    36  	return &EchoActor{}
    37  }
    38  
    39  func (*EchoActor) Receive(context Context) {
    40  	switch context.Message().(type) {
    41  	case EchoRequest:
    42  		context.Respond(EchoResponse{})
    43  	}
    44  }
    45  
    46  func TestActorCanReplyToMessage(t *testing.T) {
    47  	pid := rootContext.Spawn(PropsFromProducer(NewEchoActor))
    48  	defer rootContext.Stop(pid)
    49  	err := rootContext.RequestFuture(pid, EchoRequest{}, testTimeout).Wait()
    50  	if err != nil {
    51  		assert.Fail(t, "timed out")
    52  		return
    53  	}
    54  }