def test_typing(self): typing = self.hs.get_typing_handler() room_id = "!bar:blue" self.reconnect() typing._push_update(member=RoomMember(room_id, USER_ID), typing=True) self.reactor.advance(0) # We should now see an attempt to connect to the master request = self.handle_http_replication_attempt() self.assert_request_is_get_repl_stream_updates(request, "typing") self.test_handler.on_rdata.assert_called_once() stream_name, _, token, rdata_rows = self.test_handler.on_rdata.call_args[ 0] self.assertEqual(stream_name, "typing") self.assertEqual(1, len(rdata_rows)) row = rdata_rows[0] # type: TypingStream.TypingStreamRow self.assertEqual(room_id, row.room_id) self.assertEqual([USER_ID], row.user_ids) # Now let's disconnect and insert some data. self.disconnect() self.test_handler.on_rdata.reset_mock() typing._push_update(member=RoomMember(room_id, USER_ID), typing=False) self.test_handler.on_rdata.assert_not_called() self.reconnect() self.pump(0.1) # We should now see an attempt to connect to the master request = self.handle_http_replication_attempt() self.assert_request_is_get_repl_stream_updates(request, "typing") # The from token should be the token from the last RDATA we got. self.assertEqual(int(request.args[b"from_token"][0]), token) self.test_handler.on_rdata.assert_called_once() stream_name, _, token, rdata_rows = self.test_handler.on_rdata.call_args[ 0] self.assertEqual(stream_name, "typing") self.assertEqual(1, len(rdata_rows)) row = rdata_rows[0] self.assertEqual(room_id, row.room_id) self.assertEqual([], row.user_ids)
def test_stopped_typing(self): self.room_members = [self.u_apple, self.u_banana, self.u_onion] put_json = self.mock_http_client.put_json put_json.expect_call_and_return( call( "farm", path="/_matrix/federation/v1/send/1000000/", data=_expect_edu( "farm", "m.typing", content={ "room_id": self.room_id, "user_id": self.u_apple.to_string(), "typing": False, } ), json_data_callback=ANY, long_retries=True, ), defer.succeed((200, "OK")) ) # Gut-wrenching from synapse.handlers.typing import RoomMember member = RoomMember(self.room_id, self.u_apple.to_string()) self.handler._member_typing_until[member] = 1002000 self.handler._member_typing_timer[member] = ( self.clock.call_later(1002, lambda: 0) ) self.handler._room_typing[self.room_id] = set((self.u_apple.to_string(),)) self.assertEquals(self.event_source.get_current_key(), 0) yield self.handler.stopped_typing( target_user=self.u_apple, auth_user=self.u_apple, room_id=self.room_id, ) self.on_new_event.assert_has_calls([ call('typing_key', 1, rooms=[self.room_id]), ]) yield put_json.await_calls() self.assertEquals(self.event_source.get_current_key(), 1) events = yield self.event_source.get_new_events( room_ids=[self.room_id], from_key=0, ) self.assertEquals(events[0], [{ "type": "m.typing", "room_id": self.room_id, "content": { "user_ids": [], }, }])
def test_stopped_typing(self): self.room_members = [U_APPLE, U_BANANA, U_ONION] # Gut-wrenching from synapse.handlers.typing import RoomMember member = RoomMember(ROOM_ID, U_APPLE.to_string()) self.handler._member_typing_until[member] = 1002000 self.handler._room_typing[ROOM_ID] = {U_APPLE.to_string()} self.assertEquals(self.event_source.get_current_key(), 0) self.get_success( self.handler.stopped_typing( target_user=U_APPLE, requester=create_requester(U_APPLE), room_id=ROOM_ID, )) self.on_new_event.assert_has_calls( [call("typing_key", 1, rooms=[ROOM_ID])]) put_json = self.hs.get_federation_http_client().put_json put_json.assert_called_once_with( "farm", path="/_matrix/federation/v1/send/1000000", data=_expect_edu_transaction( "m.typing", content={ "room_id": ROOM_ID, "user_id": U_APPLE.to_string(), "typing": False, }, ), json_data_callback=ANY, long_retries=True, backoff_on_404=True, try_trailing_slash_on_400=True, ) self.assertEquals(self.event_source.get_current_key(), 1) events = self.get_success( self.event_source.get_new_events(user=U_APPLE, from_key=0, limit=None, room_ids=[ROOM_ID], is_guest=False)) self.assertEquals( events[0], [{ "type": "m.typing", "room_id": ROOM_ID, "content": { "user_ids": [] } }], )
def test_stopped_typing(self): self.room_members = [self.u_apple, self.u_banana, self.u_onion] put_json = self.mock_http_client.put_json put_json.expect_call_and_return( call( "farm", path="/_matrix/federation/v1/send/1000000/", data=_expect_edu("farm", "m.typing", content={ "room_id": self.room_id, "user_id": self.u_apple.to_string(), "typing": False, }), on_send_callback=ANY, ), defer.succeed((200, "OK"))) # Gut-wrenching from synapse.handlers.typing import RoomMember self.handler._member_typing_until[RoomMember(self.room_id, self.u_apple)] = 1002000 yield self.handler.stopped_typing( target_user=self.u_apple, auth_user=self.u_apple, room_id=self.room_id, ) self.mock_update_client.assert_has_calls([ call(observer_user=self.u_banana, observed_user=self.u_apple, room_id=self.room_id, typing=False), ]) yield put_json.await_calls()
def test_reset(self): """ Test what happens when a typing stream resets. This is emulated by jumping the stream ahead, then reconnecting (which sends the proper position and RDATA). """ typing = self.hs.get_typing_handler() self.reconnect() typing._push_update(member=RoomMember(ROOM_ID, USER_ID), typing=True) self.reactor.advance(0) # We should now see an attempt to connect to the master request = self.handle_http_replication_attempt() self.assert_request_is_get_repl_stream_updates(request, "typing") self.test_handler.on_rdata.assert_called_once() stream_name, _, token, rdata_rows = self.test_handler.on_rdata.call_args[ 0] self.assertEqual(stream_name, "typing") self.assertEqual(1, len(rdata_rows)) row: TypingStream.TypingStreamRow = rdata_rows[0] self.assertEqual(ROOM_ID, row.room_id) self.assertEqual([USER_ID], row.user_ids) # Push the stream forward a bunch so it can be reset. for i in range(100): typing._push_update(member=RoomMember(ROOM_ID, "@test%s:blue" % i), typing=True) self.reactor.advance(0) # Disconnect. self.disconnect() # Reset the typing handler self.hs.get_replication_streams()["typing"].last_token = 0 self.hs.get_tcp_replication()._streams["typing"].last_token = 0 typing._latest_room_serial = 0 typing._typing_stream_change_cache = StreamChangeCache( "TypingStreamChangeCache", typing._latest_room_serial) typing._reset() # Reconnect. self.reconnect() self.pump(0.1) # We should now see an attempt to connect to the master request = self.handle_http_replication_attempt() self.assert_request_is_get_repl_stream_updates(request, "typing") # Reset the test code. self.test_handler.on_rdata.reset_mock() self.test_handler.on_rdata.assert_not_called() # Push additional data. typing._push_update(member=RoomMember(ROOM_ID_2, USER_ID_2), typing=False) self.reactor.advance(0) self.test_handler.on_rdata.assert_called_once() stream_name, _, token, rdata_rows = self.test_handler.on_rdata.call_args[ 0] self.assertEqual(stream_name, "typing") self.assertEqual(1, len(rdata_rows)) row = rdata_rows[0] self.assertEqual(ROOM_ID_2, row.room_id) self.assertEqual([], row.user_ids) # The token should have been reset. self.assertEqual(token, 1)