Skip to content

Commit

Permalink
feat(client): allow customization of client timeout (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaelollivier authored and mefellows committed Jul 13, 2018
1 parent a9a544c commit 5275db7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
17 changes: 8 additions & 9 deletions dsl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ import (
"github.com/pact-foundation/pact-go/types"
)

var (
timeoutDuration = 10 * time.Second
)

// PactClient is the main interface into starting/stopping
// the underlying Pact CLI subsystem
type PactClient struct {
Expand All @@ -36,6 +32,9 @@ type PactClient struct {

// Address the Daemon is listening on
Address string

// TimeoutDuration specifies how long to wait for Pact CLI to start
TimeoutDuration time.Duration
}

// newClient creates a new Pact client manager with the provided services
Expand Down Expand Up @@ -63,8 +62,8 @@ func (p *PactClient) StartServer(args []string, port int) *types.MockServer {
svc := p.pactMockSvcManager.NewService(args)
cmd := svc.Start()

waitForPort(port, p.getNetworkInterface(), p.Address, fmt.Sprintf(`Timed out waiting for Mock Server to
start on port %d - are you sure it's running?`, port))
waitForPort(port, p.getNetworkInterface(), p.Address, p.TimeoutDuration,
fmt.Sprintf(`Timed out waiting for Mock Server to start on port %d - are you sure it's running?`, port))

return &types.MockServer{
Pid: cmd.Process.Pid,
Expand Down Expand Up @@ -122,8 +121,8 @@ func (p *PactClient) VerifyProvider(request types.VerifyRequest) (types.Provider

port := getPort(request.ProviderBaseURL)

waitForPort(port, p.getNetworkInterface(), p.Address, fmt.Sprintf(`Timed out waiting for Provider API to start
on port %d - are you sure it's running?`, port))
waitForPort(port, p.getNetworkInterface(), p.Address, p.TimeoutDuration,
fmt.Sprintf(`Timed out waiting for Provider API to start on port %d - are you sure it's running?`, port))

// Run command, splitting out stderr and stdout. The command can fail for
// several reasons:
Expand Down Expand Up @@ -312,7 +311,7 @@ func getPort(rawURL string) int {

// Use this to wait for a port to be running prior
// to running tests.
var waitForPort = func(port int, network string, address string, message string) error {
var waitForPort = func(port int, network string, address string, timeoutDuration time.Duration, message string) error {
log.Println("[DEBUG] waiting for port", port, "to become available")
timeout := time.After(timeoutDuration)

Expand Down
8 changes: 1 addition & 7 deletions dsl/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,12 @@ func TestClient_StartServer(t *testing.T) {
}
}

var oldTimeoutDuration = timeoutDuration

func TestClient_StartServerFail(t *testing.T) {
timeoutDuration = 50 * time.Millisecond

client, _ := createClient(false)
server := client.StartServer([]string{}, 0)
if server.Port != 0 {
t.Fatalf("Expected server to be empty %v", server)
}
timeoutDuration = oldTimeoutDuration
}

func TestClient_StopServer(t *testing.T) {
Expand All @@ -58,7 +53,6 @@ func TestClient_StopServer(t *testing.T) {
}

func TestClient_StopServerFail(t *testing.T) {
timeoutDuration = 50 * time.Millisecond
client, _ := createClient(true)
res, err := client.StopServer(&types.MockServer{})
should := &types.MockServer{}
Expand All @@ -68,7 +62,6 @@ func TestClient_StopServerFail(t *testing.T) {
if err != nil {
t.Fatalf("wanted error, got none")
}
timeoutDuration = oldTimeoutDuration
}

func TestClient_VerifyProvider(t *testing.T) {
Expand Down Expand Up @@ -213,6 +206,7 @@ func createClient(success bool) (*PactClient, *ServiceMock) {
}()

d := newClient(svc, svc, svc)
d.TimeoutDuration = 50 * time.Millisecond
return d, svc
}

Expand Down
15 changes: 13 additions & 2 deletions dsl/pact.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"path/filepath"
"reflect"
"testing"
"time"

"github.com/hashicorp/logutils"
"github.com/pact-foundation/pact-go/install"
Expand Down Expand Up @@ -88,6 +89,11 @@ type Pact struct {
// the tests, which should speed up large test suites significantly
DisableToolValidityCheck bool

// ClientTimeout specifies how long to wait for Pact CLI to start
// Can be increased to reduce likelihood of intermittent failure
// Defaults to 10s
ClientTimeout time.Duration

// Check if CLI tools are up to date
toolValidityCheck bool
}
Expand Down Expand Up @@ -144,8 +150,13 @@ func (p *Pact) Setup(startMockServer bool) *Pact {
p.SpecificationVersion = 2
}

if p.ClientTimeout == 0 {
p.ClientTimeout = 10 * time.Second
}

if p.pactClient == nil {
p.pactClient = NewClient()
p.pactClient.TimeoutDuration = p.ClientTimeout
}

if p.PactFileWriteMode == "" {
Expand Down Expand Up @@ -463,8 +474,8 @@ func (p *Pact) VerifyMessageProviderRaw(request VerifyMessageRequest) (types.Pro
log.Printf("[DEBUG] API handler starting: port %d (%s)", port, ln.Addr())
go http.Serve(ln, mux)

portErr := waitForPort(port, "tcp", "localhost", fmt.Sprintf(`Timed out waiting for Daemon on port %d - are you
sure it's running?`, port))
portErr := waitForPort(port, "tcp", "localhost", p.ClientTimeout,
fmt.Sprintf(`Timed out waiting for Daemon on port %d - are you sure it's running?`, port))

if portErr != nil {
log.Fatal("Error:", err)
Expand Down
3 changes: 2 additions & 1 deletion dsl/pact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"strings"
"testing"
"time"

"github.com/pact-foundation/pact-go/types"
)
Expand Down Expand Up @@ -372,7 +373,7 @@ func captureOutput(action func()) string {
func stubPorts() func() {
log.Println("Stubbing port timeout")
old := waitForPort
waitForPort = func(int, string, string, string) error {
waitForPort = func(int, string, string, time.Duration, string) error {
return nil
}
return func() { waitForPort = old }
Expand Down

0 comments on commit 5275db7

Please sign in to comment.