Skip to content

Commit

Permalink
request-server: add test cases for server errors
Browse files Browse the repository at this point in the history
  • Loading branch information
drakkan committed Nov 5, 2020
1 parent c7bfc47 commit e8953ca
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions request-server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (
var _ = fmt.Print

type csPair struct {
cli *Client
svr *RequestServer
cli *Client
svr *RequestServer
srvResult chan error
}

// these must be closed in order, else client.Close will hang
Expand All @@ -39,6 +40,9 @@ func clientRequestServerPair(t *testing.T) *csPair {
skipIfPlan9(t)
ready := make(chan bool)
os.Remove(sock) // either this or signal handling
pair := &csPair{
srvResult: make(chan error),
}
var server *RequestServer
go func() {
l, err := net.Listen("unix", sock)
Expand All @@ -55,7 +59,8 @@ func clientRequestServerPair(t *testing.T) *csPair {
options = append(options, WithRSAllocator())
}
server = NewRequestServer(fd, handlers, options...)
server.Serve()
err = server.Serve()
pair.srvResult <- err
}()
<-ready
defer os.Remove(sock)
Expand All @@ -65,7 +70,9 @@ func clientRequestServerPair(t *testing.T) *csPair {
if err != nil {
t.Fatalf("%+v\n", err)
}
return &csPair{client, server}
pair.svr = server
pair.cli = client
return pair
}

func checkRequestServerAllocator(t *testing.T, p *csPair) {
Expand Down Expand Up @@ -714,6 +721,34 @@ func TestRequestReaddir(t *testing.T) {
checkRequestServerAllocator(t, p)
}

func TestCleanDisconnect(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()

err := p.cli.conn.Close()
require.NoError(t, err)
// server must return io.EOF after a clean client close
// with no pending open requests
err = <-p.srvResult
require.EqualError(t, err, io.EOF.Error())
checkRequestServerAllocator(t, p)
}

func TestUncleanDisconnect(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()

foo := NewRequest("", "foo")
p.svr.nextRequest(foo)
err := p.cli.conn.Close()
require.NoError(t, err)
// the foo request above is still open after the client disconnects
// so the server will convert io.EOF to io.ErrUnexpectedEOF
err = <-p.srvResult
require.EqualError(t, err, io.ErrUnexpectedEOF.Error())
checkRequestServerAllocator(t, p)
}

func TestCleanPath(t *testing.T) {
assert.Equal(t, "/", cleanPath("/"))
assert.Equal(t, "/", cleanPath("."))
Expand Down

0 comments on commit e8953ca

Please sign in to comment.