github.com/Jeffail/benthos/v3@v3.65.0/lib/test/integration/mqtt_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  	mqtt "github.com/eclipse/paho.mqtt.golang"
    10  	"github.com/ory/dockertest/v3"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  var _ = registerIntegrationTest("mqtt", 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("ncarlier/mqtt", "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  		inConf := mqtt.NewClientOptions().SetClientID("UNIT_TEST")
    31  		inConf = inConf.AddBroker(fmt.Sprintf("tcp://localhost:%v", resource.GetPort("1883/tcp")))
    32  
    33  		mIn := mqtt.NewClient(inConf)
    34  		tok := mIn.Connect()
    35  		tok.Wait()
    36  		if cErr := tok.Error(); cErr != nil {
    37  			return cErr
    38  		}
    39  		mIn.Disconnect(0)
    40  		return nil
    41  	}))
    42  
    43  	template := `
    44  output:
    45    mqtt:
    46      urls: [ tcp://localhost:$PORT ]
    47      qos: 1
    48      topic: topic-$ID
    49      client_id: client-output-$ID
    50      dynamic_client_id_suffix: "$VAR1"
    51      max_in_flight: $MAX_IN_FLIGHT
    52  
    53  input:
    54    mqtt:
    55      urls: [ tcp://localhost:$PORT ]
    56      topics: [ topic-$ID ]
    57      client_id: client-input-$ID
    58      dynamic_client_id_suffix: "$VAR1"
    59      clean_session: false
    60  `
    61  	suite := integration.StreamTests(
    62  		integration.StreamTestOpenClose(),
    63  		// integration.StreamTestMetadata(), TODO
    64  		integration.StreamTestSendBatch(10),
    65  		integration.StreamTestStreamParallel(1000),
    66  		// integration.StreamTestStreamParallelLossy(1000),
    67  	)
    68  	suite.Run(
    69  		t, template,
    70  		integration.StreamTestOptSleepAfterInput(100*time.Millisecond),
    71  		integration.StreamTestOptSleepAfterOutput(100*time.Millisecond),
    72  		integration.StreamTestOptPort(resource.GetPort("1883/tcp")),
    73  	)
    74  	t.Run("with max in flight", func(t *testing.T) {
    75  		t.Parallel()
    76  		suite.Run(
    77  			t, template,
    78  			integration.StreamTestOptSleepAfterInput(100*time.Millisecond),
    79  			integration.StreamTestOptSleepAfterOutput(100*time.Millisecond),
    80  			integration.StreamTestOptPort(resource.GetPort("1883/tcp")),
    81  			integration.StreamTestOptMaxInFlight(10),
    82  		)
    83  	})
    84  	t.Run("with generated suffix", func(t *testing.T) {
    85  		t.Parallel()
    86  		suite.Run(
    87  			t, template,
    88  			integration.StreamTestOptSleepAfterInput(100*time.Millisecond),
    89  			integration.StreamTestOptSleepAfterOutput(100*time.Millisecond),
    90  			integration.StreamTestOptPort(resource.GetPort("1883/tcp")),
    91  			integration.StreamTestOptMaxInFlight(10),
    92  			integration.StreamTestOptVarOne("nanoid"),
    93  		)
    94  	})
    95  })