Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:add test for redis commands, including LPush, RPushX, BgSave, FlushDb and SetEx #2901

Merged
merged 10 commits into from
Sep 24, 2024
23 changes: 23 additions & 0 deletions tests/integration/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1291,5 +1291,28 @@ var _ = Describe("List Commands", func() {
// Expect(lRange.Err()).NotTo(HaveOccurred())
// Expect(lRange.Val()).To(Equal([]string{"san"}))
//})

It("should lpush and rpushx", func() {
lpush := client.LPush(ctx, "list1", 1, 2, 3, 4)
Expect(lpush.Err()).NotTo(HaveOccurred())
Expect(lpush.Val()).To(Equal(int64(4)))

getRes, err := client.Get(ctx, "list1").Result()
Expect(err).To(HaveOccurred()) // An error is expected since listkey is a list, not a string
Expect(getRes).To(Equal(""))

lrang := client.LRange(ctx, "list1", 0, -1)
Expect(lrang.Err()).NotTo(HaveOccurred())
Expect(lrang.Val()).To(Equal([]string{"4", "3", "2", "1"}))

rpush := client.RPushX(ctx, "list1", 5)
Expect(rpush.Err()).NotTo(HaveOccurred())
Expect(rpush.Val()).To(Equal(int64(5)))

lrang = client.LRange(ctx, "list1", 0, -1)
Expect(lrang.Err()).NotTo(HaveOccurred())
Expect(lrang.Val()).To(Equal([]string{"4", "3", "2", "1", "5"}))

})
})
})
106 changes: 105 additions & 1 deletion tests/integration/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package pika_integration
import (
"context"
"time"

"os"
"path/filepath"
. "github.com/bsm/ginkgo/v2"
. "github.com/bsm/gomega"
"github.com/redis/go-redis/v9"
Expand Down Expand Up @@ -161,6 +162,97 @@ var _ = Describe("Server", func() {
Expect(val).To(ContainSubstring("Background append only file rewriting"))
})

It("should BgSave", func() {
res := client.Set(ctx, "bgsava_key1", "bgsava_value1", 0)
Expect(res.Err()).NotTo(HaveOccurred())
_ = client.Set(ctx, "bgsava_key2", "bgsava_value2", 0)
Expect(res.Err()).NotTo(HaveOccurred())

lastSaveBefore, err := client.LastSave(ctx).Result()
Expect(err).NotTo(HaveOccurred())

currentDir, err := os.Getwd()
Expect(err).NotTo(HaveOccurred())

var pikaDir string
for currentDir != "/" {
if filepath.Base(currentDir) == "pika" {
pikaDir = currentDir
break
}
currentDir = filepath.Dir(currentDir)
}
Expect(pikaDir).NotTo(BeEmpty(), "Could not find 'pika' directory")

var dumpDirs []string
err = filepath.WalkDir(pikaDir, func(path string, info os.DirEntry, err error) error {
if err != nil {
return err
}
if info.IsDir() && info.Name() == "dump" {
absPath, err := filepath.Abs(path)
if err != nil {
return err
}
dumpDirs = append(dumpDirs, absPath)
}
return nil
})
Expect(err).NotTo(HaveOccurred(), "Error while searching for 'dump' directories")
Expect(len(dumpDirs)).To(BeNumerically(">", 0), "No 'dump' directories found in 'pika'")

shortestDumpDir := dumpDirs[0]
for _, dir := range dumpDirs {
if len(dir) < len(shortestDumpDir) {
shortestDumpDir = dir
}
}

dumpConfig := client.ConfigGet(ctx, "dump-path")
Expect(dumpConfig.Err()).NotTo(HaveOccurred())
originalDumpPath, ok := dumpConfig.Val()["dump-path"]
Expect(ok).To(BeTrue())

dumpPath := filepath.Join(filepath.Dir(shortestDumpDir), originalDumpPath)

res2, err2 := client.BgSave(ctx).Result()
Expect(err2).NotTo(HaveOccurred())
Expect(res.Err()).NotTo(HaveOccurred())
Expect(res2).To(ContainSubstring("Background saving started"))

startTime := time.Now()
maxWaitTime := 10 * time.Second

for {
lastSaveAfter, err := client.LastSave(ctx).Result()
Expect(err).NotTo(HaveOccurred())

if lastSaveAfter > lastSaveBefore {
break
}

if time.Since(startTime) > maxWaitTime {
Fail("BGSAVE did not complete within the expected time")
}

time.Sleep(500 * time.Millisecond)
}


files, err := os.ReadDir(dumpPath)
Expect(err).NotTo(HaveOccurred(), "Failed to read dump_path")
Expect(len(files)).To(BeNumerically(">", 0), "dump_tmp directory is empty, BGSAVE failed to create a file")
})

It("should FlushDb", func() {
res := client.Do(ctx, "flushdb")
Expect(res.Err()).NotTo(HaveOccurred())
Expect(res.Val()).To(Equal("OK"))

keys, err := client.Keys(ctx, "*").Result()
Expect(err).NotTo(HaveOccurred())
Expect(keys).To(BeEmpty())
})
// Test scenario: Execute the del command, after executing bgsave, the get data will be wrong
//It("should BgSave", func() {
// res := client.Set(ctx, "bgsava_key", "bgsava_value", 0)
Expand Down Expand Up @@ -446,6 +538,18 @@ var _ = Describe("Server", func() {
Expect(info.Val()).To(ContainSubstring(`used_cpu_sys`))
})

It("should Info after second", func() {
info := client.Info(ctx)
time.Sleep(1 * time.Second)
Expect(info.Err()).NotTo(HaveOccurred())
Expect(info.Val()).NotTo(Equal(""))

info = client.Info(ctx, "all")
time.Sleep(1 * time.Second)
Expect(info.Err()).NotTo(HaveOccurred())
Expect(info.Val()).NotTo(Equal(""))
})

It("should Info cpu", func() {
info := client.Info(ctx, "cpu")
Expect(info.Err()).NotTo(HaveOccurred())
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,25 @@ var _ = Describe("String Commands", func() {
}, "2s", "100ms").Should(Equal(redis.Nil))
})

It("should SetEX ten seconds", func() {
err := client.SetEx(ctx, "x", "y", 10 * time.Second).Err()
Expect(err).NotTo(HaveOccurred())

val, err := client.Get(ctx, "x").Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("y"))

time.Sleep(11 * time.Second)
//sleep 10 second x still exists

err = client.Do(ctx, "compact").Err()
Expect(err).NotTo(HaveOccurred())

keys, err := client.Keys(ctx, "x").Result()
Expect(err).NotTo(HaveOccurred())
Expect(keys).To(BeEmpty())
})

It("should SetNX", func() {
_, err := client.Del(ctx, "key").Result()
Expect(err).NotTo(HaveOccurred())
Expand Down
Loading