Ejemplo n.º 1
0
class TestConnector(TestCase):
    def setUp(self):
        self.server = MockServer()

    def tearDown(self):
        self.server.shutdown()
        del self.server

    def test_iterlines(self):
        thread = Thread(target=self.server.random_send)
        thread.start()
        connector = Connector(('localhost', self.server.port))
        data = open('tests/s2c.log')
        for line in connector.iterlines():
            eq_(line, data.next()[:-1])
        assert_raises(StopIteration, data.next)
        connector.stop_and_wait()

        thread.join()

    def test_send(self):
        messages = ("the first test message\n",
                    "the second message is very l" + "o" * 10000 + "ng\n"
                   )
        message = "".join(messages)
        thread = Thread(target=self.server.recieve_all)
        thread.start()
        connector = Connector(('localhost', self.server.port))

        for m in messages:
            connector.send(m)

        connector.stop_and_wait()
        thread.join()

        eq_(message, self.server.recieved)
Ejemplo n.º 2
0
class TestClientCore(TestCase):
    def setUp(self):
        self.server = MockServer()
    
    def tearDown(self):
        self.server.shutdown()
        del self.server

    def test_set_handler(self):
        thread = Thread(target=self.server.random_send)
        thread.start()
        core = ClientCore(('localhost', self.server.port))

        def c_generator():
            for line in open('tests/s2c.log'):
                if line[0] == 'c':
                    yield line[:-1]
        c_generator = c_generator()

        def c_handler(c_message):
            eq_(c_generator.next(), c_message)

        def lab_generator():
            for line in open('tests/s2c.log'):
                if line[0] in 'lab':
                    yield line[:-1]
        lab_generator = lab_generator()

        def lab_handler(lab_message):
            eq_(lab_generator.next(), lab_message)

        def R_handler(R_message):
            raise Exception("Unreachable code")

        core.set_handler('c', c_handler)
        core.set_handler('lab', lab_handler)
        core.set_handler('R', R_handler)
        core.remove_handler('R')
        core.run()
        core.stop_and_wait()

        thread.join()

        assert_raises(StopIteration, c_generator.next)
        assert_raises(StopIteration, lab_generator.next)


    def test_send(self):
        messages = ("the first test message\n",
                    "the second message is very l" + "o" * 10000 + "ng\n"
                   )
        message = "".join(messages)
        thread = Thread(target=self.server.recieve_all)
        thread.start()
        core = ClientCore(('localhost', self.server.port))

        for m in messages:
            core.send(m)

        core.stop()
        thread.join()

        eq_(message, self.server.recieved)