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.

841 lines
20 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
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. err := sw.Start()
  198. require.NoError(t, err)
  199. t.Cleanup(func() {
  200. if err := sw.Stop(); err != nil {
  201. t.Error(err)
  202. }
  203. })
  204. // simulate remote peer
  205. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  206. rp.Start()
  207. t.Cleanup(rp.Stop)
  208. p, err := sw.transport.Dial(*rp.Addr(), peerConfig{
  209. chDescs: sw.chDescs,
  210. onPeerError: sw.StopPeerForError,
  211. isPersistent: sw.IsPeerPersistent,
  212. reactorsByCh: sw.reactorsByCh,
  213. })
  214. if err != nil {
  215. t.Fatal(err)
  216. }
  217. err = sw.addPeer(p)
  218. if err, ok := err.(ErrRejected); ok {
  219. if !err.IsFiltered() {
  220. t.Errorf("expected peer to be filtered")
  221. }
  222. } else {
  223. t.Errorf("expected ErrRejected")
  224. }
  225. }
  226. func TestSwitchPeerFilterTimeout(t *testing.T) {
  227. var (
  228. filters = []PeerFilterFunc{
  229. func(_ IPeerSet, _ Peer) error {
  230. time.Sleep(10 * time.Millisecond)
  231. return nil
  232. },
  233. }
  234. sw = MakeSwitch(
  235. cfg,
  236. 1,
  237. "testing",
  238. "123.123.123",
  239. initSwitchFunc,
  240. SwitchFilterTimeout(5*time.Millisecond),
  241. SwitchPeerFilters(filters...),
  242. )
  243. )
  244. err := sw.Start()
  245. require.NoError(t, err)
  246. t.Cleanup(func() {
  247. if err := sw.Stop(); err != nil {
  248. t.Log(err)
  249. }
  250. })
  251. // simulate remote peer
  252. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  253. rp.Start()
  254. defer rp.Stop()
  255. p, err := sw.transport.Dial(*rp.Addr(), peerConfig{
  256. chDescs: sw.chDescs,
  257. onPeerError: sw.StopPeerForError,
  258. isPersistent: sw.IsPeerPersistent,
  259. reactorsByCh: sw.reactorsByCh,
  260. })
  261. if err != nil {
  262. t.Fatal(err)
  263. }
  264. err = sw.addPeer(p)
  265. if _, ok := err.(ErrFilterTimeout); !ok {
  266. t.Errorf("expected ErrFilterTimeout")
  267. }
  268. }
  269. func TestSwitchPeerFilterDuplicate(t *testing.T) {
  270. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  271. err := sw.Start()
  272. require.NoError(t, err)
  273. t.Cleanup(func() {
  274. if err := sw.Stop(); err != nil {
  275. t.Error(err)
  276. }
  277. })
  278. // simulate remote peer
  279. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  280. rp.Start()
  281. defer rp.Stop()
  282. p, err := sw.transport.Dial(*rp.Addr(), peerConfig{
  283. chDescs: sw.chDescs,
  284. onPeerError: sw.StopPeerForError,
  285. isPersistent: sw.IsPeerPersistent,
  286. reactorsByCh: sw.reactorsByCh,
  287. })
  288. if err != nil {
  289. t.Fatal(err)
  290. }
  291. if err := sw.addPeer(p); err != nil {
  292. t.Fatal(err)
  293. }
  294. err = sw.addPeer(p)
  295. if errRej, ok := err.(ErrRejected); ok {
  296. if !errRej.IsDuplicate() {
  297. t.Errorf("expected peer to be duplicate. got %v", errRej)
  298. }
  299. } else {
  300. t.Errorf("expected ErrRejected, got %v", err)
  301. }
  302. }
  303. func assertNoPeersAfterTimeout(t *testing.T, sw *Switch, timeout time.Duration) {
  304. time.Sleep(timeout)
  305. if sw.Peers().Size() != 0 {
  306. t.Fatalf("Expected %v to not connect to some peers, got %d", sw, sw.Peers().Size())
  307. }
  308. }
  309. func TestSwitchStopsNonPersistentPeerOnError(t *testing.T) {
  310. assert, require := assert.New(t), require.New(t)
  311. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  312. err := sw.Start()
  313. if err != nil {
  314. t.Error(err)
  315. }
  316. t.Cleanup(func() {
  317. if err := sw.Stop(); err != nil {
  318. t.Error(err)
  319. }
  320. })
  321. // simulate remote peer
  322. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  323. rp.Start()
  324. defer rp.Stop()
  325. p, err := sw.transport.Dial(*rp.Addr(), peerConfig{
  326. chDescs: sw.chDescs,
  327. onPeerError: sw.StopPeerForError,
  328. isPersistent: sw.IsPeerPersistent,
  329. reactorsByCh: sw.reactorsByCh,
  330. })
  331. require.Nil(err)
  332. err = sw.addPeer(p)
  333. require.Nil(err)
  334. require.NotNil(sw.Peers().Get(rp.ID()))
  335. // simulate failure by closing connection
  336. err = p.(*peer).CloseConn()
  337. require.NoError(err)
  338. assertNoPeersAfterTimeout(t, sw, 100*time.Millisecond)
  339. assert.False(p.IsRunning())
  340. }
  341. func TestSwitchStopPeerForError(t *testing.T) {
  342. s := httptest.NewServer(promhttp.Handler())
  343. defer s.Close()
  344. scrapeMetrics := func() string {
  345. resp, err := http.Get(s.URL)
  346. require.NoError(t, err)
  347. defer resp.Body.Close()
  348. buf, _ := ioutil.ReadAll(resp.Body)
  349. return string(buf)
  350. }
  351. namespace, subsystem, name := config.TestInstrumentationConfig().Namespace, MetricsSubsystem, "peers"
  352. re := regexp.MustCompile(namespace + `_` + subsystem + `_` + name + ` ([0-9\.]+)`)
  353. peersMetricValue := func() float64 {
  354. matches := re.FindStringSubmatch(scrapeMetrics())
  355. f, _ := strconv.ParseFloat(matches[1], 64)
  356. return f
  357. }
  358. p2pMetrics := PrometheusMetrics(namespace)
  359. // make two connected switches
  360. sw1, sw2 := MakeSwitchPair(t, func(i int, sw *Switch) *Switch {
  361. // set metrics on sw1
  362. if i == 0 {
  363. opt := WithMetrics(p2pMetrics)
  364. opt(sw)
  365. }
  366. return initSwitchFunc(i, sw)
  367. })
  368. assert.Equal(t, len(sw1.Peers().List()), 1)
  369. assert.EqualValues(t, 1, peersMetricValue())
  370. // send messages to the peer from sw1
  371. p := sw1.Peers().List()[0]
  372. p.Send(0x1, []byte("here's a message to send"))
  373. // stop sw2. this should cause the p to fail,
  374. // which results in calling StopPeerForError internally
  375. t.Cleanup(func() {
  376. if err := sw2.Stop(); err != nil {
  377. t.Error(err)
  378. }
  379. })
  380. // now call StopPeerForError explicitly, eg. from a reactor
  381. sw1.StopPeerForError(p, fmt.Errorf("some err"))
  382. assert.Equal(t, len(sw1.Peers().List()), 0)
  383. assert.EqualValues(t, 0, peersMetricValue())
  384. }
  385. func TestSwitchReconnectsToOutboundPersistentPeer(t *testing.T) {
  386. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  387. err := sw.Start()
  388. require.NoError(t, err)
  389. t.Cleanup(func() {
  390. if err := sw.Stop(); err != nil {
  391. t.Error(err)
  392. }
  393. })
  394. // 1. simulate failure by closing connection
  395. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  396. rp.Start()
  397. defer rp.Stop()
  398. err = sw.AddPersistentPeers([]string{rp.Addr().String()})
  399. require.NoError(t, err)
  400. err = sw.DialPeerWithAddress(rp.Addr())
  401. require.Nil(t, err)
  402. require.NotNil(t, sw.Peers().Get(rp.ID()))
  403. p := sw.Peers().List()[0]
  404. err = p.(*peer).CloseConn()
  405. require.NoError(t, err)
  406. waitUntilSwitchHasAtLeastNPeers(sw, 1)
  407. assert.False(t, p.IsRunning()) // old peer instance
  408. assert.Equal(t, 1, sw.Peers().Size()) // new peer instance
  409. // 2. simulate first time dial failure
  410. rp = &remotePeer{
  411. PrivKey: ed25519.GenPrivKey(),
  412. Config: cfg,
  413. // Use different interface to prevent duplicate IP filter, this will break
  414. // beyond two peers.
  415. listenAddr: "127.0.0.1:0",
  416. }
  417. rp.Start()
  418. defer rp.Stop()
  419. conf := config.DefaultP2PConfig()
  420. conf.TestDialFail = true // will trigger a reconnect
  421. err = sw.addOutboundPeerWithConfig(rp.Addr(), conf)
  422. require.NotNil(t, err)
  423. // DialPeerWithAddres - sw.peerConfig resets the dialer
  424. waitUntilSwitchHasAtLeastNPeers(sw, 2)
  425. assert.Equal(t, 2, sw.Peers().Size())
  426. }
  427. func TestSwitchReconnectsToInboundPersistentPeer(t *testing.T) {
  428. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  429. err := sw.Start()
  430. require.NoError(t, err)
  431. t.Cleanup(func() {
  432. if err := sw.Stop(); err != nil {
  433. t.Error(err)
  434. }
  435. })
  436. // 1. simulate failure by closing the connection
  437. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  438. rp.Start()
  439. defer rp.Stop()
  440. err = sw.AddPersistentPeers([]string{rp.Addr().String()})
  441. require.NoError(t, err)
  442. conn, err := rp.Dial(sw.NetAddress())
  443. require.NoError(t, err)
  444. time.Sleep(50 * time.Millisecond)
  445. require.NotNil(t, sw.Peers().Get(rp.ID()))
  446. conn.Close()
  447. waitUntilSwitchHasAtLeastNPeers(sw, 1)
  448. assert.Equal(t, 1, sw.Peers().Size())
  449. }
  450. func TestSwitchDialPeersAsync(t *testing.T) {
  451. if testing.Short() {
  452. return
  453. }
  454. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  455. err := sw.Start()
  456. require.NoError(t, err)
  457. t.Cleanup(func() {
  458. if err := sw.Stop(); err != nil {
  459. t.Error(err)
  460. }
  461. })
  462. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  463. rp.Start()
  464. defer rp.Stop()
  465. err = sw.DialPeersAsync([]string{rp.Addr().String()})
  466. require.NoError(t, err)
  467. time.Sleep(dialRandomizerIntervalMilliseconds * time.Millisecond)
  468. require.NotNil(t, sw.Peers().Get(rp.ID()))
  469. }
  470. func waitUntilSwitchHasAtLeastNPeers(sw *Switch, n int) {
  471. for i := 0; i < 20; i++ {
  472. time.Sleep(250 * time.Millisecond)
  473. has := sw.Peers().Size()
  474. if has >= n {
  475. break
  476. }
  477. }
  478. }
  479. func TestSwitchFullConnectivity(t *testing.T) {
  480. switches := MakeConnectedSwitches(cfg, 3, initSwitchFunc, Connect2Switches)
  481. defer func() {
  482. for _, sw := range switches {
  483. sw := sw
  484. t.Cleanup(func() {
  485. if err := sw.Stop(); err != nil {
  486. t.Error(err)
  487. }
  488. })
  489. }
  490. }()
  491. for i, sw := range switches {
  492. if sw.Peers().Size() != 2 {
  493. t.Fatalf("Expected each switch to be connected to 2 other, but %d switch only connected to %d", sw.Peers().Size(), i)
  494. }
  495. }
  496. }
  497. func TestSwitchAcceptRoutine(t *testing.T) {
  498. cfg.MaxNumInboundPeers = 5
  499. // Create some unconditional peers.
  500. const unconditionalPeersNum = 2
  501. var (
  502. unconditionalPeers = make([]*remotePeer, unconditionalPeersNum)
  503. unconditionalPeerIDs = make([]string, unconditionalPeersNum)
  504. )
  505. for i := 0; i < unconditionalPeersNum; i++ {
  506. peer := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  507. peer.Start()
  508. unconditionalPeers[i] = peer
  509. unconditionalPeerIDs[i] = string(peer.ID())
  510. }
  511. // make switch
  512. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", initSwitchFunc)
  513. err := sw.AddUnconditionalPeerIDs(unconditionalPeerIDs)
  514. require.NoError(t, err)
  515. err = sw.Start()
  516. require.NoError(t, err)
  517. t.Cleanup(func() {
  518. if err := sw.Stop(); err != nil {
  519. t.Error(err)
  520. }
  521. })
  522. // 0. check there are no peers
  523. assert.Equal(t, 0, sw.Peers().Size())
  524. // 1. check we connect up to MaxNumInboundPeers
  525. peers := make([]*remotePeer, 0)
  526. for i := 0; i < cfg.MaxNumInboundPeers; i++ {
  527. peer := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  528. peers = append(peers, peer)
  529. peer.Start()
  530. c, err := peer.Dial(sw.NetAddress())
  531. require.NoError(t, err)
  532. // spawn a reading routine to prevent connection from closing
  533. go func(c net.Conn) {
  534. for {
  535. one := make([]byte, 1)
  536. _, err := c.Read(one)
  537. if err != nil {
  538. return
  539. }
  540. }
  541. }(c)
  542. }
  543. time.Sleep(10 * time.Millisecond)
  544. assert.Equal(t, cfg.MaxNumInboundPeers, sw.Peers().Size())
  545. // 2. check we close new connections if we already have MaxNumInboundPeers peers
  546. peer := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  547. peer.Start()
  548. conn, err := peer.Dial(sw.NetAddress())
  549. require.NoError(t, err)
  550. // check conn is closed
  551. one := make([]byte, 1)
  552. err = conn.SetReadDeadline(time.Now().Add(10 * time.Millisecond))
  553. require.NoError(t, err)
  554. _, err = conn.Read(one)
  555. assert.Equal(t, io.EOF, err)
  556. assert.Equal(t, cfg.MaxNumInboundPeers, sw.Peers().Size())
  557. peer.Stop()
  558. // 3. check we connect to unconditional peers despite the limit.
  559. for _, peer := range unconditionalPeers {
  560. c, err := peer.Dial(sw.NetAddress())
  561. require.NoError(t, err)
  562. // spawn a reading routine to prevent connection from closing
  563. go func(c net.Conn) {
  564. for {
  565. one := make([]byte, 1)
  566. _, err := c.Read(one)
  567. if err != nil {
  568. return
  569. }
  570. }
  571. }(c)
  572. }
  573. time.Sleep(10 * time.Millisecond)
  574. assert.Equal(t, cfg.MaxNumInboundPeers+unconditionalPeersNum, sw.Peers().Size())
  575. for _, peer := range peers {
  576. peer.Stop()
  577. }
  578. for _, peer := range unconditionalPeers {
  579. peer.Stop()
  580. }
  581. }
  582. type errorTransport struct {
  583. acceptErr error
  584. }
  585. func (et errorTransport) NetAddress() NetAddress {
  586. panic("not implemented")
  587. }
  588. func (et errorTransport) Accept(c peerConfig) (Peer, error) {
  589. return nil, et.acceptErr
  590. }
  591. func (errorTransport) Dial(NetAddress, peerConfig) (Peer, error) {
  592. panic("not implemented")
  593. }
  594. func (errorTransport) Cleanup(Peer) {
  595. panic("not implemented")
  596. }
  597. func TestSwitchAcceptRoutineErrorCases(t *testing.T) {
  598. sw := NewSwitch(cfg, errorTransport{ErrFilterTimeout{}})
  599. assert.NotPanics(t, func() {
  600. err := sw.Start()
  601. require.NoError(t, err)
  602. err = sw.Stop()
  603. require.NoError(t, err)
  604. })
  605. sw = NewSwitch(cfg, errorTransport{ErrRejected{conn: nil, err: errors.New("filtered"), isFiltered: true}})
  606. assert.NotPanics(t, func() {
  607. err := sw.Start()
  608. require.NoError(t, err)
  609. err = sw.Stop()
  610. require.NoError(t, err)
  611. })
  612. // TODO(melekes) check we remove our address from addrBook
  613. sw = NewSwitch(cfg, errorTransport{ErrTransportClosed{}})
  614. assert.NotPanics(t, func() {
  615. err := sw.Start()
  616. require.NoError(t, err)
  617. err = sw.Stop()
  618. require.NoError(t, err)
  619. })
  620. }
  621. // mockReactor checks that InitPeer never called before RemovePeer. If that's
  622. // not true, InitCalledBeforeRemoveFinished will return true.
  623. type mockReactor struct {
  624. *BaseReactor
  625. // atomic
  626. removePeerInProgress uint32
  627. initCalledBeforeRemoveFinished uint32
  628. }
  629. func (r *mockReactor) RemovePeer(peer Peer, reason interface{}) {
  630. atomic.StoreUint32(&r.removePeerInProgress, 1)
  631. defer atomic.StoreUint32(&r.removePeerInProgress, 0)
  632. time.Sleep(100 * time.Millisecond)
  633. }
  634. func (r *mockReactor) InitPeer(peer Peer) Peer {
  635. if atomic.LoadUint32(&r.removePeerInProgress) == 1 {
  636. atomic.StoreUint32(&r.initCalledBeforeRemoveFinished, 1)
  637. }
  638. return peer
  639. }
  640. func (r *mockReactor) InitCalledBeforeRemoveFinished() bool {
  641. return atomic.LoadUint32(&r.initCalledBeforeRemoveFinished) == 1
  642. }
  643. // see stopAndRemovePeer
  644. func TestSwitchInitPeerIsNotCalledBeforeRemovePeer(t *testing.T) {
  645. // make reactor
  646. reactor := &mockReactor{}
  647. reactor.BaseReactor = NewBaseReactor("mockReactor", reactor)
  648. // make switch
  649. sw := MakeSwitch(cfg, 1, "testing", "123.123.123", func(i int, sw *Switch) *Switch {
  650. sw.AddReactor("mock", reactor)
  651. return sw
  652. })
  653. err := sw.Start()
  654. require.NoError(t, err)
  655. t.Cleanup(func() {
  656. if err := sw.Stop(); err != nil {
  657. t.Error(err)
  658. }
  659. })
  660. // add peer
  661. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  662. rp.Start()
  663. defer rp.Stop()
  664. _, err = rp.Dial(sw.NetAddress())
  665. require.NoError(t, err)
  666. // wait till the switch adds rp to the peer set, then stop the peer asynchronously
  667. for {
  668. time.Sleep(20 * time.Millisecond)
  669. if peer := sw.Peers().Get(rp.ID()); peer != nil {
  670. go sw.StopPeerForError(peer, "test")
  671. break
  672. }
  673. }
  674. // simulate peer reconnecting to us
  675. _, err = rp.Dial(sw.NetAddress())
  676. require.NoError(t, err)
  677. // wait till the switch adds rp to the peer set
  678. time.Sleep(50 * time.Millisecond)
  679. // make sure reactor.RemovePeer is finished before InitPeer is called
  680. assert.False(t, reactor.InitCalledBeforeRemoveFinished())
  681. }
  682. func BenchmarkSwitchBroadcast(b *testing.B) {
  683. s1, s2 := MakeSwitchPair(b, func(i int, sw *Switch) *Switch {
  684. // Make bar reactors of bar channels each
  685. sw.AddReactor("foo", NewTestReactor([]*conn.ChannelDescriptor{
  686. {ID: byte(0x00), Priority: 10},
  687. {ID: byte(0x01), Priority: 10},
  688. }, false))
  689. sw.AddReactor("bar", NewTestReactor([]*conn.ChannelDescriptor{
  690. {ID: byte(0x02), Priority: 10},
  691. {ID: byte(0x03), Priority: 10},
  692. }, false))
  693. return sw
  694. })
  695. b.Cleanup(func() {
  696. if err := s1.Stop(); err != nil {
  697. b.Error(err)
  698. }
  699. })
  700. b.Cleanup(func() {
  701. if err := s2.Stop(); err != nil {
  702. b.Error(err)
  703. }
  704. })
  705. // Allow time for goroutines to boot up
  706. time.Sleep(1 * time.Second)
  707. b.ResetTimer()
  708. numSuccess, numFailure := 0, 0
  709. // Send random message from foo channel to another
  710. for i := 0; i < b.N; i++ {
  711. chID := byte(i % 4)
  712. successChan := s1.Broadcast(chID, []byte("test data"))
  713. for s := range successChan {
  714. if s {
  715. numSuccess++
  716. } else {
  717. numFailure++
  718. }
  719. }
  720. }
  721. b.Logf("success: %v, failure: %v", numSuccess, numFailure)
  722. }