Skip to content

Commit

Permalink
Refactor and enhance test cases to prevent flaky test failure
Browse files Browse the repository at this point in the history
  • Loading branch information
git-hulk committed Sep 18, 2024
1 parent 2a0c57a commit 0cba16a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
63 changes: 31 additions & 32 deletions tests/gocase/unit/log/logclean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,40 +29,39 @@ import (
)

func TestLogClean(t *testing.T) {
err := os.Mkdir("/tmp/kvrocks/logfile", os.ModePerm)
if err != nil {
return
}
srv1 := util.StartServer(t, map[string]string{
"log-dir": "/tmp/kvrocks/logfile",
"log-retention-days": "0",
})
srv1.Close()
files1, _ := os.ReadDir("/tmp/kvrocks/logfile")
time.Sleep(2000 * time.Millisecond)
srv2 := util.StartServer(t, map[string]string{
"log-dir": "/tmp/kvrocks/logfile",
logDir := "/tmp/kvrocks/logfile"
require.NoError(t, os.RemoveAll(logDir))
require.NoError(t, os.MkdirAll(logDir, os.ModePerm))

srv := util.StartServer(t, map[string]string{
"log-dir": logDir,
"log-retention-days": "0",
})
srv2.Close()
files2, _ := os.ReadDir("/tmp/kvrocks/logfile")
islogclear := false
for _, f1 := range files1 {
ishave := false
for _, f2 := range files2 {
if f1.Name() == f2.Name() {
ishave = true
break
}
}
if !ishave {
islogclear = true
break
}
}
require.Equal(t, true, islogclear)
err = os.RemoveAll("/tmp/kvrocks/logfile")
if err != nil {
defer srv.Close()

files1, err := os.ReadDir(logDir)
require.NoError(t, err)
if len(files1) == 0 {
return
}
require.Eventually(t, func() bool {
srv.Restart()

files2, err := os.ReadDir(logDir)
require.NoError(t, err)
for _, f1 := range files1 {
fileExists := false
for _, f2 := range files2 {
if f1.Name() == f2.Name() {
fileExists = true
break
}
}
// If the file does not exist, it means the file has been cleaned
if !fileExists {
return true
}
}
return false
}, 10*time.Second, 200*time.Millisecond)
}
7 changes: 6 additions & 1 deletion utils/kvrocks2redis/tests/check_consistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,13 @@ def _import_and_compare(self, num):
for key in keys:
data_type = self.src_cli.type(key)
src_data, dst_data = self._compare_data([key], data_type)

attempts = 0
if src_data != dst_data:
raise AssertionError(f"Data mismatch for key '{key}': source data: '{src_data}' destination data: '{dst_data}'")
if attempts >= 3:
raise AssertionError(f"Data mismatch for key '{key}': source data: '{src_data}' destination data: '{dst_data}'")
attempts += 1
time.sleep(0.1)


if __name__ == '__main__':
Expand Down

0 comments on commit 0cba16a

Please sign in to comment.