class Server(): def __init__(self): self.mode = os.environ['TXTRADER_TEST_MODE'] if 'TXTRADER_TEST_MODE' in os.environ else 'RTX' self.mode = self.mode.upper() testmode = self.mode print('Starting test txTrader %s server...' % self.mode) assert subprocess.call('ps -ax | egrep [t]wistd', shell=True) subprocess.call('truncate --size 0 test.log', shell=True) self.setup_env() self.logfile = open('test.log', 'a') self.process = subprocess.Popen(['envdir', '../etc/txtrader', 'twistd', '--nodaemon', '--logfile=-', '--python=../service/txtrader/txtrader.tac'], stdout=self.logfile) assert self.process print('%s created as pid %d' % (repr(self.process), self.process.pid)) print('Waiting for txtrader listen ports...') while subprocess.call('netstat -ant | egrep LISTEN | egrep 50090>/dev/null', shell=True): time.sleep(.25) assert not subprocess.call( 'ps -ax | egrep [t]wistd >/dev/null', shell=True) assert not subprocess.call( 'netstat -ant | egrep LISTEN | egrep 50090>/dev/null', shell=True) assert not subprocess.call( 'netstat -ant | egrep LISTEN | egrep 50070>/dev/null', shell=True) print('Test server ready.') def set_env(self, key, value): subprocess.call('echo "%s" > ../etc/txtrader/%s' % (value, key), shell=True) def setup_env(self): self.set_env('TXTRADER_LOG_API_MESSAGES', '1') def restore_env(self): self.set_env('TXTRADER_LOG_API_MESSAGES', '0') def init(self): self.api = API(self.mode) assert self.api self.api.set_account(self.api.account) return self.api def __del__(self): print() print('Stopping txTrader: Waiting for %s to terminate...' % repr(self.process.pid)) self.api.shutdown() #os.kill(self.process.pid, signal.SIGTERM) self.process.wait() print('Terminated; exit=%d' % (self.process.returncode)) self.logfile.close() self.restore_env() print('Waiting 15 seconds to avoid sqlBlocked at Realtick...') time.sleep(15) def __enter__(self): return self def __exit__(self, ex_type, ex_value, traceback): pass
def test_add_symbol(): #gw=API('rccg') #assert gw.add_symbol('USA') #assert not gw.add_symbol('fnord') gw=API('tws') accounts=gw.query_accounts() assert gw.set_account(accounts[0]) assert gw.add_symbol('TSLA') assert not gw.add_symbol('fnord')
def test_add_symbol(): #gw=API('rccg') #assert gw.add_symbol('USA') #assert not gw.add_symbol('fnord') gw = API('tws') accounts = gw.query_accounts() assert gw.set_account(accounts[0]) assert gw.add_symbol('TSLA') assert not gw.add_symbol('fnord')