From 6eb7e76db41accd25eb9e5c99a0eb6560c875eb1 Mon Sep 17 00:00:00 2001 From: Martijn de Vos Date: Sat, 7 Apr 2018 10:47:26 +0200 Subject: [PATCH] Disabled IPv8 during endpoint tests This can lead to a dirty threadpool due to IPv8 messages coming in after the test has ended. --- .../Modules/RestApi/test_debug_endpoint.py | 8 +----- .../RestApi/test_statistics_endpoint.py | 28 +++++++++++++++++-- .../RestApi/test_trustchain_endpoint.py | 19 +++++++++---- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/Tribler/Test/Core/Modules/RestApi/test_debug_endpoint.py b/Tribler/Test/Core/Modules/RestApi/test_debug_endpoint.py index 2fce2577ee2..76bddc7bde7 100644 --- a/Tribler/Test/Core/Modules/RestApi/test_debug_endpoint.py +++ b/Tribler/Test/Core/Modules/RestApi/test_debug_endpoint.py @@ -9,18 +9,11 @@ class TestCircuitDebugEndpoint(AbstractApiTest): - def setUpPreSession(self): - super(TestCircuitDebugEndpoint, self).setUpPreSession() - self.config.set_ipv8_enabled(True) - self.config.set_tunnel_community_enabled(True) - self.config.set_tunnel_community_socks5_listen_ports(self.get_socks5_ports()) - @deferred(timeout=10) def test_get_circuit_no_community(self): """ Testing whether the API returns error 404 if no tunnel community is loaded """ - self.session.lm.tunnel_community = None return self.do_request('debug/circuits', expected_code=404) @deferred(timeout=10) @@ -44,6 +37,7 @@ def test_get_circuits(self): mock_circuit.ctype = CIRCUIT_TYPE_DATA mock_circuit.destroy = lambda: None + self.session.lm.tunnel_community = MockObject() self.session.lm.tunnel_community.circuits = {1234: mock_circuit} def verify_response(response): diff --git a/Tribler/Test/Core/Modules/RestApi/test_statistics_endpoint.py b/Tribler/Test/Core/Modules/RestApi/test_statistics_endpoint.py index 572ce8edf4c..a51823e75ce 100644 --- a/Tribler/Test/Core/Modules/RestApi/test_statistics_endpoint.py +++ b/Tribler/Test/Core/Modules/RestApi/test_statistics_endpoint.py @@ -1,14 +1,36 @@ import Tribler.Core.Utilities.json_util as json from Tribler.Test.Core.Modules.RestApi.base_api_test import AbstractApiTest +from Tribler.Test.mocking.ipv8 import MockIPv8 from Tribler.Test.twisted_thread import deferred +from Tribler.pyipv8.ipv8.attestation.trustchain.community import TrustChainCommunity +from Tribler.pyipv8.ipv8.util import blocking_call_on_reactor_thread +from twisted.internet.defer import inlineCallbacks class TestStatisticsEndpoint(AbstractApiTest): + @blocking_call_on_reactor_thread + @inlineCallbacks + def setUp(self, autoload_discovery=True): + yield super(TestStatisticsEndpoint, self).setUp(autoload_discovery=autoload_discovery) + + self.mock_ipv8 = MockIPv8(u"low", + TrustChainCommunity, + working_directory=self.session.config.get_state_dir()) + self.mock_ipv8.overlays = [self.mock_ipv8.overlay] + self.session.lm.ipv8 = self.mock_ipv8 + self.session.config.set_ipv8_enabled(True) + + @blocking_call_on_reactor_thread + @inlineCallbacks + def tearDown(self, annotate=True): + self.session.lm.ipv8 = None + yield self.mock_ipv8.unload() + yield super(TestStatisticsEndpoint, self).tearDown(annotate=annotate) + def setUpPreSession(self): super(TestStatisticsEndpoint, self).setUpPreSession() self.config.set_dispersy_enabled(True) - self.config.set_ipv8_enabled(True) self.config.set_torrent_collecting_enabled(True) @deferred(timeout=10) @@ -39,7 +61,9 @@ def test_get_community_statistics(self): Testing whether the API returns a correct community statistics dictionary when requested """ def verify_dict(data): - self.assertTrue(json.loads(data)["dispersy_community_statistics"]) + json_data = json.loads(data) + self.assertTrue(json_data["dispersy_community_statistics"]) + self.assertTrue(json_data["ipv8_overlay_statistics"]) self.should_check_equality = False return self.do_request('statistics/communities', expected_code=200).addCallback(verify_dict) diff --git a/Tribler/Test/Core/Modules/RestApi/test_trustchain_endpoint.py b/Tribler/Test/Core/Modules/RestApi/test_trustchain_endpoint.py index 391d6829445..cf7306e2c6f 100644 --- a/Tribler/Test/Core/Modules/RestApi/test_trustchain_endpoint.py +++ b/Tribler/Test/Core/Modules/RestApi/test_trustchain_endpoint.py @@ -1,5 +1,8 @@ import json +from Tribler.Core.Modules.wallet.tc_wallet import TrustchainWallet +from Tribler.Test.mocking.ipv8 import MockIPv8 +from Tribler.pyipv8.ipv8.attestation.trustchain.community import TrustChainCommunity from twisted.internet.defer import inlineCallbacks from Tribler.dispersy.util import blocking_call_on_reactor_thread @@ -14,12 +17,18 @@ class TestTrustchainStatsEndpoint(AbstractApiTest): @inlineCallbacks def setUp(self, autoload_discovery=True): yield super(TestTrustchainStatsEndpoint, self).setUp(autoload_discovery=autoload_discovery) - self.session.lm.trustchain_community._use_main_thread = False - def setUpPreSession(self): - super(TestTrustchainStatsEndpoint, self).setUpPreSession() - self.config.set_ipv8_enabled(True) - self.config.set_trustchain_enabled(True) + self.mock_ipv8 = MockIPv8(u"low", + TrustChainCommunity, + working_directory=self.session.config.get_state_dir()) + self.session.lm.trustchain_community = self.mock_ipv8.overlay + self.session.lm.wallets['MB'] = TrustchainWallet(self.session.lm.trustchain_community) + + @blocking_call_on_reactor_thread + @inlineCallbacks + def tearDown(self, annotate=True): + yield self.mock_ipv8.unload() + yield super(TestTrustchainStatsEndpoint, self).tearDown(annotate=annotate) @deferred(timeout=10) def test_get_statistics_no_community(self):