From 74aa560456d2edb5a5b965d9e35ed524264d637a Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Thu, 15 Feb 2024 13:53:36 +0200 Subject: [PATCH 1/7] Fix monitor on go 1.19 --- .github/workflows/build.yml | 2 +- command.go | 2 ++ monitor_test.go | 52 +++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e78801685..4061bbdff 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -16,7 +16,7 @@ jobs: strategy: fail-fast: false matrix: - go-version: [1.20.x, 1.21.x] + go-version: [1.19.x, 1.20.x, 1.21.x] services: redis: diff --git a/command.go b/command.go index 9fb9a8310..d5451a52c 100644 --- a/command.go +++ b/command.go @@ -5456,7 +5456,9 @@ func (cmd *MonitorCmd) readMonitor(rd *proto.Reader, cancel context.CancelFunc) st := cmd.status cmd.mu.Unlock() if pk, _ := rd.Peek(1); len(pk) != 0 && st == monitorStatusStart { + cmd.mu.Lock() line, err := rd.ReadString() + cmd.mu.Unlock() if err != nil { return err } diff --git a/monitor_test.go b/monitor_test.go index 1bc82ecae..91a733407 100644 --- a/monitor_test.go +++ b/monitor_test.go @@ -2,8 +2,11 @@ package redis_test import ( "context" + "strings" "time" + "testing" + . "github.com/bsm/ginkgo/v2" . "github.com/bsm/gomega" @@ -46,3 +49,52 @@ var _ = Describe("Monitor command", Label("monitor"), func() { Expect(lst[3]).To(ContainSubstring(`"set" "bap" "8"`)) }) }) + +func TestMonitorCommand(t *testing.T) { + ctx := context.TODO() + client := redis.NewClient(&redis.Options{Addr: ":6379"}) + if err := client.FlushDB(ctx).Err(); err != nil { + t.Fatalf("FlushDB failed: %v", err) + } + + defer func() { + if err := client.Close(); err != nil { + t.Fatalf("Close failed: %v", err) + } + }() + + ress := make(chan string, 10) // Buffer to prevent blocking + client1 := redis.NewClient(&redis.Options{Addr: ":6379"}) // Adjust the Addr field as necessary + mn := client1.Monitor(ctx, ress) + mn.Start() + // Wait for the Redis server to be in monitoring mode. + time.Sleep(100 * time.Millisecond) + client.Set(ctx, "foo", "bar", 0) + client.Set(ctx, "bar", "baz", 0) + client.Set(ctx, "bap", 8, 0) + client.Get(ctx, "bap") + mn.Stop() + var lst []string + for i := 0; i < 5; i++ { + s := <-ress + lst = append(lst, s) + } + + // Assertions + if !containsSubstring(lst[0], "OK") { + t.Errorf("Expected lst[0] to contain 'OK', got %s", lst[0]) + } + if !containsSubstring(lst[1], `"set" "foo" "bar"`) { + t.Errorf(`Expected lst[1] to contain '"set" "foo" "bar"', got %s`, lst[1]) + } + if !containsSubstring(lst[2], `"set" "bar" "baz"`) { + t.Errorf(`Expected lst[2] to contain '"set" "bar" "baz"', got %s`, lst[2]) + } + if !containsSubstring(lst[3], `"set" "bap" "8"`) { + t.Errorf(`Expected lst[3] to contain '"set" "bap" "8"', got %s`, lst[3]) + } +} + +func containsSubstring(s, substr string) bool { + return strings.Contains(s, substr) +} From 1c42fb9d16cc1512736c852cd6cf6009f4a76552 Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Sun, 18 Feb 2024 10:16:11 +0200 Subject: [PATCH 2/7] Remove exmaple tests when go 1.19 --- Makefile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Makefile b/Makefile index dc2fe780a..6a2fb83d8 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,11 @@ GO_MOD_DIRS := $(shell find . -type f -name 'go.mod' -exec dirname {} \; | sort) test: testdeps + $(eval GO_VERSION := $(shell go version | cut -d " " -f 3 | cut -d. -f2)); set -e; for dir in $(GO_MOD_DIRS); do \ + if [ "$(GO_VERSION)" = "19" && ["$${dir}" == "./example/"]]; then \ + continue; \ + fi; \ echo "go test in $${dir}"; \ (cd "$${dir}" && \ go mod tidy -compat=1.18 && \ From 6a57686258d240da3308612ba8d8b35c12e66283 Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Sun, 18 Feb 2024 11:19:25 +0200 Subject: [PATCH 3/7] Fix typo --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 6a2fb83d8..1e37d4d86 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ GO_MOD_DIRS := $(shell find . -type f -name 'go.mod' -exec dirname {} \; | sort) test: testdeps - $(eval GO_VERSION := $(shell go version | cut -d " " -f 3 | cut -d. -f2)); + $(eval GO_VERSION := $(shell go version | cut -d " " -f 3 | cut -d. -f2)) set -e; for dir in $(GO_MOD_DIRS); do \ if [ "$(GO_VERSION)" = "19" && ["$${dir}" == "./example/"]]; then \ continue; \ From 9d782e618c836d5f4b9db9ff13f46382e4cfa132 Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Sun, 18 Feb 2024 11:48:15 +0200 Subject: [PATCH 4/7] Fix typo --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 1e37d4d86..764b09e9c 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ GO_MOD_DIRS := $(shell find . -type f -name 'go.mod' -exec dirname {} \; | sort) test: testdeps $(eval GO_VERSION := $(shell go version | cut -d " " -f 3 | cut -d. -f2)) set -e; for dir in $(GO_MOD_DIRS); do \ - if [ "$(GO_VERSION)" = "19" && ["$${dir}" == "./example/"]]; then \ + if [ "$(GO_VERSION)" = "19" && $(findstring "./example/", "$${dir}")]; then \ continue; \ fi; \ echo "go test in $${dir}"; \ From aa74a3987d498f1b059ac245706761ada9edcc9e Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Sun, 18 Feb 2024 17:59:49 +0200 Subject: [PATCH 5/7] Skip exmaple test --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 764b09e9c..86f1fd33e 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,8 @@ GO_MOD_DIRS := $(shell find . -type f -name 'go.mod' -exec dirname {} \; | sort) test: testdeps $(eval GO_VERSION := $(shell go version | cut -d " " -f 3 | cut -d. -f2)) set -e; for dir in $(GO_MOD_DIRS); do \ - if [ "$(GO_VERSION)" = "19" && $(findstring "./example/", "$${dir}")]; then \ + if [ "$(GO_VERSION)" = "19" ] && [[ "$${dir}" == *"./example"* ]]; then \ + echo "Skipping go test in $${dir} due to Go version 1.19 and dir contains ./example"; \ continue; \ fi; \ echo "go test in $${dir}"; \ From e52a9bd264ad356b737ecef4d2f0e9830eb06f5c Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Sun, 18 Feb 2024 18:27:19 +0200 Subject: [PATCH 6/7] Skip exmaple test --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 86f1fd33e..ea5321f29 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ GO_MOD_DIRS := $(shell find . -type f -name 'go.mod' -exec dirname {} \; | sort) test: testdeps $(eval GO_VERSION := $(shell go version | cut -d " " -f 3 | cut -d. -f2)) set -e; for dir in $(GO_MOD_DIRS); do \ - if [ "$(GO_VERSION)" = "19" ] && [[ "$${dir}" == *"./example"* ]]; then \ + if echo "$${dir}" | grep -q "./example" && [ "$(GO_VERSION)" = "19" ]; then \ echo "Skipping go test in $${dir} due to Go version 1.19 and dir contains ./example"; \ continue; \ fi; \ From d9733fbdbd219522907ac9d8671e9ed55f344f80 Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Thu, 21 Mar 2024 09:54:26 +0200 Subject: [PATCH 7/7] Guard Peek call with mutex for thread safety --- command.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/command.go b/command.go index d5451a52c..c6cd9db6d 100644 --- a/command.go +++ b/command.go @@ -5454,8 +5454,9 @@ func (cmd *MonitorCmd) readMonitor(rd *proto.Reader, cancel context.CancelFunc) for { cmd.mu.Lock() st := cmd.status + pk, _ := rd.Peek(1) cmd.mu.Unlock() - if pk, _ := rd.Peek(1); len(pk) != 0 && st == monitorStatusStart { + if len(pk) != 0 && st == monitorStatusStart { cmd.mu.Lock() line, err := rd.ReadString() cmd.mu.Unlock()