Skip to content

Add robust test case & run it against Redis in CI #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: CI

on:
push:
pull_request:

jobs:
tests:
name: Tests
runs-on: ubuntu-latest

services:
redis:
image: redis:6
ports:
- 6379:6379

steps:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ^1.17

- name: Check out code into the Go module directory
uses: actions/checkout@v4

- name: Get dependencies
run: go get -v -t -d ./...

- name: Build
run: go build -v ./

- name: Build example
run: cd ./_example && go build -v ./

- name: Test
run: go test -v ./...
14 changes: 14 additions & 0 deletions _example/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/go-chi/httprate-redis/_example

go 1.22.5

replace github.com/go-chi/httprate-redis => ../

require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-chi/chi/v5 v5.1.0 // indirect
github.com/go-chi/httprate v0.11.0 // indirect
github.com/go-chi/httprate-redis v0.3.0 // indirect
github.com/redis/go-redis/v9 v9.6.1 // indirect
)
12 changes: 12 additions & 0 deletions _example/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/httprate v0.11.0 h1:GKsQF0qlVSU1Zg/D07tUUnNrYTTkrtpMihwO6myxtzc=
github.com/go-chi/httprate v0.11.0/go.mod h1:TUepLXaz/pCjmCtf/obgOQJ2Sz6rC8fSf5cAt5cnTt0=
github.com/redis/go-redis/v9 v9.6.0 h1:NLck+Rab3AOTHw21CGRpvQpgTrAU4sgdCswqGtlhGRA=
github.com/redis/go-redis/v9 v9.6.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4=
github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
12 changes: 9 additions & 3 deletions _example/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"log"
"net/http"
"time"

Expand All @@ -16,18 +17,23 @@ func main() {

r.Group(func(r chi.Router) {
r.Use(httprate.Limit(
10,
10*time.Second,
5,
time.Minute,
httprate.WithKeyByIP(),
httprateredis.WithRedisLimitCounter(&httprateredis.Config{
Host: "127.0.0.1", Port: 6379,
}),
))

r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("."))
w.Write([]byte("5 req/min\n"))
})
})

log.Printf("Serving at localhost:3333")
log.Println()
log.Printf("Try running:")
log.Printf("curl -v http://localhost:3333")

http.ListenAndServe(":3333", r)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ require (
require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
golang.org/x/sync v0.7.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ github.com/redis/go-redis/v9 v9.3.0 h1:RiVDjmig62jIWp7Kk4XVLs0hzV6pI3PyTnnL0cnn0
github.com/redis/go-redis/v9 v9.3.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/redis/go-redis/v9 v9.6.0 h1:NLck+Rab3AOTHw21CGRpvQpgTrAU4sgdCswqGtlhGRA=
github.com/redis/go-redis/v9 v9.6.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
152 changes: 152 additions & 0 deletions httprateredis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package httprateredis_test

import (
"fmt"
"math/rand"
"testing"
"time"

httprateredis "github.com/go-chi/httprate-redis"
"golang.org/x/sync/errgroup"
)

func TestRedisCounter(t *testing.T) {
limitCounter, err := httprateredis.NewRedisLimitCounter(&httprateredis.Config{
Host: "localhost",
Port: 6379,
MaxIdle: 500,
MaxActive: 500,
DBIndex: 0,
ClientName: "httprateredis_test",
PrefixKey: fmt.Sprintf("httprate:test:%v", rand.Int31n(100000)), // Unique key for each test
})
if err != nil {
t.Fatalf("redis not available: %v", err)
}

limitCounter.Config(1000, time.Minute)

currentWindow := time.Now().UTC().Truncate(time.Minute)
previousWindow := currentWindow.Add(-time.Minute)

type test struct {
name string // In each test do the following:
advanceTime time.Duration // 1. advance time
incrBy int // 2. increase counter
prev int // 3. check previous window counter
curr int // and current window counter
}

tests := []test{
{
name: "t=0m: init",
prev: 0,
curr: 0,
},
{
name: "t=0m: increment 1",
incrBy: 1,
prev: 0,
curr: 1,
},
{
name: "t=0m: increment by 99",
incrBy: 99,
prev: 0,
curr: 100,
},
{
name: "t=1m: move clock by 1m",
advanceTime: time.Minute,
prev: 100,
curr: 0,
},
{
name: "t=1m: increment by 20",
incrBy: 20,
prev: 100,
curr: 20,
},
{
name: "t=1m: increment by 20",
incrBy: 20,
prev: 100,
curr: 40,
},
{
name: "t=2m: move clock by 1m",
advanceTime: time.Minute,
prev: 40,
curr: 0,
},
{
name: "t=2m: incr++",
incrBy: 1,
prev: 40,
curr: 1,
},
{
name: "t=2m: incr+=9",
incrBy: 9,
prev: 40,
curr: 10,
},
{
name: "t=2m: incr+=20",
incrBy: 20,
prev: 40,
curr: 30,
},
{
name: "t=4m: move clock by 2m",
advanceTime: 2 * time.Minute,
prev: 0,
curr: 0,
},
}

concurrentRequests := 1000

for _, tt := range tests {
if tt.advanceTime > 0 {
currentWindow = currentWindow.Add(tt.advanceTime)
previousWindow = previousWindow.Add(tt.advanceTime)
}

if tt.incrBy > 0 {
var g errgroup.Group
for i := 0; i < concurrentRequests; i++ {
i := i
g.Go(func() error {
key := fmt.Sprintf("key:%v", i)
return limitCounter.IncrementBy(key, currentWindow, tt.incrBy)
})
}
if err := g.Wait(); err != nil {
t.Errorf("%s: %v", tt.name, err)
}
}

var g errgroup.Group
for i := 0; i < concurrentRequests; i++ {
i := i
g.Go(func() error {
key := fmt.Sprintf("key:%v", i)
curr, prev, err := limitCounter.Get(key, currentWindow, previousWindow)
if err != nil {
return fmt.Errorf("%q: %w", key, err)
}
if curr != tt.curr {
return fmt.Errorf("%q: unexpected curr = %v, expected %v", key, curr, tt.curr)
}
if prev != tt.prev {
return fmt.Errorf("%q: unexpected prev = %v, expected %v", key, prev, tt.prev)
}
return nil
})
}
if err := g.Wait(); err != nil {
t.Errorf("%s: %v", tt.name, err)
}
}
}
Loading