server.go 1.1 KB

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