github.com/Jeffail/benthos/v3@v3.65.0/lib/test/integration/nats_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/Jeffail/benthos/v3/internal/integration"
     9  	"github.com/nats-io/nats.go"
    10  	"github.com/ory/dockertest/v3"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  var _ = registerIntegrationTest("nats", func(t *testing.T) {
    16  	t.Parallel()
    17  
    18  	pool, err := dockertest.NewPool("")
    19  	require.NoError(t, err)
    20  
    21  	pool.MaxWait = time.Second * 30
    22  	resource, err := pool.Run("nats", "latest", nil)
    23  	require.NoError(t, err)
    24  	t.Cleanup(func() {
    25  		assert.NoError(t, pool.Purge(resource))
    26  	})
    27  
    28  	resource.Expire(900)
    29  	require.NoError(t, pool.Retry(func() error {
    30  		natsConn, err := nats.Connect(fmt.Sprintf("tcp://localhost:%v", resource.GetPort("4222/tcp")))
    31  		if err != nil {
    32  			return err
    33  		}
    34  		natsConn.Close()
    35  		return nil
    36  	}))
    37  
    38  	template := `
    39  output:
    40    nats:
    41      urls: [ tcp://localhost:$PORT ]
    42      subject: subject-$ID
    43      max_in_flight: $MAX_IN_FLIGHT
    44  
    45  input:
    46    nats:
    47      urls: [ tcp://localhost:$PORT ]
    48      queue: queue-$ID
    49      subject: subject-$ID
    50      prefetch_count: 1048
    51  `
    52  	suite := integration.StreamTests(
    53  		integration.StreamTestOpenClose(),
    54  		// integration.StreamTestMetadata(), TODO
    55  		integration.StreamTestSendBatch(10),
    56  		integration.StreamTestStreamParallel(500),
    57  		integration.StreamTestStreamParallelLossy(500),
    58  	)
    59  	suite.Run(
    60  		t, template,
    61  		integration.StreamTestOptSleepAfterInput(100*time.Millisecond),
    62  		integration.StreamTestOptSleepAfterOutput(100*time.Millisecond),
    63  		integration.StreamTestOptPort(resource.GetPort("4222/tcp")),
    64  	)
    65  	t.Run("with max in flight", func(t *testing.T) {
    66  		t.Parallel()
    67  		suite.Run(
    68  			t, template,
    69  			integration.StreamTestOptSleepAfterInput(100*time.Millisecond),
    70  			integration.StreamTestOptSleepAfterOutput(100*time.Millisecond),
    71  			integration.StreamTestOptPort(resource.GetPort("4222/tcp")),
    72  			integration.StreamTestOptMaxInFlight(10),
    73  		)
    74  	})
    75  })