-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
58 lines (50 loc) · 981 Bytes
/
client.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
package fake
import (
"sync"
"github.com/metrico/loki-apache/pkg/promtail/api"
)
// Client is a fake client used for testing.
type Client struct {
entries chan api.Entry
received []api.Entry
once sync.Once
mtx sync.Mutex
wg sync.WaitGroup
OnStop func()
}
func New(stop func()) *Client {
c := &Client{
OnStop: stop,
entries: make(chan api.Entry),
}
c.wg.Add(1)
go func() {
defer c.wg.Done()
for e := range c.entries {
c.mtx.Lock()
c.received = append(c.received, e)
c.mtx.Unlock()
}
}()
return c
}
// Stop implements client.Client
func (c *Client) Stop() {
c.once.Do(func() { close(c.entries) })
c.wg.Wait()
c.OnStop()
}
func (c *Client) Chan() chan<- api.Entry {
return c.entries
}
func (c *Client) Received() []api.Entry {
c.mtx.Lock()
defer c.mtx.Unlock()
cpy := make([]api.Entry, len(c.received))
copy(cpy, c.received)
return cpy
}
// StopNow implements client.Client
func (c *Client) StopNow() {
c.Stop()
}