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 474e95cb..4e8b3436 100644 --- a/zipline/test/test_finance.py +++ b/zipline/test/test_finance.py @@ -1,8 +1,12 @@ """Tests for the zipline.finance package""" import mock import pytz + 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 @@ -17,22 +21,41 @@ 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) + 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 = allocator + + 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 + + @timed(DEFAULT_TIMEOUT) def test_trade_feed_protocol(self): # TODO: Perhaps something more self-documenting for variables names? @@ -85,6 +108,7 @@ class FinanceTestCase(TestCase): self.assertEqual(zp.namedict(trade), event) + @timed(DEFAULT_TIMEOUT) def test_order_protocol(self): #client places an order now = datetime.utcnow().replace(tzinfo=pytz.utc) @@ -135,13 +159,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], @@ -202,6 +227,8 @@ class FinanceTestCase(TestCase): sim_context = sim.simulate() sim_context.join() + self.assertTrue(sim.ready()) + self.assertFalse(sim.exception) # TODO: Make more assertions about the final state of the components. self.assertEqual(sim.feed.pending_messages(), 0, \ @@ -209,12 +236,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], @@ -325,4 +354,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):