add tests for new commands

add tests to coverage for the new commands
This commit is contained in:
carner 2023-05-15 15:42:45 +08:00 committed by GitHub
parent 7bd87c30a1
commit af12cb6bd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 509 additions and 0 deletions

View File

@ -502,6 +502,146 @@ var _ = Describe("Commands", func() {
Expect(ttl.Val()).To(Equal(time.Duration(-2)))
})
It("should ExpireNX", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
expire := client.ExpireNX(ctx, "key", 10*time.Second)
Expect(expire.Err()).NotTo(HaveOccurred())
Expect(expire.Val()).To(Equal(true))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(10 * time.Second))
expire = client.ExpireNX(ctx, "key", 10*time.Second)
Expect(expire.Err()).NotTo(HaveOccurred())
Expect(expire.Val()).To(Equal(false))
set = client.Set(ctx, "key", "Hello World", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
ttl = client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(time.Duration(-1)))
ttl = client.TTL(ctx, "nonexistent_key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(time.Duration(-2)))
})
It("should ExpireXX", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
expire := client.ExpireXX(ctx, "key", 10*time.Second)
Expect(expire.Err()).NotTo(HaveOccurred())
Expect(expire.Val()).To(Equal(false))
expire = client.Expire(ctx, "key", 10*time.Second)
Expect(expire.Err()).NotTo(HaveOccurred())
Expect(expire.Val()).To(Equal(true))
expire = client.ExpireXX(ctx, "key", 20*time.Second)
Expect(expire.Err()).NotTo(HaveOccurred())
Expect(expire.Val()).To(Equal(true))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(20 * time.Second))
set = client.Set(ctx, "key", "Hello World", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
ttl = client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(time.Duration(-1)))
ttl = client.TTL(ctx, "nonexistent_key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(time.Duration(-2)))
})
It("should ExpireGT", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
expire := client.Expire(ctx, "key", 10*time.Second)
Expect(expire.Err()).NotTo(HaveOccurred())
Expect(expire.Val()).To(Equal(true))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(10 * time.Second))
expire = client.ExpireGT(ctx, "key", 5*time.Second)
Expect(expire.Err()).NotTo(HaveOccurred())
Expect(expire.Val()).To(Equal(false))
expire = client.ExpireGT(ctx, "key", 20*time.Second)
Expect(expire.Err()).NotTo(HaveOccurred())
Expect(expire.Val()).To(Equal(true))
ttl = client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(20 * time.Second))
set = client.Set(ctx, "key", "Hello World", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
ttl = client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(time.Duration(-1)))
ttl = client.TTL(ctx, "nonexistent_key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(time.Duration(-2)))
})
It("should ExpireLT", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
expire := client.Expire(ctx, "key", 10*time.Second)
Expect(expire.Err()).NotTo(HaveOccurred())
Expect(expire.Val()).To(Equal(true))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(10 * time.Second))
expire = client.ExpireLT(ctx, "key", 20*time.Second)
Expect(expire.Err()).NotTo(HaveOccurred())
Expect(expire.Val()).To(Equal(false))
expire = client.ExpireLT(ctx, "key", 5*time.Second)
Expect(expire.Err()).NotTo(HaveOccurred())
Expect(expire.Val()).To(Equal(true))
ttl = client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(5 * time.Second))
set = client.Set(ctx, "key", "Hello World", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
ttl = client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(time.Duration(-1)))
ttl = client.TTL(ctx, "nonexistent_key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(time.Duration(-2)))
})
It("should ExpireAt", func() {
setCmd := client.Set(ctx, "key", "Hello", 0)
Expect(setCmd.Err()).NotTo(HaveOccurred())
@ -532,6 +672,155 @@ var _ = Describe("Commands", func() {
})
It("should ExpireAtNX", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
n, err := client.Exists(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(1)))
expireAt := time.Now().Add(time.Minute)
expireAtCmd := client.ExpireAtNX(ctx, "key", expireAt)
Expect(expireAtCmd.Err()).NotTo(HaveOccurred())
Expect(expireAtCmd.Val()).To(Equal(true))
timeCmd := client.ExpireTime(ctx, "key")
Expect(timeCmd.Err()).NotTo(HaveOccurred())
Expect(timeCmd.Val().Seconds()).To(BeNumerically("==", expireAt.Unix()))
expireAtCmd = client.ExpireAtNX(ctx, "key", expireAt)
Expect(expireAtCmd.Err()).NotTo(HaveOccurred())
Expect(expireAtCmd.Val()).To(Equal(false))
expireAtCmd = client.ExpireAt(ctx, "key", time.Now().Add(-time.Hour))
Expect(expireAtCmd.Err()).NotTo(HaveOccurred())
Expect(expireAtCmd.Val()).To(Equal(true))
n, err = client.Exists(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(0)))
})
It("should ExpireAtXX", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
n, err := client.Exists(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(1)))
expireAt := time.Now().Add(time.Minute)
expireAtCmd := client.ExpireAtXX(ctx, "key", expireAt)
Expect(expireAtCmd.Err()).NotTo(HaveOccurred())
Expect(expireAtCmd.Val()).To(Equal(false))
expireAtCmd = client.ExpireAt(ctx, "key", expireAt)
Expect(expireAtCmd.Err()).NotTo(HaveOccurred())
Expect(expireAtCmd.Val()).To(Equal(true))
expireAt = expireAt.Add(time.Minute)
expireAtCmd = client.ExpireAtXX(ctx, "key", expireAt)
Expect(expireAtCmd.Err()).NotTo(HaveOccurred())
Expect(expireAtCmd.Val()).To(Equal(true))
timeCmd := client.ExpireTime(ctx, "key")
Expect(timeCmd.Err()).NotTo(HaveOccurred())
Expect(timeCmd.Val().Seconds()).To(BeNumerically("==", expireAt.Unix()))
expireAtCmd = client.ExpireAt(ctx, "key", time.Now().Add(-time.Hour))
Expect(expireAtCmd.Err()).NotTo(HaveOccurred())
Expect(expireAtCmd.Val()).To(Equal(true))
n, err = client.Exists(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(0)))
})
It("should ExpireAtGT", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
n, err := client.Exists(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(1)))
expireAt := time.Now().Add(2 * time.Minute)
expireAtCmd := client.ExpireAt(ctx, "key", expireAt)
Expect(expireAtCmd.Err()).NotTo(HaveOccurred())
Expect(expireAtCmd.Val()).To(Equal(true))
timeCmd := client.ExpireTime(ctx, "key")
Expect(timeCmd.Err()).NotTo(HaveOccurred())
Expect(timeCmd.Val().Seconds()).To(BeNumerically("==", expireAt.Unix()))
expireAt = expireAt.Add(-time.Minute)
expireAtCmd = client.ExpireAtGT(ctx, "key", expireAt)
Expect(expireAtCmd.Err()).NotTo(HaveOccurred())
Expect(expireAtCmd.Val()).To(Equal(false))
expireAt = expireAt.Add(2 * time.Minute)
expireAtCmd = client.ExpireAtGT(ctx, "key", expireAt)
Expect(expireAtCmd.Err()).NotTo(HaveOccurred())
Expect(expireAtCmd.Val()).To(Equal(true))
timeCmd = client.ExpireTime(ctx, "key")
Expect(timeCmd.Err()).NotTo(HaveOccurred())
Expect(timeCmd.Val().Seconds()).To(BeNumerically("==", expireAt.Unix()))
expireAtCmd = client.ExpireAt(ctx, "key", time.Now().Add(-time.Hour))
Expect(expireAtCmd.Err()).NotTo(HaveOccurred())
Expect(expireAtCmd.Val()).To(Equal(true))
n, err = client.Exists(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(0)))
})
It("should ExpireAtLT", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
n, err := client.Exists(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(1)))
expireAt := time.Now().Add(2 * time.Minute)
expireAtCmd := client.ExpireAt(ctx, "key", expireAt)
Expect(expireAtCmd.Err()).NotTo(HaveOccurred())
Expect(expireAtCmd.Val()).To(Equal(true))
timeCmd := client.ExpireTime(ctx, "key")
Expect(timeCmd.Err()).NotTo(HaveOccurred())
Expect(timeCmd.Val().Seconds()).To(BeNumerically("==", expireAt.Unix()))
expireAt = expireAt.Add(time.Minute)
expireAtCmd = client.ExpireAtLT(ctx, "key", expireAt)
Expect(expireAtCmd.Err()).NotTo(HaveOccurred())
Expect(expireAtCmd.Val()).To(Equal(false))
expireAt = expireAt.Add(-2 * time.Minute)
expireAtCmd = client.ExpireAtLT(ctx, "key", expireAt)
Expect(expireAtCmd.Err()).NotTo(HaveOccurred())
Expect(expireAtCmd.Val()).To(Equal(true))
timeCmd = client.ExpireTime(ctx, "key")
Expect(timeCmd.Err()).NotTo(HaveOccurred())
Expect(timeCmd.Val().Seconds()).To(BeNumerically("==", expireAt.Unix()))
expireAtCmd = client.ExpireAt(ctx, "key", time.Now().Add(-time.Hour))
Expect(expireAtCmd.Err()).NotTo(HaveOccurred())
Expect(expireAtCmd.Val()).To(Equal(true))
n, err = client.Exists(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(0)))
})
It("should Keys", func() {
mset := client.MSet(ctx, "one", "1", "two", "2", "three", "3", "four", "4")
Expect(mset.Err()).NotTo(HaveOccurred())
@ -656,6 +945,115 @@ var _ = Describe("Commands", func() {
Expect(pttl.Val()).To(BeNumerically("~", expiration, 100*time.Millisecond))
})
It("should PExpireNX", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
expiration := 900 * time.Millisecond
pexpire := client.PExpireNX(ctx, "key", expiration)
Expect(pexpire.Err()).NotTo(HaveOccurred())
Expect(pexpire.Val()).To(Equal(true))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(time.Second))
pexpire = client.PExpireNX(ctx, "key", 1900*time.Millisecond)
Expect(pexpire.Err()).NotTo(HaveOccurred())
Expect(pexpire.Val()).To(Equal(false))
pttl := client.PTTL(ctx, "key")
Expect(pttl.Err()).NotTo(HaveOccurred())
Expect(pttl.Val()).To(BeNumerically("~", expiration, 100*time.Millisecond))
})
It("should PExpireXX", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
expiration := 900 * time.Millisecond
pexpire := client.PExpireXX(ctx, "key", expiration)
Expect(pexpire.Err()).NotTo(HaveOccurred())
Expect(pexpire.Val()).To(Equal(false))
pexpire = client.PExpire(ctx, "key", expiration)
Expect(pexpire.Err()).NotTo(HaveOccurred())
Expect(pexpire.Val()).To(Equal(true))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(time.Second))
expiration = 1900 * time.Millisecond
pexpire = client.PExpireXX(ctx, "key", expiration)
Expect(pexpire.Err()).NotTo(HaveOccurred())
Expect(pexpire.Val()).To(Equal(true))
pttl := client.PTTL(ctx, "key")
Expect(pttl.Err()).NotTo(HaveOccurred())
Expect(pttl.Val()).To(BeNumerically("~", expiration, 1000*time.Millisecond))
})
It("should PExpireGT", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
expiration := 10000 * time.Millisecond
pexpire := client.PExpire(ctx, "key", expiration)
Expect(pexpire.Err()).NotTo(HaveOccurred())
Expect(pexpire.Val()).To(Equal(true))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(10 * time.Second))
expiration = 5000 * time.Millisecond
pexpire = client.PExpireGT(ctx, "key", expiration)
Expect(pexpire.Err()).NotTo(HaveOccurred())
Expect(pexpire.Val()).To(Equal(false))
expiration = 20000 * time.Millisecond
pexpire = client.PExpireGT(ctx, "key", expiration)
Expect(pexpire.Err()).NotTo(HaveOccurred())
Expect(pexpire.Val()).To(Equal(true))
ttl = client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(20 * time.Second))
})
It("should PExpireLT", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
expiration := 10000 * time.Millisecond
pexpire := client.PExpire(ctx, "key", expiration)
Expect(pexpire.Err()).NotTo(HaveOccurred())
Expect(pexpire.Val()).To(Equal(true))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(10 * time.Second))
expiration = 20000 * time.Millisecond
pexpire = client.PExpireLT(ctx, "key", expiration)
Expect(pexpire.Err()).NotTo(HaveOccurred())
Expect(pexpire.Val()).To(Equal(false))
expiration = 5000 * time.Millisecond
pexpire = client.PExpireLT(ctx, "key", expiration)
Expect(pexpire.Err()).NotTo(HaveOccurred())
Expect(pexpire.Val()).To(Equal(true))
ttl = client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(5 * time.Second))
})
It("should PExpireAt", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
@ -675,6 +1073,117 @@ var _ = Describe("Commands", func() {
Expect(pttl.Val()).To(BeNumerically("~", expiration, 100*time.Millisecond))
})
It("should PExpireAtNX", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
expiration := 900 * time.Millisecond
pexpireAt := time.Now().Add(expiration)
pexpireAtCmd := client.PExpireAtNX(ctx, "key", pexpireAt)
Expect(pexpireAtCmd.Err()).NotTo(HaveOccurred())
Expect(pexpireAtCmd.Val()).To(Equal(true))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(time.Second))
pexpireAtCmd = client.PExpireAtNX(ctx, "key", pexpireAt.Add(time.Second))
Expect(pexpireAtCmd.Err()).NotTo(HaveOccurred())
Expect(pexpireAtCmd.Val()).To(Equal(false))
pttl := client.PTTL(ctx, "key")
Expect(pttl.Err()).NotTo(HaveOccurred())
Expect(pttl.Val()).To(BeNumerically("~", expiration, 100*time.Millisecond))
})
It("should PExpireAtXX", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
expiration := 900 * time.Millisecond
pexpireAt := time.Now().Add(expiration)
pexpireAtCmd := client.PExpireAtXX(ctx, "key", pexpireAt)
Expect(pexpireAtCmd.Err()).NotTo(HaveOccurred())
Expect(pexpireAtCmd.Val()).To(Equal(false))
pexpireAtCmd = client.PExpireAt(ctx, "key", pexpireAt)
Expect(pexpireAtCmd.Err()).NotTo(HaveOccurred())
Expect(pexpireAtCmd.Val()).To(Equal(true))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(time.Second))
pexpireAt = pexpireAt.Add(time.Second)
pexpireAtCmd = client.PExpireAtXX(ctx, "key", pexpireAt)
Expect(pexpireAtCmd.Err()).NotTo(HaveOccurred())
Expect(pexpireAtCmd.Val()).To(Equal(true))
pttl := client.PTTL(ctx, "key")
Expect(pttl.Err()).NotTo(HaveOccurred())
Expect(pttl.Val()).To(BeNumerically("~", 1900*time.Millisecond, 1000*time.Millisecond))
})
It("should PExpireAtGT", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
pexpireAt := time.Now().Add(5000 * time.Millisecond)
pexpireAtCmd := client.PExpireAt(ctx, "key", pexpireAt)
Expect(pexpireAtCmd.Err()).NotTo(HaveOccurred())
Expect(pexpireAtCmd.Val()).To(Equal(true))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(5 * time.Second))
pexpireAt = pexpireAt.Add(-2000 * time.Millisecond)
pexpireAtCmd = client.PExpireAtGT(ctx, "key", pexpireAt)
Expect(pexpireAtCmd.Err()).NotTo(HaveOccurred())
Expect(pexpireAtCmd.Val()).To(Equal(false))
pexpireAt = pexpireAt.Add(5000 * time.Millisecond)
pexpireAtCmd = client.PExpireAtGT(ctx, "key", pexpireAt)
Expect(pexpireAtCmd.Err()).NotTo(HaveOccurred())
Expect(pexpireAtCmd.Val()).To(Equal(true))
pttl := client.PTTL(ctx, "key")
Expect(pttl.Err()).NotTo(HaveOccurred())
Expect(pttl.Val()).To(BeNumerically("~", 8000*time.Millisecond, 7000*time.Millisecond))
})
It("should PExpireAtLT", func() {
set := client.Set(ctx, "key", "Hello", 0)
Expect(set.Err()).NotTo(HaveOccurred())
Expect(set.Val()).To(Equal("OK"))
pexpireAt := time.Now().Add(5000 * time.Millisecond)
pexpireAtCmd := client.PExpireAt(ctx, "key", pexpireAt)
Expect(pexpireAtCmd.Err()).NotTo(HaveOccurred())
Expect(pexpireAtCmd.Val()).To(Equal(true))
ttl := client.TTL(ctx, "key")
Expect(ttl.Err()).NotTo(HaveOccurred())
Expect(ttl.Val()).To(Equal(5 * time.Second))
pexpireAt = pexpireAt.Add(2000 * time.Millisecond)
pexpireAtCmd = client.PExpireAtLT(ctx, "key", pexpireAt)
Expect(pexpireAtCmd.Err()).NotTo(HaveOccurred())
Expect(pexpireAtCmd.Val()).To(Equal(false))
pexpireAt = pexpireAt.Add(-5000 * time.Millisecond)
pexpireAtCmd = client.PExpireAtLT(ctx, "key", pexpireAt)
Expect(pexpireAtCmd.Err()).NotTo(HaveOccurred())
Expect(pexpireAtCmd.Val()).To(Equal(true))
pttl := client.PTTL(ctx, "key")
Expect(pttl.Err()).NotTo(HaveOccurred())
Expect(pttl.Val()).To(BeNumerically("~", 2000*time.Millisecond, 1000*time.Millisecond))
})
It("should PExpireTime", func() {
// The command returns -1 if the key exists but has no associated expiration time.