github.com/rudderlabs/rudder-go-kit@v0.30.0/throttling/util_test.go (about)

     1  package throttling
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/go-redis/redis/v8"
     7  	"github.com/ory/dockertest/v3"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	redisdocker "github.com/rudderlabs/rudder-go-kit/testhelper/docker/resource/redis"
    11  )
    12  
    13  type tester interface {
    14  	Helper()
    15  	Log(...interface{})
    16  	Errorf(format string, args ...interface{})
    17  	Fatalf(format string, args ...any)
    18  	Failed() bool
    19  	FailNow()
    20  	Cleanup(f func())
    21  }
    22  
    23  type testCase struct {
    24  	rate,
    25  	window int64
    26  }
    27  
    28  func newLimiter(t tester, opts ...Option) *Limiter {
    29  	t.Helper()
    30  	l, err := New(opts...)
    31  	require.NoError(t, err)
    32  	return l
    33  }
    34  
    35  func bootstrapRedis(ctx context.Context, t tester, pool *dockertest.Pool) *redis.Client {
    36  	t.Helper()
    37  	redisContainer, err := redisdocker.Setup(ctx, pool, t)
    38  	require.NoError(t, err)
    39  
    40  	rc := redis.NewClient(&redis.Options{
    41  		Network: "tcp",
    42  		Addr:    redisContainer.Addr,
    43  	})
    44  	t.Cleanup(func() { _ = rc.Close() })
    45  
    46  	pong, err := rc.Ping(ctx).Result()
    47  	if err != nil {
    48  		t.Fatalf("Could not ping Redis cluster: %v", err)
    49  	}
    50  
    51  	require.Equal(t, "PONG", pong)
    52  
    53  	return rc
    54  }