|
| 1 | +package endpointdiscovery |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/url" |
| 5 | + "reflect" |
| 6 | + "strconv" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +func Test_cloneURL(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + value *url.URL |
| 14 | + wantClone *url.URL |
| 15 | + }{ |
| 16 | + { |
| 17 | + value: &url.URL{ |
| 18 | + Scheme: "https", |
| 19 | + Opaque: "foo", |
| 20 | + User: nil, |
| 21 | + Host: "amazonaws.com", |
| 22 | + Path: "/", |
| 23 | + RawPath: "/", |
| 24 | + ForceQuery: true, |
| 25 | + RawQuery: "thing=value", |
| 26 | + Fragment: "1234", |
| 27 | + RawFragment: "1234", |
| 28 | + }, |
| 29 | + wantClone: &url.URL{ |
| 30 | + Scheme: "https", |
| 31 | + Opaque: "foo", |
| 32 | + User: nil, |
| 33 | + Host: "amazonaws.com", |
| 34 | + Path: "/", |
| 35 | + RawPath: "/", |
| 36 | + ForceQuery: true, |
| 37 | + RawQuery: "thing=value", |
| 38 | + Fragment: "1234", |
| 39 | + RawFragment: "1234", |
| 40 | + }, |
| 41 | + }, |
| 42 | + { |
| 43 | + value: &url.URL{ |
| 44 | + Scheme: "https", |
| 45 | + Opaque: "foo", |
| 46 | + User: url.UserPassword("NOT", "VALID"), |
| 47 | + Host: "amazonaws.com", |
| 48 | + Path: "/", |
| 49 | + RawPath: "/", |
| 50 | + ForceQuery: true, |
| 51 | + RawQuery: "thing=value", |
| 52 | + Fragment: "1234", |
| 53 | + RawFragment: "1234", |
| 54 | + }, |
| 55 | + wantClone: &url.URL{ |
| 56 | + Scheme: "https", |
| 57 | + Opaque: "foo", |
| 58 | + User: url.UserPassword("NOT", "VALID"), |
| 59 | + Host: "amazonaws.com", |
| 60 | + Path: "/", |
| 61 | + RawPath: "/", |
| 62 | + ForceQuery: true, |
| 63 | + RawQuery: "thing=value", |
| 64 | + Fragment: "1234", |
| 65 | + RawFragment: "1234", |
| 66 | + }, |
| 67 | + }, |
| 68 | + } |
| 69 | + for i, tt := range tests { |
| 70 | + t.Run(strconv.Itoa(i), func(t *testing.T) { |
| 71 | + gotClone := cloneURL(tt.value) |
| 72 | + if gotClone == tt.value { |
| 73 | + t.Errorf("expct clone URL to not be same pointer address") |
| 74 | + } |
| 75 | + if tt.value.User != nil { |
| 76 | + if tt.value.User == gotClone.User { |
| 77 | + t.Errorf("expct cloned Userinfo to not be same pointer address") |
| 78 | + } |
| 79 | + } |
| 80 | + if !reflect.DeepEqual(gotClone, tt.wantClone) { |
| 81 | + t.Errorf("cloneURL() = %v, want %v", gotClone, tt.wantClone) |
| 82 | + } |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestEndpoint_Prune(t *testing.T) { |
| 88 | + endpoint := Endpoint{} |
| 89 | + |
| 90 | + endpoint.Add(WeightedAddress{ |
| 91 | + URL: &url.URL{}, |
| 92 | + Expired: time.Now().Add(5 * time.Minute), |
| 93 | + }) |
| 94 | + |
| 95 | + initial := endpoint.Addresses |
| 96 | + |
| 97 | + if e, a := false, endpoint.Prune(); e != a { |
| 98 | + t.Errorf("expect prune %v, got %v", e, a) |
| 99 | + } |
| 100 | + |
| 101 | + if e, a := &initial[0], &endpoint.Addresses[0]; e != a { |
| 102 | + t.Errorf("expect slice address to be same") |
| 103 | + } |
| 104 | + |
| 105 | + endpoint.Add(WeightedAddress{ |
22BF
| 106 | + URL: &url.URL{}, |
| 107 | + Expired: time.Now().Add(5 * -time.Minute), |
| 108 | + }) |
| 109 | + |
| 110 | + initial = endpoint.Addresses |
| 111 | + |
| 112
+ if e, a := true, endpoint.Prune(); e != a { |
| 113 | + t.Errorf("expect prune %v, got %v", e, a) |
| 114 | + } |
| 115 | + |
| 116 | + if e, a := &initial[0], &endpoint.Addresses[0]; e == a { |
| 117 | + t.Errorf("expect slice address to be different") |
| 118 | + } |
| 119 | + |
| 120 | + if e, a := 1, endpoint.Len(); e != a { |
| 121 | + t.Errorf("expect slice length %v, got %v", e, a) |
| 122 | + } |
| 123 | +} |
0 commit comments