2024-06-03 12:49:17: merge [main:72fad31e37b51d75b8c23bb36393fb2548694cec];
This commit is contained in:
1 parent
0a91198eb6
commit
bc88a51ec4
28 files changed
+1355
No files matched your search
@@ -0,0 +1,67 @@
|
||||
package tests_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/txix-open/isp-kit/test"
|
||||
"github.com/txix-open/isp-kit/test/fake"
|
||||
"isp-lock-service/conf"
|
||||
"isp-lock-service/domain"
|
||||
"isp-lock-service/repository"
|
||||
)
|
||||
|
||||
func TestRateLimiter(t *testing.T) {
|
||||
t.Parallel()
|
||||
tst, required := test.New(t)
|
||||
redis := NewRedis(tst)
|
||||
r := repository.NewRateLimiter(tst.Logger(), redis, conf.Redis{Prefix: "test"})
|
||||
|
||||
var (
|
||||
maxRps = 5
|
||||
key = "key"
|
||||
)
|
||||
for i := range maxRps {
|
||||
resp, err := r.Limit(context.Background(), key, maxRps)
|
||||
required.NoError(err)
|
||||
exp := &domain.RateLimiterResponse{
|
||||
Allow: true,
|
||||
Remaining: maxRps - (i + 1),
|
||||
RetryAfter: -1,
|
||||
}
|
||||
required.EqualValues(exp, resp)
|
||||
}
|
||||
|
||||
resp, err := r.Limit(context.Background(), key, maxRps)
|
||||
required.NoError(err)
|
||||
exp := &domain.RateLimiterResponse{
|
||||
Allow: false,
|
||||
Remaining: 0,
|
||||
RetryAfter: resp.RetryAfter,
|
||||
}
|
||||
required.Greater(exp.RetryAfter, time.Duration(0))
|
||||
required.EqualValues(exp, resp)
|
||||
|
||||
time.Sleep(time.Second)
|
||||
resp, err = r.Limit(context.Background(), key, maxRps)
|
||||
required.NoError(err)
|
||||
exp = &domain.RateLimiterResponse{
|
||||
Allow: true,
|
||||
Remaining: maxRps - 1,
|
||||
RetryAfter: -1,
|
||||
}
|
||||
required.EqualValues(exp, resp)
|
||||
|
||||
for range 1000 {
|
||||
key := fake.It[string]()
|
||||
resp, err := r.Limit(context.Background(), key, 1)
|
||||
required.NoError(err)
|
||||
exp := &domain.RateLimiterResponse{
|
||||
Allow: true,
|
||||
Remaining: 0,
|
||||
RetryAfter: -1,
|
||||
}
|
||||
required.EqualValues(exp, resp)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package tests_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"golang.org/x/sync/errgroup"
|
||||
"isp-lock-service/conf"
|
||||
"isp-lock-service/repository"
|
||||
|
||||
"github.com/txix-open/isp-kit/test"
|
||||
)
|
||||
|
||||
func NewRedis(test *test.Test) *redis.Client {
|
||||
redisHost := test.Config().Optional().String("REDIS_HOST", "localhost")
|
||||
redisPort := test.Config().Optional().String("REDIS_PORT", "6379")
|
||||
addr := fmt.Sprintf("%s:%s", redisHost, redisPort)
|
||||
return redis.NewClient(&redis.Options{Addr: addr})
|
||||
}
|
||||
|
||||
func TestOne(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tst, required := test.New(t)
|
||||
rcli := NewRedis(tst)
|
||||
ctx := context.Background()
|
||||
|
||||
r := repository.NewLocker(tst.Logger(), rcli, conf.Redis{Prefix: "testPrefix"})
|
||||
|
||||
// success story
|
||||
key := time.Now().String()
|
||||
l, err := r.Lock(ctx, key, 1)
|
||||
required.NoError(err)
|
||||
|
||||
_, err = r.UnLock(ctx, key, l.LockKey)
|
||||
required.NoError(err)
|
||||
|
||||
// look at wait
|
||||
_, err = r.Lock(ctx, key, 1)
|
||||
required.NoError(err)
|
||||
|
||||
n := time.Now()
|
||||
_, err = r.Lock(ctx, key, 1)
|
||||
required.NoError(err)
|
||||
|
||||
diff := time.Since(n)
|
||||
required.Greater(diff, time.Second)
|
||||
|
||||
// look at error
|
||||
l, err = r.Lock(ctx, key, 10)
|
||||
required.NoError(err)
|
||||
|
||||
_, err = r.Lock(ctx, key, 1)
|
||||
|
||||
required.Error(err)
|
||||
|
||||
if err != nil {
|
||||
required.Error(err, "fail lock")
|
||||
}
|
||||
|
||||
_, err = r.UnLock(ctx, key, l.LockKey)
|
||||
required.NoError(err)
|
||||
}
|
||||
|
||||
func TestConcurrency(t *testing.T) {
|
||||
t.Parallel()
|
||||
tst, required := test.New(t)
|
||||
redis := NewRedis(tst)
|
||||
|
||||
r := repository.NewLocker(tst.Logger(), redis, conf.Redis{Prefix: "testPrefix"})
|
||||
|
||||
group, ctx := errgroup.WithContext(context.Background())
|
||||
group.SetLimit(32)
|
||||
for range 10000 {
|
||||
group.Go(func() error {
|
||||
resp, err := r.Lock(ctx, "key", 5)
|
||||
required.NoError(err)
|
||||
_, err = r.UnLock(ctx, "key", resp.LockKey)
|
||||
required.NoError(err)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
err := group.Wait()
|
||||
required.NoError(err)
|
||||
}
|
||||
Reference in new issue
Block a user