-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfailover_test.go
78 lines (66 loc) · 1.56 KB
/
failover_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
package failover
import (
"fmt"
"net/http"
"net/url"
"testing"
"time"
)
func TestFailover(t *testing.T) {
f := New(CheckConnection,
OptCheckUrlBeforeAdding(true),
OptCheckUrlDelay(10*time.Second),
OptMaxAttempts(10),
)
tests := []*url.URL{
// {Host: "google.com", Scheme: "https"},
{Host: "0.0.0.0:8080", Scheme: "http"},
{Host: "0.0.0.0:8081", Scheme: "http"},
{Host: "0.0.0.0:8081", Scheme: "http"},
{Host: "0.0.0.0:8081", Scheme: "http"},
{Host: "0.0.0.0:8082", Scheme: "http"},
{Host: "0.0.0.0:8082", Scheme: "http"},
{Host: "0.0.0.0:8083", Scheme: "http"},
{Host: "0.0.0.0:8084", Scheme: "http"},
{Host: "0.0.0.0:8085", Scheme: "http"},
{Host: "0.0.0.0:8086", Scheme: "http"},
{Host: "0.0.0.0:8087", Scheme: "http"},
}
err := f.AddUrl(tests[0])
if err != nil {
t.Fatal(err)
}
err = f.AddUrl(tests[0]) // duplicate
if err != nil {
t.Fatal(err)
}
err = f.AddUrl(tests[1])
if err != nil {
t.Fatal(err)
}
err = f.AddUrls(tests...)
if err != nil {
t.Fatal(err)
}
time.Sleep(5 * time.Second)
// fmt.Println("STATUS", f.Request(Request, OptReqOnErr(ReqOnErrReconnectNext), OptMaxAttempts(4)))
fmt.Println("STATUS", f.Request(Request, OptReqOnErr(ReqOnErrReconnectNext), OptMaxAttempts(2)))
// f.Request(Request)
// f.Request(Request)
// f.Request(Request)
}
func CheckConnection(url *url.URL) error {
_, err := http.Get(url.String())
if err != nil {
fmt.Println(err)
return err
}
return nil
}
func Request(url *url.URL) error {
_, err := http.Get("http://" + url.Host)
if err != nil {
return err
}
return nil
}