-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathshovel_test.go
67 lines (50 loc) · 1.29 KB
/
shovel_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
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
package sshego
import (
"bytes"
"testing"
"time"
cv "github.com/glycerine/goconvey/convey"
)
func TestShovelStops(t *testing.T) {
cv.Convey("a Shovel should stop when requested", t, func() {
s := newShovel(false)
a := newMockRwc([]byte("hello_from_a"))
b := newMockRwc([]byte("hello_from_b"))
s.Start(b, a, "b<-a")
<-s.Halt.ReadyChan()
time.Sleep(100 * time.Millisecond)
s.Stop()
cv.So(b.sink.String(), cv.ShouldResemble, "hello_from_a")
cv.So(a.sink.String(), cv.ShouldResemble, "")
})
cv.Convey("a ShovelPair should stop when requested", t, func() {
s := newShovelPair(false)
a := newMockRwc([]byte("hello_from_a"))
b := newMockRwc([]byte("hello_from_b"))
s.Start(a, b, "a<-b", "b->a")
<-s.Halt.ReadyChan()
time.Sleep(1 * time.Millisecond)
s.Stop()
cv.So(b.sink.String(), cv.ShouldResemble, "hello_from_a")
cv.So(a.sink.String(), cv.ShouldResemble, "hello_from_b")
})
}
type mockRwc struct {
src *bytes.Buffer
sink *bytes.Buffer
}
func newMockRwc(src []byte) *mockRwc {
return &mockRwc{
src: bytes.NewBuffer(src),
sink: bytes.NewBuffer(nil),
}
}
func (m *mockRwc) Read(p []byte) (n int, err error) {
return m.src.Read(p)
}
func (m *mockRwc) Write(p []byte) (n int, err error) {
return m.sink.Write(p)
}
func (m *mockRwc) Close() error {
return nil
}