Skip to content

Commit

Permalink
try to fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gussf committed May 29, 2024
1 parent cbe6697 commit df1b0ea
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 42 deletions.
5 changes: 5 additions & 0 deletions config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,8 @@ feedbackListeners:
maxRetries: 3
database: push
connectionTimeout: 100
rateLimiter:
limit: 100
redis:
host: "localhost:6379"
pwd: ""
6 changes: 5 additions & 1 deletion config/docker_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,8 @@ feedbackListeners:
maxRetries: 3
database: push
connectionTimeout: 100

rateLimiter:
limit: 100
redis:
host: "redis:6379"
pwd: ""
6 changes: 5 additions & 1 deletion config/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,8 @@ feedbackListeners:
maxRetries: 3
database: push
connectionTimeout: 100

rateLimiter:
limit: 100
redis:
host: "redis:6379"
pwd: ""
10 changes: 10 additions & 0 deletions docker-compose-container-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,13 @@ services:
KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: kafka:9092
KAFKA_CLUSTERS_0_METRICS_PORT: 9997
DYNAMIC_CONFIG_ENABLED: 'true'

redis:
image: redis:6.0.9-alpine
ports:
- "6379:6379"
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 5s
retries: 3
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ services:
image: redis:6.0.9-alpine
ports:
- "6379:6379"
hostname: redis
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
Expand Down
9 changes: 5 additions & 4 deletions e2e/apns_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ package e2e
import (
"context"
"fmt"
"os"
"strings"
"testing"
"time"

"github.com/confluentinc/confluent-kafka-go/v2/kafka"
"github.com/google/uuid"
"github.com/sideshow/apns2"
Expand All @@ -14,10 +19,6 @@ import (
"github.com/topfreegames/pusher/pusher"
"github.com/topfreegames/pusher/structs"
"go.uber.org/mock/gomock"
"os"
"strings"
"testing"
"time"
)

const wait = 5 * time.Second
Expand Down
75 changes: 40 additions & 35 deletions extensions/rate_limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,53 @@ package extensions
import (
"context"
"fmt"
"testing"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/google/uuid"
"github.com/redis/go-redis/v9"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

func TestRateLimiter(t *testing.T) {
rl := rateLimiter{
redis: redis.NewClient(&redis.Options{
Addr: "localhost:6379",
}),
limit: 1,
l: logrus.New().WithField("test", "test"),
}
ctx := context.Background()
t.Run("should return not-allowed when rate limit is reached", func(t *testing.T) {
device := uuid.NewString()

allowed := rl.Allow(ctx, device)
assert.True(t, allowed)

// Should not allow due to reaching limit of 1
allowed = rl.Allow(ctx, device)
assert.False(t, allowed)
})

t.Run("should increment current rate if limit is not reached", func(t *testing.T) {
device := uuid.NewString()
currMin := time.Now().Minute()

allowed := rl.Allow(ctx, device)
assert.True(t, allowed)

key := fmt.Sprintf("%s:%d", device, currMin)
actual, err := rl.redis.Get(ctx, key).Result()
if err != nil {
t.Fatal(err)
var rl rateLimiter
var _ = FDescribe("Rate Limiter", func() {
Describe("[Integration]", func() {
rl = rateLimiter{
redis: redis.NewClient(&redis.Options{
Addr: "redis:6379",
}),
limit: 1,
l: logrus.New().WithField("test", "test"),
}
assert.Equal(t, "1", actual)

Describe("Rate limiting", func() {
It("should return not-allowed when rate limit is reached", func() {
ctx := context.Background()
device := uuid.NewString()
allowed := rl.Allow(ctx, device)
Expect(allowed).To(BeTrue())

// Should not allow due to reaching limit of 1
allowed = rl.Allow(ctx, device)
Expect(allowed).To(BeFalse())
})

It("should increment current rate if limit is not reached", func() {
ctx := context.Background()
device := uuid.NewString()
currMin := time.Now().Minute()

allowed := rl.Allow(ctx, device)
Expect(allowed).To(BeTrue())

key := fmt.Sprintf("%s:%d", device, currMin)
actual, err := rl.redis.Get(ctx, key).Result()
Expect(err).ToNot(HaveOccurred())
Expect(actual).To(BeEquivalentTo("1"))
})

})
})
}
})

0 comments on commit df1b0ea

Please sign in to comment.