MCPcopy
hub / github.com/redis/go-redis / TestMonitorCommand

Function TestMonitorCommand

monitor_test.go:58–105  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 9

containsSubstringFunction · 0.70
ErrMethod · 0.65
FlushDBMethod · 0.65
CloseMethod · 0.65
StartMethod · 0.65
SetMethod · 0.65
GetMethod · 0.65
StopMethod · 0.65
MonitorMethod · 0.45

Tested by

no test coverage detected