forked from cloudfoundry/gorouter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake_byte_emitter.go
51 lines (41 loc) · 876 Bytes
/
fake_byte_emitter.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
package fake
import (
"sync"
)
type FakeByteEmitter struct {
ReturnError error
Messages [][]byte
mutex *sync.RWMutex
isClosed bool
}
func NewFakeByteEmitter() *FakeByteEmitter {
return &FakeByteEmitter{mutex: new(sync.RWMutex)}
}
func (f *FakeByteEmitter) Emit(data []byte) (err error) {
if f.ReturnError != nil {
err = f.ReturnError
f.ReturnError = nil
return
}
f.mutex.Lock()
defer f.mutex.Unlock()
f.Messages = append(f.Messages, data)
return
}
func (f *FakeByteEmitter) GetMessages() (messages [][]byte) {
f.mutex.Lock()
defer f.mutex.Unlock()
messages = make([][]byte, len(f.Messages))
copy(messages, f.Messages)
return
}
func (f *FakeByteEmitter) Close() {
f.mutex.Lock()
defer f.mutex.Unlock()
f.isClosed = true
}
func (f *FakeByteEmitter) IsClosed() bool {
f.mutex.RLock()
defer f.mutex.RUnlock()
return f.isClosed
}