-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmock_flusher.go
63 lines (55 loc) · 1.2 KB
/
mock_flusher.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
package buffer
import (
"errors"
"sync"
"time"
)
var errErrInput = errors.New("error input")
// stringCounter counts how many times does string appears.
type stringCounter struct {
mu *sync.Mutex
m map[string]int
errInput string
mockFlushCost time.Duration
}
func newStringCounter(errInput string, flushCost time.Duration) *stringCounter {
return &stringCounter{
mu: &sync.Mutex{},
m: make(map[string]int),
errInput: errInput,
mockFlushCost: flushCost,
}
}
// Flush implements Flusher interface.
func (c *stringCounter) Flush(str []string) error {
time.Sleep(c.mockFlushCost)
c.mu.Lock()
defer c.mu.Unlock()
for _, v := range str {
if v == c.errInput && c.errInput != "" {
return errErrInput
}
vv := c.m[v]
vv++
c.m[v] = vv
}
return nil
}
func (c *stringCounter) result() map[string]int {
c.mu.Lock()
defer c.mu.Unlock()
result := make(map[string]int, len(c.m))
for k, v := range c.m {
result[k] = v
}
return result
}
// errRecoder records err and err elements
type errRecoder struct {
err error
elements []string
}
func (e *errRecoder) log(err error, elements []string) {
e.err = err
e.elements = elements
}