Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
refactor test and add framework function to allow for initial failure…
Browse files Browse the repository at this point in the history
…s before requiring only success

Signed-off-by: Thomas Stringer <thstring@microsoft.com>
  • Loading branch information
trstringer committed Aug 31, 2021
1 parent 62a3851 commit f296f7c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
26 changes: 5 additions & 21 deletions tests/e2e/e2e_multiple_ports_per_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,31 +80,15 @@ func testMultipleServicePorts() {
Destination: fmt.Sprintf("%s.%s", serverService.Name, serverService.Namespace),
}

// Make a set amount of HTTP requests from the client application to
// the server application. We should not use the helper function
// WaitForRepeatedSuccess, because at the moment this function does
// accept a certain amount of failure as long as there is an uninterrupted
// set amount of success. In this case, we want 100% of all requests to
// succeed without any failure. In the future we can consider adding
// a test framework function (e.g. WaitForAllSuccess) if this logic is
// also needed in other tests.
allRequestsSucceeded := true
failureStatusCode := 0
for i := 0; i < 10; i++ {
cond := Td.WaitForSuccessAfterInitialFailure(func() bool {
result := Td.HTTPRequest(clientToServerRequest)
if result.Err != nil || result.StatusCode != 200 {
Td.T.Logf("> HTTP request failed %d %v", result.StatusCode, result.Err)
allRequestsSucceeded = false
failureStatusCode = result.StatusCode
break
return false
}
Td.T.Logf("> HTTP request succeeded")
}

failureMessage := fmt.Sprintf(
"Client failed to connect to server: %d",
failureStatusCode,
)
Expect(allRequestsSucceeded).To(BeTrue(), failureMessage)
return true
}, 10, 90*time.Second)
Expect(cond).To(BeTrue())
})
}
23 changes: 23 additions & 0 deletions tests/framework/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,29 @@ func (td *OsmTestData) WaitForRepeatedSuccess(f SuccessFunction, minItForSuccess
return false
}

// WaitForSuccessAfterInitialFailure runs and expects a certain result for a certain operation a set number of consecutive times
// but requires only success after the first success.
func (td *OsmTestData) WaitForSuccessAfterInitialFailure(f SuccessFunction, minItForSuccess int, maxWaitTime time.Duration) bool {
iterations := 0
startTime := time.Now()
successHasStarted := false

By(fmt.Sprintf("[WaitForSuccessAfterFailureBuffer] waiting %v for %d iterations to succeed", maxWaitTime, minItForSuccess))
for time.Since(startTime) < maxWaitTime {
if f() {
successHasStarted = true
iterations++
if iterations >= minItForSuccess {
return true
}
} else if successHasStarted {
return false
}
time.Sleep(time.Second)
}
return false
}

// Cleanup is Used to cleanup resources once the test is done
// Cyclomatic complexity is disabled in cleanup, as it check a large number of conditions
// nolint:gocyclo
Expand Down

0 comments on commit f296f7c

Please sign in to comment.