Skip to content

Commit

Permalink
Remove sleeps and use client retries to handle the e2e async tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gdiazlo committed Mar 19, 2019
1 parent 840006a commit 5bee53e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
8 changes: 4 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func NewHTTPClient(options ...HTTPClientOptionF) (*HTTPClient, error) {
if client.discoveryEnabled {
// try to discover the cluster topology initially
if err := client.discover(); err != nil {
return nil, err
log.Infof("Unable to get QED topology, we will try it later: %v", err)
}
}

Expand All @@ -153,9 +153,9 @@ func NewHTTPClient(options ...HTTPClientOptionF) (*HTTPClient, error) {
client.healthCheck(client.healthcheckTimeout)
}

// Ensure thath we have at least one endpoint, the primary, available
// Ensure that we have at least one endpoint, the primary, available
if !client.topology.HasActivePrimary() {
return nil, ErrNoPrimary
log.Infof("QED does not have a primary node or it is down, we will try it later.")
}

// if t.discoveryEnabled {
Expand Down Expand Up @@ -215,7 +215,7 @@ func (c *HTTPClient) setRetrier(maxRetries int) error {
// Create a Retrier that will wait for 100ms between requests.
ticks := make([]int, maxRetries)
for i := 0; i < len(ticks); i++ {
ticks[i] = 100
ticks[i] = 1000
}
backoff := NewSimpleBackoff(ticks...)
c.retrier = NewBackoffRequestRetrier(c.httpClient, c.maxRetries, backoff)
Expand Down
5 changes: 2 additions & 3 deletions tests/e2e/add_verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package e2e
import (
"fmt"
"testing"
"time"

"github.com/bbva/qed/hashing"
"github.com/bbva/qed/protocol"
Expand All @@ -30,8 +29,8 @@ import (

func TestAddVerify(t *testing.T) {
before, after := setupServer(0, "", false, t)
scenario, let := scope.Scope(t, before, merge(after, delay(2*time.Second)))

scenario, let := scope.Scope(t, before, after)
// log.SetLogger("", log.DEBUG)
event := rand.RandomString(10)

scenario("Add one event and get its membership proof", func() {
Expand Down
3 changes: 1 addition & 2 deletions tests/e2e/incremental_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package e2e

import (
"testing"
"time"

"github.com/bbva/qed/hashing"
"github.com/bbva/qed/protocol"
Expand All @@ -30,7 +29,7 @@ import (

func TestIncrementalConsistency(t *testing.T) {
before, after := setupServer(0, "", false, t)
scenario, let := scope.Scope(t, before, merge(after, delay(2*time.Second)))
scenario, let := scope.Scope(t, before, after)

scenario("Add multiple events and verify consistency between two of them", func() {

Expand Down
21 changes: 18 additions & 3 deletions tests/e2e/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,23 @@ func TestStart(t *testing.T) {

scenario("Test availability of qed server", func() {
let("Query metrics endpoint", func(t *testing.T) {
resp, err := doReq("GET", "http://localhost:8800/info", APIKey, nil)
var resp *http.Response
var err error
retry(3, 1*time.Second, func() error {
resp, err = doReq("GET", "http://localhost:8800/info", APIKey, nil)
return err
})
assert.NoError(t, err, "Subprocess must not exit with non-zero status")
assert.Equal(t, resp.StatusCode, http.StatusOK, "Server should respond with http status code 200")
})

let("Query to unexpected context", func(t *testing.T) {
resp, err := doReq("GET", "http://localhost:8800/xD", APIKey, nil)
var resp *http.Response
var err error
retry(3, 1*time.Second, func() error {
resp, err = doReq("GET", "http://localhost:8800/xD", APIKey, nil)
return err
})
assert.NoError(t, err)
assert.Equal(t, resp.StatusCode, http.StatusNotFound, "Server should respond with http status code 404")

Expand All @@ -50,7 +60,12 @@ func TestStart(t *testing.T) {

scenario("Test availability of metrics server", func() {
let("Query metrics endpoint", func(t *testing.T) {
resp, err := doReq("GET", "http://localhost:8600/metrics", APIKey, nil)
var resp *http.Response
var err error
retry(3, 1*time.Second, func() error {
resp, err = doReq("GET", "http://localhost:8600/metrics", APIKey, nil)
return err
})
assert.NoError(t, err, "Subprocess must not exit with non-zero status")
assert.Equal(t, resp.StatusCode, http.StatusOK, "Server should respond with http status code 200")
})
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ func setupServer(id int, joinAddr string, tls bool, t *testing.T) (scope.TestF,
t.Log(err)
}
})()
time.Sleep(2 * time.Second)
}

after := func(t *testing.T) {
Expand Down Expand Up @@ -328,6 +327,7 @@ func getClient(t *testing.T, id int) *client.HTTPClient {
client.SetAPIKey(APIKey),
client.SetTopologyDiscovery(false),
client.SetHealthchecks(false),
client.SetMaxRetries(3),
)
if err != nil {
t.Fatal(errors.Wrap(err, "Cannot start http client: "))
Expand Down

0 comments on commit 5bee53e

Please sign in to comment.