Skip to content

Commit

Permalink
Fix monitor on go 1.19 (#2908)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ofekshenawa authored Mar 21, 2024
1 parent a923df1 commit 34dacf1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
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 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; \
echo "go test in $${dir}"; \
(cd "$${dir}" && \
go mod tidy -compat=1.18 && \
Expand Down
5 changes: 4 additions & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5454,9 +5454,12 @@ 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()
if err != nil {
return err
}
Expand Down
52 changes: 52 additions & 0 deletions monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package redis_test

import (
"context"
"strings"
"time"

"testing"

. "github.com/bsm/ginkgo/v2"
. "github.com/bsm/gomega"

Expand Down Expand Up @@ -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)
}

0 comments on commit 34dacf1

Please sign in to comment.