def setUp(self): self.hs = yield setup_test_homeserver( resource_for_federation=Mock(), http_client=None, ) self.store = self.hs.get_datastore() self.db_pool = self.hs.get_db_pool() self.message_handler = self.hs.get_handlers().message_handler self.event_injector = EventInjector(self.hs)
def setUp(self): hs = yield setup_test_homeserver( resource_for_federation=Mock(), http_client=None, ) self.store = hs.get_datastore() self.event_builder_factory = hs.get_event_builder_factory() self.event_injector = EventInjector(hs) self.handlers = hs.get_handlers() self.message_handler = self.handlers.message_handler self.u_alice = UserID.from_string("@alice:test") self.u_bob = UserID.from_string("@bob:test") self.room1 = RoomID.from_string("!abc123:test") self.room2 = RoomID.from_string("!xyx987:test")
class EventsStoreTestCase(unittest.TestCase): @defer.inlineCallbacks def setUp(self): self.hs = yield setup_test_homeserver( resource_for_federation=Mock(), http_client=None, ) self.store = self.hs.get_datastore() self.db_pool = self.hs.get_db_pool() self.message_handler = self.hs.get_handlers().message_handler self.event_injector = EventInjector(self.hs) @defer.inlineCallbacks def test_count_daily_messages(self): self.db_pool.runQuery("DELETE FROM stats_reporting") self.hs.clock.now = 100 # Never reported before, and nothing which could be reported count = yield self.store.count_daily_messages() self.assertIsNone(count) count = yield self.db_pool.runQuery("SELECT COUNT(*) FROM stats_reporting") self.assertEqual([(0,)], count) # Create something to report room = RoomID.from_string("!abc123:test") user = UserID.from_string("@raccoonlover:test") yield self.event_injector.create_room(room) self.base_event = yield self._get_last_stream_token() yield self.event_injector.inject_message(room, user, "Raccoons are really cute") # Never reported before, something could be reported, but isn't because # it isn't old enough. count = yield self.store.count_daily_messages() self.assertIsNone(count) self._assert_stats_reporting(1, self.hs.clock.now) # Already reported yesterday, two new events from today. yield self.event_injector.inject_message(room, user, "Yeah they are!") yield self.event_injector.inject_message(room, user, "Incredibly!") self.hs.clock.now += 60 * 60 * 24 count = yield self.store.count_daily_messages() self.assertEqual(2, count) # 2 since yesterday self._assert_stats_reporting(3, self.hs.clock.now) # 3 ever # Last reported too recently. yield self.event_injector.inject_message(room, user, "Who could disagree?") self.hs.clock.now += 60 * 60 * 22 count = yield self.store.count_daily_messages() self.assertIsNone(count) self._assert_stats_reporting(4, self.hs.clock.now) # Last reported too long ago yield self.event_injector.inject_message(room, user, "No one.") self.hs.clock.now += 60 * 60 * 26 count = yield self.store.count_daily_messages() self.assertIsNone(count) self._assert_stats_reporting(5, self.hs.clock.now) # And now let's actually report something yield self.event_injector.inject_message(room, user, "Indeed.") yield self.event_injector.inject_message(room, user, "Indeed.") yield self.event_injector.inject_message(room, user, "Indeed.") # A little over 24 hours is fine :) self.hs.clock.now += (60 * 60 * 24) + 50 count = yield self.store.count_daily_messages() self.assertEqual(3, count) self._assert_stats_reporting(8, self.hs.clock.now) @defer.inlineCallbacks def _get_last_stream_token(self): rows = yield self.db_pool.runQuery( "SELECT stream_ordering" " FROM events" " ORDER BY stream_ordering DESC" " LIMIT 1" ) if not rows: defer.returnValue(0) else: defer.returnValue(rows[0][0]) @defer.inlineCallbacks def _assert_stats_reporting(self, messages, time): rows = yield self.db_pool.runQuery( "SELECT reported_stream_token, reported_time FROM stats_reporting" ) self.assertEqual([(self.base_event + messages, time,)], rows)
class EventsStoreTestCase(unittest.TestCase): @defer.inlineCallbacks def setUp(self): self.hs = yield setup_test_homeserver( resource_for_federation=Mock(), http_client=None, ) self.store = self.hs.get_datastore() self.db_pool = self.hs.get_db_pool() self.message_handler = self.hs.get_handlers().message_handler self.event_injector = EventInjector(self.hs) @defer.inlineCallbacks def test_count_daily_messages(self): self.db_pool.runQuery("DELETE FROM stats_reporting") self.hs.clock.now = 100 # Never reported before, and nothing which could be reported count = yield self.store.count_daily_messages() self.assertIsNone(count) count = yield self.db_pool.runQuery( "SELECT COUNT(*) FROM stats_reporting") self.assertEqual([(0, )], count) # Create something to report room = RoomID.from_string("!abc123:test") user = UserID.from_string("@raccoonlover:test") yield self.event_injector.create_room(room) self.base_event = yield self._get_last_stream_token() yield self.event_injector.inject_message(room, user, "Raccoons are really cute") # Never reported before, something could be reported, but isn't because # it isn't old enough. count = yield self.store.count_daily_messages() self.assertIsNone(count) self._assert_stats_reporting(1, self.hs.clock.now) # Already reported yesterday, two new events from today. yield self.event_injector.inject_message(room, user, "Yeah they are!") yield self.event_injector.inject_message(room, user, "Incredibly!") self.hs.clock.now += 60 * 60 * 24 count = yield self.store.count_daily_messages() self.assertEqual(2, count) # 2 since yesterday self._assert_stats_reporting(3, self.hs.clock.now) # 3 ever # Last reported too recently. yield self.event_injector.inject_message(room, user, "Who could disagree?") self.hs.clock.now += 60 * 60 * 22 count = yield self.store.count_daily_messages() self.assertIsNone(count) self._assert_stats_reporting(4, self.hs.clock.now) # Last reported too long ago yield self.event_injector.inject_message(room, user, "No one.") self.hs.clock.now += 60 * 60 * 26 count = yield self.store.count_daily_messages() self.assertIsNone(count) self._assert_stats_reporting(5, self.hs.clock.now) # And now let's actually report something yield self.event_injector.inject_message(room, user, "Indeed.") yield self.event_injector.inject_message(room, user, "Indeed.") yield self.event_injector.inject_message(room, user, "Indeed.") # A little over 24 hours is fine :) self.hs.clock.now += (60 * 60 * 24) + 50 count = yield self.store.count_daily_messages() self.assertEqual(3, count) self._assert_stats_reporting(8, self.hs.clock.now) @defer.inlineCallbacks def _get_last_stream_token(self): rows = yield self.db_pool.runQuery("SELECT stream_ordering" " FROM events" " ORDER BY stream_ordering DESC" " LIMIT 1") if not rows: defer.returnValue(0) else: defer.returnValue(rows[0][0]) @defer.inlineCallbacks def _assert_stats_reporting(self, messages, time): rows = yield self.db_pool.runQuery( "SELECT reported_stream_token, reported_time FROM stats_reporting") self.assertEqual([( self.base_event + messages, time, )], rows)
class StreamStoreTestCase(unittest.TestCase): @defer.inlineCallbacks def setUp(self): hs = yield setup_test_homeserver( resource_for_federation=Mock(), http_client=None, ) self.store = hs.get_datastore() self.event_builder_factory = hs.get_event_builder_factory() self.event_injector = EventInjector(hs) self.handlers = hs.get_handlers() self.message_handler = self.handlers.message_handler self.u_alice = UserID.from_string("@alice:test") self.u_bob = UserID.from_string("@bob:test") self.room1 = RoomID.from_string("!abc123:test") self.room2 = RoomID.from_string("!xyx987:test") @defer.inlineCallbacks def test_event_stream_get_other(self): # Both bob and alice joins the room yield self.event_injector.inject_room_member( self.room1, self.u_alice, Membership.JOIN ) yield self.event_injector.inject_room_member( self.room1, self.u_bob, Membership.JOIN ) # Initial stream key: start = yield self.store.get_room_events_max_id() yield self.event_injector.inject_message(self.room1, self.u_alice, u"test") end = yield self.store.get_room_events_max_id() results, _ = yield self.store.get_room_events_stream( self.u_bob.to_string(), start, end, ) self.assertEqual(1, len(results)) event = results[0] self.assertObjectHasAttributes( { "type": EventTypes.Message, "user_id": self.u_alice.to_string(), "content": {"body": "test", "msgtype": "message"}, }, event, ) @defer.inlineCallbacks def test_event_stream_get_own(self): # Both bob and alice joins the room yield self.event_injector.inject_room_member( self.room1, self.u_alice, Membership.JOIN ) yield self.event_injector.inject_room_member( self.room1, self.u_bob, Membership.JOIN ) # Initial stream key: start = yield self.store.get_room_events_max_id() yield self.event_injector.inject_message(self.room1, self.u_alice, u"test") end = yield self.store.get_room_events_max_id() results, _ = yield self.store.get_room_events_stream( self.u_alice.to_string(), start, end, ) self.assertEqual(1, len(results)) event = results[0] self.assertObjectHasAttributes( { "type": EventTypes.Message, "user_id": self.u_alice.to_string(), "content": {"body": "test", "msgtype": "message"}, }, event, ) @defer.inlineCallbacks def test_event_stream_join_leave(self): # Both bob and alice joins the room yield self.event_injector.inject_room_member( self.room1, self.u_alice, Membership.JOIN ) yield self.event_injector.inject_room_member( self.room1, self.u_bob, Membership.JOIN ) # Then bob leaves again. yield self.event_injector.inject_room_member( self.room1, self.u_bob, Membership.LEAVE ) # Initial stream key: start = yield self.store.get_room_events_max_id() yield self.event_injector.inject_message(self.room1, self.u_alice, u"test") end = yield self.store.get_room_events_max_id() results, _ = yield self.store.get_room_events_stream( self.u_bob.to_string(), start, end, ) # We should not get the message, as it happened *after* bob left. self.assertEqual(0, len(results)) @defer.inlineCallbacks def test_event_stream_prev_content(self): yield self.event_injector.inject_room_member( self.room1, self.u_bob, Membership.JOIN ) event1 = yield self.event_injector.inject_room_member( self.room1, self.u_alice, Membership.JOIN ) start = yield self.store.get_room_events_max_id() event2 = yield self.event_injector.inject_room_member( self.room1, self.u_alice, Membership.JOIN, ) end = yield self.store.get_room_events_max_id() results, _ = yield self.store.get_room_events_stream( self.u_bob.to_string(), start, end, ) # We should not get the message, as it happened *after* bob left. self.assertEqual(1, len(results)) event = results[0] self.assertTrue( "prev_content" in event.unsigned, msg="No prev_content key" )
class StreamStoreTestCase(unittest.TestCase): @defer.inlineCallbacks def setUp(self): hs = yield setup_test_homeserver( resource_for_federation=Mock(), http_client=None, ) self.store = hs.get_datastore() self.event_builder_factory = hs.get_event_builder_factory() self.event_injector = EventInjector(hs) self.handlers = hs.get_handlers() self.message_handler = self.handlers.message_handler self.u_alice = UserID.from_string("@alice:test") self.u_bob = UserID.from_string("@bob:test") self.room1 = RoomID.from_string("!abc123:test") self.room2 = RoomID.from_string("!xyx987:test") @defer.inlineCallbacks def test_event_stream_get_other(self): # Both bob and alice joins the room yield self.event_injector.inject_room_member( self.room1, self.u_alice, Membership.JOIN ) yield self.event_injector.inject_room_member( self.room1, self.u_bob, Membership.JOIN ) # Initial stream key: start = yield self.store.get_room_events_max_id() yield self.event_injector.inject_message(self.room1, self.u_alice, u"test") end = yield self.store.get_room_events_max_id() results, _ = yield self.store.get_room_events_stream( self.u_bob.to_string(), start, end, ) self.assertEqual(1, len(results)) event = results[0] self.assertObjectHasAttributes( { "type": EventTypes.Message, "user_id": self.u_alice.to_string(), "content": {"body": "test", "msgtype": "message"}, }, event, ) @defer.inlineCallbacks def test_event_stream_get_own(self): # Both bob and alice joins the room yield self.event_injector.inject_room_member( self.room1, self.u_alice, Membership.JOIN ) yield self.event_injector.inject_room_member( self.room1, self.u_bob, Membership.JOIN ) # Initial stream key: start = yield self.store.get_room_events_max_id() yield self.event_injector.inject_message(self.room1, self.u_alice, u"test") end = yield self.store.get_room_events_max_id() results, _ = yield self.store.get_room_events_stream( self.u_alice.to_string(), start, end, ) self.assertEqual(1, len(results)) event = results[0] self.assertObjectHasAttributes( { "type": EventTypes.Message, "user_id": self.u_alice.to_string(), "content": {"body": "test", "msgtype": "message"}, }, event, ) @defer.inlineCallbacks def test_event_stream_join_leave(self): # Both bob and alice joins the room yield self.event_injector.inject_room_member( self.room1, self.u_alice, Membership.JOIN ) yield self.event_injector.inject_room_member( self.room1, self.u_bob, Membership.JOIN ) # Then bob leaves again. yield self.event_injector.inject_room_member( self.room1, self.u_bob, Membership.LEAVE ) # Initial stream key: start = yield self.store.get_room_events_max_id() yield self.event_injector.inject_message(self.room1, self.u_alice, u"test") end = yield self.store.get_room_events_max_id() results, _ = yield self.store.get_room_events_stream( self.u_bob.to_string(), start, end, ) # We should not get the message, as it happened *after* bob left. self.assertEqual(0, len(results)) @defer.inlineCallbacks def test_event_stream_prev_content(self): yield self.event_injector.inject_room_member( self.room1, self.u_bob, Membership.JOIN ) yield self.event_injector.inject_room_member( self.room1, self.u_alice, Membership.JOIN ) start = yield self.store.get_room_events_max_id() yield self.event_injector.inject_room_member( self.room1, self.u_alice, Membership.JOIN, ) end = yield self.store.get_room_events_max_id() results, _ = yield self.store.get_room_events_stream( self.u_bob.to_string(), start, end, ) # We should not get the message, as it happened *after* bob left. self.assertEqual(1, len(results)) event = results[0] self.assertTrue( "prev_content" in event.unsigned, msg="No prev_content key" )