Skip to content

Commit

Permalink
DiceDB#406 Fix MSET command consistency issue with Redis (DiceDB#594)
Browse files Browse the repository at this point in the history
  • Loading branch information
ar1fKhan authored Sep 18, 2024
1 parent 9670a5a commit 6ac24e1
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
51 changes: 51 additions & 0 deletions integration_tests/commands/mset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,54 @@ func TestMset(t *testing.T) {
})
}
}
func TestMSETInconsistency(t *testing.T) {

conn := getLocalConnection()
defer conn.Close()

testCases := []struct {
name string
commands []string
expected []interface{}
}{
{
name: "MSET with one key-value pair",
commands: []string{"MSET k1 v1", "GET k1"},
expected: []interface{}{"OK", "v1"},
},
{
name: "MSET with multiple key-value pairs",
commands: []string{"MSET k1 v1 k2 v2", "GET k1", "GET k2"},
expected: []interface{}{"OK", "v1", "v2"},
},
{
name: "MSET with odd number of arguments",
commands: []string{"MSET k1 v1 k2"},
expected: []interface{}{"ERR wrong number of arguments for 'mset' command"},
},
{
name: "MSET with multiple key-value pairs",
commands: []string{"MSET k1 v1 k2 v2", "GET k1", "GET k2"},
expected: []interface{}{"OK", "v1", "v2"},
},
{
name: "MSET with integers arguments",
commands: []string{"MSET key1 12345 key2 67890", "GET key1", "GET key2"},
expected: []interface{}{"OK", int64(12345), int64(67890)},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// deleteTestKeys([]string{"k1", "k2"}, store)
FireCommand(conn, "DEL k1")
FireCommand(conn, "DEL k1")

for i, cmd := range tc.commands {
result := FireCommand(conn, cmd)
assert.DeepEqual(t, tc.expected[i], result)
}
})
}

}
11 changes: 10 additions & 1 deletion internal/eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,16 @@ func evalMSET(args []string, store *dstore.Store) []byte {
for i := 0; i < len(args); i += 2 {
key, value := args[i], args[i+1]
oType, oEnc := deduceTypeEncoding(value)
insertMap[key] = store.NewObj(value, exDurationMs, oType, oEnc)
var storedValue interface{}
switch oEnc {
case object.ObjEncodingInt:
storedValue, _ = strconv.ParseInt(value, 10, 64)
case object.ObjEncodingEmbStr, object.ObjEncodingRaw:
storedValue = value
default:
return clientio.Encode(fmt.Errorf("ERR unsupported encoding: %d", oEnc), false)
}
insertMap[key] = store.NewObj(storedValue, exDurationMs, oType, oEnc)
}

store.PutAll(insertMap)
Expand Down
7 changes: 7 additions & 0 deletions internal/eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2586,3 +2586,10 @@ func testEvalCOMMAND(t *testing.T, store *dstore.Store) {

runEvalTests(t, tests, evalCommand, store)
}
func TestMSETConsistency(t *testing.T) {
store := dstore.NewStore(nil)
evalMSET([]string{"KEY", "VAL", "KEY2", "VAL2"}, store)

assert.Equal(t, "VAL", store.Get("KEY").Value)
assert.Equal(t, "VAL2", store.Get("KEY2").Value)
}

0 comments on commit 6ac24e1

Please sign in to comment.