| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| Mateusz Zalega | ddf19b4 | 2022-06-22 12:27:37 +0200 | [diff] [blame] | 4 | // This file implements test helper functions that augment the way any given |
| 5 | // test is run. |
| 6 | package util |
| 7 | |
| 8 | import ( |
| 9 | "context" |
| 10 | "errors" |
| Serge Bazanski | 9104e38 | 2023-04-04 20:08:21 +0200 | [diff] [blame] | 11 | "fmt" |
| Mateusz Zalega | ddf19b4 | 2022-06-22 12:27:37 +0200 | [diff] [blame] | 12 | "testing" |
| 13 | "time" |
| 14 | ) |
| 15 | |
| Serge Bazanski | c16f048 | 2024-03-21 11:57:41 +0100 | [diff] [blame] | 16 | // TestEventual creates a new subtest looping the given function until it either |
| 17 | // doesn't return an error anymore, the timeout is exceeded or PermanentError is |
| 18 | // returned. The last returned non-context-related error is being used as the |
| 19 | // test error. |
| 20 | func TestEventual(t *testing.T, name string, ctx context.Context, timeout time.Duration, f func(context.Context) error) bool { |
| Serge Bazanski | 05f813b | 2023-03-16 17:58:39 +0100 | [diff] [blame] | 21 | start := time.Now() |
| Mateusz Zalega | ddf19b4 | 2022-06-22 12:27:37 +0200 | [diff] [blame] | 22 | ctx, cancel := context.WithTimeout(ctx, timeout) |
| 23 | t.Helper() |
| Tim Windelschmidt | 47eb65b | 2025-03-27 16:49:27 +0100 | [diff] [blame] | 24 | fmt.Printf("Test: %s: starting...\n", name) |
| Serge Bazanski | c16f048 | 2024-03-21 11:57:41 +0100 | [diff] [blame] | 25 | return t.Run(name, func(t *testing.T) { |
| Mateusz Zalega | ddf19b4 | 2022-06-22 12:27:37 +0200 | [diff] [blame] | 26 | defer cancel() |
| 27 | var lastErr = errors.New("test didn't run to completion at least once") |
| Mateusz Zalega | ddf19b4 | 2022-06-22 12:27:37 +0200 | [diff] [blame] | 28 | for { |
| 29 | err := f(ctx) |
| 30 | if err == nil { |
| Tim Windelschmidt | 47eb65b | 2025-03-27 16:49:27 +0100 | [diff] [blame] | 31 | fmt.Printf("Test: %s: okay after %.1f seconds\n", name, time.Since(start).Seconds()) |
| Mateusz Zalega | ddf19b4 | 2022-06-22 12:27:37 +0200 | [diff] [blame] | 32 | return |
| 33 | } |
| Tim Windelschmidt | d5f851b | 2024-04-23 14:59:37 +0200 | [diff] [blame] | 34 | if errors.Is(err, ctx.Err()) { |
| Mateusz Zalega | ddf19b4 | 2022-06-22 12:27:37 +0200 | [diff] [blame] | 35 | t.Fatal(lastErr) |
| 36 | } |
| Serge Bazanski | 9104e38 | 2023-04-04 20:08:21 +0200 | [diff] [blame] | 37 | if errors.Is(err, &PermanentError{}) { |
| 38 | t.Fatal(err) |
| 39 | } |
| Mateusz Zalega | ddf19b4 | 2022-06-22 12:27:37 +0200 | [diff] [blame] | 40 | lastErr = err |
| 41 | select { |
| 42 | case <-ctx.Done(): |
| 43 | t.Fatal(lastErr) |
| 44 | case <-time.After(1 * time.Second): |
| 45 | } |
| 46 | } |
| 47 | }) |
| 48 | } |
| Serge Bazanski | 9104e38 | 2023-04-04 20:08:21 +0200 | [diff] [blame] | 49 | |
| Serge Bazanski | c16f048 | 2024-03-21 11:57:41 +0100 | [diff] [blame] | 50 | // MustTestEventual is like TestEventual, but aborts the `t` test with Fatal if a |
| 51 | // timeout occurred or PermanentError was returned. |
| 52 | func MustTestEventual(t *testing.T, name string, ctx context.Context, timeout time.Duration, f func(context.Context) error) { |
| 53 | if !TestEventual(t, name, ctx, timeout, f) { |
| 54 | t.Fatalf("Test: %s: fatal failure", name) |
| 55 | } |
| 56 | } |
| 57 | |
| Serge Bazanski | 9104e38 | 2023-04-04 20:08:21 +0200 | [diff] [blame] | 58 | // PermanentError can be returned inside TestEventual to indicate that the test |
| 59 | // is 'stuck', that it will not make progress anymore and that it should be |
| 60 | // failed immediately. |
| 61 | type PermanentError struct { |
| 62 | Err error |
| 63 | } |
| 64 | |
| 65 | func (p *PermanentError) Error() string { |
| 66 | return fmt.Sprintf("test permanently failed: %v", p.Err) |
| 67 | } |
| 68 | |
| 69 | func (p *PermanentError) Unwrap() error { |
| 70 | return p.Err |
| 71 | } |
| 72 | |
| 73 | func (p *PermanentError) Is(o error) bool { |
| Tim Windelschmidt | af821c8 | 2024-04-23 15:03:52 +0200 | [diff] [blame] | 74 | var op *PermanentError |
| 75 | if !errors.As(o, &op) { |
| Serge Bazanski | 9104e38 | 2023-04-04 20:08:21 +0200 | [diff] [blame] | 76 | return false |
| 77 | } |
| 78 | if p.Err == nil || op.Err == nil { |
| 79 | return true |
| 80 | } |
| 81 | return errors.Is(p.Err, op.Err) |
| 82 | } |
| 83 | |
| 84 | // Permanent wraps the given error into a PermanentError, which will cause |
| 85 | // TestEventual to immediately fail the test it's returned within. |
| 86 | func Permanent(err error) error { |
| 87 | return &PermanentError{ |
| 88 | Err: err, |
| 89 | } |
| 90 | } |