-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server: first pass at Shutdown and Close (#34)
* server: first pass at Shutdown and Close * server: remove unused struct field * server: replace http reference in error closes: #22
- Loading branch information
Showing
2 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package ssh | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestServerShutdown(t *testing.T) { | ||
l := newLocalListener() | ||
testBytes := []byte("Hello world\n") | ||
s := &Server{ | ||
Handler: func(s Session) { | ||
s.Write(testBytes) | ||
time.Sleep(50 * time.Millisecond) | ||
}, | ||
} | ||
go func() { | ||
err := s.Serve(l) | ||
if err != nil && err != ErrServerClosed { | ||
t.Fatal(err) | ||
} | ||
}() | ||
sessDone := make(chan struct{}) | ||
sess, cleanup := newClientSession(t, l.Addr().String(), nil) | ||
go func() { | ||
defer cleanup() | ||
defer close(sessDone) | ||
var stdout bytes.Buffer | ||
sess.Stdout = &stdout | ||
if err := sess.Run(""); err != nil { | ||
t.Fatal(err) | ||
} | ||
if !bytes.Equal(stdout.Bytes(), testBytes) { | ||
t.Fatalf("expected = %s; got %s", testBytes, stdout.Bytes()) | ||
} | ||
}() | ||
|
||
srvDone := make(chan struct{}) | ||
go func() { | ||
defer close(srvDone) | ||
err := s.Shutdown(context.Background()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
}() | ||
|
||
timeout := time.After(2 * time.Second) | ||
select { | ||
case <-timeout: | ||
t.Fatal("timeout") | ||
return | ||
case <-srvDone: | ||
// TODO: add timeout for sessDone | ||
<-sessDone | ||
return | ||
} | ||
} | ||
|
||
func TestServerClose(t *testing.T) { | ||
l := newLocalListener() | ||
s := &Server{ | ||
Handler: func(s Session) { | ||
time.Sleep(5 * time.Second) | ||
}, | ||
} | ||
go func() { | ||
err := s.Serve(l) | ||
if err != nil && err != ErrServerClosed { | ||
t.Fatal(err) | ||
} | ||
}() | ||
|
||
doneCh := make(chan struct{}) | ||
sess, cleanup := newClientSession(t, l.Addr().String(), nil) | ||
go func() { | ||
defer cleanup() | ||
defer close(doneCh) | ||
if err := sess.Run(""); err != nil && err != io.EOF { | ||
t.Fatal(err) | ||
} | ||
}() | ||
|
||
go func() { | ||
err := s.Close() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
}() | ||
|
||
timeout := time.After(100 * time.Millisecond) | ||
select { | ||
case <-timeout: | ||
t.Error("timeout") | ||
return | ||
case <-s.getDoneChan(): | ||
<-doneCh | ||
return | ||
} | ||
} |