Skip to content

Commit

Permalink
Replaces use of time.Sleep with time.Ticker in echovault and echovaul…
Browse files Browse the repository at this point in the history
…t tests
  • Loading branch information
kelvinmwinuka committed Jun 2, 2024
1 parent b7e691b commit bc6537a
Show file tree
Hide file tree
Showing 6 changed files with 805 additions and 773 deletions.
1,526 changes: 763 additions & 763 deletions coverage/coverage.out

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions echovault/api_admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions echovault/echovault.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}()
Expand Down
27 changes: 23 additions & 4 deletions echovault/echovault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion echovault/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit bc6537a

Please sign in to comment.