-
Notifications
You must be signed in to change notification settings - Fork 54
/
taskutils_test.go
40 lines (33 loc) · 994 Bytes
/
taskutils_test.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
package oonimkall
//
// This file contains testing code reused by other `_test.go` files.
//
import "sync"
// CollectorTaskEmitter is a thread-safe taskEmitter
// that stores all the events inside itself.
type CollectorTaskEmitter struct {
// events contains the events
events []*event
// mu provides mutual exclusion
mu sync.Mutex
}
// ensures that a CollectorTaskEmitter is a taskEmitter.
var _ taskEmitter = &CollectorTaskEmitter{}
// Emit implements the taskEmitter.Emit method.
func (e *CollectorTaskEmitter) Emit(key string, value interface{}) {
e.mu.Lock()
e.events = append(e.events, &event{Key: key, Value: value})
e.mu.Unlock()
}
// Collect returns a copy of the collected events. It is safe
// to read the events. It's a data race to modify them.
//
// After this function has been called, the internal array
// of events will now be empty.
func (e *CollectorTaskEmitter) Collect() (out []*event) {
e.mu.Lock()
out = e.events
e.events = nil
e.mu.Unlock()
return
}