github.com/Jeffail/benthos/v3@v3.65.0/lib/output/inproc_test.go (about)

     1  package output_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/Jeffail/benthos/v3/lib/log"
     8  	"github.com/Jeffail/benthos/v3/lib/manager"
     9  	"github.com/Jeffail/benthos/v3/lib/metrics"
    10  	"github.com/Jeffail/benthos/v3/lib/output"
    11  	"github.com/Jeffail/benthos/v3/lib/types"
    12  
    13  	_ "github.com/Jeffail/benthos/v3/public/components/all"
    14  )
    15  
    16  //------------------------------------------------------------------------------
    17  
    18  func TestInproc(t *testing.T) {
    19  	mgr, err := manager.New(manager.NewConfig(), nil, log.Noop(), metrics.Noop())
    20  	if err != nil {
    21  		t.Fatal(err)
    22  	}
    23  
    24  	if _, err = mgr.GetPipe("foo"); err != types.ErrPipeNotFound {
    25  		t.Errorf("Wrong error returned: %v != %v", err, types.ErrPipeNotFound)
    26  	}
    27  
    28  	conf := output.NewConfig()
    29  	conf.Inproc = "foo"
    30  
    31  	var ip output.Type
    32  	if ip, err = output.NewInproc(conf, mgr, log.Noop(), metrics.Noop()); err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	tinchan := make(chan types.Transaction)
    37  	if err = ip.Consume(tinchan); err != nil {
    38  		t.Fatal(err)
    39  	}
    40  
    41  	select {
    42  	case tinchan <- types.NewTransaction(nil, nil):
    43  	case <-time.After(time.Second):
    44  		t.Error("Timed out")
    45  	}
    46  
    47  	var toutchan <-chan types.Transaction
    48  	if toutchan, err = mgr.GetPipe("foo"); err != nil {
    49  		t.Error(err)
    50  	}
    51  
    52  	select {
    53  	case <-toutchan:
    54  	case <-time.After(time.Second):
    55  		t.Error("Timed out")
    56  	}
    57  
    58  	ip.CloseAsync()
    59  	if err = ip.WaitForClose(time.Second); err != nil {
    60  		t.Error(err)
    61  	}
    62  
    63  	select {
    64  	case _, open := <-toutchan:
    65  		if open {
    66  			t.Error("transaction chan not closed")
    67  		}
    68  	case <-time.After(time.Second):
    69  		t.Error("Timed out")
    70  	}
    71  	if _, err = mgr.GetPipe("foo"); err != types.ErrPipeNotFound {
    72  		t.Errorf("Wrong error returned: %v != %v", err, types.ErrPipeNotFound)
    73  	}
    74  }
    75  
    76  //------------------------------------------------------------------------------