From 6171cdf582456b569a25ffbf5c5cd32dd91b0472 Mon Sep 17 00:00:00 2001 From: Stephen Diehl Date: Mon, 12 Mar 2012 10:11:29 -0400 Subject: [PATCH 1/3] Fix hanging test cases by backporting some sim code. --- zipline/simulator.py | 6 ++--- zipline/test/test_finance.py | 44 ++++++++++++++++++++++++++++------ zipline/test/test_messaging.py | 9 +++++++ 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/zipline/simulator.py b/zipline/simulator.py index 1e3e3f5f..b63d6d9b 100644 --- a/zipline/simulator.py +++ b/zipline/simulator.py @@ -61,10 +61,8 @@ class Simulator(ComponentHost): if not self.running: return - try: - self.controller.shutdown(context=self.context) - except: - import pdb; pdb.set_trace() + #if self.controller: + #self.controller.shutdown() for component in self.components.itervalues(): component.shutdown() diff --git a/zipline/test/test_finance.py b/zipline/test/test_finance.py index a1825d43..74d4f089 100644 --- a/zipline/test/test_finance.py +++ b/zipline/test/test_finance.py @@ -1,8 +1,11 @@ """Tests for the zipline.finance package""" import mock import pytz +import gevent + from unittest2 import TestCase from datetime import datetime, timedelta +from collections import defaultdict import zipline.test.factory as factory import zipline.util as qutil @@ -17,21 +20,44 @@ TradeSimulationClient from zipline.simulator import AddressAllocator, Simulator from zipline.monitor import Controller - class FinanceTestCase(TestCase): + leased_sockets = defaultdict(list) + def setUp(self): qutil.configure_logging() self.benchmark_returns, self.treasury_curves = \ factory.load_market_data() - + self.trading_environment = risk.TradingEnvironment( - self.benchmark_returns, + self.benchmark_returns, self.treasury_curves ) - - self.allocator = AddressAllocator(8) - + + self.allocator = AddressAllocator(1000) + + def tearDown(self): + self.unallocate_sockets() + + def allocate_sockets(self, n): + """ + Allocate sockets local to this test case, track them so + we can gc after test run. + """ + + assert isinstance(n, int) + assert n > 0 + + leased = self.allocator.lease(n) + + self.leased_sockets[self.id()].extend(leased) + return leased + + def unallocate_sockets(self): + """ + Unallocate sockets after we are done with them. + """ + self.allocator.reaquire(*self.leased_sockets[self.id()]) def test_trade_feed_protocol(self): @@ -196,6 +222,10 @@ class FinanceTestCase(TestCase): sim_context = sim.simulate() sim_context.join() + self.assertTrue(sim.ready()) + self.assertFalse(sim.exception) + + import pdb; pdb.set_trace() # TODO: Make more assertions about the final state of the components. self.assertEqual(sim.feed.pending_messages(), 0, \ @@ -320,4 +350,4 @@ as the simulator emits." perf_tracker.cumulative_performance.positions[133].sid, 133, "Portfolio should have one position in 133." - ) \ No newline at end of file + ) diff --git a/zipline/test/test_messaging.py b/zipline/test/test_messaging.py index 187b68b8..c901f984 100644 --- a/zipline/test/test_messaging.py +++ b/zipline/test/test_messaging.py @@ -17,6 +17,15 @@ from nose.tools import timed # it up as a test. Its a Mixin of sorts at this point. class SimulatorTestCase(object): + # Leased sockets is a defaultdict keyed by the test case. + # This lets you debug the sockets being allocated in the + # specific test cases and tear them down appropriately. + # + # { + # 'test_orders' : ['tcp : //127.0.0.1 : 1000', ... ], + # 'test_performance' : ['tcp : //127.0.0.1 : 1025', ... ], + # } + leased_sockets = defaultdict(list) def setUp(self): From e6296111e8337dcb3f5ec3e0682b952d8d5fbf8b Mon Sep 17 00:00:00 2001 From: Stephen Diehl Date: Mon, 12 Mar 2012 10:15:19 -0400 Subject: [PATCH 2/3] Woops left pdb in. --- zipline/test/test_finance.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/zipline/test/test_finance.py b/zipline/test/test_finance.py index 74d4f089..985fab8d 100644 --- a/zipline/test/test_finance.py +++ b/zipline/test/test_finance.py @@ -225,8 +225,6 @@ class FinanceTestCase(TestCase): self.assertTrue(sim.ready()) self.assertFalse(sim.exception) - import pdb; pdb.set_trace() - # TODO: Make more assertions about the final state of the components. self.assertEqual(sim.feed.pending_messages(), 0, \ "The feed should be drained of all messages, found {n} remaining." \ From defa3186cae261f58ff3bc373abc26a727e33eae Mon Sep 17 00:00:00 2001 From: Stephen Diehl Date: Mon, 12 Mar 2012 10:36:08 -0400 Subject: [PATCH 3/3] Time all the tests! --- zipline/test/test_finance.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/zipline/test/test_finance.py b/zipline/test/test_finance.py index 985fab8d..bf26ae0e 100644 --- a/zipline/test/test_finance.py +++ b/zipline/test/test_finance.py @@ -1,12 +1,13 @@ """Tests for the zipline.finance package""" import mock import pytz -import gevent from unittest2 import TestCase from datetime import datetime, timedelta from collections import defaultdict +from nose.tools import timed + import zipline.test.factory as factory import zipline.util as qutil import zipline.finance.risk as risk @@ -20,6 +21,10 @@ TradeSimulationClient from zipline.simulator import AddressAllocator, Simulator from zipline.monitor import Controller +DEFAULT_TIMEOUT = 5 # seconds + +allocator = AddressAllocator(1000) + class FinanceTestCase(TestCase): leased_sockets = defaultdict(list) @@ -34,10 +39,7 @@ class FinanceTestCase(TestCase): self.treasury_curves ) - self.allocator = AddressAllocator(1000) - - def tearDown(self): - self.unallocate_sockets() + self.allocator = allocator def allocate_sockets(self, n): """ @@ -53,12 +55,7 @@ class FinanceTestCase(TestCase): self.leased_sockets[self.id()].extend(leased) return leased - def unallocate_sockets(self): - """ - Unallocate sockets after we are done with them. - """ - self.allocator.reaquire(*self.leased_sockets[self.id()]) - + @timed(DEFAULT_TIMEOUT) def test_trade_feed_protocol(self): # TODO: Perhaps something more self-documenting for variables names? @@ -111,6 +108,7 @@ class FinanceTestCase(TestCase): self.assertEqual(zp.namedict(trade), event) + @timed(DEFAULT_TIMEOUT) def test_order_protocol(self): #client places an order order_msg = zp.ORDER_FRAME(133, 100) @@ -155,13 +153,14 @@ class FinanceTestCase(TestCase): self.assertEqual(recovered_tx.sid, 133) self.assertEqual(recovered_tx.amount, 100) + @timed(DEFAULT_TIMEOUT) def test_orders(self): # Just verify sending and receiving orders. # -------------- # Allocate sockets for the simulator components - sockets = self.allocator.lease(8) + sockets = self.allocate_sockets(8) addresses = { 'sync_address' : sockets[0], @@ -231,12 +230,14 @@ class FinanceTestCase(TestCase): .format(n=sim.feed.pending_messages())) + @timed(DEFAULT_TIMEOUT) def test_performance(self): + # verify order -> transaction -> portfolio position. # -------------- # Allocate sockets for the simulator components - sockets = self.allocator.lease(8) + sockets = self.allocate_sockets(8) addresses = { 'sync_address' : sockets[0],