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