-
Notifications
You must be signed in to change notification settings - Fork 568
/
fake.go
106 lines (96 loc) · 1.73 KB
/
fake.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
package connection
import (
"fmt"
"github.com/hdt3213/godis/lib/logger"
"io"
"sync"
)
// FakeConn implements redis.Connection for test
type FakeConn struct {
Connection
buf []byte
offset int
waitOn chan struct{}
closed bool
mu sync.Mutex
}
func NewFakeConn() *FakeConn {
c := &FakeConn{}
return c
}
// Write writes data to buffer
func (c *FakeConn) Write(b []byte) (int, error) {
if c.closed {
return 0, io.EOF
}
c.mu.Lock()
c.buf = append(c.buf, b...)
c.mu.Unlock()
c.notify()
return len(b), nil
}
func (c *FakeConn) notify() {
if c.waitOn != nil {
c.mu.Lock()
if c.waitOn != nil {
logger.Debug(fmt.Sprintf("notify %p", c.waitOn))
close(c.waitOn)
c.waitOn = nil
}
c.mu.Unlock()
}
}
func (c *FakeConn) wait(offset int) {
c.mu.Lock()
if c.offset != offset { // new data during waiting lock
return
}
if c.waitOn == nil {
c.waitOn = make(chan struct{})
}
waitOn := c.waitOn
logger.Debug(fmt.Sprintf("wait on %p", waitOn))
c.mu.Unlock()
<-waitOn
logger.Debug(fmt.Sprintf("wait on %p finish", waitOn))
}
// Read reads data from buffer
func (c *FakeConn) Read(p []byte) (int, error) {
c.mu.Lock()
n := copy(p, c.buf[c.offset:])
c.offset += n
offset := c.offset
c.mu.Unlock()
if n == 0 {
if c.closed {
return n, io.EOF
}
c.wait(offset)
// after notify
if c.closed {
return n, io.EOF
}
n = copy(p, c.buf[c.offset:])
c.offset += n
return n, nil
}
if c.closed {
return n, io.EOF
}
return n, nil
}
// Clean resets the buffer
func (c *FakeConn) Clean() {
c.waitOn = make(chan struct{})
c.buf = nil
c.offset = 0
}
// Bytes returns written data
func (c *FakeConn) Bytes() []byte {
return c.buf
}
func (c *FakeConn) Close() error {
c.closed = true
c.notify()
return nil
}