Replaced all time.After instances to time.Ticker

This commit is contained in:
Kelvin Clement Mwinuka
2024-06-02 17:55:15 +08:00
parent bc6537ad8f
commit 555387494b
5 changed files with 1057 additions and 1034 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -373,14 +373,17 @@ func Test_Cluster(t *testing.T) {
t.Error(err) t.Error(err)
return return
} }
// 3. Check the delete count is equal to length of tests. // 3. Check the delete count is equal to length of tests.
if res.Integer() != len(tests) { if res.Integer() != len(tests) {
t.Errorf("expected delete response to be %d, got %d", len(tests), res.Integer()) t.Errorf("expected delete response to be %d, got %d", len(tests), res.Integer())
} }
<-time.After(200 * time.Millisecond) // Yield // Yield
ticker.Reset(200 * time.Millisecond)
<-ticker.C
// Check if the data is absent in quorum (majority of the cluster). // 4. Check if the data is absent in quorum (majority of the cluster).
for i, test := range tests { for i, test := range tests {
count := 0 count := 0
for j := 0; j < len(nodes); j++ { for j := 0; j < len(nodes); j++ {
@@ -399,7 +402,7 @@ func Test_Cluster(t *testing.T) {
count += 1 // If the expected value is found, increment the count. count += 1 // If the expected value is found, increment the count.
} }
} }
// Fail if count is less than quorum. // 5. Fail if count is less than quorum.
if count < quorum { if count < quorum {
t.Errorf("could not find value %s at key %s in cluster quorum", test.value, test.key) t.Errorf("could not find value %s at key %s in cluster quorum", test.value, test.key)
} }
@@ -461,7 +464,9 @@ func Test_Cluster(t *testing.T) {
} }
} }
<-time.After(200 * time.Millisecond) // Yield to give key deletion time to take effect across cluster. // Yield to give key deletion time to take effect across cluster.
ticker.Reset(200 * time.Millisecond)
<-ticker.C
// Check if the data is absent in quorum (majority of the cluster). // Check if the data is absent in quorum (majority of the cluster).
for i, test := range tests { for i, test := range tests {
@@ -557,8 +562,10 @@ func Test_Cluster(t *testing.T) {
doneChan <- struct{}{} doneChan <- struct{}{}
}() }()
ticker.Reset(5 * time.Second)
select { select {
case <-time.After(5 * time.Second): case <-ticker.C:
if forwardError != nil { if forwardError != nil {
t.Errorf("timeout error: %v\n", forwardError) t.Errorf("timeout error: %v\n", forwardError)
} }

View File

@@ -118,7 +118,12 @@ func Test_AOFEngine(t *testing.T) {
state[command[1]] = internal.KeyData{Value: command[2], ExpireAt: time.Time{}} state[command[1]] = internal.KeyData{Value: command[2], ExpireAt: time.Time{}}
engine.QueueCommand(marshalRespCommand(command)) engine.QueueCommand(marshalRespCommand(command))
} }
<-time.After(100 * time.Millisecond) ticker := time.NewTicker(100 * time.Millisecond)
defer func() {
ticker.Stop()
}()
<-ticker.C
// Trigger log rewrite // Trigger log rewrite
if err = engine.RewriteLog(); err != nil { if err = engine.RewriteLog(); err != nil {
@@ -136,7 +141,9 @@ func Test_AOFEngine(t *testing.T) {
state[command[1]] = internal.KeyData{Value: command[2], ExpireAt: time.Time{}} state[command[1]] = internal.KeyData{Value: command[2], ExpireAt: time.Time{}}
engine.QueueCommand(marshalRespCommand(command)) engine.QueueCommand(marshalRespCommand(command))
} }
<-time.After(100 * time.Millisecond)
ticker.Reset(100 * time.Millisecond)
<-ticker.C
// Restore logs // Restore logs
if err = engine.Restore(); err != nil { if err = engine.Restore(); err != nil {

View File

@@ -35,6 +35,10 @@ func marshalRespCommand(command []string) []byte {
} }
func Test_AppendStore(t *testing.T) { func Test_AppendStore(t *testing.T) {
t.Cleanup(func() {
_ = os.RemoveAll(path.Join(".", "testdata"))
})
tests := []struct { tests := []struct {
name string name string
directory string directory string
@@ -133,14 +137,16 @@ func Test_AppendStore(t *testing.T) {
done <- struct{}{} done <- struct{}{}
}() }()
ticker := time.NewTicker(200 * time.Millisecond)
defer func() {
ticker.Stop()
}()
select { select {
case <-done: case <-done:
_ = os.RemoveAll(test.directory) case <-ticker.C:
case <-time.After(200 * time.Millisecond):
_ = os.RemoveAll(test.directory)
t.Error("timeout error") t.Error("timeout error")
} }
} }
_ = os.RemoveAll("./testdata")
} }

View File

@@ -31,10 +31,13 @@ func Test_CacheLRU(t *testing.T) {
} }
access := []string{"key3", "key4", "key1", "key2", "key5"} access := []string{"key3", "key4", "key1", "key2", "key5"}
ticker := time.NewTicker(200 * time.Millisecond)
for _, key := range access { for _, key := range access {
cache.Update(key) cache.Update(key)
<-time.After(1 * time.Millisecond) // Yield
<-ticker.C
} }
ticker.Stop()
for i := len(access) - 1; i >= 0; i-- { for i := len(access) - 1; i >= 0; i-- {
key := heap.Pop(&cache).(string) key := heap.Pop(&cache).(string)