Tests: add a way to read raw RESP protocol reponses (#9193)

This makes it possible to distinguish between null response and an empty
array (currently the tests infra translates both to an empty string/list)
This commit is contained in:
Oran Agra 2021-07-04 19:43:58 +03:00 committed by GitHub
parent a8518cce95
commit 7103367ad4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 1 deletions

View File

@ -34,13 +34,14 @@ array set ::redis::fd {}
array set ::redis::addr {}
array set ::redis::blocking {}
array set ::redis::deferred {}
array set ::redis::readraw {}
array set ::redis::reconnect {}
array set ::redis::tls {}
array set ::redis::callback {}
array set ::redis::state {} ;# State in non-blocking reply reading
array set ::redis::statestack {} ;# Stack of states, for nested mbulks
proc redis {{server 127.0.0.1} {port 6379} {defer 0} {tls 0} {tlsoptions {}}} {
proc redis {{server 127.0.0.1} {port 6379} {defer 0} {tls 0} {tlsoptions {}} {readraw 0}} {
if {$tls} {
package require tls
::tls::init \
@ -58,6 +59,7 @@ proc redis {{server 127.0.0.1} {port 6379} {defer 0} {tls 0} {tlsoptions {}}} {
set ::redis::addr($id) [list $server $port]
set ::redis::blocking($id) 1
set ::redis::deferred($id) $defer
set ::redis::readraw($id) $readraw
set ::redis::reconnect($id) 0
set ::redis::tls($id) $tls
::redis::redis_reset_state $id
@ -158,6 +160,7 @@ proc ::redis::__method__close {id fd} {
catch {unset ::redis::addr($id)}
catch {unset ::redis::blocking($id)}
catch {unset ::redis::deferred($id)}
catch {unset ::redis::readraw($id)}
catch {unset ::redis::reconnect($id)}
catch {unset ::redis::tls($id)}
catch {unset ::redis::state($id)}
@ -174,6 +177,10 @@ proc ::redis::__method__deferred {id fd val} {
set ::redis::deferred($id) $val
}
proc ::redis::__method__readraw {id fd val} {
set ::redis::readraw($id) $val
}
proc ::redis::redis_write {fd buf} {
puts -nonewline $fd $buf
}
@ -241,6 +248,10 @@ proc ::redis::redis_read_null fd {
}
proc ::redis::redis_read_reply {id fd} {
if {$::redis::readraw($id)} {
return [redis_read_line $fd]
}
set type [read $fd 1]
switch -exact -- $type {
_ {redis_read_null $fd}

View File

@ -102,6 +102,40 @@ start_server {tags {"protocol network"}} {
} {*Protocol error*}
}
unset c
# recover the broken connection
reconnect
r ping
# raw RESP response tests
r readraw 1
test "raw protocol response" {
r srandmember nonexisting_key
} {*-1}
r deferred 1
test "raw protocol response - deferred" {
r srandmember nonexisting_key
r read
} {*-1}
test "raw protocol response - multiline" {
r sadd ss a
assert_equal [r read] {:1}
r srandmember ss 100
assert_equal [r read] {*1}
assert_equal [r read] {$1}
assert_equal [r read] {a}
}
# restore connection settings
r readraw 0
r deferred 0
# check the connection still works
assert_equal [r ping] {PONG}
}
start_server {tags {"regression"}} {