From b8acc85c869d71edd1e117717cdb85c29c953e1a Mon Sep 17 00:00:00 2001 From: dingxiaoshuai123 <2486016589@qq.com> Date: Thu, 9 Nov 2023 20:44:58 +0800 Subject: [PATCH] Fix the timestamp bug and add test cases. --- tests/integration/slowlog_test.go | 46 ++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/tests/integration/slowlog_test.go b/tests/integration/slowlog_test.go index 782d774311..a4eea60412 100644 --- a/tests/integration/slowlog_test.go +++ b/tests/integration/slowlog_test.go @@ -9,6 +9,10 @@ import ( "github.com/redis/go-redis/v9" ) +func isBetween(first, second, target time.Time) bool { + return (target.Before(second) || target.Equal(second)) && (target.After(first) || target.Equal(first)) +} + var _ = Describe("Slowlog Commands", func() { ctx := context.TODO() var client *redis.Client @@ -23,7 +27,7 @@ var _ = Describe("Slowlog Commands", func() { Expect(client.Close()).NotTo(HaveOccurred()) }) - Describe("SlowLogGet", func() { + Describe("SlowLog", func() { It("returns slow query result", func() { const key = "slowlog-log-slower-than" @@ -40,5 +44,45 @@ var _ = Describe("Slowlog Commands", func() { Expect(err).NotTo(HaveOccurred()) Expect(len(result)).NotTo(BeZero()) }) + It("Should be able to handle the situation when the log is full correctly", func() { + const key1 = "slowlog-log-slower-than" + old := client.ConfigGet(ctx, key1).Val() + defer Expect(client.ConfigSet(ctx, key1, old[key1]).Err()).NotTo(HaveOccurred()) + client.ConfigSet(ctx, key1, "0") + + const key2 = "slowlog-max-len" + oldMaxLen := client.ConfigGet(ctx, key2).Val() + defer Expect(client.ConfigSet(ctx, key2, oldMaxLen[key2]).Err()).NotTo(HaveOccurred()) + client.ConfigSet(ctx, key2, "10") + + for i := 0; i < 20; i++ { + Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred()) + } + + result, err := client.SlowLogGet(ctx, -1).Result() + Expect(err).NotTo(HaveOccurred()) + Expect(len(result)).To(Equal(int(10))) + }) + It("Make sure that the returned timestamp is correct.", func() { + const key1 = "slowlog-log-slower-than" + old := client.ConfigGet(ctx, key1).Val() + defer Expect(client.ConfigSet(ctx, key1, old[key1]).Err()).NotTo(HaveOccurred()) + client.ConfigSet(ctx, key1, "0") + + time1 := time.Now() + time.Sleep(100 * time.Millisecond) + for i := 0; i < 10; i++ { + Expect(client.Ping(ctx).Err()).NotTo(HaveOccurred()) + } + result, err := client.SlowLogGet(ctx, 10).Result() + time.Sleep(100 * time.Millisecond) + time2 := time.Now() + Expect(err).NotTo(HaveOccurred()) + + for i := 0; i < 10; i++ { + Expect(isBetween(time1, time2, time.Unix(0, result[i].Time.Unix()*1e6))).To(Equal(true)) + } + }) }) + })