commands: Fix failing Travis server test

This commit is contained in:
Bjørn Erik Pedersen 2018-04-12 09:31:53 +02:00
parent 14c35c8a56
commit 9c782d5147
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
2 changed files with 9 additions and 16 deletions

View file

@ -41,8 +41,6 @@ import (
type serverCmd struct { type serverCmd struct {
// Can be used to stop the server. Useful in tests // Can be used to stop the server. Useful in tests
stop <-chan bool stop <-chan bool
// Can be used to receive notification about when the server is started. Useful in tests.
started chan<- bool
disableLiveReload bool disableLiveReload bool
navigateToChanged bool navigateToChanged bool
@ -60,11 +58,11 @@ type serverCmd struct {
} }
func newServerCmd() *serverCmd { func newServerCmd() *serverCmd {
return newServerCmdSignaled(nil, nil) return newServerCmdSignaled(nil)
} }
func newServerCmdSignaled(stop <-chan bool, started chan<- bool) *serverCmd { func newServerCmdSignaled(stop <-chan bool) *serverCmd {
cc := &serverCmd{stop: stop, started: started} cc := &serverCmd{stop: stop}
cc.baseBuilderCmd = newBuilderCmd(&cobra.Command{ cc.baseBuilderCmd = newBuilderCmd(&cobra.Command{
Use: "server", Use: "server",
@ -400,10 +398,6 @@ func (c *commandeer) serve(s *serverCmd) error {
}() }()
} }
if s.started != nil {
s.started <- true
}
jww.FEEDBACK.Println("Press Ctrl+C to stop") jww.FEEDBACK.Println("Press Ctrl+C to stop")
if s.stop != nil { if s.stop != nil {

View file

@ -38,9 +38,9 @@ func TestServer(t *testing.T) {
os.RemoveAll(dir) os.RemoveAll(dir)
}() }()
stop, started := make(chan bool), make(chan bool) stop := make(chan bool)
scmd := newServerCmdSignaled(stop, started) scmd := newServerCmdSignaled(stop)
cmd := scmd.getCommand() cmd := scmd.getCommand()
cmd.SetArgs([]string{"-s=" + dir, fmt.Sprintf("-p=%d", port)}) cmd.SetArgs([]string{"-s=" + dir, fmt.Sprintf("-p=%d", port)})
@ -50,11 +50,10 @@ func TestServer(t *testing.T) {
assert.NoError(err) assert.NoError(err)
}() }()
select { // There is no way to know exactly when the server is ready for connections.
case <-started: // We could improve by something like https://golang.org/pkg/net/http/httptest/#Server
case <-time.After(2 * time.Second): // But for now, let us sleep and pray!
t.Fatal("server start took too long") time.Sleep(2 * time.Second)
}
resp, err := http.Get("http://localhost:1331/") resp, err := http.Get("http://localhost:1331/")
assert.NoError(err) assert.NoError(err)