blob: da40a4e7b3faaa4b7b8ec998c7507f61f29b8095 (
plain) (
blame)
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
|
package health
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
)
func Test_health_overloaded_cancellation(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(1 * time.Second)
w.WriteHeader(http.StatusOK)
}))
defer ts.Close()
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
h := &health{
Addr: ts.URL,
stop: cancel,
}
var err error
h.healthURI, err = url.Parse(ts.URL)
if err != nil {
t.Fatal(err)
}
h.healthURI.Path = "/health"
stopped := make(chan struct{})
go func() {
h.overloaded(ctx)
stopped <- struct{}{}
}()
// wait for overloaded function to start atleast once
time.Sleep(1 * time.Second)
cancel()
select {
case <-stopped:
case <-time.After(5 * time.Second):
t.Fatal("overloaded function should have been cancelled")
}
}
|