def test_inbound_filter(self): # --- Borrowed from test_send_reply --- agent = StubAgent(self.bot) mod = StubReplier(self.bot) filter = StubFilter(self.bot) self.bot.add_instance('stub_agent', agent) self.bot.add_instance('stub_module', mod) self.bot.add_instance('stub_filter', filter) self.assertTrue(agent.inited) self.assertTrue(mod.inited) self.assertTrue(filter.inited) self.assertTrue(agent.eventloop.is_running()) self.assertTrue(mod.eventloop.is_running()) self.assertTrue(filter.eventloop.is_running()) foo = halibot.Message(body='foo') # --- end borrow --- self.bot.config["filters"] = { "inbound": { "stub_module": ["stub_filter"] } } agent.send_to(foo, ['stub_module']) util.waitOrTimeout(50, lambda: len(agent.received) != 0) self.assertEqual(len(agent.received), 1) self.assertEqual(agent.received[0].body, "foobar") self.assertTrue(filter.ran)
def test_drop_filter(self): agent = StubAgent(self.bot) mod = StubReplier(self.bot) filter = StubRejectFilter(self.bot) self.bot.add_instance('stub_agent', agent) self.bot.add_instance('stub_module', mod) self.bot.add_instance('stub_filter', filter) self.assertTrue(agent.inited) self.assertTrue(mod.inited) self.assertTrue(filter.inited) self.assertTrue(agent.eventloop.is_running()) self.assertTrue(mod.eventloop.is_running()) self.assertTrue(filter.eventloop.is_running()) foo = halibot.Message(body='foo') self.bot.config["filters"] = { "inbound": { "stub_module": ["stub_filter"] } } filter.accept = False agent.send_to(foo, ['stub_module']) time.sleep(1) self.assertEqual(len(agent.received), 0) filter.accept = True agent.send_to(foo, ['stub_module']) util.waitOrTimeout(50, lambda: len(agent.received) != 0) self.assertEqual(len(agent.received), 1)
def test_send_reply(self): agent = StubAgent(self.bot) mod = StubReplier(self.bot) self.bot.add_instance('stub_agent', agent) self.bot.add_instance('stub_module', mod) self.assertTrue(agent.inited) self.assertTrue(mod.inited) self.assertTrue(agent.eventloop.is_running()) foo = halibot.Message(body='foo') agent.send_to(foo, ['stub_module']) util.waitOrTimeout(100, lambda: len(agent.received) != 0) self.assertEqual(len(agent.received), 1) self.assertEqual(agent.received[0].body, "foobar")
def test_send_recv(self): agent = StubAgent(self.bot) mod = StubModule(self.bot) mod2 = StubModule(self.bot) self.bot.add_instance('stub_agent', agent) self.bot.add_instance('stub_mod', mod) self.bot.add_instance('stub_mod2', mod2) # mod should receive: foo, bar, baz, qua # mod2 should receive: foo, bar, qua foo = halibot.Message(body='foo') bar = halibot.Message(body='bar') baz = halibot.Message(body='baz', origin='glub_agent') qua = halibot.Message(body='qua') qua2 = halibot.Message(body='qua', type='mytype') agent.dispatch(foo) # 0 agent.send_to(bar, ['stub_mod/able', 'stub_mod2/baker']) # 1 agent.connect(mod) agent.dispatch(baz) # 2 agent.connect(mod2) agent.dispatch(qua) # 3 agent.dispatch(qua2) # 3 util.waitOrTimeout( 100, lambda: len(mod.received) == 4 and len(mod2.received) == 3) # Check mod received mesages self.assertEqual(4, len(mod.received)) self.assertEqual(foo.body, mod.received[0].body) self.assertEqual(bar.body, mod.received[1].body) self.assertEqual(baz.body, mod.received[2].body) self.assertEqual(qua.body, mod.received[3].body) self.assertEqual('', mod.received[0].whom()) self.assertEqual('able', mod.received[1].whom()) self.assertEqual('', mod.received[2].whom()) self.assertEqual('', mod.received[3].whom()) self.assertEqual('stub_mod', mod.received[0].target) self.assertEqual('stub_mod/able', mod.received[1].target) self.assertEqual('stub_mod', mod.received[2].target) self.assertEqual('stub_mod', mod.received[3].target) self.assertEqual('stub_agent', mod.received[0].origin) self.assertEqual('stub_agent', mod.received[1].origin) self.assertEqual('glub_agent', mod.received[2].origin) self.assertEqual('stub_agent', mod.received[3].origin) # Check mod2 received mesages self.assertEqual(3, len(mod2.received)) self.assertEqual(foo.body, mod2.received[0].body) self.assertEqual(bar.body, mod2.received[1].body) self.assertEqual(qua.body, mod2.received[2].body) self.assertEqual('', mod.received[0].whom()) self.assertEqual('able', mod.received[1].whom()) self.assertEqual('', mod.received[2].whom()) self.assertEqual('stub_mod2', mod2.received[0].target) self.assertEqual('stub_mod2/baker', mod2.received[1].target) self.assertEqual('stub_mod2', mod2.received[2].target) self.assertEqual('stub_agent', mod2.received[0].origin) self.assertEqual('stub_agent', mod2.received[1].origin) self.assertEqual('stub_agent', mod2.received[2].origin) # Check mytype messages self.assertEqual(1, len(mod.received_mytype)) self.assertEqual(1, len(mod2.received_mytype)) self.assertEqual(qua2.body, mod.received_mytype[0].body) self.assertEqual(qua2.body, mod2.received_mytype[0].body)