Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: healthcheck flakey test #744

Merged
merged 3 commits into from
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading