sftp/request-server_test.go

39 lines
874 B
Go
Raw Normal View History

2016-07-09 03:38:35 +08:00
package sftp
import (
"github.com/stretchr/testify/assert"
"io"
"testing"
)
2016-07-12 01:58:51 +08:00
func clientRequestServerPair(t *testing.T) (*Client, *RequestServer) {
2016-07-09 03:38:35 +08:00
cr, sw := io.Pipe()
sr, cw := io.Pipe()
2016-07-12 01:58:51 +08:00
server, err := NewRequestServer(struct {
2016-07-09 03:38:35 +08:00
io.Reader
io.WriteCloser
}{sr, sw})
if err != nil { t.Fatal(err) }
go server.Serve()
client, err := NewClientPipe(cr, cw)
if err != nil { t.Fatalf("%+v\n", err) }
return client, server
}
func TestPsRequestCache(t *testing.T) {
2016-07-12 04:15:17 +08:00
_, rs := clientRequestServerPair(t)
2016-07-09 03:38:35 +08:00
foo := &Request{Filepath: "foo"}
bar := &Request{Filepath: "bar"}
2016-07-12 04:15:17 +08:00
rs.nextRequest(foo)
rs.nextRequest(bar)
assert.Len(t, rs.openRequests, 2)
_foo, ok := rs.getRequest("foo")
2016-07-09 03:38:35 +08:00
assert.Equal(t, foo, _foo)
assert.True(t, ok)
2016-07-12 04:15:17 +08:00
_, ok = rs.getRequest("zed")
2016-07-09 03:38:35 +08:00
assert.False(t, ok)
2016-07-12 04:15:17 +08:00
rs.closeRequest("foo")
rs.closeRequest("bar")
assert.Len(t, rs.openRequests, 0)
2016-07-09 03:38:35 +08:00
}