-
Notifications
You must be signed in to change notification settings - Fork 1
/
narada_test.go
56 lines (46 loc) · 1.05 KB
/
narada_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
package narada
import (
"net/http"
"os"
"testing"
"time"
)
func TestNew(t *testing.T) {
os.Setenv("NARADA_CONFIG", "./fixtures/config.yml")
os.Setenv("BIND_API", ":12345")
os.Setenv("LOGGER_LEVEL", "error")
defer os.Clearenv()
app := New("testing", "dev")
t.Run("It should start on and run servers", func(t *testing.T) {
errChan := make(chan error, 1)
doneChan := make(chan bool, 1)
go app.Start(func(s *Multiserver) error {
mux := http.NewServeMux()
mux.HandleFunc("/ping", func(writer http.ResponseWriter, request *http.Request) {
doneChan <- true
writer.WriteHeader(200)
})
if err := s.Add("api", mux); err != nil {
errChan <- err
return nil
}
return nil
})
go func() {
for {
time.Sleep(time.Millisecond * 50)
if _, err := http.Get("http://127.0.0.1:12345/ping"); err != nil {
continue
}
}
}()
select {
case err := <-errChan:
t.Fatalf("responded with error: %v", err)
case <-doneChan:
app.Stop()
case <-time.After(time.Second * 5):
t.Fatalf("timeout exceeded")
}
})
}