Skip to content

Commit

Permalink
Changes in naming Conventions for DiceDbMock
Browse files Browse the repository at this point in the history
  • Loading branch information
rishavvajpayee committed Oct 3, 2024
1 parent 03e1b2d commit fd969d2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions internal/middleware/ratelimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"log/slog"
"net/http"
"server/internal/db"
mock "server/internal/tests/dbmocks" // Import the mock DB for testing
mock "server/internal/tests/dbmocks"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -85,7 +85,7 @@ func RateLimiter(client *db.DiceDB, next http.Handler, limit int64, window float
})
}

func MockRateLimiter(client *mock.InMemoryDiceDB, next http.Handler, limit int64, window float64) http.Handler {
func MockRateLimiter(client *mock.DiceDBMock, next http.Handler, limit int64, window float64) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Handle CORS for requests
origin := r.Header.Get("Origin")
Expand Down
14 changes: 7 additions & 7 deletions internal/tests/dbmocks/mock_dicedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import (
"time"
)

type InMemoryDiceDB struct {
type DiceDBMock struct {
data map[string]string
mutex sync.Mutex
}

func NewInMemoryDiceDB() *InMemoryDiceDB {
return &InMemoryDiceDB{
func NewDiceDBMock() *DiceDBMock {
return &DiceDBMock{
data: make(map[string]string),
}
}

func (db *InMemoryDiceDB) Get(ctx context.Context, key string) (string, error) {
func (db *DiceDBMock) Get(ctx context.Context, key string) (string, error) {
db.mutex.Lock()
defer db.mutex.Unlock()

Expand All @@ -29,15 +29,15 @@ func (db *InMemoryDiceDB) Get(ctx context.Context, key string) (string, error) {
return val, nil
}

func (db *InMemoryDiceDB) Set(ctx context.Context, key, value string, expiration time.Duration) error {
func (db *DiceDBMock) Set(ctx context.Context, key, value string, expiration time.Duration) error {
db.mutex.Lock()
defer db.mutex.Unlock()

db.data[key] = value
return nil
}

func (db *InMemoryDiceDB) Incr(ctx context.Context, key string) (int64, error) {
func (db *DiceDBMock) Incr(ctx context.Context, key string) (int64, error) {
db.mutex.Lock()
defer db.mutex.Unlock()

Expand All @@ -54,6 +54,6 @@ func (db *InMemoryDiceDB) Incr(ctx context.Context, key string) (int64, error) {
return count, nil
}

func (db *InMemoryDiceDB) Expire(ctx context.Context, key string, expiration time.Duration) error {
func (db *DiceDBMock) Expire(ctx context.Context, key string, expiration time.Duration) error {
return nil
}
2 changes: 1 addition & 1 deletion pkg/util/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func MockHandler(w http.ResponseWriter, r *http.Request) {
}

func SetupRateLimiter(limit int64, window float64) (*httptest.ResponseRecorder, *http.Request, http.Handler) {
mockClient := db.NewInMemoryDiceDB()
mockClient := db.NewDiceDBMock()

r := httptest.NewRequest("GET", "/cli/somecommand", http.NoBody)
w := httptest.NewRecorder()
Expand Down

0 comments on commit fd969d2

Please sign in to comment.