Skip to content

Fixed outdated testcases and all compiler warning issues #1662

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 6 additions & 6 deletions internal/eval/bloom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func TestBloomFilter(t *testing.T) {
store := dstore.NewStore(nil, nil)
store := dstore.NewStore(nil, nil, 0)
// This test only contains some basic checks for all the bloom filter
// operations like BFRESERVE, BFADD, BFEXISTS. It assumes that the
// functions called in the main function are working correctly and
Expand All @@ -26,7 +26,7 @@ func TestBloomFilter(t *testing.T) {
var args []string // empty args
resp := evalBFRESERVE(args, store)
assert.Nil(t, resp.Result)
assert.Error(t, resp.Error, "ERR wrong number of arguments for 'bf.reserve' command")
assert.Error(t, resp.Error, "wrong number of arguments for 'bf.reserve' command")

// BF.RESERVE bf 0.01 10000
args = append(args, "bf", "0.01", "10000") // Add key, error rate and capacity
Expand All @@ -37,13 +37,13 @@ func TestBloomFilter(t *testing.T) {
// BF.RESERVE bf1
args = []string{"bf1"}
resp = evalBFRESERVE(args, store)
assert.Error(t, resp.Error, "ERR wrong number of arguments for 'bf.reserve' command")
assert.Error(t, resp.Error, "wrong number of arguments for 'bf.reserve' command")
assert.Nil(t, resp.Result)

// BF.ADD with wrong arguments, must return non-nil error and nil result
args = []string{"bf"}
resp = evalBFADD(args, store)
assert.EqualError(t, resp.Error, "ERR wrong number of arguments for 'bf.add' command")
assert.EqualError(t, resp.Error, "wrong number of arguments for 'BF.ADD' command")
assert.Nil(t, resp.Result)

// BF.ADD bf hello, must return 1 and nil error
Expand All @@ -70,7 +70,7 @@ func TestBloomFilter(t *testing.T) {
// BF.EXISTS arity test
args = []string{"bf"}
resp = evalBFEXISTS(args, store)
assert.EqualError(t, resp.Error, "ERR wrong number of arguments for 'bf.exists' command")
assert.EqualError(t, resp.Error, "wrong number of arguments for 'BF.EXISTS' command")
assert.Nil(t, resp.Result)

args = []string{"bf", "hello"} // BF.EXISTS bf hello
Expand All @@ -96,7 +96,7 @@ func TestBloomFilter(t *testing.T) {
}

func TestGetOrCreateBloomFilter(t *testing.T) {
store := dstore.NewStore(nil, nil)
store := dstore.NewStore(nil, nil, 0)
// Create a key and default opts
key := "bf"
opts := defaultBloomOpts()
Expand Down
2 changes: 1 addition & 1 deletion internal/eval/countminsketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestCountMinSketch(t *testing.T) {
store := dstore.NewStore(nil, nil)
store := dstore.NewStore(nil, nil, 0)

testCMSInitByDim(t, store)
testCMSInitByProb(t, store)
Expand Down
75 changes: 75 additions & 0 deletions internal/eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,78 @@ func evalSLEEP(args []string, store *dstore.Store) []byte {
time.Sleep(time.Duration(durationSec) * time.Second)
return RespOK
}

func evalPING(args []string, store *dstore.Store) []byte {
// PING command returns PONG if no argument is provided, otherwise it returns the argument
if len(args) == 0 {
return Encode("PONG", false)
}
return Encode(args[0], false)
}

func evalECHO(args []string, store *dstore.Store) []byte {
if len(args) == 1 {
return diceerrors.NewErrArity("ECHO")
}
return Encode(args[0], false)
}

func evalSET(args []string, store *dstore.Store) *EvalResponse {
return nil
}

func evalGETEX(args []string, store *dstore.Store) *EvalResponse {
return nil
}

func evalGETDEL(args []string, store *dstore.Store) *EvalResponse {
return nil
}

func evalGET(args []string, store *dstore.Store) *EvalResponse {
return nil
}

func evalEXPIRE(args []string, store *dstore.Store) *EvalResponse {
return nil
}

func evalEXPIRETIME(args []string, store *dstore.Store) *EvalResponse {
return nil
}

func evalEXPIREAT(args []string, store *dstore.Store) *EvalResponse {
return nil
}

func evalTTL(args []string, store *dstore.Store) *EvalResponse {
return nil
}

func evalDEL(args []string, store *dstore.Store) *EvalResponse {
return nil
}

func evalTYPE(args []string, store *dstore.Store) *EvalResponse {
return nil
}

func evalSETEX(args []string, store *dstore.Store) *EvalResponse {
return nil
}

func evalINCR(args []string, store *dstore.Store) *EvalResponse {
return nil
}

func evalINCRBY(args []string, store *dstore.Store) *EvalResponse {
return nil
}

func evalDECR(args []string, store *dstore.Store) *EvalResponse {
return nil
}

func evalDECRBY(args []string, store *dstore.Store) *EvalResponse {
return nil
}
Comment on lines +146 to +219
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Create a registration mechanism for command handlers.

All the eval functions are currently flagged as unused by static analysis. A common pattern for command handlers is to register them in a map or command table, so they can be looked up dynamically.

Consider implementing a command registration mechanism to connect these handlers to their respective commands:

// commandHandlers maps command names to their handler functions
var commandHandlers = map[string]func([]string, *dstore.Store) interface{}{
    "PING": evalPING,
    "ECHO": evalECHO,
    // Add other commands as they are implemented
}

// GetCommandHandler returns the handler function for a given command
func GetCommandHandler(command string) (func([]string, *dstore.Store) interface{}, bool) {
    handler, exists := commandHandlers[command]
    return handler, exists
}

This would solve the "unused function" warnings while providing a structured way to look up command handlers.

🧰 Tools
🪛 golangci-lint (1.64.8)

146-146: func evalPING is unused

(unused)


146-146: evalPING - store is unused

(unparam)


154-154: func evalECHO is unused

(unused)


154-154: evalECHO - store is unused

(unparam)


161-161: func evalSET is unused

(unused)


165-165: func evalGETEX is unused

(unused)


169-169: func evalGETDEL is unused

(unused)


173-173: func evalGET is unused

(unused)


177-177: func evalEXPIRE is unused

(unused)


181-181: func evalEXPIRETIME is unused

(unused)


185-185: func evalEXPIREAT is unused

(unused)


189-189: func evalTTL is unused

(unused)

43 changes: 21 additions & 22 deletions internal/eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func setupTest(store *dstore.Store) *dstore.Store {
}

func TestEval(t *testing.T) {
store := dstore.NewStore(nil, nil)
store := dstore.NewStore(nil, nil, 0)

testEvalECHO(t, store)
testEvalHELLO(t, store)
Expand Down Expand Up @@ -81,7 +81,6 @@ func TestEval(t *testing.T) {
testEvalEXPIRE(t, store)
testEvalEXPIRETIME(t, store)
testEvalEXPIREAT(t, store)
testEvalGETSET(t, store)
testEvalHSET(t, store)
testEvalHMSET(t, store)
testEvalHKEYS(t, store)
Expand Down Expand Up @@ -1490,7 +1489,7 @@ func testEvalJSONOBJLEN(t *testing.T, store *dstore.Store) {

func BenchmarkEvalJSONOBJLEN(b *testing.B) {
sizes := []int{0, 10, 100, 1000, 10000, 100000} // Various sizes of JSON objects
store := dstore.NewStore(nil, nil)
store := dstore.NewStore(nil, nil, 0)

for _, size := range sizes {
b.Run(fmt.Sprintf("JSONObjectSize_%d", size), func(b *testing.B) {
Expand Down Expand Up @@ -4348,7 +4347,7 @@ func runEvalTestsMultiShard(t *testing.T, tests map[string]evalMultiShardTestCas
}

func BenchmarkEvalHSET(b *testing.B) {
store := dstore.NewStore(nil, nil)
store := dstore.NewStore(nil, nil, 0)
for i := 0; i < b.N; i++ {
evalHSET([]string{"KEY", fmt.Sprintf("FIELD_%d", i), fmt.Sprintf("VALUE_%d", i)}, store)
}
Expand Down Expand Up @@ -4641,7 +4640,7 @@ func testEvalHKEYS(t *testing.T, store *dstore.Store) {
}

func BenchmarkEvalHKEYS(b *testing.B) {
store := dstore.NewStore(nil, nil)
store := dstore.NewStore(nil, nil, 0)

for i := 0; i < b.N; i++ {
evalHSET([]string{"KEY", fmt.Sprintf("FIELD_%d", i), fmt.Sprintf("VALUE_%d", i)}, store)
Expand All @@ -4653,7 +4652,7 @@ func BenchmarkEvalHKEYS(b *testing.B) {
}

func BenchmarkEvalPFCOUNT(b *testing.B) {
store := *dstore.NewStore(nil, nil)
store := *dstore.NewStore(nil, nil, 0)

// Helper function to create and insert HLL objects
createAndInsertHLL := func(key string, items []string) {
Expand Down Expand Up @@ -4999,7 +4998,7 @@ func testEvalHLEN(t *testing.T, store *dstore.Store) {

func BenchmarkEvalHLEN(b *testing.B) {
sizes := []int{0, 10, 100, 1000, 10000, 100000}
store := dstore.NewStore(nil, nil)
store := dstore.NewStore(nil, nil, 0)

for _, size := range sizes {
b.Run(fmt.Sprintf("HashSize_%d", size), func(b *testing.B) {
Expand Down Expand Up @@ -5296,7 +5295,7 @@ func testEvalTYPE(t *testing.T, store *dstore.Store) {
}

func BenchmarkEvalTYPE(b *testing.B) {
store := dstore.NewStore(nil, nil)
store := dstore.NewStore(nil, nil, 0)

// Define different types of objects to benchmark
objectTypes := map[string]func(){
Expand Down Expand Up @@ -5631,7 +5630,7 @@ func testEvalJSONOBJKEYS(t *testing.T, store *dstore.Store) {

func BenchmarkEvalJSONOBJKEYS(b *testing.B) {
sizes := []int{0, 10, 100, 1000, 10000, 100000} // Various sizes of JSON objects
store := dstore.NewStore(nil, nil)
store := dstore.NewStore(nil, nil, 0)

for _, size := range sizes {
b.Run(fmt.Sprintf("JSONObjectSize_%d", size), func(b *testing.B) {
Expand Down Expand Up @@ -5889,7 +5888,7 @@ func testEvalGETRANGE(t *testing.T, store *dstore.Store) {
}

func BenchmarkEvalGETRANGE(b *testing.B) {
store := dstore.NewStore(nil, nil)
store := dstore.NewStore(nil, nil, 0)
store.Put("BENCHMARK_KEY", store.NewObj("Hello World", maxExDuration, object.ObjTypeString))

inputs := []struct {
Expand All @@ -5914,7 +5913,7 @@ func BenchmarkEvalGETRANGE(b *testing.B) {
}

func BenchmarkEvalHSETNX(b *testing.B) {
store := dstore.NewStore(nil, nil)
store := dstore.NewStore(nil, nil, 0)
for i := 0; i < b.N; i++ {
evalHSETNX([]string{"KEY", fmt.Sprintf("FIELD_%d", i/2), fmt.Sprintf("VALUE_%d", i)}, store)
}
Expand Down Expand Up @@ -6018,7 +6017,7 @@ func testEvalHSETNX(t *testing.T, store *dstore.Store) {
}

func BenchmarkEvalHINCRBY(b *testing.B) {
store := dstore.NewStore(nil, nil)
store := dstore.NewStore(nil, nil, 0)

// creating new fields
for i := 0; i < b.N; i++ {
Expand Down Expand Up @@ -6270,7 +6269,7 @@ func testEvalSETEX(t *testing.T, store *dstore.Store) {
}

func BenchmarkEvalSETEX(b *testing.B) {
store := dstore.NewStore(nil, nil)
store := dstore.NewStore(nil, nil, 0)

b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand Down Expand Up @@ -6428,7 +6427,7 @@ func testEvalINCRBYFLOAT(t *testing.T, store *dstore.Store) {
}

func BenchmarkEvalINCRBYFLOAT(b *testing.B) {
store := dstore.NewStore(nil, nil)
store := dstore.NewStore(nil, nil, 0)
store.Put("key1", store.NewObj("1", maxExDuration, object.ObjTypeString))
store.Put("key2", store.NewObj("1.2", maxExDuration, object.ObjTypeString))

Expand Down Expand Up @@ -6743,7 +6742,7 @@ func testEvalAPPEND(t *testing.T, store *dstore.Store) {
}

func BenchmarkEvalAPPEND(b *testing.B) {
store := dstore.NewStore(nil, nil)
store := dstore.NewStore(nil, nil, 0)
for i := 0; i < b.N; i++ {
evalAPPEND([]string{"key", fmt.Sprintf("val_%d", i)}, store)
}
Expand Down Expand Up @@ -7471,7 +7470,7 @@ func BenchmarkEvalZPOPMIN(b *testing.B) {
},
}

store := dstore.NewStore(nil, nil)
store := dstore.NewStore(nil, nil, 0)

for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
Expand Down Expand Up @@ -7637,7 +7636,7 @@ func testEvalZREM(t *testing.T, store *dstore.Store) {
}

func BenchmarkEvalZRANK(b *testing.B) {
store := dstore.NewStore(nil, nil)
store := dstore.NewStore(nil, nil, 0)

// Set up initial sorted set
evalZADD([]string{"myzset", "1", "member1", "2", "member2", "3", "member3"}, store)
Expand Down Expand Up @@ -7919,7 +7918,7 @@ func testEvalHINCRBYFLOAT(t *testing.T, store *dstore.Store) {
}

func BenchmarkEvalHINCRBYFLOAT(b *testing.B) {
store := dstore.NewStore(nil, nil)
store := dstore.NewStore(nil, nil, 0)

// Setting initial fields with some values
store.Put("key1", store.NewObj(HashMap{"field1": "1.0", "field2": "1.2"}, maxExDuration, object.ObjTypeSSMap))
Expand Down Expand Up @@ -8420,7 +8419,7 @@ func testEvalJSONSTRAPPEND(t *testing.T, store *dstore.Store) {
}

func BenchmarkEvalJSONSTRAPPEND(b *testing.B) {
store := dstore.NewStore(nil, nil)
store := dstore.NewStore(nil, nil, 0)

// Setup a sample JSON document
key := "doc1"
Expand Down Expand Up @@ -8544,7 +8543,7 @@ func BenchmarkEvalZPOPMAX(b *testing.B) {
},
}

store := dstore.NewStore(nil, nil)
store := dstore.NewStore(nil, nil, 0)

for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
Expand All @@ -8560,7 +8559,7 @@ func BenchmarkEvalZPOPMAX(b *testing.B) {
}
}
func BenchmarkZCOUNT(b *testing.B) {
store := dstore.NewStore(nil, nil)
store := dstore.NewStore(nil, nil, 0)

// Populate the sorted set with some members for basic benchmarks
evalZADD([]string{"key", "10", "member1", "20", "member2", "30", "member3"}, store)
Expand Down Expand Up @@ -8589,7 +8588,7 @@ func BenchmarkZCOUNT(b *testing.B) {
// Benchmark for edge cases
b.Run("Edge Case ZCOUNT", func(b *testing.B) {
// Reset the store and set up members
store = dstore.NewStore(nil, nil)
store = dstore.NewStore(nil, nil, 0)
evalZADD([]string{"key", "5", "member1", "15", "member2", "25", "member3"}, store)

b.ResetTimer()
Expand Down
14 changes: 7 additions & 7 deletions internal/eval/hmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestHashMapBuilder(t *testing.T) {
assert.Equal(t, "value2", *val2, "Expected value2 for key2")

keyValuePairs = []string{"key1", "value1", "key2"}
hmap, numSet, err = hashMapBuilder(keyValuePairs, nil)
_, numSet, err = hashMapBuilder(keyValuePairs, nil)
assert.NotNil(t, err, "Expected error for odd number of key-value pairs")
assert.Equal(t, int64(-1), numSet, "Expected -1 for number of keys set when error occurs")
}
Expand All @@ -62,18 +62,18 @@ func TestHashMapIncrementValue(t *testing.T) {
assert.Equal(t, int64(15), val, "Expected value to be incremented to 15")

hmap.Set("field2", "notAnInt")
val, err = hmap.incrementValue("field2", 1)
_, err = hmap.incrementValue("field2", 1)
assert.NotNil(t, err, "Expected error when incrementing a non-integer value")
assert.Equal(t, errors.HashValueNotIntegerErr, err.Error(), "Expected hash value not integer error")

hmap.Set("field3", strconv.FormatInt(math.MaxInt64, 10))
val, err = hmap.incrementValue("field3", 1)
_, err = hmap.incrementValue("field3", 1)
assert.NotNil(t, err, "Expected error when integer overflow occurs")
assert.Equal(t, errors.IncrDecrOverflowErr, err.Error(), "Expected increment overflow error")
}

func TestGetValueFromHashMap(t *testing.T) {
store := store.NewStore(nil, nil)
store := store.NewStore(nil, nil, 0)
key := "key1"
field := "field1"
value := "value1"
Expand Down Expand Up @@ -117,17 +117,17 @@ func TestHashMapIncrementFloatValue(t *testing.T) {
assert.Equal(t, "10", val, "Expected value to be incremented to 10")

hmap.Set("field2", "notAFloat")
val, err = hmap.incrementFloatValue("field2", 1.0)
_, err = hmap.incrementFloatValue("field2", 1.0)
assert.NotNil(t, err, "Expected error when incrementing a non-float value")
assert.Equal(t, errors.IntOrFloatErr, err.Error(), "Expected int or float error")

inf := math.MaxFloat64

val, err = hmap.incrementFloatValue("field1", inf+float64(1e308))
_, err = hmap.incrementFloatValue("field1", inf+float64(1e308))
assert.NotNil(t, err, "Expected error when incrementing a overflowing value")
assert.Equal(t, errors.IncrDecrOverflowErr, err.Error(), "Expected overflow to be detected")

val, err = hmap.incrementFloatValue("field1", -inf-float64(1e308))
_, err = hmap.incrementFloatValue("field1", -inf-float64(1e308))
assert.NotNil(t, err, "Expected error when incrementing a overflowing value")
assert.Equal(t, errors.IncrDecrOverflowErr, err.Error(), "Expected overflow to be detected")
}
2 changes: 1 addition & 1 deletion internal/eval/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func TestMain(m *testing.M) {
store := dstore.NewStore(nil, nil)
store := dstore.NewStore(nil, nil, 0)
store.ResetStore()

exitCode := m.Run()
Expand Down
Loading