From a6c8e248b8ef5c40452805d3fb0794de9547b26b Mon Sep 17 00:00:00 2001 From: Yousaf Nabi Date: Sun, 25 Feb 2024 00:20:54 +0000 Subject: [PATCH] defer cleanup plugins as rogue processes --- consumer/http.go | 3 +-- consumer/http_v4.go | 2 ++ internal/native/message_server_test.go | 3 +++ internal/native/mock_server_test.go | 1 + internal/native/verifier_test.go | 17 ++++++++++++----- message/v4/asynchronous_message.go | 10 +++++----- message/v4/synchronous_message.go | 12 +++++++----- provider/verify_request.go | 2 +- 8 files changed, 32 insertions(+), 18 deletions(-) diff --git a/consumer/http.go b/consumer/http.go index 67b166eae..593f67af1 100644 --- a/consumer/http.go +++ b/consumer/http.go @@ -130,6 +130,7 @@ func (p *httpMockProvider) configure() error { // and write the pact file if successful func (p *httpMockProvider) ExecuteTest(t *testing.T, integrationTest func(MockServerConfig) error) error { log.Println("[DEBUG] pact verify") + defer p.mockserver.CleanupPlugins() var err error if p.config.AllowedMockServerPorts != "" && p.config.Port <= 0 { @@ -170,8 +171,6 @@ func (p *httpMockProvider) ExecuteTest(t *testing.T, integrationTest func(MockSe return fmt.Errorf("pact validation failed: %+v", mismatches) } - p.mockserver.CleanupPlugins() - return p.writePact() } diff --git a/consumer/http_v4.go b/consumer/http_v4.go index 8dd986b79..9a0294d58 100644 --- a/consumer/http_v4.go +++ b/consumer/http_v4.go @@ -316,6 +316,7 @@ func (i *V4ResponseBuilder) BodyMatch(body interface{}) *V4ResponseBuilder { // ExecuteTest runs the current test case against a Mock Service. func (m *V4InteractionWithResponse) ExecuteTest(t *testing.T, integrationTest func(MockServerConfig) error) error { + defer m.provider.mockserver.CleanupPlugins() return m.provider.ExecuteTest(t, integrationTest) } @@ -432,6 +433,7 @@ type V4InteractionWithPluginResponse struct { // ExecuteTest runs the current test case against a Mock Service. func (m *V4InteractionWithPluginResponse) ExecuteTest(t *testing.T, integrationTest func(MockServerConfig) error) error { + defer m.provider.mockserver.CleanupPlugins() return m.provider.ExecuteTest(t, integrationTest) } diff --git a/internal/native/message_server_test.go b/internal/native/message_server_test.go index 22f7eb0b9..ab4f18be7 100644 --- a/internal/native/message_server_test.go +++ b/internal/native/message_server_test.go @@ -191,6 +191,7 @@ func TestGetPluginSyncMessageContentsAsBytes(t *testing.T) { m := NewMessageServer("test-message-consumer", "test-message-provider") // Protobuf plugin test + defer m.CleanupPlugins() err := m.UsingPlugin("protobuf", "0.3.14") assert.NoError(t, err) @@ -248,6 +249,7 @@ func TestGetPluginAsyncMessageContentsAsBytes(t *testing.T) { m := NewMessageServer("test-message-consumer", "test-message-provider") // Protobuf plugin test + defer m.CleanupPlugins() _ = m.UsingPlugin("protobuf", "0.3.14") i := m.NewAsyncMessageInteraction("grpc interaction") @@ -288,6 +290,7 @@ func TestGrpcPluginInteraction(t *testing.T) { m := NewMessageServer("test-message-consumer", "test-message-provider") // Protobuf plugin test + defer m.CleanupPlugins() _ = m.UsingPlugin("protobuf", "0.3.14") i := m.NewSyncMessageInteraction("grpc interaction") diff --git a/internal/native/mock_server_test.go b/internal/native/mock_server_test.go index 43215fff7..6d870ff70 100644 --- a/internal/native/mock_server_test.go +++ b/internal/native/mock_server_test.go @@ -172,6 +172,7 @@ func TestPluginInteraction(t *testing.T) { m := NewHTTPPact("test-plugin-consumer", "test-plugin-provider") // Protobuf plugin test + defer m.CleanupPlugins() _ = m.UsingPlugin("protobuf", "0.3.14") m.WithSpecificationVersion(SPECIFICATION_VERSION_V4) diff --git a/internal/native/verifier_test.go b/internal/native/verifier_test.go index 665b839ac..3e1e97350 100644 --- a/internal/native/verifier_test.go +++ b/internal/native/verifier_test.go @@ -35,6 +35,8 @@ func TestVerifier_Verify(t *testing.T) { res := v.Verify(args) assert.Error(t, res) + + // v.Shutdown() }) } @@ -42,14 +44,17 @@ func TestVerifier_NewForApplication(t *testing.T) { v := NewVerifier("pact-go", "test") assert.NotNil(t, v.handle) + v.Shutdown() } -func TestVerifier_Execute(t *testing.T) { - v := NewVerifier("pact-go", "test") - err := v.Execute() +// func TestVerifier_Execute(t *testing.T) { +// v := NewVerifier("pact-go", "test") +// err := v.Execute() - assert.NoError(t, err) -} +// assert.NoError(t, err) + +// v.Shutdown() +// } func TestVerifier_Shutdown(t *testing.T) { v := NewVerifier("pact-go", "test") @@ -59,9 +64,11 @@ func TestVerifier_Shutdown(t *testing.T) { func TestVerifier_SetProviderInfo(t *testing.T) { v := NewVerifier("pact-go", "test") v.SetProviderInfo("name", "http", "localhost", 1234, "/") + v.Shutdown() } func TestVerifier_SetConsumerFilters(t *testing.T) { v := NewVerifier("pact-go", "test") v.SetConsumerFilters([]string{"consumer1", "consumer2"}) + v.Shutdown() } diff --git a/message/v4/asynchronous_message.go b/message/v4/asynchronous_message.go index a36016cff..c1adfae9b 100644 --- a/message/v4/asynchronous_message.go +++ b/message/v4/asynchronous_message.go @@ -94,6 +94,7 @@ type AsynchronousMessageWithPluginContents struct { } func (s *AsynchronousMessageWithPluginContents) ExecuteTest(t *testing.T, integrationTest func(m AsynchronousMessage) error) error { + s.rootBuilder.pact.messageserver.CleanupPlugins() message, err := getAsynchronousMessageWithReifiedContents(s.rootBuilder.messageHandle, s.rootBuilder.Type) if err != nil { return err @@ -106,8 +107,6 @@ func (s *AsynchronousMessageWithPluginContents) ExecuteTest(t *testing.T, integr return err } - s.rootBuilder.pact.messageserver.CleanupPlugins() - return s.rootBuilder.pact.messageserver.WritePactFile(s.rootBuilder.pact.config.PactDir, false) } @@ -133,13 +132,14 @@ type AsynchronousMessageWithTransport struct { } func (s *AsynchronousMessageWithTransport) ExecuteTest(t *testing.T, integrationTest func(tc TransportConfig, m AsynchronousMessage) error) error { + defer s.rootBuilder.pact.messageserver.CleanupPlugins() + defer s.rootBuilder.pact.messageserver.CleanupMockServer(s.transport.Port) + message, err := getAsynchronousMessageWithReifiedContents(s.rootBuilder.messageHandle, s.rootBuilder.Type) if err != nil { return err } - defer s.rootBuilder.pact.messageserver.CleanupMockServer(s.transport.Port) - err = integrationTest(s.transport, message) if err != nil { @@ -277,7 +277,7 @@ func (p *AsynchronousPact) AddAsynchronousMessage() *AsynchronousMessageBuilder // request was provided. func (p *AsynchronousPact) verifyMessageConsumerRaw(messageToVerify *AsynchronousMessageBuilder, handler AsynchronousConsumer) error { log.Printf("[DEBUG] verify message") - + defer p.messageserver.CleanupPlugins() m, err := getAsynchronousMessageWithReifiedContents(messageToVerify.messageHandle, messageToVerify.Type) if err != nil { return err diff --git a/message/v4/synchronous_message.go b/message/v4/synchronous_message.go index 1c74a26b7..59ce5134d 100644 --- a/message/v4/synchronous_message.go +++ b/message/v4/synchronous_message.go @@ -208,6 +208,7 @@ type SynchronousMessageWithPluginContents struct { // Will cleanup interactions between tests within a suite // and write the pact file if successful func (m *SynchronousMessageWithPluginContents) ExecuteTest(t *testing.T, integrationTest func(m SynchronousMessage) error) error { + defer m.pact.mockserver.CleanupPlugins() message, err := getSynchronousMessageWithContents(m.messageHandle) if err != nil { return err @@ -246,13 +247,14 @@ type SynchronousMessageWithTransport struct { } func (s *SynchronousMessageWithTransport) ExecuteTest(t *testing.T, integrationTest func(tc TransportConfig, m SynchronousMessage) error) error { + defer s.pact.mockserver.CleanupPlugins() + defer s.pact.mockserver.CleanupMockServer(s.transport.Port) + message, err := getSynchronousMessageWithContents(s.messageHandle) if err != nil { return err } - defer s.pact.mockserver.CleanupMockServer(s.transport.Port) - err = integrationTest(s.transport, message) // matched := s.pact.mockserver.MockServerMatched(s.transport.Port) @@ -266,9 +268,6 @@ func (s *SynchronousMessageWithTransport) ExecuteTest(t *testing.T, integrationT if err != nil { return err } - - s.pact.mockserver.CleanupPlugins() - return s.pact.mockserver.WritePactFileForServer(s.transport.Port, s.pact.config.PactDir, false) } @@ -322,6 +321,9 @@ func (m *SynchronousPact) AddSynchronousMessage(description string) *Unconfigure // Will cleanup interactions between tests within a suite // and write the pact file if successful func (m *SynchronousMessageWithResponse) ExecuteTest(t *testing.T, integrationTest func(md SynchronousMessage) error) error { + // defer s.pact.mockserver.CleanupPlugins() + // defer s.pact.mockserver.CleanupMockServer(s.transport.Port) + defer m.pact.mockserver.CleanupPlugins() message, err := getSynchronousMessageWithContents(m.messageHandle) if err != nil { return err diff --git a/provider/verify_request.go b/provider/verify_request.go index c2ba9acb7..12d31d905 100644 --- a/provider/verify_request.go +++ b/provider/verify_request.go @@ -291,6 +291,7 @@ type outputWriter interface { } func (v *VerifyRequest) Verify(handle *native.Verifier, writer outputWriter) error { + defer handle.Shutdown() for _, transport := range v.Transports { log.Println("[DEBUG] adding transport to verification", transport) handle.AddTransport(transport.Protocol, transport.Port, transport.Path, transport.Scheme) @@ -301,7 +302,6 @@ func (v *VerifyRequest) Verify(handle *native.Verifier, writer outputWriter) err } res := handle.Execute() - defer handle.Shutdown() return res }