Skip to content

Commit

Permalink
fix: healthcheck flakey test (#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
wesl-ee committed Sep 13, 2024
1 parent da17d94 commit ceb671d
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions service/servers/oracle/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"fmt"
"io"
"math/big"
"net"
"net/http"
"strconv"
"testing"
"time"

Expand All @@ -29,6 +27,7 @@ import (

const (
localhost = "localhost"
port = "8080"
timeout = 1 * time.Second
delay = 20 * time.Second
grpcErrPrefix = "rpc error: code = Unknown desc = "
Expand All @@ -43,7 +42,6 @@ type ServerTestSuite struct {
httpClient *http.Client
ctx context.Context
cancel context.CancelFunc
port string
}

func TestServerTestSuite(t *testing.T) {
Expand All @@ -57,15 +55,10 @@ func (s *ServerTestSuite) SetupTest() {
s.mockOracle = mocks.NewOracle(s.T())
s.srv = server.NewOracleServer(s.mockOracle, logger)

// listen on a random port and extract that port number
l, err := net.Listen("tcp", localhost+":0")
s.Require().NoError(err)
defer l.Close()
s.port = strconv.Itoa(l.Addr().(*net.TCPAddr).Port)

var err error
s.client, err = client.NewClient(
log.NewTestLogger(s.T()),
localhost+":"+s.port,
localhost+":"+port,
timeout,
metrics.NewNopMetrics(),
client.WithBlockingDial(), // block on dialing the server
Expand All @@ -79,11 +72,23 @@ func (s *ServerTestSuite) SetupTest() {
s.ctx, s.cancel = context.WithCancel(context.Background())

// start server + client w/ context
go s.srv.StartServer(s.ctx, "0.0.0.0", s.port)
go s.srv.StartServer(s.ctx, localhost, port)

dialCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
s.Require().NoError(s.client.Start(dialCtx))

// Health check
for i := 0; ; i++ {
_, err := s.httpClient.Get(fmt.Sprintf("http://%s:%s/slinky/oracle/v1/prices", localhost, port))
if err == nil {
break
}
if i == 10 {
s.T().Fatal("failed to connect to server")
}
time.Sleep(1 * time.Second)
}
}

// teardown test suite.
Expand Down Expand Up @@ -164,7 +169,7 @@ func (s *ServerTestSuite) TestOracleServerPrices() {
s.Require().Equal(resp.Timestamp, ts.UTC())

// call from http client
httpResp, err := s.httpClient.Get(fmt.Sprintf("http://%s:%s/slinky/oracle/v1/prices", localhost, s.port))
httpResp, err := s.httpClient.Get(fmt.Sprintf("http://%s:%s/slinky/oracle/v1/prices", localhost, port))
s.Require().NoError(err)

// check response
Expand Down

0 comments on commit ceb671d

Please sign in to comment.