-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook_manager.go
159 lines (128 loc) · 4.24 KB
/
hook_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package handoff
import (
"context"
"fmt"
"log/slog"
"sync"
// "github.com/raphi011/handoff/internal/hook"
"github.com/raphi011/handoff/internal/hook"
"github.com/raphi011/handoff/internal/model"
)
type TestFinishedListener interface {
Hook
TestFinished(suite model.TestSuite, run model.TestSuiteRun, testName string, context model.TestContext)
}
type AsyncTestFinishedListener interface {
Hook
TestFinishedAsync(suite model.TestSuite, run model.TestSuiteRun, testName string, context map[string]any, callback AsyncHookCallback)
}
type TestSuiteFinishedListener interface {
Hook
TestSuiteFinished(suite model.TestSuite, run model.TestSuiteRun)
}
type AsyncTestSuiteFinishedListener interface {
Hook
TestSuiteFinishedAsync(suite model.TestSuite, run model.TestSuiteRun, callback func(context map[string]any))
}
// AsyncHookCallback allows async hooks to add additional context
// to a testsuite or testrun.
type AsyncHookCallback func(context map[string]any)
type Hook interface {
Name() string
Init() error
}
type hookManager struct {
all []Hook
testFinished []TestFinishedListener
testFinishedAsync []AsyncTestFinishedListener
testSuiteFinished []TestSuiteFinishedListener
testSuiteFinishedAsync []AsyncTestSuiteFinishedListener
asyncCallback asyncHookCallback
asyncHooksRunning sync.WaitGroup
log *slog.Logger
}
type asyncHookCallback func(p Hook, context map[string]any)
func newHookManager(hookCallback asyncHookCallback, log *slog.Logger) *hookManager {
return &hookManager{
all: []Hook{},
testFinished: []TestFinishedListener{},
testFinishedAsync: []AsyncTestFinishedListener{},
testSuiteFinished: []TestSuiteFinishedListener{},
testSuiteFinishedAsync: []AsyncTestSuiteFinishedListener{},
asyncCallback: hookCallback,
log: log,
}
}
// TODO: do not pass slack specific stuff here. THis is only for testing purposes
func (s *hookManager) init(channelID, slackToken string) error {
if slackToken != "" && channelID != "" {
s.all = append(s.all, hook.NewSlackHook(channelID, slackToken, s.log))
}
for _, p := range s.all {
if err := p.Init(); err != nil {
return fmt.Errorf("initiating hook %q: %w", p.Name(), err)
}
registeredHook := false
if l, ok := p.(TestFinishedListener); ok {
s.testFinished = append(s.testFinished, l)
registeredHook = true
}
if l, ok := p.(TestSuiteFinishedListener); ok {
s.testSuiteFinished = append(s.testSuiteFinished, l)
registeredHook = true
}
if l, ok := p.(AsyncTestSuiteFinishedListener); ok {
s.testSuiteFinishedAsync = append(s.testSuiteFinishedAsync, l)
registeredHook = true
}
if !registeredHook {
return fmt.Errorf("hook %q does not implement any listener", p.Name())
}
}
return nil
}
func (s *hookManager) shutdown() context.Context {
cancelCtx, cancel := context.WithCancel(context.Background())
go func() {
s.asyncHooksRunning.Wait()
cancel()
}()
return cancelCtx
}
func (s *hookManager) notifyTestSuiteFinished(suite model.TestSuite, testSuiteRun model.TestSuiteRun) {
for _, p := range s.testSuiteFinished {
p.TestSuiteFinished(suite, testSuiteRun)
}
}
func (s *hookManager) notifyTestSuiteFinishedAsync(suite model.TestSuite, testSuiteRun model.TestSuiteRun) {
for _, p := range s.testSuiteFinishedAsync {
s.asyncHooksRunning.Add(1)
hook := p
go func() {
// TODO catch panics
defer s.asyncHooksRunning.Done()
hook.TestSuiteFinishedAsync(suite, testSuiteRun, s.newAsyncHookCallback(hook))
}()
}
}
func (s *hookManager) notifyTestFinished(suite model.TestSuite, testRun model.TestSuiteRun, name string, runContext model.TestContext) {
for _, p := range s.testFinished {
p.TestFinished(suite, testRun, name, runContext)
}
}
func (s *hookManager) notifyTestFinishedAync(suite model.TestSuite, testRun model.TestSuiteRun, name string, runContext map[string]any) {
for _, p := range s.testFinishedAsync {
s.asyncHooksRunning.Add(1)
hook := p
go func() {
// TODO catch panics
defer s.asyncHooksRunning.Done()
hook.TestFinishedAsync(suite, testRun, name, runContext, s.newAsyncHookCallback(hook))
}()
}
}
func (s *hookManager) newAsyncHookCallback(p Hook) AsyncHookCallback {
return func(c map[string]any) {
s.asyncCallback(p, c)
}
}