Fix hanging test cases by backporting some sim code.

This commit is contained in:
Stephen Diehl
2012-03-12 10:11:29 -04:00
parent ecd98f1aa0
commit 6171cdf582
3 changed files with 48 additions and 11 deletions
+2 -4
View File
@@ -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()
+37 -7
View File
@@ -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."
)
)
+9
View File
@@ -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):