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

     1  package plugin
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/asynkron/protoactor-go/actor"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  var system = actor.NewActorSystem()
    12  
    13  type SmartActor struct {
    14  	PassivationHolder
    15  }
    16  
    17  func (state *SmartActor) Receive(context actor.Context) {
    18  	switch context.Message().(type) {
    19  	}
    20  }
    21  
    22  func TestPassivation(t *testing.T) {
    23  	if testing.Short() {
    24  		t.SkipNow()
    25  	}
    26  
    27  	UnitOfTime := 200 * time.Millisecond
    28  	PassivationDuration := 3 * UnitOfTime
    29  	rootContext := system.Root
    30  	props := actor.
    31  		PropsFromProducer(func() actor.Actor { return &SmartActor{} },
    32  			actor.WithReceiverMiddleware(Use(&PassivationPlugin{Duration: PassivationDuration})))
    33  
    34  	pid := rootContext.Spawn(props)
    35  	time.Sleep(UnitOfTime)
    36  	time.Sleep(UnitOfTime)
    37  	{
    38  		_, found := system.ProcessRegistry.GetLocal(pid.Id)
    39  		assert.True(t, found)
    40  	}
    41  	rootContext.Send(pid, "keepalive")
    42  	time.Sleep(UnitOfTime)
    43  	time.Sleep(UnitOfTime)
    44  	{
    45  		_, found := system.ProcessRegistry.GetLocal(pid.Id)
    46  		assert.True(t, found)
    47  	}
    48  	time.Sleep(UnitOfTime)
    49  	time.Sleep(UnitOfTime)
    50  	{
    51  		_, found := system.ProcessRegistry.GetLocal(pid.Id)
    52  		assert.False(t, found)
    53  	}
    54  }