jarcoal/httpmock

Failed to run tests in parallel with Ginkgo

mentalmap opened this issue · 2 comments

DEMO

package main

import (
	"log"
	"net/http"
	"os"
	"testing"

	"github.com/jarcoal/httpmock"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = BeforeSuite(func() {
	httpmock.Activate()
})

var _ = BeforeEach(func() {
	httpmock.Reset()
})

var _ = AfterSuite(func() {
	httpmock.DeactivateAndReset()
})

var _ = Describe("Articles", func() {
	It("case a", func() {
		httpmock.RegisterResponder("GET", "https://1.com",
			httpmock.NewStringResponder(200, ""))

		resp, err := http.Get("https://1.com")
		Expect(err).Should(BeNil())
		Expect(resp.StatusCode).To(Equal(200))

		log.Println(os.Getpid())
	})

	It("case b", func() {
		httpmock.RegisterResponder("GET", "https://2.com",
			httpmock.NewStringResponder(200, ""))

		resp, err := http.Get("https://2.com")
		Expect(err).Should(BeNil())
		Expect(resp.StatusCode).To(Equal(200))

		log.Println(os.Getpid())
	})
})

func TestParallel(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "Test Suite")
}

Exec

ginkgo -nodes=2

Result

Running Suite: Test Suite
=========================
Random Seed: 1589013597
Will run 2 specs

Running in parallel across 2 nodes

failed to iterate over tests:
Get "http://127.0.0.1:53853/counter": no responder found


failed to iterate over tests:
Get "http://127.0.0.1:53853/counter": no responder found


Ran 0 of 0 Specs in 0.002 seconds
FAIL! -- 0 Passed | 0 Failed | 0 Pending | 0 Skipped


Ginkgo ran 1 suite in 3.272456673s
Test Suite Failed

Hi,
it seems something is trying to reach http://127.0.0.1:53853/counter, probably ginkgo itself as I see it implements this route here.

httpmock.Activate() overrides http.DefaultTransport with its own transport which seems to be used by ginko to request its own server. That is why the ginkgo request is intercepted by httpmock.

To avoid that, you should use your own http.Client for your requests, and so use ActivateNonDefault() on it to avoid this conflict.

@maxatome now is ok, thanks.

change

httpmock.Activate()

to

httpmock.ActivateNonDefault(http.DefaultClient)