Skip to content

Commit

Permalink
fixed deque + copy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
apoorvyadav1111 committed Nov 13, 2024
1 parent d9d42f0 commit d17b70a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 56 deletions.
23 changes: 8 additions & 15 deletions integration_tests/commands/http/deque_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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"})

Expand Down Expand Up @@ -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"})

Expand Down Expand Up @@ -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"})

Expand Down Expand Up @@ -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"})

Expand Down Expand Up @@ -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"})

Expand Down Expand Up @@ -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"})

Expand Down Expand Up @@ -470,7 +466,6 @@ func TestRPushRPop(t *testing.T) {
}

func TestLRPushLRPop(t *testing.T) {
deqTestInit()
exec := NewHTTPCommandExecutor()
exec.FireCommand(HTTPCommand{Command: "FLUSHDB"})

Expand Down Expand Up @@ -511,7 +506,6 @@ func TestLRPushLRPop(t *testing.T) {
}

func TestLLEN(t *testing.T) {
deqTestInit()
exec := NewHTTPCommandExecutor()
exec.FireCommand(HTTPCommand{Command: "FLUSHDB"})

Expand Down Expand Up @@ -558,7 +552,6 @@ func TestLLEN(t *testing.T) {
}

func TestLPOPCount(t *testing.T) {
deqTestInit()
exec := NewHTTPCommandExecutor()
exec.FireCommand(HTTPCommand{Command: "FLUSHDB"})

Expand Down
18 changes: 16 additions & 2 deletions integration_tests/commands/resp/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resp

import (
"testing"
"time"

"github.com/dicedb/dice/testutils"
"github.com/stretchr/testify/assert"
Expand All @@ -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},
},
}

Expand All @@ -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)
Expand Down
58 changes: 28 additions & 30 deletions integration_tests/commands/resp/deque_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -110,7 +111,7 @@ func TestLPush(t *testing.T) {
}

func TestRPush(t *testing.T) {
deqTestInit()
deqNormalValues, deqEdgeValues := deqTestInit()
conn := getLocalConnection()
defer conn.Close()

Expand Down Expand Up @@ -149,7 +150,7 @@ func TestRPush(t *testing.T) {
}

func TestLPushLPop(t *testing.T) {
deqTestInit()
deqNormalValues, deqEdgeValues := deqTestInit()
conn := getLocalConnection()
defer conn.Close()

Expand Down Expand Up @@ -203,7 +204,7 @@ func TestLPushLPop(t *testing.T) {
}

func TestLPushRPop(t *testing.T) {
deqTestInit()
deqNormalValues, deqEdgeValues := deqTestInit()
conn := getLocalConnection()
defer conn.Close()

Expand Down Expand Up @@ -257,7 +258,7 @@ func TestLPushRPop(t *testing.T) {
}

func TestRPushLPop(t *testing.T) {
deqTestInit()
deqNormalValues, deqEdgeValues := deqTestInit()
conn := getLocalConnection()
defer conn.Close()

Expand Down Expand Up @@ -311,7 +312,7 @@ func TestRPushLPop(t *testing.T) {
}

func TestRPushRPop(t *testing.T) {
deqTestInit()
deqNormalValues, deqEdgeValues := deqTestInit()
conn := getLocalConnection()
defer conn.Close()

Expand Down Expand Up @@ -365,7 +366,6 @@ func TestRPushRPop(t *testing.T) {
}

func TestLRPushLRPop(t *testing.T) {
deqTestInit()
conn := getLocalConnection()
defer conn.Close()

Expand Down Expand Up @@ -404,7 +404,6 @@ func TestLRPushLRPop(t *testing.T) {
}

func TestLLEN(t *testing.T) {
deqTestInit()
conn := getLocalConnection()
defer conn.Close()

Expand Down Expand Up @@ -531,7 +530,6 @@ func TestLRange(t *testing.T) {
}

func TestLPOPCount(t *testing.T) {
deqTestInit()
conn := getLocalConnection()
defer conn.Close()

Expand All @@ -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),
},
},
}
Expand Down
Loading

0 comments on commit d17b70a

Please sign in to comment.