-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_test.go
87 lines (69 loc) · 1.88 KB
/
run_test.go
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"context"
"net/http"
"net/http/httptest"
"os"
"sync"
"syscall"
"testing"
"time"
"github.com/matryer/is"
log "golang.org/x/exp/slog"
)
func Test_Run(t *testing.T) {
is := is.New(t)
// unset test environment
defer os.Clearenv()
// clear pending workers
defer clearCache()
wg := sync.WaitGroup{}
wg.Add(1)
ts := defaultApiServer()
// imup setup
os.Setenv("IMUP_ADDRESS", ts.URL)
os.Setenv("IMUP_ADDRESS_SPEEDTEST", ts.URL)
os.Setenv("IMUP_LIVENESS_CHECKIN_ADDRESS", ts.URL)
os.Setenv("IMUP_SHOULD_RUN_SPEEDTEST_ADDRESS", ts.URL)
os.Setenv("IMUP_SPEED_TEST_RESULTS_ADDRESS", ts.URL)
os.Setenv("IMUP_SPEED_TEST_STATUS_ADDRESS", ts.URL)
os.Setenv("IMUP_REALTIME_AUTHORIZED", ts.URL)
os.Setenv("IMUP_REALTIME_CONFIG", ts.URL)
os.Setenv("PING_INTERVAL", "1")
os.Setenv("PING_DELAY", "100")
os.Setenv("PING_REQUESTS", "2")
os.Setenv("PING_ADDRESS", "127.0.0.1")
os.Setenv("PING_ADDRESS_INTERNAL", "127.0.0.1")
os.Setenv("IMUP_REALTIME_CONFIG", ts.URL)
// application configuration
os.Setenv("EMAIL", "test@example.com")
os.Setenv("VERBOSITY", "DEBUG")
os.Setenv("LOG_TO_FILE", "false")
shutdown := make(chan os.Signal, 1)
// cctx, cancel := context.WithCancel(context.Background())
start := time.Now().Unix()
var err error
go func() {
defer wg.Done()
err = run(context.Background(), shutdown)
log.Info("test error", err)
}()
// cancel()
shutdown <- syscall.SIGINT
wg.Wait()
elapsed := time.Now().Unix() - start
is.True(elapsed < 5)
is.NoErr(err)
}
func defaultApiServer() *httptest.Server {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
return s
}
func defaultConfigurableApiServer(retcode int) *httptest.Server {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
}))
return s
}