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

     1  package actor_test
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  	"time"
     7  
     8  	"github.com/asynkron/protoactor-go/actor"
     9  )
    10  
    11  var system = actor.NewActorSystem()
    12  
    13  func ExampleFuture_PipeTo() {
    14  	var wg sync.WaitGroup
    15  	wg.Add(1)
    16  
    17  	// test actor that will be the target of the future PipeTo
    18  	pid := system.Root.Spawn(actor.PropsFromFunc(func(ctx actor.Context) {
    19  		// check if the message is a string and therefore
    20  		// the "hello world" message piped from the future
    21  		if m, ok := ctx.Message().(string); ok {
    22  			fmt.Println(m)
    23  			wg.Done()
    24  		}
    25  	}))
    26  
    27  	f := actor.NewFuture(system, 50*time.Millisecond)
    28  	f.PipeTo(pid)
    29  	// resolve the future and pipe to waiting actor
    30  	system.Root.Send(f.PID(), "hello world")
    31  	wg.Wait()
    32  
    33  	// Output: hello world
    34  }