|
| 1 | +package network |
| 2 | + |
| 3 | +import ( |
| 4 | +"net" |
| 5 | +"sync" |
| 6 | +"testing" |
| 7 | + |
| 8 | +"github.com/gatewayd-io/gatewayd/config" |
| 9 | +"github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +// TestNewConsistentHash verifies that a new ConsistentHash instance is properly created. |
| 13 | +// It checks that the original load balancing strategy is preserved, that the useSourceIp |
| 14 | +// setting is correctly applied, and that the hashMap is initialized. |
| 15 | +func TestNewConsistentHash(t *testing.T) { |
| 16 | +server := &Server{ |
| 17 | +LoadbalancerConsistentHash: &config.ConsistentHash{UseSourceIP: true}, |
| 18 | +} |
| 19 | +originalStrategy := NewRandom(server) |
| 20 | +consistentHash := NewConsistentHash(server, originalStrategy) |
| 21 | + |
| 22 | +assert.NotNil(t, consistentHash) |
| 23 | +assert.Equal(t, originalStrategy, consistentHash.originalStrategy) |
| 24 | +assert.True(t, consistentHash.useSourceIP) |
| 25 | +assert.NotNil(t, consistentHash.hashMap) |
| 26 | +} |
| 27 | + |
| 28 | +// TestConsistentHashNextProxyUseSourceIpExists ensures that when useSourceIp is enabled, |
| 29 | +// and the hashed IP exists in the hashMap, the correct proxy is returned. |
| 30 | +// It mocks a connection with a specific IP and verifies the proxy retrieval from the hashMap. |
| 31 | +func TestConsistentHashNextProxyUseSourceIpExists(t *testing.T) { |
| 32 | +proxies := []IProxy{ |
| 33 | +MockProxy{name: "proxy1"}, |
| 34 | +MockProxy{name: "proxy2"}, |
| 35 | +MockProxy{name: "proxy3"}, |
| 36 | +} |
| 37 | +server := &Server{ |
| 38 | +Proxies: proxies, |
| 39 | +LoadbalancerConsistentHash: &config.ConsistentHash{UseSourceIP: true}, |
| 40 | +} |
| 41 | +originalStrategy := NewRandom(server) |
| 42 | +consistentHash := NewConsistentHash(server, originalStrategy) |
| 43 | +mockConn := new(MockConnWrapper) |
| 44 | + |
| 45 | +// Mock LocalAddr to return a specific IP:port format |
| 46 | +mockAddr := &net.TCPAddr{IP: net.ParseIP("192.168.1.1"), Port: 1234} |
| 47 | +mockConn.On("LocalAddr").Return(mockAddr) |
| 48 | + |
| 49 | +key := "192.168.1.1" |
| 50 | +hash := hashKey(key) |
| 51 | + |
| 52 | +consistentHash.hashMap[hash] = proxies[2] |
| 53 | + |
| 54 | +proxy, err := consistentHash.NextProxy(mockConn) |
| 55 | +assert.Nil(t, err) |
| 56 | +assert.Equal(t, proxies[2], proxy) |
| 57 | + |
| 58 | +// Clean up |
| 59 | +mockConn.AssertExpectations(t) |
| 60 | +} |
| 61 | + |
| 62 | +// TestConsistentHashNextProxyUseFullAddress verifies the behavior when useSourceIp is disabled. |
| 63 | +// It ensures that the full connection address is used for hashing, and the correct proxy is returned |
| 64 | +// and cached in the hashMap. The test also checks that the hash value is computed based on the full address. |
| 65 | +func TestConsistentHashNextProxyUseFullAddress(t *testing.T) { |
| 66 | +mockConn := new(MockConnWrapper) |
| 67 | +proxies := []IProxy{ |
| 68 | +MockProxy{name: "proxy1"}, |
| 69 | +MockProxy{name: "proxy2"}, |
| 70 | +MockProxy{name: "proxy3"}, |
| 71 | +} |
| 72 | +server := &Server{ |
| 73 | +Proxies: proxies, |
| 74 | +LoadbalancerConsistentHash: &config.ConsistentHash{ |
| 75 | +UseSourceIP: false, |
| 76 | +}, |
| 77 | +} |
| 78 | +mockStrategy := NewRoundRobin(server) |
| 79 | + |
| 80 | +// Mock LocalAddr to return full address |
| 81 | +mockAddr := &net.TCPAddr{IP: net.ParseIP("192.168.1.1"), Port: 1234} |
| 82 | +mockConn.On("LocalAddr").Return(mockAddr) |
| 83 | + |
| 84 | +consistentHash := NewConsistentHash(server, mockStrategy) |
| 85 | + |
| 86 | +proxy, err := consistentHash.NextProxy(mockConn) |
| 87 | +assert.Nil(t, err) |
| 88 | +assert.NotNil(t, proxy) |
| 89 | +assert.Equal(t, proxies[1], proxy) |
| 90 | + |
| 91 | +// Hash should be calculated using the full address and cached in hashMap |
| 92 | +hash := hashKey("192.168.1.1:1234") |
| 93 | +cachedProxy, exists := consistentHash.hashMap[hash] |
| 94 | + |
| 95 | +assert.True(t, exists) |
| 96 | +assert.Equal(t, proxies[1], cachedProxy) |
| 97 | + |
| 98 | +// Clean up |
| 99 | +mockConn.AssertExpectations(t) |
| 100 | +} |
| 101 | + |
| 102 | +// TestConsistentHashNextProxyConcurrency tests the concurrency safety of the NextProxy method |
| 103 | +// in the ConsistentHash struct. It ensures that multiple goroutines can concurrently call |
| 104 | +// NextProxy without causing race conditions or inconsistent behavior. |
| 105 | +func TestConsistentHashNextProxyConcurrency(t *testing.T) { |
| 106 | +// Setup mocks |
| 107 | +conn1 := new(MockConnWrapper) |
| 108 | +conn2 := new(MockConnWrapper) |
| 109 | +proxies := []IProxy{ |
| 110 | +MockProxy{name: "proxy1"}, |
| 111 | +MockProxy{name: "proxy2"}, |
| 112 | +MockProxy{name: "proxy3"}, |
| 113 | +} |
| 114 | +server := &Server{ |
| 115 | +Proxies: proxies, |
| 116 | +LoadbalancerConsistentHash: &config.ConsistentHash{UseSourceIP: true}, |
| 117 | +} |
| 118 | +originalStrategy := NewRoundRobin(server) |
| 119 | + |
| 120 | +// Mock IP addresses |
| 121 | +mockAddr1 := &net.TCPAddr{IP: net.ParseIP("192.168.1.1"), Port: 1234} |
| 122 | +mockAddr2 := &net.TCPAddr{IP: net.ParseIP("192.168.1.2"), Port: 1234} |
| 123 | +conn1.On("LocalAddr").Return(mockAddr1) |
| 124 | +conn2.On("LocalAddr").Return(mockAddr2) |
| 125 | + |
| 126 | +// Initialize the ConsistentHash |
| 127 | +consistentHash := NewConsistentHash(server, originalStrategy) |
| 128 | + |
| 129 | +// Run the test concurrently |
| 130 | +var waitGroup sync.WaitGroup |
| 131 | +const numGoroutines = 100 |
| 132 | + |
| 133 | +for range numGoroutines { |
| 134 | +waitGroup.Add(1) |
| 135 | +go func() { |
| 136 | +defer waitGroup.Done() |
| 137 | +p, err := consistentHash.NextProxy(conn1) |
| 138 | +assert.Nil(t, err) |
| 139 | +assert.Equal(t, proxies[1], p) |
| 140 | +}() |
| 141 | +} |
| 142 | + |
| 143 | +waitGroup.Wait() |
| 144 | + |
| 145 | +// Ensure that the proxy is consistently the same |
| 146 | +proxy, err := consistentHash.NextProxy(conn1) |
| 147 | +assert.Nil(t, err) |
| 148 | +assert.Equal(t, proxies[1], proxy) |
| 149 | + |
| 150 | +// Ensure that connecting from a different address returns a different proxy |
| 151 | +proxy, err = consistentHash.NextProxy(conn2) |
| 152 | +assert.Nil(t, err) |
| 153 | +assert.Equal(t, proxies[2], proxy) |
| 154 | +} |
0 commit comments