You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

855 lines
21 KiB

p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
p2p: file descriptor leaks (#3150) * close peer's connection to avoid fd leak Fixes #2967 * rename peer#Addr to RemoteAddr * fix test * fixes after Ethan's review * bring back the check * changelog entry * write a test for switch#acceptRoutine * increase timeouts? :( * remove extra assertNPeersWithTimeout * simplify test * assert number of peers (just to be safe) * Cleanup in OnStop * run tests with verbose flag on CircleCI * spawn a reading routine to prevent connection from closing * get port from the listener random port is faster, but often results in ``` panic: listen tcp 127.0.0.1:44068: bind: address already in use [recovered] panic: listen tcp 127.0.0.1:44068: bind: address already in use goroutine 79 [running]: testing.tRunner.func1(0xc0001bd600) /usr/local/go/src/testing/testing.go:792 +0x387 panic(0x974d20, 0xc0001b0500) /usr/local/go/src/runtime/panic.go:513 +0x1b9 github.com/tendermint/tendermint/p2p.MakeSwitch(0xc0000f42a0, 0x0, 0x9fb9cc, 0x9, 0x9fc346, 0xb, 0xb42128, 0x0, 0x0, 0x0, ...) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:182 +0xa28 github.com/tendermint/tendermint/p2p.MakeConnectedSwitches(0xc0000f42a0, 0x2, 0xb42128, 0xb41eb8, 0x4f1205, 0xc0001bed80, 0x4f16ed) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/test_util.go:75 +0xf9 github.com/tendermint/tendermint/p2p.MakeSwitchPair(0xbb8d20, 0xc0001bd600, 0xb42128, 0x2f7, 0x4f16c0) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:94 +0x4c github.com/tendermint/tendermint/p2p.TestSwitches(0xc0001bd600) /home/vagrant/go/src/github.com/tendermint/tendermint/p2p/switch_test.go:117 +0x58 testing.tRunner(0xc0001bd600, 0xb42038) /usr/local/go/src/testing/testing.go:827 +0xbf created by testing.(*T).Run /usr/local/go/src/testing/testing.go:878 +0x353 exit status 2 FAIL github.com/tendermint/tendermint/p2p 0.350s ```
6 years ago
  1. package p2p
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "net"
  9. "net/http"
  10. "net/http/httptest"
  11. "regexp"
  12. "strconv"
  13. "sync/atomic"
  14. "testing"
  15. "time"
  16. "github.com/prometheus/client_golang/prometheus/promhttp"
  17. "github.com/stretchr/testify/assert"
  18. "github.com/stretchr/testify/require"
  19. "github.com/tendermint/tendermint/config"
  20. "github.com/tendermint/tendermint/crypto/ed25519"
  21. "github.com/tendermint/tendermint/libs/log"
  22. tmsync "github.com/tendermint/tendermint/libs/sync"
  23. "github.com/tendermint/tendermint/p2p/conn"
  24. )
  25. var (
  26. cfg *config.P2PConfig
  27. )
  28. func init() {
  29. cfg = config.DefaultP2PConfig()
  30. cfg.PexReactor = true
  31. cfg.AllowDuplicateIP = true
  32. }
  33. type PeerMessage struct {
  34. PeerID ID
  35. Bytes []byte
  36. Counter int
  37. }
  38. type TestReactor struct {
  39. BaseReactor
  40. mtx tmsync.Mutex
  41. channels []*conn.ChannelDescriptor
  42. logMessages bool
  43. msgsCounter int
  44. msgsReceived map[byte][]PeerMessage
  45. }
  46. func NewTestReactor(channels []*conn.ChannelDescriptor, logMessages bool) *TestReactor {
  47. tr := &TestReactor{
  48. channels: channels,
  49. logMessages: logMessages,
  50. msgsReceived: make(map[byte][]PeerMessage),
  51. }
  52. tr.BaseReactor = *NewBaseReactor("TestReactor", tr)
  53. tr.SetLogger(log.TestingLogger())
  54. return tr
  55. }
  56. func (tr *TestReactor) GetChannels() []*conn.ChannelDescriptor {
  57. return tr.channels
  58. }
  59. func (tr *TestReactor) AddPeer(peer Peer) {}
  60. func (tr *TestReactor) RemovePeer(peer Peer, reason interface{}) {}
  61. func (tr *TestReactor) Receive(chID byte, peer Peer, msgBytes []byte) {
  62. if tr.logMessages {
  63. tr.mtx.Lock()
  64. defer tr.mtx.Unlock()
  65. //fmt.Printf("Received: %X, %X\n", chID, msgBytes)
  66. tr.msgsReceived[chID] = append(tr.msgsReceived[chID], PeerMessage{peer.ID(), msgBytes, tr.msgsCounter})
  67. tr.msgsCounter++
  68. }
  69. }
  70. func (tr *TestReactor) getMsgs(chID byte) []PeerMessage {
  71. tr.mtx.Lock()
  72. defer tr.mtx.Unlock()
  73. return tr.msgsReceived[chID]
  74. }
  75. //-----------------------------------------------------------------------------
  76. // convenience method for creating two switches connected to each other.
  77. // XXX: note this uses net.Pipe and not a proper TCP conn
  78. func MakeSwitchPair(t testing.TB, initSwitch func(int, *Switch) *Switch) (*Switch, *Switch) {
  79. // Create two switches that will be interconnected.
  80. switches := MakeConnectedSwitches(cfg, 2, initSwitch, Connect2Switches)
  81. return switches[0], switches[1]
  82. }
  83. func initSwitchFunc(i int, sw *Switch) *Switch {
  84. sw.SetAddrBook(&addrBookMock{
  85. addrs: make(map[string]struct{}),
  86. ourAddrs: make(map[string]struct{})})
  87. // Make two reactors of two channels each
  88. sw.AddReactor("foo", NewTestReactor([]*conn.ChannelDescriptor{
  89. {ID: byte(0x00), Priority: 10},
  90. {ID: byte(0x01), Priority: 10},
  91. }, true))
  92. sw.AddReactor("bar", NewTestReactor([]*conn.ChannelDescriptor{
  93. {ID: byte(0x02), Priority: 10},
  94. {ID: byte(0x03), Priority: 10},
  95. }, true))
  96. return sw
  97. }
  98. func TestSwitches(t *testing.T) {
  99. s1, s2 := MakeSwitchPair(t, initSwitchFunc)
  100. t.Cleanup(func() {
  101. if err := s1.Stop(); err != nil {
  102. t.Error(err)
  103. }
  104. })
  105. t.Cleanup(func() {
  106. if err := s2.Stop(); err != nil {
  107. t.Error(err)
  108. }
  109. })
  110. if s1.Peers().Size() != 1 {
  111. t.Errorf("expected exactly 1 peer in s1, got %v", s1.Peers().Size())
  112. }
  113. if s2.Peers().Size() != 1 {
  114. t.Errorf("expected exactly 1 peer in s2, got %v", s2.Peers().Size())
  115. }
  116. // Lets send some messages
  117. ch0Msg := []byte("channel zero")
  118. ch1Msg := []byte("channel foo")
  119. ch2Msg := []byte("channel bar")
  120. s1.Broadcast(byte(0x00), ch0Msg)
  121. s1.Broadcast(byte(0x01), ch1Msg)
  122. s1.Broadcast(byte(0x02), ch2Msg)
  123. assertMsgReceivedWithTimeout(t,
  124. ch0Msg,
  125. byte(0x00),
  126. s2.Reactor("foo").(*TestReactor), 10*time.Millisecond, 5*time.Second)
  127. assertMsgReceivedWithTimeout(t,
  128. ch1Msg,
  129. byte(0x01),
  130. s2.Reactor("foo").(*TestReactor), 10*time.Millisecond, 5*time.Second)
  131. assertMsgReceivedWithTimeout(t,
  132. ch2Msg,
  133. byte(0x02),
  134. s2.Reactor("bar").(*TestReactor), 10*time.Millisecond, 5*time.Second)
  135. }
  136. func assertMsgReceivedWithTimeout(
  137. t *testing.T,
  138. msgBytes []byte,
  139. channel byte,
  140. reactor *TestReactor,
  141. checkPeriod,
  142. timeout time.Duration,
  143. ) {
  144. ticker := time.NewTicker(checkPeriod)
  145. for {
  146. select {
  147. case <-ticker.C:
  148. msgs := reactor.getMsgs(channel)
  149. if len(msgs) > 0 {
  150. if !bytes.Equal(msgs[0].Bytes, msgBytes) {
  151. t.Fatalf("Unexpected message bytes. Wanted: %X, Got: %X", msgBytes, msgs[0].Bytes)
  152. }
  153. return
  154. }
  155. case <-time.After(timeout):
  156. t.Fatalf("Expected to have received 1 message in channel #%v, got zero", channel)
  157. }
  158. }
  159. }
  160. func TestSwitchFiltersOutItself(t *testing.T) {
  161. s1 := MakeSwitch(cfg, 1, "127.0.0.1", "123.123.123", initSwitchFunc)
  162. // simulate s1 having a public IP by creating a remote peer with the same ID
  163. rp := &remotePeer{PrivKey: s1.nodeKey.PrivKey, Config: cfg}
  164. rp.Start()
  165. // addr should be rejected in addPeer based on the same ID
  166. err := s1.DialPeerWithAddress(rp.Addr())
  167. if assert.Error(t, err) {
  168. if err, ok := err.(ErrRejected); ok {
  169. if !err.IsSelf() {
  170. t.Errorf("expected self to be rejected")
  171. }
  172. } else {
  173. t.Errorf("expected ErrRejected")
  174. }
  175. }
  176. assert.True(t, s1.addrBook.OurAddress(rp.Addr()))
  177. assert.False(t, s1.addrBook.HasAddress(rp.Addr()))
  178. rp.Stop()
  179. assertNoPeersAfterTimeout(t, s1, 100*time.Millisecond)
  180. }
  181. func TestSwitchPeerFilter(t *testing.T) {
  182. var (
  183. filters = []PeerFilterFunc{
  184. func(_ IPeerSet, _ Peer) error { return nil },
  185. func(_ IPeerSet, _ Peer) error { return fmt.Errorf("denied") },
  186. func(_ IPeerSet, _ Peer) error { return nil },
  187. }
  188. sw = MakeSwitch(
  189. cfg,
  190. 1,
  191. "testing",
  192. "123.123.123",
  193. initSwitchFunc,
  194. SwitchPeerFilters(filters...),
  195. )
  196. )
  197. sw.Start()
  198. t.Cleanup(func() {
  199. if err := sw.Stop(); err != nil {
  200. t.Error(err)
  201. }
  202. })
  203. // simulate remote peer
  204. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  205. rp.Start()
  206. t.Cleanup(rp.Stop)
  207. p, err := sw.transport.Dial(*rp.Addr(), peerConfig{
  208. chDescs: sw.chDescs,
  209. onPeerError: sw.StopPeerForError,
  210. isPersistent: sw.IsPeerPersistent,
  211. reactorsByCh: sw.reactorsByCh,
  212. })
  213. if err != nil {
  214. t.Fatal(err)
  215. }
  216. err = sw.addPeer(p)
  217. if err, ok := err.(ErrRejected); ok {
  218. if !err.IsFiltered() {
  219. t.Errorf("expected peer to be filtered")
  220. }
  221. } else {
  222. t.Errorf("expected ErrRejected")
  223. }
  224. }
  225. func TestSwitchPeerFilterTimeout(t *testing.T) {
  226. var (
  227. filters = []PeerFilterFunc{
  228. func(_ IPeerSet, _ Peer) error {
  229. time.Sleep(10 * time.Millisecond)
  230. return nil
  231. },
  232. }
  233. sw = MakeSwitch(
  234. cfg,
  235. 1,
  236. "testing",
  237. "123.123.123",
  238. initSwitchFunc,
  239. SwitchFilterTimeout(5*time.Millisecond),
  240. SwitchPeerFilters(filters...),
  241. )
  242. )
  243. sw.Start()
  244. t.Cleanup(func() {
  245. if err := sw.Stop(); err != nil {
  246. t.Log(err)
  247. }
  248. })
  249. // simulate remote peer
  250. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  251. rp.Start()
  252. defer rp.Stop()
  253. p, err := sw.transport.Dial(*rp.Addr(), peerConfig{
  254. chDescs: sw.chDescs,
  255. onPeerError: sw.StopPeerForError,
  256. isPersistent: sw.IsPeerPersistent,
  257. reactorsByCh: sw.reactorsByCh,
  258. })
  259. if err != nil {
  260. t.Fatal(err)
  261. }
  262. err = sw.addPeer(p)
  263. if _, ok := err.(ErrFilterTimeout); !ok {
  264. t.Errorf("expected ErrFilterTimeout")
  265. }
  266. }
  267. func TestSwitchPeerFilterDuplicate(t *testing.T) {
  268. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  269. err := sw.Start()
  270. require.NoError(t, err)
  271. t.Cleanup(func() {
  272. if err := sw.Stop(); err != nil {
  273. t.Error(err)
  274. }
  275. })
  276. // simulate remote peer
  277. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  278. rp.Start()
  279. defer rp.Stop()
  280. p, err := sw.transport.Dial(*rp.Addr(), peerConfig{
  281. chDescs: sw.chDescs,
  282. onPeerError: sw.StopPeerForError,
  283. isPersistent: sw.IsPeerPersistent,
  284. reactorsByCh: sw.reactorsByCh,
  285. })
  286. if err != nil {
  287. t.Fatal(err)
  288. }
  289. if err := sw.addPeer(p); err != nil {
  290. t.Fatal(err)
  291. }
  292. err = sw.addPeer(p)
  293. if errRej, ok := err.(ErrRejected); ok {
  294. if !errRej.IsDuplicate() {
  295. t.Errorf("expected peer to be duplicate. got %v", errRej)
  296. }
  297. } else {
  298. t.Errorf("expected ErrRejected, got %v", err)
  299. }
  300. }
  301. func assertNoPeersAfterTimeout(t *testing.T, sw *Switch, timeout time.Duration) {
  302. time.Sleep(timeout)
  303. if sw.Peers().Size() != 0 {
  304. t.Fatalf("Expected %v to not connect to some peers, got %d", sw, sw.Peers().Size())
  305. }
  306. }
  307. func TestSwitchStopsNonPersistentPeerOnError(t *testing.T) {
  308. assert, require := assert.New(t), require.New(t)
  309. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  310. err := sw.Start()
  311. if err != nil {
  312. t.Error(err)
  313. }
  314. t.Cleanup(func() {
  315. if err := sw.Stop(); err != nil {
  316. t.Error(err)
  317. }
  318. })
  319. // simulate remote peer
  320. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  321. rp.Start()
  322. defer rp.Stop()
  323. p, err := sw.transport.Dial(*rp.Addr(), peerConfig{
  324. chDescs: sw.chDescs,
  325. onPeerError: sw.StopPeerForError,
  326. isPersistent: sw.IsPeerPersistent,
  327. reactorsByCh: sw.reactorsByCh,
  328. })
  329. require.Nil(err)
  330. err = sw.addPeer(p)
  331. require.Nil(err)
  332. require.NotNil(sw.Peers().Get(rp.ID()))
  333. // simulate failure by closing connection
  334. p.(*peer).CloseConn()
  335. assertNoPeersAfterTimeout(t, sw, 100*time.Millisecond)
  336. assert.False(p.IsRunning())
  337. }
  338. func TestSwitchStopPeerForError(t *testing.T) {
  339. s := httptest.NewServer(promhttp.Handler())
  340. defer s.Close()
  341. scrapeMetrics := func() string {
  342. resp, err := http.Get(s.URL)
  343. assert.NoError(t, err)
  344. defer resp.Body.Close()
  345. buf, _ := ioutil.ReadAll(resp.Body)
  346. return string(buf)
  347. }
  348. namespace, subsystem, name := config.TestInstrumentationConfig().Namespace, MetricsSubsystem, "peers"
  349. re := regexp.MustCompile(namespace + `_` + subsystem + `_` + name + ` ([0-9\.]+)`)
  350. peersMetricValue := func() float64 {
  351. matches := re.FindStringSubmatch(scrapeMetrics())
  352. f, _ := strconv.ParseFloat(matches[1], 64)
  353. return f
  354. }
  355. p2pMetrics := PrometheusMetrics(namespace)
  356. // make two connected switches
  357. sw1, sw2 := MakeSwitchPair(t, func(i int, sw *Switch) *Switch {
  358. // set metrics on sw1
  359. if i == 0 {
  360. opt := WithMetrics(p2pMetrics)
  361. opt(sw)
  362. }
  363. return initSwitchFunc(i, sw)
  364. })
  365. assert.Equal(t, len(sw1.Peers().List()), 1)
  366. assert.EqualValues(t, 1, peersMetricValue())
  367. // send messages to the peer from sw1
  368. p := sw1.Peers().List()[0]
  369. p.Send(0x1, []byte("here's a message to send"))
  370. // stop sw2. this should cause the p to fail,
  371. // which results in calling StopPeerForError internally
  372. t.Cleanup(func() {
  373. if err := sw2.Stop(); err != nil {
  374. t.Error(err)
  375. }
  376. })
  377. // now call StopPeerForError explicitly, eg. from a reactor
  378. sw1.StopPeerForError(p, fmt.Errorf("some err"))
  379. assert.Equal(t, len(sw1.Peers().List()), 0)
  380. assert.EqualValues(t, 0, peersMetricValue())
  381. }
  382. func TestSwitchReconnectsToOutboundPersistentPeer(t *testing.T) {
  383. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  384. err := sw.Start()
  385. require.NoError(t, err)
  386. t.Cleanup(func() {
  387. if err := sw.Stop(); err != nil {
  388. t.Error(err)
  389. }
  390. })
  391. // 1. simulate failure by closing connection
  392. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  393. rp.Start()
  394. defer rp.Stop()
  395. err = sw.AddPersistentPeers([]string{rp.Addr().String()})
  396. require.NoError(t, err)
  397. err = sw.DialPeerWithAddress(rp.Addr())
  398. require.Nil(t, err)
  399. require.NotNil(t, sw.Peers().Get(rp.ID()))
  400. p := sw.Peers().List()[0]
  401. p.(*peer).CloseConn()
  402. waitUntilSwitchHasAtLeastNPeers(sw, 1)
  403. assert.False(t, p.IsRunning()) // old peer instance
  404. assert.Equal(t, 1, sw.Peers().Size()) // new peer instance
  405. // 2. simulate first time dial failure
  406. rp = &remotePeer{
  407. PrivKey: ed25519.GenPrivKey(),
  408. Config: cfg,
  409. // Use different interface to prevent duplicate IP filter, this will break
  410. // beyond two peers.
  411. listenAddr: "127.0.0.1:0",
  412. }
  413. rp.Start()
  414. defer rp.Stop()
  415. conf := config.DefaultP2PConfig()
  416. conf.TestDialFail = true // will trigger a reconnect
  417. err = sw.addOutboundPeerWithConfig(rp.Addr(), conf)
  418. require.NotNil(t, err)
  419. // DialPeerWithAddres - sw.peerConfig resets the dialer
  420. waitUntilSwitchHasAtLeastNPeers(sw, 2)
  421. assert.Equal(t, 2, sw.Peers().Size())
  422. }
  423. func TestSwitchReconnectsToInboundPersistentPeer(t *testing.T) {
  424. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  425. err := sw.Start()
  426. require.NoError(t, err)
  427. t.Cleanup(func() {
  428. if err := sw.Stop(); err != nil {
  429. t.Error(err)
  430. }
  431. })
  432. // 1. simulate failure by closing the connection
  433. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  434. rp.Start()
  435. defer rp.Stop()
  436. err = sw.AddPersistentPeers([]string{rp.Addr().String()})
  437. require.NoError(t, err)
  438. conn, err := rp.Dial(sw.NetAddress())
  439. require.NoError(t, err)
  440. time.Sleep(50 * time.Millisecond)
  441. require.NotNil(t, sw.Peers().Get(rp.ID()))
  442. conn.Close()
  443. waitUntilSwitchHasAtLeastNPeers(sw, 1)
  444. assert.Equal(t, 1, sw.Peers().Size())
  445. }
  446. func TestSwitchDialPeersAsync(t *testing.T) {
  447. if testing.Short() {
  448. return
  449. }
  450. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  451. err := sw.Start()
  452. require.NoError(t, err)
  453. t.Cleanup(func() {
  454. if err := sw.Stop(); err != nil {
  455. t.Error(err)
  456. }
  457. })
  458. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  459. rp.Start()
  460. defer rp.Stop()
  461. err = sw.DialPeersAsync([]string{rp.Addr().String()})
  462. require.NoError(t, err)
  463. time.Sleep(dialRandomizerIntervalMilliseconds * time.Millisecond)
  464. require.NotNil(t, sw.Peers().Get(rp.ID()))
  465. }
  466. func waitUntilSwitchHasAtLeastNPeers(sw *Switch, n int) {
  467. for i := 0; i < 20; i++ {
  468. time.Sleep(250 * time.Millisecond)
  469. has := sw.Peers().Size()
  470. if has >= n {
  471. break
  472. }
  473. }
  474. }
  475. func TestSwitchFullConnectivity(t *testing.T) {
  476. switches := MakeConnectedSwitches(cfg, 3, initSwitchFunc, Connect2Switches)
  477. defer func() {
  478. for _, sw := range switches {
  479. sw := sw
  480. t.Cleanup(func() {
  481. if err := sw.Stop(); err != nil {
  482. t.Error(err)
  483. }
  484. })
  485. }
  486. }()
  487. for i, sw := range switches {
  488. if sw.Peers().Size() != 2 {
  489. t.Fatalf("Expected each switch to be connected to 2 other, but %d switch only connected to %d", sw.Peers().Size(), i)
  490. }
  491. }
  492. }
  493. func TestSwitchAcceptRoutine(t *testing.T) {
  494. cfg.MaxNumInboundPeers = 5
  495. // Create some unconditional peers.
  496. const unconditionalPeersNum = 2
  497. var (
  498. unconditionalPeers = make([]*remotePeer, unconditionalPeersNum)
  499. unconditionalPeerIDs = make([]string, unconditionalPeersNum)
  500. )
  501. for i := 0; i < unconditionalPeersNum; i++ {
  502. peer := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  503. peer.Start()
  504. unconditionalPeers[i] = peer
  505. unconditionalPeerIDs[i] = string(peer.ID())
  506. }
  507. // make switch
  508. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  509. sw.AddUnconditionalPeerIDs(unconditionalPeerIDs)
  510. err := sw.Start()
  511. require.NoError(t, err)
  512. t.Cleanup(func() {
  513. if err := sw.Stop(); err != nil {
  514. t.Error(err)
  515. }
  516. })
  517. // 0. check there are no peers
  518. assert.Equal(t, 0, sw.Peers().Size())
  519. // 1. check we connect up to MaxNumInboundPeers
  520. peers := make([]*remotePeer, 0)
  521. for i := 0; i < cfg.MaxNumInboundPeers; i++ {
  522. peer := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  523. peers = append(peers, peer)
  524. peer.Start()
  525. c, err := peer.Dial(sw.NetAddress())
  526. require.NoError(t, err)
  527. // spawn a reading routine to prevent connection from closing
  528. go func(c net.Conn) {
  529. for {
  530. one := make([]byte, 1)
  531. _, err := c.Read(one)
  532. if err != nil {
  533. return
  534. }
  535. }
  536. }(c)
  537. }
  538. time.Sleep(10 * time.Millisecond)
  539. assert.Equal(t, cfg.MaxNumInboundPeers, sw.Peers().Size())
  540. // 2. check we close new connections if we already have MaxNumInboundPeers peers
  541. peer := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  542. peer.Start()
  543. conn, err := peer.Dial(sw.NetAddress())
  544. require.NoError(t, err)
  545. // check conn is closed
  546. one := make([]byte, 1)
  547. conn.SetReadDeadline(time.Now().Add(10 * time.Millisecond))
  548. _, err = conn.Read(one)
  549. assert.Equal(t, io.EOF, err)
  550. assert.Equal(t, cfg.MaxNumInboundPeers, sw.Peers().Size())
  551. peer.Stop()
  552. // 3. check we connect to unconditional peers despite the limit.
  553. for _, peer := range unconditionalPeers {
  554. c, err := peer.Dial(sw.NetAddress())
  555. require.NoError(t, err)
  556. // spawn a reading routine to prevent connection from closing
  557. go func(c net.Conn) {
  558. for {
  559. one := make([]byte, 1)
  560. _, err := c.Read(one)
  561. if err != nil {
  562. return
  563. }
  564. }
  565. }(c)
  566. }
  567. time.Sleep(10 * time.Millisecond)
  568. assert.Equal(t, cfg.MaxNumInboundPeers+unconditionalPeersNum, sw.Peers().Size())
  569. for _, peer := range peers {
  570. peer.Stop()
  571. }
  572. for _, peer := range unconditionalPeers {
  573. peer.Stop()
  574. }
  575. }
  576. type errorTransport struct {
  577. acceptErr error
  578. }
  579. func (et errorTransport) NetAddress() NetAddress {
  580. panic("not implemented")
  581. }
  582. func (et errorTransport) Accept(c peerConfig) (Peer, error) {
  583. return nil, et.acceptErr
  584. }
  585. func (errorTransport) Dial(NetAddress, peerConfig) (Peer, error) {
  586. panic("not implemented")
  587. }
  588. func (errorTransport) Cleanup(Peer) {
  589. panic("not implemented")
  590. }
  591. func TestSwitchAcceptRoutineErrorCases(t *testing.T) {
  592. sw := NewSwitch(cfg, errorTransport{ErrFilterTimeout{}})
  593. assert.NotPanics(t, func() {
  594. err := sw.Start()
  595. assert.NoError(t, err)
  596. sw.Stop()
  597. })
  598. sw = NewSwitch(cfg, errorTransport{ErrRejected{conn: nil, err: errors.New("filtered"), isFiltered: true}})
  599. assert.NotPanics(t, func() {
  600. err := sw.Start()
  601. assert.NoError(t, err)
  602. sw.Stop()
  603. })
  604. // TODO(melekes) check we remove our address from addrBook
  605. sw = NewSwitch(cfg, errorTransport{ErrTransportClosed{}})
  606. assert.NotPanics(t, func() {
  607. err := sw.Start()
  608. assert.NoError(t, err)
  609. err = sw.Stop()
  610. require.NoError(t, err)
  611. })
  612. }
  613. // mockReactor checks that InitPeer never called before RemovePeer. If that's
  614. // not true, InitCalledBeforeRemoveFinished will return true.
  615. type mockReactor struct {
  616. *BaseReactor
  617. // atomic
  618. removePeerInProgress uint32
  619. initCalledBeforeRemoveFinished uint32
  620. }
  621. func (r *mockReactor) RemovePeer(peer Peer, reason interface{}) {
  622. atomic.StoreUint32(&r.removePeerInProgress, 1)
  623. defer atomic.StoreUint32(&r.removePeerInProgress, 0)
  624. time.Sleep(100 * time.Millisecond)
  625. }
  626. func (r *mockReactor) InitPeer(peer Peer) Peer {
  627. if atomic.LoadUint32(&r.removePeerInProgress) == 1 {
  628. atomic.StoreUint32(&r.initCalledBeforeRemoveFinished, 1)
  629. }
  630. return peer
  631. }
  632. func (r *mockReactor) InitCalledBeforeRemoveFinished() bool {
  633. return atomic.LoadUint32(&r.initCalledBeforeRemoveFinished) == 1
  634. }
  635. // see stopAndRemovePeer
  636. func TestSwitchInitPeerIsNotCalledBeforeRemovePeer(t *testing.T) {
  637. // make reactor
  638. reactor := &mockReactor{}
  639. reactor.BaseReactor = NewBaseReactor("mockReactor", reactor)
  640. // make switch
  641. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", func(i int, sw *Switch) *Switch {
  642. sw.AddReactor("mock", reactor)
  643. return sw
  644. })
  645. err := sw.Start()
  646. require.NoError(t, err)
  647. defer sw.Stop()
  648. // add peer
  649. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  650. rp.Start()
  651. defer rp.Stop()
  652. _, err = rp.Dial(sw.NetAddress())
  653. require.NoError(t, err)
  654. // wait till the switch adds rp to the peer set, then stop the peer asynchronously
  655. for {
  656. time.Sleep(20 * time.Millisecond)
  657. if peer := sw.Peers().Get(rp.ID()); peer != nil {
  658. go sw.StopPeerForError(peer, "test")
  659. break
  660. }
  661. }
  662. // simulate peer reconnecting to us
  663. _, err = rp.Dial(sw.NetAddress())
  664. require.NoError(t, err)
  665. // wait till the switch adds rp to the peer set
  666. time.Sleep(50 * time.Millisecond)
  667. // make sure reactor.RemovePeer is finished before InitPeer is called
  668. assert.False(t, reactor.InitCalledBeforeRemoveFinished())
  669. }
  670. func BenchmarkSwitchBroadcast(b *testing.B) {
  671. s1, s2 := MakeSwitchPair(b, func(i int, sw *Switch) *Switch {
  672. // Make bar reactors of bar channels each
  673. sw.AddReactor("foo", NewTestReactor([]*conn.ChannelDescriptor{
  674. {ID: byte(0x00), Priority: 10},
  675. {ID: byte(0x01), Priority: 10},
  676. }, false))
  677. sw.AddReactor("bar", NewTestReactor([]*conn.ChannelDescriptor{
  678. {ID: byte(0x02), Priority: 10},
  679. {ID: byte(0x03), Priority: 10},
  680. }, false))
  681. return sw
  682. })
  683. b.Cleanup(func() {
  684. if err := s1.Stop(); err != nil {
  685. b.Error(err)
  686. }
  687. })
  688. b.Cleanup(func() {
  689. if err := s2.Stop(); err != nil {
  690. b.Error(err)
  691. }
  692. })
  693. // Allow time for goroutines to boot up
  694. time.Sleep(1 * time.Second)
  695. b.ResetTimer()
  696. numSuccess, numFailure := 0, 0
  697. // Send random message from foo channel to another
  698. for i := 0; i < b.N; i++ {
  699. chID := byte(i % 4)
  700. successChan := s1.Broadcast(chID, []byte("test data"))
  701. for s := range successChan {
  702. if s {
  703. numSuccess++
  704. } else {
  705. numFailure++
  706. }
  707. }
  708. }
  709. b.Logf("success: %v, failure: %v", numSuccess, numFailure)
  710. }
  711. type addrBookMock struct {
  712. addrs map[string]struct{}
  713. ourAddrs map[string]struct{}
  714. }
  715. var _ AddrBook = (*addrBookMock)(nil)
  716. func (book *addrBookMock) AddAddress(addr *NetAddress, src *NetAddress) error {
  717. book.addrs[addr.String()] = struct{}{}
  718. return nil
  719. }
  720. func (book *addrBookMock) AddOurAddress(addr *NetAddress) { book.ourAddrs[addr.String()] = struct{}{} }
  721. func (book *addrBookMock) OurAddress(addr *NetAddress) bool {
  722. _, ok := book.ourAddrs[addr.String()]
  723. return ok
  724. }
  725. func (book *addrBookMock) MarkGood(ID) {}
  726. func (book *addrBookMock) HasAddress(addr *NetAddress) bool {
  727. _, ok := book.addrs[addr.String()]
  728. return ok
  729. }
  730. func (book *addrBookMock) RemoveAddress(addr *NetAddress) {
  731. delete(book.addrs, addr.String())
  732. }
  733. func (book *addrBookMock) Save() {}