Skip to content

Commit

Permalink
Fix the timestamp bug and add test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
dingxiaoshuai123 committed Nov 10, 2023
1 parent e64b4af commit c3aa85b
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion tests/integration/slowlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"

Expand All @@ -40,5 +44,46 @@ 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()
Expect(client.Do(ctx, "SLOWLOG", "reset").Val()).To(Equal("OK"))

time.Sleep(100 * time.Millisecond)
for i := 0; i < 15; 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))
}
})
})

})

0 comments on commit c3aa85b

Please sign in to comment.