pact-go icon indicating copy to clipboard operation
pact-go copied to clipboard

panic when accessing pact.Server.Port before setting up an interaction

Open mohanraj-r opened this issue 7 years ago • 5 comments

Software versions

  • OS: Mac OSX 10.13.2
  • Consumer Pact library:
$ pact-go version
Pact Go CLI v0.0.11
  • Provider Pact library: v0.0.12
  • Golang Version: 1.9.2
  • Golang environment:
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/mohanrajr/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.9.2/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.9.2/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/d0/742yxrrx0glg93s4z_rwyzkxm0whbv/T/go-build176059632=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

Expected behaviour

pact.Server.Port should return the port once dsl.Pact{} is created

Actual behaviour

Panics instead

2018/01/23 12:11:04 [DEBUG] teardown
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x13fe54c]

goroutine 4 [running]:
testing.tRunner.func1(0xc4201880f0)
	/usr/local/Cellar/go/1.9.2/libexec/src/testing/testing.go:711 +0x2d2
panic(0x14756e0, 0x177e580)
	/usr/local/Cellar/go/1.9.2/libexec/src/runtime/panic.go:491 +0x283

Steps to reproduce

      // Create Pact connecting to local Daemon
	pact := &dsl.Pact{
		Host:     pactDaemonHost,
		Port:     pactDaemonPort,
		Consumer: "Consumer",
		Provider: "Provider",
	}
	defer pact.Teardown()

        // accessing pact server port results in a panic before setting up an interaction
	// sleeping doesn't help either
	//time.Sleep(10 * time.Second)
	fmt.Println("Pact port before:", pact.Server.Port)

	// Set up expected interactions
	pact.
		AddInteraction()

      // accessing pact server port works as expected after setting up the interaction
	fmt.Println("Pact port after:", pact.Server.Port)

Relevant log files

NA

mohanraj-r avatar Jan 23 '18 20:01 mohanraj-r

Thanks!

mefellows avatar Jan 23 '18 22:01 mefellows

I guess the workaround is to call pact.Setup(true) immediately after initializing the struct and before calling pact.Server.Port. Wondering what a fix to this would look like in pact.go .. because there is no easy way to force initialization of a struct in a specific way in Go. Could add a method e.g. GetPact() or New() that returns a initialized pact struct after calling setup on it .. but then it would need to take all/most of the params of the Pact{} struct - or have the struct as the arg itself. Maybe the temp workaround is to document that Setup() needs to be called if the pact needs to be used before calling AddInteraction() ?

mohanraj-r avatar Mar 13 '18 23:03 mohanraj-r

I think a more idiomatic go way would be to create a NewPact(...) type interface to it, that would do this for us, or potentially just wrapping Port into a GetPort() accessor.

That being said, what's your use case where this is required so early?

mefellows avatar Mar 14 '18 00:03 mefellows

a more idiomatic go way would be to create a NewPact(...) type interface to it

👍

what's your use case where this is required so early?

As part of the common setup required that is reused by multiple tests I am doing

        pact       = getPact()
	pactServer = fmt.Sprintf("%s:%d", pactDaemonHost, pact.Server.Port)

My current workaround is to call pact.Setup(true) in getPact() in addition to initializing a pact.

mohanraj-r avatar Mar 14 '18 00:03 mohanraj-r

I think that workaround is fine. A NewPact function will just do that anyway.

Also, moving forward this interface will change to accommodation not-http contracts and this will disappear entirely.

mefellows avatar Mar 14 '18 00:03 mefellows

Fixed in 2.x.x, closing.

mefellows avatar Mar 07 '23 11:03 mefellows