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 | |
| 15 | // TestEventual creates a new subtest looping the given function until it |
| 16 | // either doesn't return an error anymore or the timeout is exceeded. The last |
| 17 | // returned non-context-related error is being used as the test error. |
| 18 | func TestEventual(t *testing.T, name string, ctx context.Context, timeout time.Duration, f func(context.Context) error) { |
Serge Bazanski | 05f813b | 2023-03-16 17:58:39 +0100 | [diff] [blame] | 19 | start := time.Now() |
Mateusz Zalega | ddf19b4 | 2022-06-22 12:27:37 +0200 | [diff] [blame] | 20 | ctx, cancel := context.WithTimeout(ctx, timeout) |
| 21 | t.Helper() |
Serge Bazanski | 05f813b | 2023-03-16 17:58:39 +0100 | [diff] [blame] | 22 | launch.Log("Test: %s: starting...", name) |
Mateusz Zalega | ddf19b4 | 2022-06-22 12:27:37 +0200 | [diff] [blame] | 23 | t.Run(name, func(t *testing.T) { |
| 24 | defer cancel() |
| 25 | 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] | 26 | for { |
| 27 | err := f(ctx) |
| 28 | if err == nil { |
Serge Bazanski | 05f813b | 2023-03-16 17:58:39 +0100 | [diff] [blame] | 29 | 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] | 30 | return |
| 31 | } |
| 32 | if err == ctx.Err() { |
| 33 | t.Fatal(lastErr) |
| 34 | } |
Serge Bazanski | 9104e38 | 2023-04-04 20:08:21 +0200 | [diff] [blame^] | 35 | if errors.Is(err, &PermanentError{}) { |
| 36 | t.Fatal(err) |
| 37 | } |
Mateusz Zalega | ddf19b4 | 2022-06-22 12:27:37 +0200 | [diff] [blame] | 38 | lastErr = err |
| 39 | select { |
| 40 | case <-ctx.Done(): |
| 41 | t.Fatal(lastErr) |
| 42 | case <-time.After(1 * time.Second): |
| 43 | } |
| 44 | } |
| 45 | }) |
| 46 | } |
Serge Bazanski | 9104e38 | 2023-04-04 20:08:21 +0200 | [diff] [blame^] | 47 | |
| 48 | // PermanentError can be returned inside TestEventual to indicate that the test |
| 49 | // is 'stuck', that it will not make progress anymore and that it should be |
| 50 | // failed immediately. |
| 51 | type PermanentError struct { |
| 52 | Err error |
| 53 | } |
| 54 | |
| 55 | func (p *PermanentError) Error() string { |
| 56 | return fmt.Sprintf("test permanently failed: %v", p.Err) |
| 57 | } |
| 58 | |
| 59 | func (p *PermanentError) Unwrap() error { |
| 60 | return p.Err |
| 61 | } |
| 62 | |
| 63 | func (p *PermanentError) Is(o error) bool { |
| 64 | op, ok := o.(*PermanentError) |
| 65 | if !ok { |
| 66 | return false |
| 67 | } |
| 68 | if p.Err == nil || op.Err == nil { |
| 69 | return true |
| 70 | } |
| 71 | return errors.Is(p.Err, op.Err) |
| 72 | } |
| 73 | |
| 74 | // Permanent wraps the given error into a PermanentError, which will cause |
| 75 | // TestEventual to immediately fail the test it's returned within. |
| 76 | func Permanent(err error) error { |
| 77 | return &PermanentError{ |
| 78 | Err: err, |
| 79 | } |
| 80 | } |