Fix test instabilities by refactoring to make address dynamic and readable

This commit is contained in:
Marc Di Luzio 2020-06-06 11:52:12 +01:00
parent bc366583a4
commit 1d2087e2b9
7 changed files with 82 additions and 31 deletions

View file

@ -2,6 +2,7 @@ package main
import (
"flag"
"fmt"
"os"
"path"
"testing"
@ -11,14 +12,26 @@ import (
"github.com/stretchr/testify/assert"
)
var address string
func TestMain(m *testing.M) {
s := server.NewServer(server.OptionPort(8080))
s.Initialise()
s := server.NewServer()
if err := s.Initialise(); err != nil {
fmt.Println(err)
os.Exit(1)
}
address = s.Addr()
go s.Run()
fmt.Printf("Test server hosted on %s", address)
code := m.Run()
s.Close()
if err := s.Close(); err != nil {
fmt.Println(err)
os.Exit(1)
}
os.Exit(code)
}
@ -31,7 +44,7 @@ func Test_InnerMain(t *testing.T) {
assert.Error(t, InnerMain("status"))
// Now set the host
flag.Set("host", "localhost:8080")
flag.Set("host", address)
// No error now as we have a host
assert.NoError(t, InnerMain("status"))