github.com/ulule/limiter/v3@v3.11.3-0.20230613131926-4cb9c1da4633/drivers/store/redis/store_test.go (about) 1 package redis_test 2 3 import ( 4 "context" 5 "os" 6 "testing" 7 "time" 8 9 libredis "github.com/redis/go-redis/v9" 10 "github.com/stretchr/testify/require" 11 12 "github.com/ulule/limiter/v3" 13 "github.com/ulule/limiter/v3/drivers/store/redis" 14 "github.com/ulule/limiter/v3/drivers/store/tests" 15 ) 16 17 func TestRedisStoreSequentialAccess(t *testing.T) { 18 is := require.New(t) 19 20 client, err := newRedisClient() 21 is.NoError(err) 22 is.NotNil(client) 23 24 store, err := redis.NewStoreWithOptions(client, limiter.StoreOptions{ 25 Prefix: "limiter:redis:sequential-test", 26 }) 27 is.NoError(err) 28 is.NotNil(store) 29 30 tests.TestStoreSequentialAccess(t, store) 31 } 32 33 func TestRedisStoreConcurrentAccess(t *testing.T) { 34 is := require.New(t) 35 36 client, err := newRedisClient() 37 is.NoError(err) 38 is.NotNil(client) 39 40 store, err := redis.NewStoreWithOptions(client, limiter.StoreOptions{ 41 Prefix: "limiter:redis:concurrent-test", 42 }) 43 is.NoError(err) 44 is.NotNil(store) 45 46 tests.TestStoreConcurrentAccess(t, store) 47 } 48 49 func TestRedisClientExpiration(t *testing.T) { 50 is := require.New(t) 51 52 client, err := newRedisClient() 53 is.NoError(err) 54 is.NotNil(client) 55 56 key := "foobar" 57 value := 642 58 keyNoExpiration := -1 * time.Nanosecond 59 keyNotExist := -2 * time.Nanosecond 60 61 ctx := context.Background() 62 delCmd := client.Del(ctx, key) 63 _, err = delCmd.Result() 64 is.NoError(err) 65 66 expCmd := client.PTTL(ctx, key) 67 ttl, err := expCmd.Result() 68 is.NoError(err) 69 is.Equal(keyNotExist, ttl) 70 71 setCmd := client.Set(ctx, key, value, 0) 72 _, err = setCmd.Result() 73 is.NoError(err) 74 75 expCmd = client.PTTL(ctx, key) 76 ttl, err = expCmd.Result() 77 is.NoError(err) 78 is.Equal(keyNoExpiration, ttl) 79 80 setCmd = client.Set(ctx, key, value, time.Second) 81 _, err = setCmd.Result() 82 is.NoError(err) 83 84 time.Sleep(100 * time.Millisecond) 85 86 expCmd = client.PTTL(ctx, key) 87 ttl, err = expCmd.Result() 88 is.NoError(err) 89 90 expected := int64(0) 91 actual := int64(ttl) 92 is.Greater(actual, expected) 93 } 94 95 func BenchmarkRedisStoreSequentialAccess(b *testing.B) { 96 is := require.New(b) 97 98 client, err := newRedisClient() 99 is.NoError(err) 100 is.NotNil(client) 101 102 store, err := redis.NewStoreWithOptions(client, limiter.StoreOptions{ 103 Prefix: "limiter:redis:sequential-benchmark", 104 }) 105 is.NoError(err) 106 is.NotNil(store) 107 108 tests.BenchmarkStoreSequentialAccess(b, store) 109 } 110 111 func BenchmarkRedisStoreConcurrentAccess(b *testing.B) { 112 is := require.New(b) 113 114 client, err := newRedisClient() 115 is.NoError(err) 116 is.NotNil(client) 117 118 store, err := redis.NewStoreWithOptions(client, limiter.StoreOptions{ 119 Prefix: "limiter:redis:concurrent-benchmark", 120 }) 121 is.NoError(err) 122 is.NotNil(store) 123 124 tests.BenchmarkStoreConcurrentAccess(b, store) 125 } 126 127 func newRedisClient() (*libredis.Client, error) { 128 uri := "redis://localhost:6379/0" 129 if os.Getenv("REDIS_URI") != "" { 130 uri = os.Getenv("REDIS_URI") 131 } 132 133 opt, err := libredis.ParseURL(uri) 134 if err != nil { 135 return nil, err 136 } 137 138 client := libredis.NewClient(opt) 139 return client, nil 140 }