<commit_before>"""
Test suite for the messaging infrastructure of QSim.
"""
#don't worry about excessive public methods pylint: disable=R0904  

import unittest2 as unittest
import multiprocessing
import time

from zipline.core import ThreadSimulator, ProcessSimulator, DataFeed
from zipline.transforms.technical import MovingAverage
from zipline.sources import RandomEquityTrades
import zipline.util as qutil
import zipline.messaging as qmsg

from zipline.test.client import TestClient


class MessagingTestCase(unittest.TestCase):  
    """Tests the message passing: datasources -> feed -> transforms -> merge -> client"""

    def setUp(self):
        """generate some config objects for the datafeed, sources, and transforms."""
        qutil.configure_logging() 

    def get_simulator(self, sources, transforms, client, feed=None, merge=None):
        return ProcessSimulator(sources, transforms, client, feed=feed, merge=merge)

    def test_sources_only(self):
        """streams events from two data sources, no transforms."""

        ret1 = RandomEquityTrades(133, "ret1", 400)
        ret2 = RandomEquityTrades(134, "ret2", 400)
        sources = {"ret1":ret1, "ret2":ret2}
        client = TestClient(self, expected_msg_count=800)
        sim = self.get_simulator(sources, {}, client)
        sim.simulate()
              
        self.assertEqual(sim.feed.data_buffer.pending_messages(), 0, 
                        "The feed should be drained of all messages, found {n} remaining."
                            .format(n=sim.feed.data_buffer.pending_messages()))
    
    
    def test_merged_to_client(self):
        """
        2 datasources -> feed -> 2 moving average transforms -> transform merge -> testclient
        verify message count at client.
        """
        
        ret1 = RandomEquityTrades(133, "ret1", 400)
        ret2 = RandomEquityTrades(134, "ret2", 400)
        sources = {"ret1":ret1, "ret2":ret2}
        mavg1 = MovingAverage("mavg1", 30)
        mavg2 = MovingAverage("mavg2", 60)
        transforms = {"mavg1":mavg1, "mavg2":mavg2}
        client = TestClient(self, expected_msg_count=800)
        sim = self.get_simulator(sources, transforms, client)
        sim.simulate()
        
        
        self.assertEqual(sim.feed.data_buffer.pending_messages(), 0, "The feed should be drained of all messages.")
        
    def dtest_error_in_feed(self):
        ret1 = RandomEquityTrades(133, "ret1", 400)
        ret2 = RandomEquityTrades(134, "ret2", 400)
        sources = {"ret1":ret1, "ret2":ret2}
        mavg1 = MovingAverage("mavg1", 30)
        mavg2 = MovingAverage("mavg2", 60)
        transforms = {"mavg1":mavg1, "mavg2":mavg2}
        client = TestClient(self, expected_msg_count=0)
        sim = self.get_simulator(sources, transforms, client)
        sim.feed = DataFeedErr(sources.keys(), sim.data_address, sim.feed_address, qmsg.Sync(sim, "DataFeedErrorGenerator"))<commit_msg>re-enabled error test case<commit_after>"""
Test suite for the messaging infrastructure of QSim.
"""
#don't worry about excessive public methods pylint: disable=R0904  

import unittest2 as unittest
import multiprocessing
import time

from zipline.core import ThreadSimulator, ProcessSimulator, DataFeed
from zipline.transforms.technical import MovingAverage
from zipline.sources import RandomEquityTrades
import zipline.util as qutil
import zipline.messaging as qmsg

from zipline.test.client import TestClient


class MessagingTestCase(unittest.TestCase):  
    """Tests the message passing: datasources -> feed -> transforms -> merge -> client"""

    def setUp(self):
        """generate some config objects for the datafeed, sources, and transforms."""
        qutil.configure_logging() 

    def get_simulator(self, sources, transforms, client, feed=None, merge=None):
        return ProcessSimulator(sources, transforms, client, feed=feed, merge=merge)

    def test_sources_only(self):
        """streams events from two data sources, no transforms."""

        ret1 = RandomEquityTrades(133, "ret1", 400)
        ret2 = RandomEquityTrades(134, "ret2", 400)
        sources = {"ret1":ret1, "ret2":ret2}
        client = TestClient(self, expected_msg_count=800)
        sim = self.get_simulator(sources, {}, client)
        sim.simulate()
              
        self.assertEqual(sim.feed.data_buffer.pending_messages(), 0, 
                        "The feed should be drained of all messages, found {n} remaining."
                            .format(n=sim.feed.data_buffer.pending_messages()))
    
    
    def test_merged_to_client(self):
        """
        2 datasources -> feed -> 2 moving average transforms -> transform merge -> testclient
        verify message count at client.
        """
        
        ret1 = RandomEquityTrades(133, "ret1", 400)
        ret2 = RandomEquityTrades(134, "ret2", 400)
        sources = {"ret1":ret1, "ret2":ret2}
        mavg1 = MovingAverage("mavg1", 30)
        mavg2 = MovingAverage("mavg2", 60)
        transforms = {"mavg1":mavg1, "mavg2":mavg2}
        client = TestClient(self, expected_msg_count=800)
        sim = self.get_simulator(sources, transforms, client)
        sim.simulate()
        
        
        self.assertEqual(sim.feed.data_buffer.pending_messages(), 0, "The feed should be drained of all messages.")
        
    def test_error_in_feed(self):
        ret1 = RandomEquityTrades(133, "ret1", 400)
        ret2 = RandomEquityTrades(134, "ret2", 400)
        sources = {"ret1":ret1, "ret2":ret2}
        mavg1 = MovingAverage("mavg1", 30)
        mavg2 = MovingAverage("mavg2", 60)
        transforms = {"mavg1":mavg1, "mavg2":mavg2}
        client = TestClient(self, expected_msg_count=0)
        sim = self.get_simulator(sources, transforms, client)
        sim.feed = DataFeedErr(sources.keys(), sim.data_address, sim.feed_address, qmsg.Sync(sim, "DataFeedErrorGenerator"))
{'fraction_non_alphanumeric': 0.10913102511880515, 'fraction_numerical': 0.03394433129667346, 'mean_word_length': 3.0613370089593386, 'pattern_counts': {'":': 20, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5192637', 'n_tokens_mistral': 1907, 'n_tokens_neox': 1716, 'n_words': 442}