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

     1  package cache
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/Jeffail/benthos/v3/internal/integration"
     9  	"github.com/Jeffail/benthos/v3/lib/cache"
    10  	"github.com/Jeffail/benthos/v3/lib/log"
    11  	"github.com/Jeffail/benthos/v3/lib/metrics"
    12  	"github.com/ory/dockertest/v3"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  var _ = registerIntegrationTest("redis", func(t *testing.T) {
    18  	t.Parallel()
    19  
    20  	pool, err := dockertest.NewPool("")
    21  	require.NoError(t, err)
    22  
    23  	pool.MaxWait = time.Second * 30
    24  
    25  	resource, err := pool.Run("redis", "latest", nil)
    26  	require.NoError(t, err)
    27  	t.Cleanup(func() {
    28  		assert.NoError(t, pool.Purge(resource))
    29  	})
    30  
    31  	resource.Expire(900)
    32  	require.NoError(t, pool.Retry(func() error {
    33  		url := fmt.Sprintf("tcp://localhost:%v/1", resource.GetPort("6379/tcp"))
    34  		conf := cache.NewConfig()
    35  		conf.Redis.URL = url
    36  
    37  		r, cErr := cache.NewRedis(conf, nil, log.Noop(), metrics.Noop())
    38  		if cErr != nil {
    39  			return cErr
    40  		}
    41  		cErr = r.Set("benthos_test_redis_connect", []byte("foo bar"))
    42  		return cErr
    43  	}))
    44  
    45  	template := `
    46  cache_resources:
    47    - label: testcache
    48      redis:
    49        url: tcp://localhost:$PORT/1
    50        prefix: $ID
    51  `
    52  	suite := integration.CacheTests(
    53  		integration.CacheTestOpenClose(),
    54  		integration.CacheTestMissingKey(),
    55  		integration.CacheTestDoubleAdd(),
    56  		integration.CacheTestDelete(),
    57  		integration.CacheTestGetAndSet(50),
    58  	)
    59  	suite.Run(
    60  		t, template,
    61  		integration.CacheTestOptPort(resource.GetPort("6379/tcp")),
    62  	)
    63  })