Fix tests (#12)
* This fixes the tests for me locally * Fixing spelling * Adding examples back * I think race is killing the server before all the tests run * Check if server is running
This commit is contained in:
parent
b67b84f263
commit
3e11dcff04
1
go.sum
1
go.sum
@ -11,6 +11,7 @@ github.com/stretchr/testify v1.6.0 h1:jlIyCplCJFULU/01vCkhKuTyc3OorI3bJFuw6obfgh
|
||||
github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
golang.org/x/net v0.0.0-20181220203305-927f97764cc3 h1:eH6Eip3UpmR+yM/qI9Ijluzb1bNv/cAU/n+6l8tRSis=
|
||||
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
@ -646,8 +646,7 @@ func (s *Server) Serve(l net.Listener) error {
|
||||
for {
|
||||
rw, err := l.Accept()
|
||||
if err != nil {
|
||||
fmt.Errorf("error acceptig new client: %s", err)
|
||||
return err
|
||||
return fmt.Errorf("error accepting new client: %s", err)
|
||||
}
|
||||
|
||||
c := s.newConn(rw)
|
||||
|
@ -5,15 +5,67 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/prologic/go-gopher"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
ch := startTestServer()
|
||||
defer stopTestServer(ch)
|
||||
|
||||
// Because it can take some time for the server to spin up
|
||||
// the tests are inconsistent - they'll fail if the server isn't
|
||||
// ready, but pass otherwise. This problem seems more pronounced
|
||||
// when running via the makefile.
|
||||
//
|
||||
// It seems like there should be a better way to do this
|
||||
for attempts := 3; attempts > 0; attempts-- {
|
||||
_, err := gopher.Get("gopher://localhost:7000")
|
||||
if err == nil {
|
||||
fmt.Println("Server ready")
|
||||
break
|
||||
}
|
||||
fmt.Printf("Server not ready, going to try again in a sec. %v", err)
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
}
|
||||
/////
|
||||
|
||||
code := m.Run()
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
func hello(w gopher.ResponseWriter, r *gopher.Request) {
|
||||
w.WriteInfo("Hello World!")
|
||||
}
|
||||
|
||||
func startTestServer() chan bool {
|
||||
quit := make(chan bool)
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-quit:
|
||||
return
|
||||
default:
|
||||
gopher.Handle("/", gopher.FileServer(gopher.Dir("./testdata")))
|
||||
gopher.HandleFunc("/hello", hello)
|
||||
log.Println("Test server starting on 7000")
|
||||
err := gopher.ListenAndServe("localhost:7000", nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return quit
|
||||
}
|
||||
|
||||
func stopTestServer(c chan bool) {
|
||||
c <- true
|
||||
}
|
||||
|
||||
func Example_client() {
|
||||
res, err := gopher.Get("gopher://gopher.floodgap.com")
|
||||
if err != nil {
|
||||
@ -57,7 +109,9 @@ func TestFileServer(t *testing.T) {
|
||||
assert.Nil(err)
|
||||
|
||||
log.Println(string(json))
|
||||
assert.JSONEq(string(json), `{"items":[{"type":"0","description":"hello.txt","selector":"hello.txt","host":"127.0.0.1","port":7000,"extras":[]}]}`)
|
||||
assert.JSONEq(
|
||||
`{"items":[{"type":"0","description":"hello.txt","selector":"/hello.txt","host":"127.0.0.1","port":7000,"extras":[]}]}`,
|
||||
string(json))
|
||||
}
|
||||
|
||||
func TestParseItemNull(t *testing.T) {
|
||||
@ -121,13 +175,3 @@ func TestParseItemMarshalIdempotency(t *testing.T) {
|
||||
assert.NotNil(item1)
|
||||
assert.Equal(item, item1)
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
gopher.Handle("/", gopher.FileServer(gopher.Dir("./testdata")))
|
||||
gopher.HandleFunc("/hello", hello)
|
||||
go func() {
|
||||
log.Fatal(gopher.ListenAndServe("localhost:7000", nil))
|
||||
}()
|
||||
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
2
testdata/gophermap
vendored
Normal file
2
testdata/gophermap
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
|
||||
0hello.txt /hello.txt 127.0.0.1 7000
|
Loading…
Reference in New Issue
Block a user