Skip to content

Commit 34dacf1

Browse files
authored
Fix monitor on go 1.19 (#2908)
* Fix monitor on go 1.19 * Remove exmaple tests when go 1.19 * Fix typo * Fix typo * Skip exmaple test * Skip exmaple test * Guard Peek call with mutex for thread safety
1 parent a923df1 commit 34dacf1

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
strategy:
1717
fail-fast: false
1818
matrix:
19-
go-version: [1.20.x, 1.21.x]
19+
go-version: [1.19.x, 1.20.x, 1.21.x]
2020

2121
services:
2222
redis:

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
GO_MOD_DIRS := $(shell find . -type f -name 'go.mod' -exec dirname {} \; | sort)
22

33
test: testdeps
4+
$(eval GO_VERSION := $(shell go version | cut -d " " -f 3 | cut -d. -f2))
45
set -e; for dir in $(GO_MOD_DIRS); do \
6+
if echo "$${dir}" | grep -q "./example" && [ "$(GO_VERSION)" = "19" ]; then \
7+
echo "Skipping go test in $${dir} due to Go version 1.19 and dir contains ./example"; \
8+
continue; \
9+
fi; \
510
echo "go test in $${dir}"; \
611
(cd "$${dir}" && \
712
go mod tidy -compat=1.18 && \

command.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -5454,9 +5454,12 @@ func (cmd *MonitorCmd) readMonitor(rd *proto.Reader, cancel context.CancelFunc)
54545454
for {
54555455
cmd.mu.Lock()
54565456
st := cmd.status
5457+
pk, _ := rd.Peek(1)
54575458
cmd.mu.Unlock()
5458-
if pk, _ := rd.Peek(1); len(pk) != 0 && st == monitorStatusStart {
5459+
if len(pk) != 0 && st == monitorStatusStart {
5460+
cmd.mu.Lock()
54595461
line, err := rd.ReadString()
5462+
cmd.mu.Unlock()
54605463
if err != nil {
54615464
return err
54625465
}

monitor_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package redis_test
22

33
import (
44
"context"
5+
"strings"
56
"time"
67

8+
"testing"
9+
710
. "github.com/bsm/ginkgo/v2"
811
. "github.com/bsm/gomega"
912

@@ -46,3 +49,52 @@ var _ = Describe("Monitor command", Label("monitor"), func() {
4649
Expect(lst[3]).To(ContainSubstring(`"set" "bap" "8"`))
4750
})
4851
})
52+
53+
func TestMonitorCommand(t *testing.T) {
54+
ctx := context.TODO()
55+
client := redis.NewClient(&redis.Options{Addr: ":6379"})
56+
if err := client.FlushDB(ctx).Err(); err != nil {
57+
t.Fatalf("FlushDB failed: %v", err)
58+
}
59+
60+
defer func() {
61+
if err := client.Close(); err != nil {
62+
t.Fatalf("Close failed: %v", err)
63+
}
64+
}()
65+
66+
ress := make(chan string, 10) // Buffer to prevent blocking
67+
client1 := redis.NewClient(&redis.Options{Addr: ":6379"}) // Adjust the Addr field as necessary
68+
mn := client1.Monitor(ctx, ress)
69+
mn.Start()
70+
// Wait for the Redis server to be in monitoring mode.
71+
time.Sleep(100 * time.Millisecond)
72+
client.Set(ctx, "foo", "bar", 0)
73+
client.Set(ctx, "bar", "baz", 0)
74+
client.Set(ctx, "bap", 8, 0)
75+
client.Get(ctx, "bap")
76+
mn.Stop()
77+
var lst []string
78+
for i := 0; i < 5; i++ {
79+
s := <-ress
80+
lst = append(lst, s)
81+
}
82+
83+
// Assertions
84+
if !containsSubstring(lst[0], "OK") {
85+
t.Errorf("Expected lst[0] to contain 'OK', got %s", lst[0])
86+
}
87+
if !containsSubstring(lst[1], `"set" "foo" "bar"`) {
88+
t.Errorf(`Expected lst[1] to contain '"set" "foo" "bar"', got %s`, lst[1])
89+
}
90+
if !containsSubstring(lst[2], `"set" "bar" "baz"`) {
91+
t.Errorf(`Expected lst[2] to contain '"set" "bar" "baz"', got %s`, lst[2])
92+
}
93+
if !containsSubstring(lst[3], `"set" "bap" "8"`) {
94+
t.Errorf(`Expected lst[3] to contain '"set" "bap" "8"', got %s`, lst[3])
95+
}
96+
}
97+
98+
func containsSubstring(s, substr string) bool {
99+
return strings.Contains(s, substr)
100+
}

0 commit comments

Comments
 (0)