diff --git a/src/scripting.c b/src/scripting.c index 6d0bf2349..75604e4d8 100644 --- a/src/scripting.c +++ b/src/scripting.c @@ -407,9 +407,9 @@ void luaReplyToRedisReply(client *c, lua_State *lua) { lua_pushnil(lua); /* Use nil to start iteration. */ while (lua_next(lua,-2)) { /* Stack now: table, key, value */ - luaReplyToRedisReply(c, lua); /* Return value. */ - lua_pushvalue(lua,-1); /* Dup key before consuming. */ + lua_pushvalue(lua,-2); /* Dup key before consuming. */ luaReplyToRedisReply(c, lua); /* Return key. */ + luaReplyToRedisReply(c, lua); /* Return value. */ /* Stack now: table, key. */ maplen++; } diff --git a/tests/support/redis.tcl b/tests/support/redis.tcl index 9eb5b94e2..8eca2ac32 100644 --- a/tests/support/redis.tcl +++ b/tests/support/redis.tcl @@ -214,20 +214,19 @@ proc ::redis::redis_multi_bulk_read {id fd} { proc ::redis::redis_read_map {id fd} { set count [redis_read_line $fd] if {$count == -1} return {} - set l {} + set d {} set err {} for {set i 0} {$i < $count} {incr i} { if {[catch { - set t {} - lappend t [redis_read_reply $id $fd] ; # key - lappend t [redis_read_reply $id $fd] ; # value - lappend l $t + set k [redis_read_reply $id $fd] ; # key + set v [redis_read_reply $id $fd] ; # value + dict set d $k $v } e] && $err eq {}} { set err $e } } if {$err ne {}} {return -code error $err} - return $l + return $d } proc ::redis::redis_read_line fd { diff --git a/tests/unit/scripting.tcl b/tests/unit/scripting.tcl index 0da1eda9f..6fd152594 100644 --- a/tests/unit/scripting.tcl +++ b/tests/unit/scripting.tcl @@ -563,6 +563,30 @@ start_server {tags {"scripting"}} { } e set e } {*wrong number*} + + test {Script with RESP3 map} { + set expected_dict [dict create field value] + set expected_list [list field value] + + # Sanity test for RESP3 without scripts + r HELLO 3 + r hset hash field value + set res [r hgetall hash] + assert_equal $res $expected_dict + + # Test RESP3 client with script in both RESP2 and RESP3 modes + set res [r eval {redis.setresp(3); return redis.call('hgetall', KEYS[1])} 1 hash] + assert_equal $res $expected_dict + set res [r eval {redis.setresp(2); return redis.call('hgetall', KEYS[1])} 1 hash] + assert_equal $res $expected_list + + # Test RESP2 client with script in both RESP2 and RESP3 modes + r HELLO 2 + set res [r eval {redis.setresp(3); return redis.call('hgetall', KEYS[1])} 1 hash] + assert_equal $res $expected_list + set res [r eval {redis.setresp(2); return redis.call('hgetall', KEYS[1])} 1 hash] + assert_equal $res $expected_list + } } # Start a new server since the last test in this stanza will kill the diff --git a/tests/unit/tracking.tcl b/tests/unit/tracking.tcl index 6f8b46c84..8ed2801e7 100644 --- a/tests/unit/tracking.tcl +++ b/tests/unit/tracking.tcl @@ -132,31 +132,22 @@ start_server {tags {"tracking"}} { test {HELLO 3 reply is correct} { set reply [r HELLO 3] - assert {[lindex $reply 2] eq {proto 3}} + assert_equal [dict get $reply proto] 3 } test {HELLO without protover} { set reply [r HELLO 3] - assert {[lindex $reply 2] eq {proto 3}} + assert_equal [dict get $reply proto] 3 set reply [r HELLO] - assert {[lindex $reply 2] eq {proto 3}} - - set reply [r HELLO] - assert {[lindex $reply 2] eq {proto 3}} + assert_equal [dict get $reply proto] 3 set reply [r HELLO 2] - assert {[lindex $reply 4] eq "proto"} - assert {[lindex $reply 5] eq 2} + assert_equal [dict get $reply proto] 2 set reply [r HELLO] - assert {[lindex $reply 4] eq "proto"} - assert {[lindex $reply 5] eq 2} + assert_equal [dict get $reply proto] 2 - set reply [r HELLO] - assert {[lindex $reply 4] eq "proto"} - assert {[lindex $reply 5] eq 2} - # restore RESP3 for next test r HELLO 3 }