Serge Bazanski | ac6b644 | 2020-05-06 19:13:43 +0200 | [diff] [blame] | 1 | // 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 | |
| 17 | package supervisor |
Serge Bazanski | f8a8e65 | 2021-07-06 16:23:43 +0200 | [diff] [blame] | 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "errors" |
Serge Bazanski | ec19b60 | 2022-03-09 20:41:31 +0100 | [diff] [blame] | 22 | "log" |
| 23 | "sort" |
Serge Bazanski | f8a8e65 | 2021-07-06 16:23:43 +0200 | [diff] [blame] | 24 | "testing" |
Serge Bazanski | ec19b60 | 2022-03-09 20:41:31 +0100 | [diff] [blame] | 25 | "time" |
Serge Bazanski | f8a8e65 | 2021-07-06 16:23:43 +0200 | [diff] [blame] | 26 | |
| 27 | "source.monogon.dev/metropolis/pkg/logtree" |
| 28 | ) |
| 29 | |
| 30 | // TestHarness runs a supervisor in a harness designed for unit testing |
| 31 | // runnables and runnable trees. |
| 32 | // |
| 33 | // The given runnable will be run in a new supervisor, and the logs from this |
| 34 | // supervisor will be streamed to stderr. If the runnable returns a non-context |
| 35 | // error, the harness will throw a test error, but will not abort the test. |
| 36 | // |
| 37 | // The harness also returns a context cancel function that can be used to |
Serge Bazanski | ec19b60 | 2022-03-09 20:41:31 +0100 | [diff] [blame] | 38 | // terminate the started supervisor early. Regardless of manual cancellation, |
Serge Bazanski | f8a8e65 | 2021-07-06 16:23:43 +0200 | [diff] [blame] | 39 | // the supervisor will always be terminated up at the end of the test/benchmark |
Serge Bazanski | ec19b60 | 2022-03-09 20:41:31 +0100 | [diff] [blame] | 40 | // it's running in. The supervision tree will also be cleaned up and the test |
| 41 | // will block until all runnables have exited. |
Serge Bazanski | f8a8e65 | 2021-07-06 16:23:43 +0200 | [diff] [blame] | 42 | // |
| 43 | // The second returned value is the logtree used by this supervisor. It can be |
| 44 | // used to assert some log messages are emitted in tests that exercise some |
| 45 | // log-related functionality. |
Serge Bazanski | e25b3a4 | 2023-03-17 00:07:53 +0100 | [diff] [blame] | 46 | func TestHarness(t testing.TB, r func(ctx context.Context) error) (context.CancelFunc, *logtree.LogTree) { |
Serge Bazanski | f8a8e65 | 2021-07-06 16:23:43 +0200 | [diff] [blame] | 47 | t.Helper() |
| 48 | |
| 49 | ctx, ctxC := context.WithCancel(context.Background()) |
Serge Bazanski | f8a8e65 | 2021-07-06 16:23:43 +0200 | [diff] [blame] | 50 | |
| 51 | lt := logtree.New() |
Serge Bazanski | e25b3a4 | 2023-03-17 00:07:53 +0100 | [diff] [blame] | 52 | |
| 53 | // Only log to stderr when we're running in a test, not in a fuzz harness or a |
| 54 | // benchmark - otherwise we just waste CPU cycles. |
| 55 | verbose := false |
| 56 | if _, ok := t.(*testing.T); ok { |
| 57 | verbose = true |
| 58 | } |
| 59 | if verbose { |
Serge Bazanski | 29974f3 | 2023-04-05 12:29:09 +0200 | [diff] [blame] | 60 | logtree.PipeAllToTest(t, lt) |
Serge Bazanski | e25b3a4 | 2023-03-17 00:07:53 +0100 | [diff] [blame] | 61 | } |
Serge Bazanski | f8a8e65 | 2021-07-06 16:23:43 +0200 | [diff] [blame] | 62 | |
Serge Bazanski | ec19b60 | 2022-03-09 20:41:31 +0100 | [diff] [blame] | 63 | sup := New(ctx, func(ctx context.Context) error { |
Serge Bazanski | 3379a5d | 2021-09-09 12:56:40 +0200 | [diff] [blame] | 64 | Logger(ctx).Infof("Starting test %s...", t.Name()) |
Serge Bazanski | f8a8e65 | 2021-07-06 16:23:43 +0200 | [diff] [blame] | 65 | if err := r(ctx); err != nil && !errors.Is(err, ctx.Err()) { |
| 66 | t.Errorf("Supervised runnable in harness returned error: %v", err) |
Serge Bazanski | 826a9e9 | 2021-10-05 21:23:48 +0200 | [diff] [blame] | 67 | return err |
Serge Bazanski | f8a8e65 | 2021-07-06 16:23:43 +0200 | [diff] [blame] | 68 | } |
| 69 | return nil |
| 70 | }, WithExistingLogtree(lt)) |
Serge Bazanski | ec19b60 | 2022-03-09 20:41:31 +0100 | [diff] [blame] | 71 | |
| 72 | t.Cleanup(func() { |
Serge Bazanski | ec19b60 | 2022-03-09 20:41:31 +0100 | [diff] [blame] | 73 | ctxC() |
Serge Bazanski | e25b3a4 | 2023-03-17 00:07:53 +0100 | [diff] [blame] | 74 | if verbose { |
| 75 | log.Printf("supervisor.TestHarness: Waiting for supervisor runnables to die...") |
| 76 | } |
Serge Bazanski | ec19b60 | 2022-03-09 20:41:31 +0100 | [diff] [blame] | 77 | timeoutNag := time.Now().Add(5 * time.Second) |
| 78 | |
| 79 | for { |
| 80 | live := sup.liveRunnables() |
| 81 | if len(live) == 0 { |
Serge Bazanski | e25b3a4 | 2023-03-17 00:07:53 +0100 | [diff] [blame] | 82 | if verbose { |
| 83 | log.Printf("supervisor.TestHarness: All done.") |
| 84 | } |
Serge Bazanski | ec19b60 | 2022-03-09 20:41:31 +0100 | [diff] [blame] | 85 | return |
| 86 | } |
| 87 | |
| 88 | if time.Now().After(timeoutNag) { |
| 89 | timeoutNag = time.Now().Add(5 * time.Second) |
| 90 | sort.Strings(live) |
Serge Bazanski | e25b3a4 | 2023-03-17 00:07:53 +0100 | [diff] [blame] | 91 | if verbose { |
| 92 | log.Printf("supervisor.TestHarness: Still live:") |
| 93 | for _, l := range live { |
| 94 | log.Printf("supervisor.TestHarness: - %s", l) |
| 95 | } |
Serge Bazanski | ec19b60 | 2022-03-09 20:41:31 +0100 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | time.Sleep(time.Second) |
| 100 | } |
| 101 | }) |
Serge Bazanski | f8a8e65 | 2021-07-06 16:23:43 +0200 | [diff] [blame] | 102 | return ctxC, lt |
| 103 | } |