github.com/Jeffail/benthos/v3@v3.65.0/lib/message/roundtrip/result_store_test.go (about)

     1  package roundtrip
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/Jeffail/benthos/v3/lib/message"
     8  	"github.com/Jeffail/benthos/v3/lib/types"
     9  )
    10  
    11  func TestResultStore(t *testing.T) {
    12  	impl := &resultStoreImpl{}
    13  	ctx := context.WithValue(context.Background(), ResultStoreKey, impl)
    14  	msg := message.New(nil)
    15  	var p types.Part = message.NewPart([]byte("foo"))
    16  	p = message.WithContext(ctx, p)
    17  	msg.Append(p)
    18  	msg.Append(message.NewPart([]byte("bar")))
    19  
    20  	impl.Add(msg)
    21  	results := impl.Get()
    22  	if len(results) != 1 {
    23  		t.Fatalf("Wrong count of result batches: %v", len(results))
    24  	}
    25  	if results[0].Len() != 2 {
    26  		t.Fatalf("Wrong count of messages: %v", results[0].Len())
    27  	}
    28  	if exp, act := "foo", string(results[0].Get(0).Get()); exp != act {
    29  		t.Errorf("Wrong message contents: %v != %v", act, exp)
    30  	}
    31  	if exp, act := "bar", string(results[0].Get(1).Get()); exp != act {
    32  		t.Errorf("Wrong message contents: %v != %v", act, exp)
    33  	}
    34  	if store := message.GetContext(results[0].Get(0)).Value(ResultStoreKey); store != nil {
    35  		t.Error("Unexpected nested result store")
    36  	}
    37  
    38  	impl.Clear()
    39  	if exp, act := len(impl.Get()), 0; exp != act {
    40  		t.Errorf("Unexpected count of stored messages: %v != %v", act, exp)
    41  	}
    42  }