Replaces use of time.Sleep with time.Ticker in echovault and echovault tests

This commit is contained in:
Kelvin Clement Mwinuka
2024-06-02 17:34:20 +08:00
parent b7e691bbf1
commit bc6537ad8f
6 changed files with 601 additions and 569 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -511,6 +511,7 @@ func TestEchoVault_CommandCount(t *testing.T) {
func TestEchoVault_Save(t *testing.T) {
conf := DefaultConfig()
conf.DataDir = path.Join(".", "testdata", "data")
conf.EvictionPolicy = constants.NoEviction
server := createEchoVaultWithConfig(conf)
tests := []struct {

View File

@@ -273,10 +273,10 @@ func NewEchoVault(options ...func(echovault *EchoVault)) (*EchoVault, error) {
// If eviction policy is not noeviction, start a goroutine to evict keys every 100 milliseconds.
if echovault.config.EvictionPolicy != constants.NoEviction {
go func() {
for {
<-time.After(echovault.config.EvictionInterval)
ticker := time.NewTicker(echovault.config.EvictionInterval)
for _ = range ticker.C {
if err := echovault.evictKeysWithExpiredTTL(context.Background()); err != nil {
log.Println(err)
log.Printf("evict with ttl: %v\n", err)
}
}
}()

View File

@@ -275,7 +275,12 @@ func Test_Cluster(t *testing.T) {
}
}
// <-time.After(3 * time.Second) // Yield
// Yield
ticker := time.NewTicker(200 * time.Millisecond)
defer func() {
ticker.Stop()
}()
<-ticker.C
// Check if the data has been replicated on a quorum (majority of the cluster).
quorum := int(math.Ceil(float64(len(nodes)/2)) + 1)
@@ -318,7 +323,12 @@ func Test_Cluster(t *testing.T) {
}
}
// <-time.After(3 * time.Second) // Yield
// Yield
ticker := time.NewTicker(200 * time.Millisecond)
defer func() {
ticker.Stop()
}()
<-ticker.C
// Check if the data has been replicated on a quorum (majority of the cluster).
quorum := int(math.Ceil(float64(len(nodes)/2)) + 1)
@@ -410,7 +420,12 @@ func Test_Cluster(t *testing.T) {
}
}
// <-time.After(3 * time.Second) // Yield
// Yield
ticker := time.NewTicker(200 * time.Millisecond)
defer func() {
ticker.Stop()
}()
<-ticker.C
// Check if the data has been replicated on a quorum (majority of the cluster).
quorum := int(math.Ceil(float64(len(nodes)/2)) + 1)
@@ -497,7 +512,11 @@ func Test_Cluster(t *testing.T) {
}
}
<-time.After(3 * time.Second) // Yield.
ticker := time.NewTicker(1 * time.Second)
defer func() {
ticker.Stop()
}()
<-ticker.C
// Check if the data has been replicated on a quorum (majority of the cluster).
var forwardError error

View File

@@ -4,12 +4,14 @@ import (
"context"
"github.com/echovault/echovault/internal"
"github.com/echovault/echovault/internal/config"
"github.com/echovault/echovault/internal/constants"
)
func createEchoVault() *EchoVault {
ev, _ := NewEchoVault(
WithConfig(config.Config{
DataDir: "",
DataDir: "",
EvictionPolicy: constants.NoEviction,
}),
)
return ev

View File

@@ -447,8 +447,13 @@ func GetConnection(addr string, port int) (net.Conn, error) {
done <- struct{}{}
}()
ticker := time.NewTicker(10 * time.Second)
defer func() {
ticker.Stop()
}()
select {
case <-time.After(10 * time.Second):
case <-ticker.C:
return nil, errors.New("connection timeout")
case <-done:
return conn, err
@@ -472,8 +477,13 @@ func GetTLSConnection(addr string, port int, config *tls.Config) (net.Conn, erro
done <- struct{}{}
}()
ticker := time.NewTicker(10 * time.Second)
defer func() {
ticker.Stop()
}()
select {
case <-time.After(10 * time.Second):
case <-ticker.C:
return nil, errors.New("connection timeout")
case <-done:
return conn, err