blob: dcc9eac3be4bdf616b94c230cd3132bef6e045d2 [file] [log] [blame]
Lorenz Brunfc5dbc62020-05-28 12:18:07 +02001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package e2e
18
19import (
20 "context"
21 "errors"
22 "testing"
23 "time"
24)
25
Serge Bazanski216fe7b2021-05-21 18:36:16 +020026// testEventual creates a new subtest looping the given function until it
27// either doesn't return an error anymore or the timeout is exceeded. The last
28// returned non-context-related error is being used as the test error.
Lorenz Brunfc5dbc62020-05-28 12:18:07 +020029func testEventual(t *testing.T, name string, ctx context.Context, timeout time.Duration, f func(context.Context) error) {
30 ctx, cancel := context.WithTimeout(ctx, timeout)
31 t.Helper()
32 t.Run(name, func(t *testing.T) {
33 defer cancel()
34 var lastErr = errors.New("test didn't run to completion at least once")
35 t.Parallel()
36 for {
37 err := f(ctx)
38 if err == nil {
39 return
40 }
41 if err == ctx.Err() {
42 t.Fatal(lastErr)
43 }
44 lastErr = err
45 select {
46 case <-ctx.Done():
47 t.Fatal(lastErr)
48 case <-time.After(1 * time.Second):
49 }
50 }
51 })
52}