]>
Commit | Line | Data |
---|---|---|
1 | start_server {tags {"cli"}} { | |
2 | proc open_cli {} { | |
3 | set ::env(TERM) dumb | |
4 | set fd [open [format "|src/redis-cli -p %d -n 9" [srv port]] "r+"] | |
5 | fconfigure $fd -buffering none | |
6 | fconfigure $fd -blocking false | |
7 | fconfigure $fd -translation binary | |
8 | assert_equal "redis> " [read_cli $fd] | |
9 | set _ $fd | |
10 | } | |
11 | ||
12 | proc close_cli {fd} { | |
13 | close $fd | |
14 | } | |
15 | ||
16 | proc read_cli {fd} { | |
17 | set buf [read $fd] | |
18 | while {[string length $buf] == 0} { | |
19 | # wait some time and try again | |
20 | after 10 | |
21 | set buf [read $fd] | |
22 | } | |
23 | set _ $buf | |
24 | } | |
25 | ||
26 | proc write_cli {fd buf} { | |
27 | puts $fd $buf | |
28 | flush $fd | |
29 | } | |
30 | ||
31 | proc run_command {fd cmd} { | |
32 | write_cli $fd $cmd | |
33 | set lines [split [read_cli $fd] "\n"] | |
34 | assert_equal "redis> " [lindex $lines end] | |
35 | join [lrange $lines 0 end-1] "\n" | |
36 | } | |
37 | ||
38 | proc test_interactive_cli {name code} { | |
39 | set fd [open_cli] | |
40 | test "Interactive CLI: $name" $code | |
41 | close_cli $fd | |
42 | } | |
43 | ||
44 | test_interactive_cli "INFO response should be printed raw" { | |
45 | set lines [split [run_command $fd info] "\n"] | |
46 | foreach line $lines { | |
47 | assert [regexp {^[a-z0-9_]+:[a-z0-9_]+} $line] | |
48 | } | |
49 | } | |
50 | ||
51 | test_interactive_cli "Status reply" { | |
52 | assert_equal "OK" [run_command $fd "set key foo"] | |
53 | } | |
54 | ||
55 | test_interactive_cli "Integer reply" { | |
56 | assert_equal "(integer) 1" [run_command $fd "incr counter"] | |
57 | } | |
58 | ||
59 | test_interactive_cli "Bulk reply" { | |
60 | r set key foo | |
61 | assert_equal "\"foo\"" [run_command $fd "get key"] | |
62 | } | |
63 | ||
64 | test_interactive_cli "Multi-bulk reply" { | |
65 | r rpush list foo | |
66 | r rpush list bar | |
67 | assert_equal "1. \"foo\"\n2. \"bar\"" [run_command $fd "lrange list 0 -1"] | |
68 | } | |
69 | } |