|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +package controlplane_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "io/ioutil" |
| 10 | + "net" |
| 11 | + "os" |
| 12 | + "strconv" |
| 13 | + "testing" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | + "github.com/talos-systems/go-retry/retry" |
| 19 | + "go.uber.org/goleak" |
| 20 | + |
| 21 | + "github.com/talos-systems/go-loadbalancer/controlplane" |
| 22 | +) |
| 23 | + |
| 24 | +type mockUpstream struct { |
| 25 | + addr string |
| 26 | + l net.Listener |
| 27 | + |
| 28 | + identity string |
| 29 | +} |
| 30 | + |
| 31 | +func (u *mockUpstream) Start() error { |
| 32 | + var err error |
| 33 | + |
| 34 | + u.l, err = net.Listen("tcp", "localhost:0") |
| 35 | + if err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + |
| 39 | + u.addr = u.l.Addr().String() |
| 40 | + |
| 41 | + go u.serve() |
| 42 | + |
| 43 | + return nil |
| 44 | +} |
| 45 | + |
| 46 | +func (u *mockUpstream) serve() { |
| 47 | + for { |
| 48 | + c, err := u.l.Accept() |
| 49 | + if err != nil { |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + c.Write([]byte(u.identity)) //nolint: errcheck |
| 54 | + c.Close() //nolint: errcheck |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func (u *mockUpstream) Close() { |
| 59 | + u.l.Close() //nolint: errcheck |
| 60 | +} |
| 61 | + |
| 62 | +func TestLoadBalancer(t *testing.T) { |
| 63 | + defer goleak.VerifyNone(t, goleak.IgnoreCurrent()) |
| 64 | + |
| 65 | + const ( |
| 66 | + upstreamCount = 5 |
| 67 | + pivot = 2 |
| 68 | + ) |
| 69 | + |
| 70 | + upstreams := make([]mockUpstream, upstreamCount) |
| 71 | + for i := range upstreams { |
| 72 | + upstreams[i].identity = strconv.Itoa(i) |
| 73 | + require.NoError(t, upstreams[i].Start()) |
| 74 | + } |
| 75 | + |
| 76 | + upstreamAddrs := make([]string, len(upstreams)) |
| 77 | + for i := range upstreamAddrs { |
| 78 | + upstreamAddrs[i] = upstreams[i].addr |
| 79 | + } |
| 80 | + |
| 81 | + lb, err := controlplane.NewLoadBalancer("localhost", 0, os.Stderr) |
| 82 | + require.NoError(t, err) |
| 83 | + |
| 84 | + upstreamCh := make(chan []string) |
| 85 | + |
| 86 | + require.NoError(t, lb.Start(upstreamCh)) |
| 87 | + |
| 88 | + upstreamCh <- upstreamAddrs[:pivot] |
| 89 | + |
| 90 | + readIdentity := func() (int, error) { |
| 91 | + c, err := net.Dial("tcp", lb.Endpoint()) |
| 92 | + if err != nil { |
| 93 | + return 0, retry.ExpectedError(err) |
| 94 | + } |
| 95 | + |
| 96 | + defer c.Close() //nolint:errcheck |
| 97 | + |
| 98 | + id, err := ioutil.ReadAll(c) |
| 99 | + if err != nil { |
| 100 | + return 0, retry.ExpectedError(err) |
| 101 | + } |
| 102 | + |
| 103 | + return strconv.Atoi(string(id)) |
| 104 | + } |
| 105 | + |
| 106 | + assert.NoError(t, retry.Constant(10*time.Second, retry.WithUnits(time.Second)).Retry(func() error { |
| 107 | + identity, err := readIdentity() |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + if identity < 0 || identity > pivot-1 { |
| 113 | + return fmt.Errorf("unexpected response: %d", identity) |
| 114 | + } |
| 115 | + |
| 116 | + return nil |
| 117 | + })) |
| 118 | + |
| 119 | + // change the upstreams |
| 120 | + upstreamCh <- upstreamAddrs[pivot:] |
| 121 | + |
| 122 | + assert.NoError(t, retry.Constant(10*time.Second, retry.WithUnits(time.Second)).Retry(func() error { |
| 123 | + identity, err := readIdentity() |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + |
| 128 | + // upstreams are not changed immediately, there might be some stale responses |
| 129 | + if identity < pivot { |
| 130 | + return retry.ExpectedErrorf("unexpected response: %d", identity) |
| 131 | + } |
| 132 | + |
| 133 | + return nil |
| 134 | + })) |
| 135 | + |
| 136 | + assert.NoError(t, lb.Shutdown()) |
| 137 | + |
| 138 | + for i := range upstreams { |
| 139 | + upstreams[i].Close() |
| 140 | + } |
| 141 | +} |
0 commit comments