-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathoutputconsumer.go
122 lines (102 loc) · 2.68 KB
/
outputconsumer.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
package termtest
import (
"fmt"
"sync"
"time"
)
type consumer func(buffer string) (matchEndPos int, err error)
type outputConsumer struct {
_test_id string // for testing purposes only, not used for non-testing logic
consume consumer
waiter chan error
opts *OutputConsumerOpts
isalive bool
mutex *sync.Mutex
}
type OutputConsumerOpts struct {
*Opts
Timeout time.Duration
}
type SetConsOpt func(o *OutputConsumerOpts)
func OptConsInherit(o *Opts) func(o *OutputConsumerOpts) {
return func(oco *OutputConsumerOpts) {
oco.Opts = o
}
}
func OptsConsTimeout(timeout time.Duration) func(o *OutputConsumerOpts) {
return func(oco *OutputConsumerOpts) {
oco.Timeout = timeout
}
}
func newOutputConsumer(consume consumer, opts ...SetConsOpt) *outputConsumer {
oc := &outputConsumer{
consume: consume,
opts: &OutputConsumerOpts{
Opts: NewOpts(),
Timeout: 5 * time.Second, // Default timeout
},
waiter: make(chan error, 1),
isalive: true,
mutex: &sync.Mutex{},
}
for _, optSetter := range opts {
optSetter(oc.opts)
}
return oc
}
func (e *outputConsumer) IsAlive() bool {
return e.isalive
}
// Report will consume the given buffer and will block unless wait() has been called
func (e *outputConsumer) Report(buffer []byte) (int, error) {
e.mutex.Lock()
defer e.mutex.Unlock()
pos, err := e.consume(string(buffer))
if err != nil {
err = fmt.Errorf("consumer threw error: %w", err)
}
if err == nil && pos > len(buffer) {
err = fmt.Errorf("consumer returned endPos %d which is greater than buffer length %d", pos, len(buffer))
}
if err != nil || pos > 0 {
e.opts.Logger.Printf("closing waiter from report, err: %v, endPos: %d\n", err, pos)
// This prevents report() from blocking in case wait() has not been called yet
go func() {
e.waiter <- err
}()
}
return pos, err
}
type errConsumerStopped struct {
reason error
}
func (e errConsumerStopped) Error() string {
return fmt.Sprintf("consumer stopped, reason: %s", e.reason)
}
func (e errConsumerStopped) Unwrap() error {
return e.reason
}
func (e *outputConsumer) Stop(reason error) {
e.opts.Logger.Printf("stopping consumer, reason: %s\n", reason)
e.waiter <- errConsumerStopped{reason}
}
func (e *outputConsumer) wait() error {
e.opts.Logger.Println("started waiting")
defer e.opts.Logger.Println("stopped waiting")
defer func() {
e.isalive = false
e.mutex.Unlock()
}()
select {
case err := <-e.waiter:
e.mutex.Lock()
if err != nil {
e.opts.Logger.Printf("Encountered error: %s\n", err.Error())
}
return err
case <-time.After(e.opts.Timeout):
e.mutex.Lock()
e.opts.Logger.Println("Encountered timeout")
return fmt.Errorf("after %s: %w", e.opts.Timeout, TimeoutError)
}
}