diff --git a/cmd/testExecutor.go b/cmd/testExecutor.go index fa4de4f..c3e8948 100644 --- a/cmd/testExecutor.go +++ b/cmd/testExecutor.go @@ -73,13 +73,14 @@ func runTestAndWait(mc connectors.MicrocksClient, params testParams) (bool, stri future := now + params.waitForMillis + 10000 var success = false + var inProgress = true for nowInMilliseconds() < future { testResultSummary, err := mc.GetTestResult(testResultID) if err != nil { return false, "", fmt.Errorf("checking test result: %w", err) } success = testResultSummary.Success - inProgress := testResultSummary.InProgress + inProgress = testResultSummary.InProgress if _, err := fmt.Fprintf(progress, "MicrocksClient got status for test \"%s\" - success: %s, inProgress: %s \n", testResultID, fmt.Sprint(success), fmt.Sprint(inProgress)); err != nil { return false, testResultID, errors.Wrap(errors.KindEnvironment, err) } @@ -94,6 +95,13 @@ func runTestAndWait(mc connectors.MicrocksClient, params testParams) (bool, stri time.Sleep(2 * time.Second) } + // If the polling deadline expired while the test was still running, return a KindConnection error + if inProgress { + return false, testResultID, errors.Wrapf(errors.KindConnection, + "test polling timed out after %dms waiting for result %s", + params.waitForMillis+10000, testResultID) + } + if !params.suppressOutput { if err := renderTestResult(mc, testResultID, params.outputFormat, params.artifactPath); err != nil { return false, testResultID, err diff --git a/cmd/testExecutor_test.go b/cmd/testExecutor_test.go new file mode 100644 index 0000000..7071f72 --- /dev/null +++ b/cmd/testExecutor_test.go @@ -0,0 +1,58 @@ +/* + * Copyright The Microcks Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package cmd + +import ( + "testing" + + "github.com/microcks/microcks-cli/pkg/connectors" + "github.com/microcks/microcks-cli/pkg/errors" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +type mockMicrocksClientPollingTimeout struct { + connectors.MicrocksClient +} + +func (m *mockMicrocksClientPollingTimeout) CreateTestResult(serviceID, testEndpoint, runnerType, secretName string, timeout int64, filteredOperations, operationsHeaders, oAuth2Context string) (string, error) { + return "test-result-timeout-id", nil +} + +func (m *mockMicrocksClientPollingTimeout) GetTestResult(testResultID string) (*connectors.TestResultSummary, error) { + return &connectors.TestResultSummary{ + ID: testResultID, + Success: false, + InProgress: true, + }, nil +} + +func TestRunTestAndWait_TimeoutReturnsKindConnection(t *testing.T) { + client := &mockMicrocksClientPollingTimeout{} + params := testParams{ + waitForMillis: -10000, // deadline <= now so the polling loop deadline has already expired + suppressOutput: true, + } + + success, id, err := runTestAndWait(client, params) + + assert.False(t, success) + assert.Equal(t, "test-result-timeout-id", id) + require.Error(t, err) + assert.Equal(t, errors.KindConnection, errors.KindOf(err)) + assert.Equal(t, 11, ExitCodeFor(err)) + assert.Contains(t, err.Error(), "test polling timed out after 0ms waiting for result test-result-timeout-id") +}