|
| 1 | +package redis_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + . "github.com/bsm/ginkgo/v2" |
| 8 | + . "github.com/bsm/gomega" |
| 9 | + |
| 10 | + "github.com/redis/go-redis/v9" |
| 11 | +) |
| 12 | + |
| 13 | +var _ = Describe("Monitor command", Label("monitor"), func() { |
| 14 | + ctx := context.TODO() |
| 15 | + var client *redis.Client |
| 16 | + |
| 17 | + BeforeEach(func() { |
| 18 | + client = redis.NewClient(&redis.Options{Addr: ":6379"}) |
| 19 | + Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred()) |
| 20 | + }) |
| 21 | + |
| 22 | + AfterEach(func() { |
| 23 | + Expect(client.Close()).NotTo(HaveOccurred()) |
| 24 | + }) |
| 25 | + |
| 26 | + It("should monitor", Label("monitor"), func() { |
| 27 | + ress := make(chan string) |
| 28 | + client1 := redis.NewClient(&redis.Options{Addr: rediStackAddr}) |
| 29 | + mn := client1.Monitor(ctx, ress) |
| 30 | + mn.Start() |
| 31 | + // Wait for the Redis server to be in monitoring mode. |
| 32 | + time.Sleep(100 * time.Millisecond) |
| 33 | + client.Set(ctx, "foo", "bar", 0) |
| 34 | + client.Set(ctx, "bar", "baz", 0) |
| 35 | + client.Set(ctx, "bap", 8, 0) |
| 36 | + client.Get(ctx, "bap") |
| 37 | + lst := []string{} |
| 38 | + for i := 0; i < 5; i++ { |
| 39 | + s := <-ress |
| 40 | + lst = append(lst, s) |
| 41 | + } |
| 42 | + mn.Stop() |
| 43 | + Expect(lst[0]).To(ContainSubstring("OK")) |
| 44 | + Expect(lst[1]).To(ContainSubstring(`"set" "foo" "bar"`)) |
| 45 | + Expect(lst[2]).To(ContainSubstring(`"set" "bar" "baz"`)) |
| 46 | + Expect(lst[3]).To(ContainSubstring(`"set" "bap" "8"`)) |
| 47 | + }) |
| 48 | +}) |
0 commit comments