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):