mirror of
https://github.com/wassname/catalyst.git
synced 2026-07-16 11:18:11 +08:00
exception handling code revised. gevent, pypy, and threadsim vestiges removed.
devel flag removed.
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import logging
|
||||
from gevent_zeromq import zmq
|
||||
|
||||
import zipline.protocol as zp
|
||||
from zipline.core.component import Component
|
||||
|
||||
@@ -2,14 +2,17 @@ import zmq
|
||||
|
||||
from unittest2 import TestCase
|
||||
from collections import defaultdict
|
||||
from logbook.compat import LoggingHandler
|
||||
|
||||
from zipline.test_algorithms import ExceptionAlgorithm
|
||||
from zipline.test_algorithms import ExceptionAlgorithm, NoopAlgorithm
|
||||
from zipline.finance.trading import SIMULATION_STYLE
|
||||
from zipline.core.devsimulator import AddressAllocator
|
||||
from zipline.lines import SimulatedTrading
|
||||
|
||||
from zipline.utils.test_utils import drain_zipline, check
|
||||
from zipline.utils.test_utils import \
|
||||
drain_zipline, \
|
||||
check, \
|
||||
setup_logger, \
|
||||
teardown_logger
|
||||
|
||||
DEFAULT_TIMEOUT = 15 # seconds
|
||||
EXTENDED_TIMEOUT = 90
|
||||
@@ -30,67 +33,126 @@ class ExceptionTestCase(TestCase):
|
||||
'simulation_style' : SIMULATION_STYLE.FIXED_SLIPPAGE
|
||||
}
|
||||
self.ctx = zmq.Context()
|
||||
|
||||
self.log_handler = LoggingHandler()
|
||||
self.log_handler.push_application()
|
||||
setup_logger(self)
|
||||
|
||||
def tearDown(self):
|
||||
self.log_handler.pop_application()
|
||||
self.ctx.term()
|
||||
teardown_logger(self)
|
||||
|
||||
def test_exception_in_init(self):
|
||||
# Simulation
|
||||
# ----------
|
||||
self.zipline_test_config['algorithm'] = \
|
||||
ExceptionAlgorithm(
|
||||
'initialize',
|
||||
self.zipline_test_config['sid']
|
||||
)
|
||||
|
||||
zipline = SimulatedTrading.create_test_zipline(
|
||||
**self.zipline_test_config
|
||||
)
|
||||
output, _ = drain_zipline(self, zipline)
|
||||
self.assertEqual(len(output), 1)
|
||||
self.assertEqual(output[-1]['prefix'], 'EXCEPTION')
|
||||
payload = output[-1]['payload']
|
||||
#check(self, payload, INITIALIZE_TB)
|
||||
|
||||
self.assertTrue(zipline.sim.ready())
|
||||
self.assertFalse(zipline.sim.exception)
|
||||
|
||||
|
||||
def test_exception_in_handle_data(self):
|
||||
|
||||
# Simulation
|
||||
# ----------
|
||||
|
||||
self.zipline_test_config['algorithm'] = \
|
||||
ExceptionAlgorithm('initialize')
|
||||
ExceptionAlgorithm(
|
||||
'handle_data',
|
||||
self.zipline_test_config['sid']
|
||||
)
|
||||
|
||||
zipline = SimulatedTrading.create_test_zipline(
|
||||
**self.zipline_test_config
|
||||
)
|
||||
|
||||
output, _ = drain_zipline(self, zipline)
|
||||
|
||||
self.assertEqual(len(output), 1)
|
||||
self.assertEqual(output[-1]['prefix'], 'EXCEPTION')
|
||||
payload = output[-1]['payload']
|
||||
check(self, payload, INITIALIZE_STACK_TB)
|
||||
#check(self, payload, HANDLE_DATA_TB)
|
||||
|
||||
import nose.tools; nose.tools.set_trace()
|
||||
self.assertTrue(zipline.sim.ready())
|
||||
self.assertFalse(zipline.sim.exception)
|
||||
|
||||
|
||||
# TODO:
|
||||
# - exception protocol to use prefix/payload as EXCEPT,
|
||||
# and the stack trace
|
||||
# - test exception in handle_data
|
||||
# - define more zipline failure modes: exception in other
|
||||
# components, exception in Monitor, etc. write tests
|
||||
# for those scenarios.
|
||||
|
||||
|
||||
|
||||
INITIALIZE_STACK_TB =\
|
||||
INITIALIZE_TB =\
|
||||
[{'file': '/Users/fawce/projects/qexec/zipline_repo/zipline/core/component.py',
|
||||
'line': 'self._run()',
|
||||
'lineno': 229,
|
||||
'method': 'run'},
|
||||
{'file': '/Users/fawce/projects/qexec/zipline_repo/zipline/core/component.py',
|
||||
'line': 'self.open()',
|
||||
'lineno': 208,
|
||||
'line': 'self.loop()',
|
||||
'lineno': 220,
|
||||
'method': '_run'},
|
||||
{'file': '/Users/fawce/projects/qexec/zipline_repo/zipline/core/component.py',
|
||||
'line': 'self.do_work()',
|
||||
'lineno': 257,
|
||||
'method': 'loop'},
|
||||
{'file': '/Users/fawce/projects/qexec/zipline_repo/zipline/components/tradesimulation.py',
|
||||
'line': 'self.initialize_algo()',
|
||||
'lineno': 73,
|
||||
'method': 'open'},
|
||||
'lineno': 100,
|
||||
'method': 'do_work'},
|
||||
{'file': '/Users/fawce/projects/qexec/zipline_repo/zipline/components/tradesimulation.py',
|
||||
'line': 'self.do_op(self.algorithm.initialize)',
|
||||
'lineno': 83,
|
||||
'method': 'initialize_algo'},
|
||||
{'file': '/Users/fawce/projects/qexec/zipline_repo/zipline/components/tradesimulation.py',
|
||||
'line': 'callable_op(*args, **kwargs)',
|
||||
'lineno': 205,
|
||||
'lineno': 208,
|
||||
'method': 'do_op'},
|
||||
{'file': '/Users/fawce/projects/qexec/zipline_repo/zipline/test_algorithms.py',
|
||||
'line': 'raise Exception("Algo exception in initialize")',
|
||||
'lineno': 161,
|
||||
'lineno': 162,
|
||||
'method': 'initialize'}]
|
||||
|
||||
HANDLE_DATA_TB =\
|
||||
[{'file': '/Users/fawce/projects/qexec/zipline_repo/zipline/core/component.py',
|
||||
'line': 'self._run()',
|
||||
'lineno': 229,
|
||||
'method': 'run'},
|
||||
{'file': '/Users/fawce/projects/qexec/zipline_repo/zipline/core/component.py',
|
||||
'line': 'self.loop()',
|
||||
'lineno': 220,
|
||||
'method': '_run'},
|
||||
{'file': '/Users/fawce/projects/qexec/zipline_repo/zipline/core/component.py',
|
||||
'line': 'self.do_work()',
|
||||
'lineno': 257,
|
||||
'method': 'loop'},
|
||||
{'file': '/Users/fawce/projects/qexec/zipline_repo/zipline/components/tradesimulation.py',
|
||||
'line': 'self.process_event(event)',
|
||||
'lineno': 119,
|
||||
'method': 'do_work'},
|
||||
{'file': '/Users/fawce/projects/qexec/zipline_repo/zipline/components/tradesimulation.py',
|
||||
'line': 'self.run_algorithm()',
|
||||
'lineno': 167,
|
||||
'method': 'process_event'},
|
||||
{'file': '/Users/fawce/projects/qexec/zipline_repo/zipline/components/tradesimulation.py',
|
||||
'line': 'self.do_op(self.algorithm.handle_data, data)',
|
||||
'lineno': 189,
|
||||
'method': 'run_algorithm'},
|
||||
{'file': '/Users/fawce/projects/qexec/zipline_repo/zipline/components/tradesimulation.py',
|
||||
'line': 'callable_op(*args, **kwargs)',
|
||||
'lineno': 208,
|
||||
'method': 'do_op'},
|
||||
{'file': '/Users/fawce/projects/qexec/zipline_repo/zipline/test_algorithms.py',
|
||||
'line': 'raise Exception("Algo exception in handle_data")',
|
||||
'lineno': 183,
|
||||
'method': 'handle_data'}]
|
||||
|
||||
@@ -21,8 +21,13 @@ from zipline.lines import SimulatedTrading
|
||||
from zipline.finance.performance import PerformanceTracker
|
||||
from zipline.utils.protocol_utils import ndict
|
||||
from zipline.finance.trading import TransactionSimulator, SIMULATION_STYLE
|
||||
from zipline.utils.test_utils import assert_single_position,\
|
||||
drain_zipline
|
||||
from zipline.utils.test_utils import \
|
||||
drain_zipline, \
|
||||
check, \
|
||||
setup_logger, \
|
||||
teardown_logger,\
|
||||
assert_single_position
|
||||
|
||||
|
||||
DEFAULT_TIMEOUT = 15 # seconds
|
||||
EXTENDED_TIMEOUT = 90
|
||||
@@ -42,11 +47,10 @@ class FinanceTestCase(TestCase):
|
||||
}
|
||||
self.ctx = zmq.Context()
|
||||
|
||||
self.log_handler = LoggingHandler()
|
||||
self.log_handler.push_application()
|
||||
setup_logger(self)
|
||||
|
||||
def tearDown(self):
|
||||
self.log_handler.pop_application()
|
||||
teardown_logger(self)
|
||||
|
||||
@timed(DEFAULT_TIMEOUT)
|
||||
def test_factory_daily(self):
|
||||
@@ -143,7 +147,6 @@ class FinanceTestCase(TestCase):
|
||||
zipline = SimulatedTrading.create_test_zipline(
|
||||
**self.zipline_test_config
|
||||
)
|
||||
|
||||
output, transaction_count = drain_zipline(self, zipline)
|
||||
|
||||
self.assertTrue(zipline.sim.ready())
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import gevent
|
||||
from logbook.compat import LoggingHandler
|
||||
from unittest2 import TestCase, skip
|
||||
|
||||
@@ -25,18 +24,3 @@ class TestMonitor(TestCase):
|
||||
|
||||
con = Controller(pub_socket, route_socket, )
|
||||
con.manage([ 'a', 'b', 'c', 'd' ])
|
||||
|
||||
@skip
|
||||
def test_poll(self):
|
||||
from mock_zmq import zmq_synthetic
|
||||
pub_socket = 'tcp://127.0.0.1:5000'
|
||||
route_socket = 'tcp://127.0.0.1:5001'
|
||||
cancel_socket = 'tcp://127.0.0.1:5002'
|
||||
|
||||
con = Controller(pub_socket, route_socket, cancel_socket)
|
||||
con.manage([ 'a', 'b', 'c', 'd' ])
|
||||
con.zmq = zmq_synthetic
|
||||
con.zmq_flavor = 'green'
|
||||
|
||||
con.period = 0.00001
|
||||
gevent.spawn(con.run).join(timeout=con.period)
|
||||
|
||||
Reference in New Issue
Block a user