1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-12-14 11:47:33 +00:00
binwiederhier-ntfy/test/server.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

41 lines
1.1 KiB
Go
Raw Permalink Normal View History

2021-12-22 22:20:43 +00:00
package test
import (
"fmt"
2023-11-17 01:54:58 +00:00
"heckel.io/ntfy/v2/server"
2021-12-22 22:20:43 +00:00
"math/rand"
"net/http"
2022-01-16 03:33:35 +00:00
"path/filepath"
2021-12-22 22:20:43 +00:00
"testing"
)
// StartServer starts a server.Server with a random port and waits for the server to be up
func StartServer(t *testing.T) (*server.Server, int) {
2021-12-24 14:01:29 +00:00
return StartServerWithConfig(t, server.NewConfig())
}
// StartServerWithConfig starts a server.Server with a random port and waits for the server to be up
func StartServerWithConfig(t *testing.T, conf *server.Config) (*server.Server, int) {
2024-03-21 01:33:54 +00:00
port := 10000 + rand.Intn(30000)
2021-12-22 22:20:43 +00:00
conf.ListenHTTP = fmt.Sprintf(":%d", port)
2022-01-16 03:33:35 +00:00
conf.AttachmentCacheDir = t.TempDir()
conf.CacheFile = filepath.Join(t.TempDir(), "cache.db")
2021-12-22 22:20:43 +00:00
s, err := server.New(conf)
if err != nil {
t.Fatal(err)
}
go func() {
if err := s.Run(); err != nil && err != http.ErrServerClosed {
panic(err) // 'go vet' complains about 't.Fatal(err)'
}
}()
WaitForPortUp(t, port)
return s, port
}
// StopServer stops the test server and waits for the port to be down
func StopServer(t *testing.T, s *server.Server, port int) {
s.Stop()
WaitForPortDown(t, port)
}