mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-07 17:00:56 +08:00
Replaces use of time.Sleep with time.Ticker in echovault and echovault tests
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -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 {
|
||||
|
@@ -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)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user