mirror of
https://github.com/binwiederhier/ntfy.git
synced 2026-03-18 21:30:44 +01:00
Refine
This commit is contained in:
@@ -3,7 +3,7 @@ package test
|
||||
import (
|
||||
"fmt"
|
||||
"heckel.io/ntfy/v2/server"
|
||||
"math/rand"
|
||||
"net"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -16,7 +16,7 @@ func StartServer(t *testing.T) (*server.Server, int) {
|
||||
|
||||
// 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) {
|
||||
port := 10000 + rand.Intn(30000)
|
||||
port := findAvailablePort(t)
|
||||
conf.ListenHTTP = fmt.Sprintf(":%d", port)
|
||||
conf.AttachmentCacheDir = t.TempDir()
|
||||
conf.CacheFile = filepath.Join(t.TempDir(), "cache.db")
|
||||
@@ -33,6 +33,17 @@ func StartServerWithConfig(t *testing.T, conf *server.Config) (*server.Server, i
|
||||
return s, port
|
||||
}
|
||||
|
||||
// findAvailablePort asks the OS for a free port by binding to :0
|
||||
func findAvailablePort(t *testing.T) int {
|
||||
listener, err := net.Listen("tcp", ":0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
port := listener.Addr().(*net.TCPAddr).Port
|
||||
listener.Close()
|
||||
return 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()
|
||||
|
||||
Reference in New Issue
Block a user