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

     1  package router
     2  
     3  import (
     4  	"strconv"
     5  	"sync"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/asynkron/protoactor-go/actor"
    10  )
    11  
    12  var system = actor.NewActorSystem()
    13  
    14  func TestBroadcastRouterThreadSafe(t *testing.T) {
    15  	wg := sync.WaitGroup{}
    16  	wg.Add(2)
    17  
    18  	props := actor.PropsFromFunc(func(c actor.Context) {})
    19  
    20  	grp := system.Root.Spawn(NewBroadcastGroup())
    21  	go func() {
    22  		count := 100
    23  		for i := 0; i < count; i++ {
    24  			pid, _ := system.Root.SpawnNamed(props, strconv.Itoa(i))
    25  			system.Root.Send(grp, &AddRoutee{PID: pid})
    26  			time.Sleep(10 * time.Millisecond)
    27  		}
    28  		wg.Done()
    29  	}()
    30  	go func() {
    31  		count := 100
    32  		for c := 0; c < count; c++ {
    33  			system.Root.Send(grp, struct{}{})
    34  			time.Sleep(10 * time.Millisecond)
    35  		}
    36  		wg.Done()
    37  	}()
    38  
    39  	wg.Wait()
    40  }