...
1
16
17 package rest
18
19 import (
20 "net/url"
21 "testing"
22 "time"
23
24 "k8s.io/client-go/util/flowcontrol"
25 )
26
27 func parse(raw string) *url.URL {
28 theUrl, _ := url.Parse(raw)
29 return theUrl
30 }
31
32 func TestURLBackoffFunctionalityCollisions(t *testing.T) {
33 myBackoff := &URLBackoff{
34 Backoff: flowcontrol.NewBackOff(1*time.Second, 60*time.Second),
35 }
36
37
38 myBackoff.UpdateBackoff(parse("http://100.200.300.400:8080"), nil, 500)
39
40 myBackoff.UpdateBackoff(parse("http://1.2.3.4:8080"), nil, 500)
41
42 if myBackoff.CalculateBackoff(parse("http://1.2.3.4:100")) > 0 {
43 t.Errorf("URLs are colliding in the backoff map!")
44 }
45 }
46
47
48 func TestURLBackoffFunctionality(t *testing.T) {
49 myBackoff := &URLBackoff{
50 Backoff: flowcontrol.NewBackOff(1*time.Second, 60*time.Second),
51 }
52
53
54
55
56 seconds := []int{0,
57 1, 2, 4, 8, 0,
58 1, 2}
59 returnCodes := []int{
60 429, 500, 501, 502, 300,
61 500, 501, 502,
62 }
63
64 if len(seconds) != len(returnCodes) {
65 t.Fatalf("responseCode to backoff arrays should be the same length... sanity check failed.")
66 }
67
68 for i, sec := range seconds {
69 backoffSec := myBackoff.CalculateBackoff(parse("http://1.2.3.4:100"))
70 if backoffSec < time.Duration(sec)*time.Second || backoffSec > time.Duration(sec+5)*time.Second {
71 t.Errorf("Backoff out of range %v: %v %v", i, sec, backoffSec)
72 }
73 myBackoff.UpdateBackoff(parse("http://1.2.3.4:100/responseCodeForFuncTest"), nil, returnCodes[i])
74 }
75
76 if myBackoff.CalculateBackoff(parse("http://1.2.3.4:100")) == 0 {
77 t.Errorf("The final return code %v should have resulted in a backoff ! ", returnCodes[7])
78 }
79 }
80
View as plain text