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

     1  package actor
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  )
     7  
     8  type dummyMessageBatch struct {
     9  	messages []interface{}
    10  }
    11  
    12  func (d dummyMessageBatch) GetMessages() []interface{} {
    13  	return d.messages
    14  }
    15  
    16  func TestActorReceivesEachMessageInAMessageBatch(t *testing.T) {
    17  	// each message in the batch
    18  	seenMessagesWg := sync.WaitGroup{}
    19  	seenMessagesWg.Add(10)
    20  
    21  	// the batch message itself
    22  	seenBatchMessageWg := sync.WaitGroup{}
    23  	seenBatchMessageWg.Add(1)
    24  
    25  	pid := rootContext.Spawn(PropsFromFunc(func(ctx Context) {
    26  		if _, ok := ctx.Message().(*DummyMessage); ok {
    27  			seenMessagesWg.Done()
    28  		}
    29  
    30  		if _, ok := ctx.Message().(*dummyMessageBatch); ok {
    31  			seenBatchMessageWg.Done()
    32  		}
    33  	}))
    34  
    35  	batch := &dummyMessageBatch{messages: make([]interface{}, 10)}
    36  
    37  	for i := 0; i < 10; i++ {
    38  		batch.messages[i] = &DummyMessage{}
    39  	}
    40  
    41  	rootContext.Send(pid, batch)
    42  
    43  	seenMessagesWg.Wait()
    44  	seenBatchMessageWg.Wait()
    45  }