Skip to content

Commit

Permalink
chore(tests): refactor and enhance test cases to prevent flaky test f…
Browse files Browse the repository at this point in the history
…ailure (#2544)
  • Loading branch information
git-hulk authored Sep 20, 2024
1 parent 2a0c57a commit a7dbd74
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 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)
}
13 changes: 9 additions & 4 deletions utils/kvrocks2redis/tests/check_consistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,17 @@ def _import_and_compare(self, num):
time.sleep(0.02)
keys = [key, incr_key, hash_key, set_key, zset_key]
for key in keys:
data_type = self.src_cli.type(key)
src_data, dst_data = self._compare_data([key], data_type)
if src_data != dst_data:
attempts = 0
while attempts <= 3:
data_type = self.src_cli.type(key)
src_data, dst_data = self._compare_data([key], data_type)
if src_data == dst_data:
break
attempts += 1
time.sleep(0.1)
else:
raise AssertionError(f"Data mismatch for key '{key}': source data: '{src_data}' destination data: '{dst_data}'")


if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Redis Comparator')
parser.add_argument('--src_host', type=str, default='127.0.0.1', help='Source Redis host')
Expand Down

0 comments on commit a7dbd74

Please sign in to comment.