@@ -347,7 +347,14 @@ func (p *Pact) VerifyProviderRaw(request types.VerifyRequest) ([]types.ProviderV
347
347
// This maps the 'description' field of a message pact, to a function handler
348
348
// that will implement the message producer. This function must return an object and optionally
349
349
// and error. The object will be marshalled to JSON for comparison.
350
- port , err := proxy .HTTPReverseProxy (opts )
350
+ listener , err := proxy .HTTPReverseProxy (opts )
351
+ if err != nil {
352
+ log .Printf ("[ERROR] unable to start http verification proxy: %v" , err )
353
+ return nil , err
354
+ }
355
+ defer listener .Close ()
356
+
357
+ port := listener .Addr ().(* net.TCPAddr ).Port
351
358
352
359
// Backwards compatibility, setup old provider states URL if given
353
360
// Otherwise point to proxy
@@ -669,14 +676,18 @@ func (p *Pact) VerifyMessageProviderRaw(request VerifyMessageRequest) ([]types.P
669
676
// and error. The object will be marshalled to JSON for comparison.
670
677
mux := http .NewServeMux ()
671
678
672
- port , err := utils . GetFreePort ( )
679
+ ln , err := net . Listen ( "tcp" , "localhost:0" )
673
680
if err != nil {
674
- return response , fmt .Errorf ("unable to allocate a port for verification: %v" , err )
681
+ log .Printf ("[ERROR] unable to allocate a port for verification: %v" , err )
682
+ return nil , err
675
683
}
684
+ defer ln .Close ()
685
+
686
+ log .Printf ("[DEBUG] API handler starting at %s" , ln .Addr ())
676
687
677
688
// Construct verifier request
678
689
verificationRequest := types.VerifyRequest {
679
- ProviderBaseURL : fmt .Sprintf ("http://localhost:%d " , port ),
690
+ ProviderBaseURL : fmt .Sprintf ("http://%s " , ln . Addr () ),
680
691
PactURLs : request .PactURLs ,
681
692
BrokerURL : request .BrokerURL ,
682
693
Tags : request .Tags ,
@@ -695,25 +706,18 @@ func (p *Pact) VerifyMessageProviderRaw(request VerifyMessageRequest) ([]types.P
695
706
696
707
mux .HandleFunc ("/" , messageVerificationHandler (request .MessageHandlers , request .StateHandlers ))
697
708
698
- ln , err := net .Listen ("tcp" , fmt .Sprintf (":%d" , port ))
699
- if err != nil {
700
- log .Fatal (err )
701
- }
702
- defer ln .Close ()
703
-
704
- log .Printf ("[DEBUG] API handler starting: port %d (%s)" , port , ln .Addr ())
705
709
go func () {
706
- if err := http .Serve (ln , mux ); err != nil {
707
- // NOTE: calling Fatalf causing test failures due to "accept tcp [::]:<port>: use of closed network connection"
710
+ if err := http .Serve (ln , mux ); err != nil && ! strings .HasSuffix (err .Error (), "use of closed network connection" ) {
708
711
log .Printf ("[DEBUG] API handler start failed: %v" , err )
709
712
}
710
713
}()
711
714
715
+ port := ln .Addr ().(* net.TCPAddr ).Port
716
+
712
717
portErr := waitForPort (port , "tcp" , "localhost" , p .ClientTimeout ,
713
718
fmt .Sprintf (`Timed out waiting for pact proxy on port %d - check for errors` , port ))
714
719
715
720
if portErr != nil {
716
- log .Fatal ("Error:" , err )
717
721
return response , portErr
718
722
}
719
723
0 commit comments