From d17b70aa3bd448205d89e871c5944e81d5ee74f6 Mon Sep 17 00:00:00 2001 From: apoorvyadav1111 Date: Wed, 13 Nov 2024 21:18:38 +0530 Subject: [PATCH] fixed deque + copy tests --- integration_tests/commands/http/deque_test.go | 23 +++----- integration_tests/commands/resp/copy_test.go | 18 +++++- integration_tests/commands/resp/deque_test.go | 58 +++++++++---------- .../commands/websocket/deque_test.go | 17 +++--- 4 files changed, 60 insertions(+), 56 deletions(-) diff --git a/integration_tests/commands/http/deque_test.go b/integration_tests/commands/http/deque_test.go index 96f48031f..db315a559 100644 --- a/integration_tests/commands/http/deque_test.go +++ b/integration_tests/commands/http/deque_test.go @@ -12,11 +12,6 @@ import ( var deqRandGenerator *rand.Rand var deqRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_!@#$%^&*()-=+[]\\;':,.<>/?~.|") -var ( - deqNormalValues []string - deqEdgeValues []string -) - func deqRandStr(n int) string { b := make([]rune, n) for i := range b { @@ -25,7 +20,7 @@ func deqRandStr(n int) string { return string(b) } -func deqTestInit() { +func deqTestInit() (deqNormalValues, deqEdgeValues []string) { randSeed := time.Now().UnixNano() deqRandGenerator = rand.New(rand.NewSource(randSeed)) fmt.Printf("rand seed: %v", randSeed) @@ -66,10 +61,11 @@ func deqTestInit() { "-9223372036854775808", // min 64 bit int "9223372036854775807", // max 64 bit int } + return deqNormalValues, deqEdgeValues } func TestLPush(t *testing.T) { - deqTestInit() + deqNormalValues, deqEdgeValues := deqTestInit() exec := NewHTTPCommandExecutor() exec.FireCommand(HTTPCommand{Command: "FLUSHDB"}) @@ -112,7 +108,7 @@ func TestLPush(t *testing.T) { } func TestRPush(t *testing.T) { - deqTestInit() + deqNormalValues, deqEdgeValues := deqTestInit() exec := NewHTTPCommandExecutor() exec.FireCommand(HTTPCommand{Command: "FLUSHDB"}) @@ -155,7 +151,7 @@ func TestRPush(t *testing.T) { } func TestLPushLPop(t *testing.T) { - deqTestInit() + deqNormalValues, deqEdgeValues := deqTestInit() exec := NewHTTPCommandExecutor() exec.FireCommand(HTTPCommand{Command: "FLUSHDB"}) @@ -233,7 +229,7 @@ func TestLPushLPop(t *testing.T) { } func TestLPushRPop(t *testing.T) { - deqTestInit() + deqNormalValues, deqEdgeValues := deqTestInit() exec := NewHTTPCommandExecutor() exec.FireCommand(HTTPCommand{Command: "FLUSHDB"}) @@ -312,7 +308,7 @@ func TestLPushRPop(t *testing.T) { } func TestRPushLPop(t *testing.T) { - deqTestInit() + deqNormalValues, deqEdgeValues := deqTestInit() exec := NewHTTPCommandExecutor() exec.FireCommand(HTTPCommand{Command: "FLUSHDB"}) @@ -391,7 +387,7 @@ func TestRPushLPop(t *testing.T) { } func TestRPushRPop(t *testing.T) { - deqTestInit() + deqNormalValues, deqEdgeValues := deqTestInit() exec := NewHTTPCommandExecutor() exec.FireCommand(HTTPCommand{Command: "FLUSHDB"}) @@ -470,7 +466,6 @@ func TestRPushRPop(t *testing.T) { } func TestLRPushLRPop(t *testing.T) { - deqTestInit() exec := NewHTTPCommandExecutor() exec.FireCommand(HTTPCommand{Command: "FLUSHDB"}) @@ -511,7 +506,6 @@ func TestLRPushLRPop(t *testing.T) { } func TestLLEN(t *testing.T) { - deqTestInit() exec := NewHTTPCommandExecutor() exec.FireCommand(HTTPCommand{Command: "FLUSHDB"}) @@ -558,7 +552,6 @@ func TestLLEN(t *testing.T) { } func TestLPOPCount(t *testing.T) { - deqTestInit() exec := NewHTTPCommandExecutor() exec.FireCommand(HTTPCommand{Command: "FLUSHDB"}) diff --git a/integration_tests/commands/resp/copy_test.go b/integration_tests/commands/resp/copy_test.go index c1fd243b1..95f8817ae 100644 --- a/integration_tests/commands/resp/copy_test.go +++ b/integration_tests/commands/resp/copy_test.go @@ -2,6 +2,7 @@ package resp import ( "testing" + "time" "github.com/dicedb/dice/testutils" "github.com/stretchr/testify/assert" @@ -17,51 +18,61 @@ func TestCopy(t *testing.T) { name string commands []string expected []interface{} + delays []time.Duration }{ { name: "COPY when source key doesn't exist", commands: []string{"COPY k1 k2"}, expected: []interface{}{int64(0)}, + delays: []time.Duration{0}, }, { name: "COPY with no REPLACE", commands: []string{"SET k1 v1", "COPY k1 k2", "GET k1", "GET k2"}, expected: []interface{}{"OK", int64(1), "v1", "v1"}, + delays: []time.Duration{0, 0, 0, 0}, }, { name: "COPY with REPLACE", commands: []string{"SET k1 v1", "SET k2 v2", "GET k2", "COPY k1 k2 REPLACE", "GET k2"}, expected: []interface{}{"OK", "OK", "v2", int64(1), "v1"}, + delays: []time.Duration{0, 0, 0, 0, 0}, }, { name: "COPY with JSON integer", commands: []string{"JSON.SET k1 $ 2", "COPY k1 k2", "JSON.GET k2"}, expected: []interface{}{"OK", int64(1), "2"}, + delays: []time.Duration{0, 0, 0}, }, { name: "COPY with JSON boolean", commands: []string{"JSON.SET k1 $ true", "COPY k1 k2", "JSON.GET k2"}, expected: []interface{}{"OK", int64(1), "true"}, + delays: []time.Duration{0, 0, 0}, }, { name: "COPY with JSON array", commands: []string{`JSON.SET k1 $ [1,2,3]`, "COPY k1 k2", "JSON.GET k2"}, expected: []interface{}{"OK", int64(1), `[1,2,3]`}, + delays: []time.Duration{0, 0, 0}, }, { name: "COPY with JSON simple JSON", commands: []string{`JSON.SET k1 $ ` + simpleJSON, "COPY k1 k2", "JSON.GET k2"}, expected: []interface{}{"OK", int64(1), simpleJSON}, + delays: []time.Duration{0, 0, 0}, }, { name: "COPY with no expiry", commands: []string{"SET k1 v1", "COPY k1 k2", "TTL k1", "TTL k2"}, expected: []interface{}{"OK", int64(1), int64(-1), int64(-1)}, + delays: []time.Duration{0, 0, 0, 0}, }, { name: "COPY with expiry making sure copy expires", - commands: []string{"SET k1 v1 EX 5", "COPY k1 k2", "GET k1", "GET k2", "SLEEP 7", "GET k1", "GET k2"}, - expected: []interface{}{"OK", int64(1), "v1", "v1", "OK", "(nil)", "(nil)"}, + commands: []string{"SET k1 v1 EX 3", "COPY k1 k2", "GET k1", "GET k2", "GET k1", "GET k2"}, + expected: []interface{}{"OK", int64(1), "v1", "v1", "(nil)", "(nil)"}, + delays: []time.Duration{0, 0, 0, 0, 3 * time.Second, 0}, }, } @@ -75,6 +86,9 @@ func TestCopy(t *testing.T) { FireCommand(conn, "DEL k1") FireCommand(conn, "DEL k2") for i, cmd := range tc.commands { + if tc.delays[i] > 0 { + time.Sleep(tc.delays[i]) + } result := FireCommand(conn, cmd) resStr, resOk := result.(string) expStr, expOk := tc.expected[i].(string) diff --git a/integration_tests/commands/resp/deque_test.go b/integration_tests/commands/resp/deque_test.go index f9d13ea3d..82268398c 100644 --- a/integration_tests/commands/resp/deque_test.go +++ b/integration_tests/commands/resp/deque_test.go @@ -27,7 +27,7 @@ func deqRandStr(n int) string { return string(b) } -func deqTestInit() { +func deqTestInit() (deqNormalValues, deqEdgeValues []string) { randSeed := time.Now().UnixNano() deqRandGenerator = rand.New(rand.NewSource(randSeed)) fmt.Printf("rand seed: %v", randSeed) @@ -68,10 +68,11 @@ func deqTestInit() { "-9223372036854775808", // min 64 bit int "9223372036854775807", // max 64 bit int } + return deqNormalValues, deqEdgeValues } func TestLPush(t *testing.T) { - deqTestInit() + deqNormalValues, deqEdgeValues := deqTestInit() conn := getLocalConnection() defer conn.Close() @@ -110,7 +111,7 @@ func TestLPush(t *testing.T) { } func TestRPush(t *testing.T) { - deqTestInit() + deqNormalValues, deqEdgeValues := deqTestInit() conn := getLocalConnection() defer conn.Close() @@ -149,7 +150,7 @@ func TestRPush(t *testing.T) { } func TestLPushLPop(t *testing.T) { - deqTestInit() + deqNormalValues, deqEdgeValues := deqTestInit() conn := getLocalConnection() defer conn.Close() @@ -203,7 +204,7 @@ func TestLPushLPop(t *testing.T) { } func TestLPushRPop(t *testing.T) { - deqTestInit() + deqNormalValues, deqEdgeValues := deqTestInit() conn := getLocalConnection() defer conn.Close() @@ -257,7 +258,7 @@ func TestLPushRPop(t *testing.T) { } func TestRPushLPop(t *testing.T) { - deqTestInit() + deqNormalValues, deqEdgeValues := deqTestInit() conn := getLocalConnection() defer conn.Close() @@ -311,7 +312,7 @@ func TestRPushLPop(t *testing.T) { } func TestRPushRPop(t *testing.T) { - deqTestInit() + deqNormalValues, deqEdgeValues := deqTestInit() conn := getLocalConnection() defer conn.Close() @@ -365,7 +366,6 @@ func TestRPushRPop(t *testing.T) { } func TestLRPushLRPop(t *testing.T) { - deqTestInit() conn := getLocalConnection() defer conn.Close() @@ -404,7 +404,6 @@ func TestLRPushLRPop(t *testing.T) { } func TestLLEN(t *testing.T) { - deqTestInit() conn := getLocalConnection() defer conn.Close() @@ -531,7 +530,6 @@ func TestLRange(t *testing.T) { } func TestLPOPCount(t *testing.T) { - deqTestInit() conn := getLocalConnection() defer conn.Close() @@ -543,28 +541,28 @@ func TestLPOPCount(t *testing.T) { { name: "LPOP with count argument - valid, invalid, and edge cases", cmds: []string{ - "RPUSH k v1 v2 v3 v4", - "LPOP k 2", - "LLEN k", - "LPOP k 0", - "LLEN k", - "LPOP k 5", - "LLEN k", - "LPOP k -1", - "LPOP k abc", - "LLEN k", + "RPUSH k v1 v2 v3 v4", + "LPOP k 2", + "LLEN k", + "LPOP k 0", + "LLEN k", + "LPOP k 5", + "LLEN k", + "LPOP k -1", + "LPOP k abc", + "LLEN k", }, expect: []any{ - int64(4), - []interface{}{"v1", "v2"}, - int64(2), - "(nil)", - int64(2), - []interface{}{"v3", "v4"}, - int64(0), - "ERR value is not an integer or out of range", - "ERR value is not an integer or a float", - int64(0), + int64(4), + []interface{}{"v1", "v2"}, + int64(2), + "(nil)", + int64(2), + []interface{}{"v3", "v4"}, + int64(0), + "ERR value is not an integer or out of range", + "ERR value is not an integer or a float", + int64(0), }, }, } diff --git a/integration_tests/commands/websocket/deque_test.go b/integration_tests/commands/websocket/deque_test.go index 1aa8e84e6..f3916c9d3 100644 --- a/integration_tests/commands/websocket/deque_test.go +++ b/integration_tests/commands/websocket/deque_test.go @@ -26,7 +26,7 @@ func deqRandStr(n int) string { return string(b) } -func deqTestInit() { +func deqTestInit() (deqNormalValues, deqEdgeValues []string) { randSeed := time.Now().UnixNano() deqRandGenerator = rand.New(rand.NewSource(randSeed)) fmt.Printf("rand seed: %v", randSeed) @@ -67,10 +67,11 @@ func deqTestInit() { "-9223372036854775808", // min 64 bit int "9223372036854775807", // max 64 bit int } + return deqNormalValues, deqEdgeValues } func TestLPush(t *testing.T) { - deqTestInit() + deqNormalValues, deqEdgeValues := deqTestInit() exec := NewWebsocketCommandExecutor() testCases := []struct { @@ -111,7 +112,7 @@ func TestLPush(t *testing.T) { } func TestRPush(t *testing.T) { - deqTestInit() + deqNormalValues, deqEdgeValues := deqTestInit() exec := NewWebsocketCommandExecutor() testCases := []struct { @@ -152,7 +153,7 @@ func TestRPush(t *testing.T) { } func TestLPushLPop(t *testing.T) { - deqTestInit() + deqNormalValues, deqEdgeValues := deqTestInit() exec := NewWebsocketCommandExecutor() getPops := func(values []string) []string { @@ -208,7 +209,7 @@ func TestLPushLPop(t *testing.T) { } func TestLPushRPop(t *testing.T) { - deqTestInit() + deqNormalValues, deqEdgeValues := deqTestInit() exec := NewWebsocketCommandExecutor() getPops := func(values []string) []string { @@ -264,7 +265,7 @@ func TestLPushRPop(t *testing.T) { } func TestRPushLPop(t *testing.T) { - deqTestInit() + deqNormalValues, deqEdgeValues := deqTestInit() exec := NewWebsocketCommandExecutor() getPops := func(values []string) []string { @@ -320,7 +321,7 @@ func TestRPushLPop(t *testing.T) { } func TestRPushRPop(t *testing.T) { - deqTestInit() + deqNormalValues, deqEdgeValues := deqTestInit() exec := NewWebsocketCommandExecutor() getPops := func(values []string) []string { @@ -376,7 +377,6 @@ func TestRPushRPop(t *testing.T) { } func TestLRPushLRPop(t *testing.T) { - deqTestInit() exec := NewWebsocketCommandExecutor() testCases := []struct { @@ -417,7 +417,6 @@ func TestLRPushLRPop(t *testing.T) { } func TestLLEN(t *testing.T) { - deqTestInit() exec := NewWebsocketCommandExecutor() testCases := []struct {