Example #1
0
class TestComm(unittest.TestCase):
    def setUp(self):
        self.config = {"offer_expiry_interval": 30, "ep_expiry_interval": 30}
        self.hcomm = HTTPComm(self.config)
        self.msg = {"msgid": 2, "a": "b"}
        self.httpd = TestServer("localhost", 8080)
        self.httpd.start()
        self.tcomm = ThreadedComm(self.hcomm)
        self.tcomm.add_agent(MockAgent())

    def tearDown(self):
        self.httpd.shutdown()

    def test_post_message(self):
        self.assertTrue(self.hcomm.post_message(self.msg))

    def test_poll_and_dispatch(self):
        self.hcomm.poll_and_dispatch()
        self.assertEqual(self.hcomm.lastpoll, 1)
        self.hcomm.poll_and_dispatch()
        self.assertEqual(self.hcomm.lastpoll, 1)

    def test_threadcomm(self):
        self.tcomm.start()
        time.sleep(2)
        self.hcomm.post_message(self.msg)
        self.tcomm.post_message(self.msg)
        self.tcomm.poll_and_dispatch()
        time.sleep(2)
        self.tcomm.stop()
Example #2
0
class TestComm(unittest.TestCase):
    def setUp(self):
        self.config = {"offer_expiry_interval": 30, "ep_expiry_interval": 30}
        self.hcomm = HTTPComm(self.config)
        self.msg = {"msgid": 2, "a": "b"}
        self.httpd = TestServer("localhost", 8080)
        self.httpd.start()
        self.tcomm = ThreadedComm(self.hcomm)
        self.tcomm.add_agent(MockAgent())

    def tearDown(self):
        self.httpd.shutdown()

    def test_post_message(self):
        self.assertTrue(self.hcomm.post_message(self.msg))

    def test_poll_and_dispatch(self):
        self.hcomm.poll_and_dispatch()
        self.assertEqual(self.hcomm.lastpoll, 1)
        self.hcomm.poll_and_dispatch()
        self.assertEqual(self.hcomm.lastpoll, 1)

    def test_threadcomm(self):
        self.tcomm.start()
        time.sleep(2)
        self.hcomm.post_message(self.msg)
        self.tcomm.post_message(self.msg)
        self.tcomm.poll_and_dispatch()
        time.sleep(2)
        self.tcomm.stop()
Example #3
0
class TestHTTPComm(unittest.TestCase):

  def setUp(self):
    self.config = { 'offer_expiry_interval' : 1}
    self.url = 'http://localhost:8080/messages'
    self.comm = HTTPComm(self.config, self.url)
    self.http_interface = MockHTTPInterface()
    self.comm.http_interface = self.http_interface
    self.agent = MockAgent()
    self.comm.add_agent(self.agent)

  def test_post_message_content(self):
    self.http_interface.reset()
    content = {'test':"TEST"}
    self.comm.post_message(content)
    posted = self.http_interface.post_log[0]
    content['msgid'] = posted['data']['msgid']
    self.assertEqual(content, posted['data'])

  def test_post_message_saves_msgid(self):
    self.http_interface.reset()
    self.comm.post_message({})
    posted = self.http_interface.post_log[0]
    self.assertTrue(posted['data']['msgid'] in self.comm.own_msgids)

  def test_post_message_url(self):
    self.http_interface.reset()
    self.comm.post_message({})
    posted = self.http_interface.post_log[0]
    self.assertEqual(self.url, posted['url'])

  def test_post_message_sets_msgid(self):
    self.http_interface.reset()
    self.comm.post_message({})
    posted = self.http_interface.post_log[0]
    self.assertTrue('msgid' in posted['data'])

  def test_post_message_returncode(self):
    self.http_interface.reset()
    self.http_interface.post_returncode = False
    self.assertFalse(self.comm.post_message({}))
    self.http_interface.post_returncode = True
    self.assertTrue(self.comm.post_message({}))

  def test_poll_and_dispatch(self):
    self.http_interface.reset()
    self.comm.poll_and_dispatch()
    expected = [MESSAGES[0]["content"], MESSAGES[1]["content"]]
    self.assertEqual(self.agent.dispatch_log, expected)