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

from collections import defaultdict
import zipline.messaging as qmsg

from zipline.transforms.technical import MovingAverage
from zipline.sources import RandomEquityTrades

from zipline.test.client import TestClient

# Should not inherit form TestCase since test runners will pick
# it up as a test. Its a Mixin of sorts at this point.
class SimulatorTestCase(object):

    leased_sockets = defaultdict(list)

    def setUp(self):
        self.setup_logging()

        # TODO: how to make Nose use this cross-process????
        self.setup_allocator()

    def tearDown(self):
        pass
        #self.unallocate_sockets()

        # Assert the sockets were properly cleaned up
        #self.assertEmpty(self.leased_sockets[self.id()].values())

        # Assert they were returned to the heap
        #self.allocator.socketheap.assert

    def get_simulator(self):
        """
        Return a new simulator instance to be tested.
        """
        raise NotImplementedError

    def get_controller(self):
        """
        Return a new controler for simulator instance to be tested.
        """
        raise NotImplementedError

    def setup_allocator(self):
        """
        Setup the socket allocator for this test case.
        """
        raise NotImplementedError

    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):
        self.allocator.reaquire(*self.leased_sockets[self.id()])

    # -------
    #  Cases
    # -------

    def test_simple(self):

        # Simple test just to make sure that the archiecture is
        # responding.

        # Base Simuation
        # --------------

        # Allocate sockets for the simulator components
        sockets = self.allocate_sockets(5)

        addresses = {
            'sync_address'   : sockets[0],
            'data_address'   : sockets[1],
            'feed_address'   : sockets[2],
            'merge_address'  : sockets[3],
            'result_address' : sockets[4]
        }

        sim = self.get_simulator(addresses)
        con = self.get_controller()

        # Simulation Components
        # ---------------------

        ret1 = RandomEquityTrades(133, "ret1", 1)
        ret2 = RandomEquityTrades(134, "ret2", 1)
        client = TestClient(self, expected_msg_count=ret1.count + ret2.count)

        sim.register_controller( con )
        sim.register_components([ret1, ret2, client])

        assert False

        # Simulation
        # ----------
        sim.simulate()

        # Stop Running
        # ------------

        # TODO: less abrupt later, just shove a StopIteration
        # down the pipe to make it stop spinning
        sim.cuc._Thread__stop()

        self.assertEqual(sim.feed.pending_messages(), 0,
            "The feed should be drained of all messages, found {n} remaining."
            .format(n=sim.feed.pending_messages())
        )


    def test_sources_only(self):

        # Base Simuation
        # --------------

        # Allocate sockets for the simulator components<commit_msg>Woops, left pdb assert in.<commit_after>"""
Test suite for the messaging infrastructure of QSim.
"""
#don't worry about excessive public methods pylint: disable=R0904

from collections import defaultdict
import zipline.messaging as qmsg

from zipline.transforms.technical import MovingAverage
from zipline.sources import RandomEquityTrades

from zipline.test.client import TestClient

# Should not inherit form TestCase since test runners will pick
# it up as a test. Its a Mixin of sorts at this point.
class SimulatorTestCase(object):

    leased_sockets = defaultdict(list)

    def setUp(self):
        self.setup_logging()

        # TODO: how to make Nose use this cross-process????
        self.setup_allocator()

    def tearDown(self):
        pass
        #self.unallocate_sockets()

        # Assert the sockets were properly cleaned up
        #self.assertEmpty(self.leased_sockets[self.id()].values())

        # Assert they were returned to the heap
        #self.allocator.socketheap.assert

    def get_simulator(self):
        """
        Return a new simulator instance to be tested.
        """
        raise NotImplementedError

    def get_controller(self):
        """
        Return a new controler for simulator instance to be tested.
        """
        raise NotImplementedError

    def setup_allocator(self):
        """
        Setup the socket allocator for this test case.
        """
        raise NotImplementedError

    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):
        self.allocator.reaquire(*self.leased_sockets[self.id()])

    # -------
    #  Cases
    # -------

    def test_simple(self):

        # Simple test just to make sure that the archiecture is
        # responding.

        # Base Simuation
        # --------------

        # Allocate sockets for the simulator components
        sockets = self.allocate_sockets(5)

        addresses = {
            'sync_address'   : sockets[0],
            'data_address'   : sockets[1],
            'feed_address'   : sockets[2],
            'merge_address'  : sockets[3],
            'result_address' : sockets[4]
        }

        sim = self.get_simulator(addresses)
        con = self.get_controller()

        # Simulation Components
        # ---------------------

        ret1 = RandomEquityTrades(133, "ret1", 1)
        ret2 = RandomEquityTrades(134, "ret2", 1)
        client = TestClient(self, expected_msg_count=ret1.count + ret2.count)

        sim.register_controller( con )
        sim.register_components([ret1, ret2, client])

        # Simulation
        # ----------
        sim.simulate()

        # Stop Running
        # ------------

        # TODO: less abrupt later, just shove a StopIteration
        # down the pipe to make it stop spinning
        sim.cuc._Thread__stop()

        self.assertEqual(sim.feed.pending_messages(), 0,
            "The feed should be drained of all messages, found {n} remaining."
            .format(n=sim.feed.pending_messages())
        )


    def test_sources_only(self):

        # Base Simuation
        # --------------

        # Allocate sockets for the simulator components
{'fraction_non_alphanumeric': 0.105826225242157, 'fraction_numerical': 0.008095995373716929, 'mean_word_length': 2.0302233902759528, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2622573', 'n_tokens_mistral': 2000, 'n_tokens_neox': 1852, 'n_words': 545}