Skip to content

Commit b50ef1c

Browse files
committed
feat: support info stats
1 parent 1a990bb commit b50ef1c

File tree

2 files changed

+55
-19
lines changed

2 files changed

+55
-19
lines changed

cmd_info.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,42 @@ package miniredis
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/alicebob/miniredis/v2/server"
78
)
89

10+
const (
11+
clientsSectionName = "clients"
12+
clientsSectionContent = "# Clients\nconnected_clients:%d\r\n"
13+
14+
statsSectionName = "stats"
15+
statsSectionContent = "# Stats\ntotal_connections_received:%d\r\ntotal_commands_processed:%d\r\n"
16+
)
17+
918
// Command 'INFO' from https://redis.io/commands/info/
1019
func (m *Miniredis) cmdInfo(c *server.Peer, cmd string, args []string) {
1120
if !m.isValidCMD(c, cmd, args, between(0, 1)) {
1221
return
1322
}
23+
var result string
24+
if len(args) == 0 {
25+
result = fmt.Sprintf(clientsSectionContent, m.Server().ClientsLen()) + fmt.Sprintf(statsSectionContent, m.Server().TotalConnections(), m.Server().TotalCommands())
26+
c.WriteBulk(result)
27+
return
28+
}
1429

1530
withTx(m, c, func(c *server.Peer, ctx *connCtx) {
16-
const (
17-
clientsSectionName = "clients"
18-
clientsSectionContent = "# Clients\nconnected_clients:%d\r\n"
19-
)
20-
21-
var result string
22-
23-
for _, key := range args {
24-
if key != clientsSectionName {
25-
setDirty(c)
26-
c.WriteError(fmt.Sprintf("section (%s) is not supported", key))
27-
return
28-
}
31+
switch section := strings.ToLower(args[0]); section {
32+
case clientsSectionName:
33+
result = fmt.Sprintf(clientsSectionContent, m.Server().ClientsLen())
34+
case statsSectionName:
35+
result = fmt.Sprintf(statsSectionContent, m.Server().TotalConnections(), m.Server().TotalCommands())
36+
default:
37+
setDirty(c)
38+
c.WriteError(fmt.Sprintf("section (%s) is not supported", section))
39+
return
2940
}
30-
result = fmt.Sprintf(clientsSectionContent, m.Server().ClientsLen())
31-
3241
c.WriteBulk(result)
3342
})
3443
}

cmd_info_test.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,24 @@ import (
88
)
99

1010
func TestMiniredis_cmdInfo(t *testing.T) {
11-
s, c := runWithClient(t)
12-
1311
t.Run("Invalid section name", func(t *testing.T) {
12+
_, c := runWithClient(t)
1413
mustDo(t, c,
1514
"INFO", "invalid_or_unsupported_section_name",
1615
proto.Error("section (invalid_or_unsupported_section_name) is not supported"),
1716
)
1817
})
1918

2019
t.Run("No section name in args", func(t *testing.T) {
20+
_, c := runWithClient(t)
2121
mustDo(t, c,
2222
"INFO",
23-
proto.String("# Clients\nconnected_clients:1\r\n"),
23+
proto.String("# Clients\nconnected_clients:1\r\n# Stats\ntotal_connections_received:1\r\ntotal_commands_processed:1\r\n"),
2424
)
2525
})
2626

27-
t.Run("Success", func(t *testing.T) {
27+
t.Run("Success for clients section", func(t *testing.T) {
28+
s, c := runWithClient(t)
2829
mustDo(t, c,
2930
"INFO", "clients",
3031
proto.String("# Clients\nconnected_clients:1\r\n"),
@@ -48,4 +49,30 @@ func TestMiniredis_cmdInfo(t *testing.T) {
4849
proto.String("# Clients\nconnected_clients:2\r\n"),
4950
)
5051
})
52+
53+
t.Run("Success for stats section", func(t *testing.T) {
54+
s, c := runWithClient(t)
55+
mustDo(t, c,
56+
"INFO", "stats",
57+
proto.String("# Stats\ntotal_connections_received:1\r\ntotal_commands_processed:1\r\n"),
58+
)
59+
60+
c2, err := proto.Dial(s.Addr())
61+
ok(t, err)
62+
mustDo(t, c2,
63+
"INFO", "stats",
64+
proto.String("# Stats\ntotal_connections_received:2\r\ntotal_commands_processed:2\r\n"),
65+
)
66+
c2.Close()
67+
68+
time.Sleep(10 * time.Millisecond)
69+
70+
c3, err := proto.Dial(s.Addr())
71+
ok(t, err)
72+
defer c3.Close()
73+
mustDo(t, c3,
74+
"INFO", "stats",
75+
proto.String("# Stats\ntotal_connections_received:3\r\ntotal_commands_processed:3\r\n"),
76+
)
77+
})
5178
}

0 commit comments

Comments
 (0)