File: server.go

package info (click to toggle)
ntfy 2.11.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 19,364 kB
  • sloc: javascript: 16,782; makefile: 282; sh: 105; php: 21; python: 19
file content (40 lines) | stat: -rw-r--r-- 1,095 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package test

import (
	"fmt"
	"heckel.io/ntfy/v2/server"
	"math/rand"
	"net/http"
	"path/filepath"
	"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) {
	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) {
	port := 10000 + rand.Intn(30000)
	conf.ListenHTTP = fmt.Sprintf(":%d", port)
	conf.AttachmentCacheDir = t.TempDir()
	conf.CacheFile = filepath.Join(t.TempDir(), "cache.db")
	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)
}