github.com/devops-filetransfer/sshego@v7.0.4+incompatible/shovel_test.go (about)

     1  package sshego
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  	"time"
     7  
     8  	cv "github.com/glycerine/goconvey/convey"
     9  )
    10  
    11  func TestShovelStops(t *testing.T) {
    12  
    13  	cv.Convey("a Shovel should stop when requested", t, func() {
    14  
    15  		s := newShovel(false)
    16  
    17  		a := newMockRwc([]byte("hello_from_a"))
    18  		b := newMockRwc([]byte("hello_from_b"))
    19  
    20  		s.Start(b, a, "b<-a")
    21  		<-s.Halt.ReadyChan()
    22  		time.Sleep(100 * time.Millisecond)
    23  		s.Stop()
    24  		cv.So(b.sink.String(), cv.ShouldResemble, "hello_from_a")
    25  		cv.So(a.sink.String(), cv.ShouldResemble, "")
    26  	})
    27  
    28  	cv.Convey("a ShovelPair should stop when requested", t, func() {
    29  
    30  		s := newShovelPair(false)
    31  
    32  		a := newMockRwc([]byte("hello_from_a"))
    33  		b := newMockRwc([]byte("hello_from_b"))
    34  
    35  		s.Start(a, b, "a<-b", "b->a")
    36  		<-s.Halt.ReadyChan()
    37  		time.Sleep(1 * time.Millisecond)
    38  		s.Stop()
    39  		cv.So(b.sink.String(), cv.ShouldResemble, "hello_from_a")
    40  		cv.So(a.sink.String(), cv.ShouldResemble, "hello_from_b")
    41  	})
    42  
    43  }
    44  
    45  type mockRwc struct {
    46  	src  *bytes.Buffer
    47  	sink *bytes.Buffer
    48  }
    49  
    50  func newMockRwc(src []byte) *mockRwc {
    51  	return &mockRwc{
    52  		src:  bytes.NewBuffer(src),
    53  		sink: bytes.NewBuffer(nil),
    54  	}
    55  }
    56  
    57  func (m *mockRwc) Read(p []byte) (n int, err error) {
    58  	return m.src.Read(p)
    59  }
    60  
    61  func (m *mockRwc) Write(p []byte) (n int, err error) {
    62  	return m.sink.Write(p)
    63  }
    64  
    65  func (m *mockRwc) Close() error {
    66  	return nil
    67  }