|
| 1 | +package check |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +type OnCompletionHook func() |
| 12 | +type CheckSuite struct { |
| 13 | + Name string |
| 14 | + Checks []*Check |
| 15 | + OnCompletion OnCompletionHook |
| 16 | + ErrOnSetup error |
| 17 | + executionTime time.Duration |
| 18 | + processed bool |
| 19 | + clean bool |
| 20 | +} |
| 21 | + |
| 22 | +func NewCheckSuite(name string) *CheckSuite { |
| 23 | + return &CheckSuite{Name: name} |
| 24 | +} |
| 25 | + |
| 26 | +func (h *CheckSuite) Process(parentCtx context.Context) { |
| 27 | + ctx, cancel := context.WithCancel(parentCtx) |
| 28 | + start := time.Now() |
| 29 | + for _, check := range h.Checks { |
| 30 | + check.Process() |
| 31 | + } |
| 32 | + h.executionTime = RoundDuration(time.Since(start), 2) |
| 33 | + h.processed = true |
| 34 | + h.runOnCompletion() |
| 35 | + cancel() |
| 36 | + |
| 37 | + select { |
| 38 | + case <-ctx.Done(): |
| 39 | + // Handle timeout |
| 40 | + if errors.Is(ctx.Err(), context.DeadlineExceeded) { |
| 41 | + h.executionTime = RoundDuration(time.Since(start), 2) |
| 42 | + h.processed = true |
| 43 | + h.runOnCompletion() |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func (h *CheckSuite) runOnCompletion() { |
| 49 | + if h.clean { |
| 50 | + return |
| 51 | + } |
| 52 | + if h.OnCompletion != nil { |
| 53 | + h.OnCompletion() |
| 54 | + } |
| 55 | + h.clean = true |
| 56 | +} |
| 57 | + |
| 58 | +func (h *CheckSuite) AddCheck(name string, checkFunc CheckFunction) *Check { |
| 59 | + check := &Check{Name: name, CheckFunc: checkFunc} |
| 60 | + h.Checks = append(h.Checks, check) |
| 61 | + return check |
| 62 | +} |
| 63 | + |
| 64 | +func (h *CheckSuite) Passed() bool { |
| 65 | + for _, check := range h.Checks { |
| 66 | + if !check.Passed() { |
| 67 | + return false |
| 68 | + } |
| 69 | + } |
| 70 | + return true |
| 71 | +} |
| 72 | + |
| 73 | +func (h *CheckSuite) Result() string { |
| 74 | + checkStr := []string{} |
| 75 | + for _, check := range h.Checks { |
| 76 | + checkStr = append(checkStr, check.Result()) |
| 77 | + } |
| 78 | + return strings.Join(checkStr, "\n") |
| 79 | +} |
| 80 | + |
| 81 | +func (h *CheckSuite) RawResult() string { |
| 82 | + checkStr := []string{} |
| 83 | + for _, check := range h.Checks { |
| 84 | + checkStr = append(checkStr, check.RawResult()) |
| 85 | + } |
| 86 | + return strings.Join(checkStr, "\n") |
| 87 | +} |
| 88 | + |
| 89 | +// Print will send output straight to stdout. |
| 90 | +func (h *CheckSuite) Print() { |
| 91 | + if h.processed { |
| 92 | + for _, check := range h.Checks { |
| 93 | + fmt.Println(check.Result()) |
| 94 | + } |
| 95 | + fmt.Printf("Total execution time of %q checks: %s\n", h.Name, h.executionTime) |
| 96 | + } else { |
| 97 | + if len(h.Checks) > 0 { |
| 98 | + fmt.Printf("%q hasn't been processed. %d check(s) pending evaluation.\n", h.Name, len(h.Checks)) |
| 99 | + } else { |
| 100 | + fmt.Printf("%q has no checks to evaluate.\n", h.Name) |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments