github.com/Jeffail/benthos/v3@v3.65.0/lib/buffer/memory_test.go (about)

     1  package buffer
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/Jeffail/benthos/v3/lib/log"
     8  	"github.com/Jeffail/benthos/v3/lib/message"
     9  	"github.com/Jeffail/benthos/v3/lib/metrics"
    10  	"github.com/Jeffail/benthos/v3/lib/response"
    11  	"github.com/Jeffail/benthos/v3/lib/types"
    12  )
    13  
    14  func TestMemoryBuffer(t *testing.T) {
    15  	conf := NewConfig()
    16  	conf.Type = "memory"
    17  
    18  	buf, err := New(conf, nil, log.Noop(), metrics.Noop())
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  
    23  	tChan, resChan := make(chan types.Transaction), make(chan types.Response)
    24  
    25  	if err = buf.Consume(tChan); err != nil {
    26  		t.Error(err)
    27  	}
    28  
    29  	msg := message.New([][]byte{
    30  		[]byte(`one`),
    31  		[]byte(`two`),
    32  	})
    33  
    34  	select {
    35  	case tChan <- types.NewTransaction(msg, resChan):
    36  	case <-time.After(time.Second):
    37  		t.Error("Timed out")
    38  	}
    39  	select {
    40  	case res, open := <-resChan:
    41  		if !open {
    42  			t.Error("buffer closed early")
    43  		}
    44  		if res.Error() != nil {
    45  			t.Error(res.Error())
    46  		}
    47  	case <-time.After(time.Second):
    48  		t.Error("Timed out")
    49  	}
    50  
    51  	var outTr types.Transaction
    52  	var open bool
    53  	select {
    54  	case outTr, open = <-buf.TransactionChan():
    55  		if !open {
    56  			t.Error("buffer closed early")
    57  		}
    58  		if exp, act := 2, outTr.Payload.Len(); exp != act {
    59  			t.Errorf("Wrong message length: %v != %v", exp, act)
    60  		} else {
    61  			if exp, act := `one`, string(outTr.Payload.Get(0).Get()); exp != act {
    62  				t.Errorf("Wrong message length: %s != %s", exp, act)
    63  			}
    64  			if exp, act := `two`, string(outTr.Payload.Get(1).Get()); exp != act {
    65  				t.Errorf("Wrong message length: %s != %s", exp, act)
    66  			}
    67  		}
    68  	case <-time.After(time.Second):
    69  		t.Error("Timed out")
    70  	}
    71  	select {
    72  	case outTr.ResponseChan <- response.NewAck():
    73  	case <-time.After(time.Second):
    74  		t.Error("Timed out")
    75  	}
    76  
    77  	buf.CloseAsync()
    78  	if err := buf.WaitForClose(time.Second * 5); err != nil {
    79  		t.Error(err)
    80  	}
    81  }