server.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package test
  2. import (
  3. "fmt"
  4. "heckel.io/ntfy/server"
  5. "math/rand"
  6. "net/http"
  7. "testing"
  8. "time"
  9. )
  10. func init() {
  11. rand.Seed(time.Now().UnixMilli())
  12. }
  13. // StartServer starts a server.Server with a random port and waits for the server to be up
  14. func StartServer(t *testing.T) (*server.Server, int) {
  15. return StartServerWithConfig(t, server.NewConfig())
  16. }
  17. // StartServerWithConfig starts a server.Server with a random port and waits for the server to be up
  18. func StartServerWithConfig(t *testing.T, conf *server.Config) (*server.Server, int) {
  19. port := 10000 + rand.Intn(20000)
  20. conf.ListenHTTP = fmt.Sprintf(":%d", port)
  21. s, err := server.New(conf)
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. go func() {
  26. if err := s.Run(); err != nil && err != http.ErrServerClosed {
  27. panic(err) // 'go vet' complains about 't.Fatal(err)'
  28. }
  29. }()
  30. WaitForPortUp(t, port)
  31. return s, port
  32. }
  33. // StopServer stops the test server and waits for the port to be down
  34. func StopServer(t *testing.T, s *server.Server, port int) {
  35. s.Stop()
  36. WaitForPortDown(t, port)
  37. }