github.com/braveheart12/insolar-09-08-19@v0.8.7/network/transport/relay/relay_test.go (about) 1 /* 2 * The Clear BSD License 3 * 4 * Copyright (c) 2019 Insolar Technologies 5 * 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met: 9 * 10 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 11 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 12 * Neither the name of Insolar Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 13 * 14 * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 15 * 16 */ 17 18 package relay 19 20 import ( 21 "strconv" 22 "testing" 23 24 "github.com/insolar/insolar/network/transport/host" 25 "github.com/insolar/insolar/testutils" 26 "github.com/stretchr/testify/require" 27 ) 28 29 func TestRelay_ClientsCount(t *testing.T) { 30 relay := NewRelay() 31 count := 20 32 33 hosts := makeHosts(count, t) 34 35 for i := range hosts { 36 relay.AddClient(hosts[i]) 37 } 38 39 require.Equal(t, count, relay.ClientsCount()) 40 } 41 42 func TestNewProxy(t *testing.T) { 43 proxy := NewProxy() 44 require.NotNil(t, proxy) 45 } 46 47 func TestNewRelay(t *testing.T) { 48 relay := NewRelay() 49 require.NotNil(t, relay) 50 } 51 52 func makeAddresses(count int, t *testing.T) []*host.Address { 53 ip := "127.0.0.1:" 54 addresses := make([]*host.Address, 0) 55 56 for i := 0; i < count; i++ { 57 address, err := host.NewAddress(ip + strconv.Itoa(i+20000)) 58 if err != nil { 59 require.Errorf(t, nil, "error: %s", err.Error()) 60 continue 61 } 62 addresses = append(addresses, address) 63 } 64 65 return addresses 66 } 67 68 func makeHosts(count int, t *testing.T) []*host.Host { 69 result := make([]*host.Host, 0) 70 addresses := makeAddresses(count, t) 71 72 for i := 0; i < count; i++ { 73 id := testutils.RandomRef() 74 result = append(result, &host.Host{NodeID: id, Address: addresses[i]}) 75 } 76 77 return result 78 } 79 80 func TestRelay_AddClient(t *testing.T) { 81 relay := NewRelay() 82 count := 20 83 84 hosts := makeHosts(count, t) 85 86 for i := range hosts { 87 err := relay.AddClient(hosts[i]) 88 require.NoError(t, err) 89 err = relay.AddClient(hosts[i]) // adding existing host 90 require.EqualError(t, err, "client exists already") 91 } 92 93 require.Equal(t, count, relay.ClientsCount()) 94 } 95 96 func TestRelay_RemoveClient(t *testing.T) { 97 relay := NewRelay() 98 count := 20 99 100 hosts := makeHosts(count, t) 101 102 for i := range hosts { 103 err := relay.AddClient(hosts[i]) 104 if err != nil { 105 require.Errorf(t, nil, "error: %s", err.Error()) 106 } 107 } 108 require.Equal(t, count, relay.ClientsCount()) 109 110 for i := range hosts { 111 err := relay.RemoveClient(hosts[i]) 112 require.NoError(t, err) 113 err = relay.RemoveClient(hosts[i]) 114 require.EqualError(t, err, "client not found") 115 } 116 117 require.Equal(t, 0, relay.ClientsCount()) 118 } 119 120 func TestRelay_NeedToRelay(t *testing.T) { 121 relay := NewRelay() 122 count := 20 123 ip := "127.0.0.2:" 124 125 hosts := makeHosts(count, t) 126 127 for i := range hosts { 128 relay.AddClient(hosts[i]) 129 } 130 131 require.Equal(t, count, relay.ClientsCount()) 132 133 for i := range hosts { 134 res := relay.NeedToRelay(hosts[i].Address.String()) 135 require.Equal(t, true, res) 136 } 137 138 for i := 0; i < count; i++ { 139 address, err := host.NewAddress(ip + strconv.Itoa(i+20000)) 140 141 if err != nil { 142 require.Errorf(t, nil, "error: %s", err.Error()) 143 continue 144 } 145 res := relay.NeedToRelay(address.String()) 146 require.Equal(t, false, res) 147 } 148 } 149 150 func TestRelay_Count(t *testing.T) { 151 relay := NewRelay() 152 count := 20 153 154 hosts := makeHosts(count, t) 155 156 for i := range hosts { 157 relay.AddClient(hosts[i]) 158 } 159 160 require.Equal(t, count, relay.ClientsCount()) 161 } 162 163 func TestProxy_AddProxyHost(t *testing.T) { 164 count := 20 165 addresses := makeAddresses(count, t) 166 proxy := NewProxy() 167 168 for i := range addresses { 169 proxy.AddProxyHost(addresses[i].String()) 170 proxy.AddProxyHost(addresses[i].String()) // adding existed host 171 } 172 173 require.Equal(t, count, proxy.ProxyHostsCount()) 174 } 175 176 func TestProxy_RemoveProxyHost(t *testing.T) { 177 count := 20 178 addresses := makeAddresses(count, t) 179 proxy := NewProxy() 180 181 for i := range addresses { 182 proxy.AddProxyHost(addresses[i].String()) 183 } 184 185 require.Equal(t, count, proxy.ProxyHostsCount()) 186 187 for i := range addresses { 188 proxy.RemoveProxyHost(addresses[i].String()) 189 proxy.RemoveProxyHost(addresses[i].String()) // remove removed host 190 } 191 192 require.Equal(t, 0, proxy.ProxyHostsCount()) 193 } 194 195 func TestProxy_GetNextProxyAddress(t *testing.T) { 196 count := 20 197 addresses := makeAddresses(count, t) 198 proxy := NewProxy() 199 idx := make(map[int]string, count) 200 201 require.Equal(t, "", proxy.GetNextProxyAddress()) 202 203 for i := range addresses { 204 proxy.AddProxyHost(addresses[i].String()) 205 idx[i] = addresses[i].String() 206 } 207 208 require.Equal(t, count, proxy.ProxyHostsCount()) 209 require.Equal(t, count, len(idx)) 210 211 for i := 0; i < proxy.ProxyHostsCount(); i++ { 212 require.Equal(t, idx[i], proxy.GetNextProxyAddress()) 213 } 214 for i := 0; i < proxy.ProxyHostsCount(); i++ { 215 require.Equal(t, idx[i], proxy.GetNextProxyAddress()) 216 } 217 } 218 219 func TestProxy_Count(t *testing.T) { 220 count := 20 221 addresses := makeAddresses(count, t) 222 proxy := NewProxy() 223 224 for i := range addresses { 225 proxy.AddProxyHost(addresses[i].String()) 226 } 227 228 require.Equal(t, count, proxy.ProxyHostsCount()) 229 } 230 231 func TestCreateProxy(t *testing.T) { 232 proxy := NewProxy() 233 234 check := true 235 236 if proxy == nil { 237 check = false 238 } 239 240 require.Equal(t, true, check) 241 }