123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- # -*- coding: utf-8 -*-
- # Copyright 2016 OpenMarket Ltd
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- from mock import Mock, call
- from synapse.api.constants import PresenceState
- from synapse.handlers.presence import (
- FEDERATION_PING_INTERVAL,
- FEDERATION_TIMEOUT,
- IDLE_TIMER,
- LAST_ACTIVE_GRANULARITY,
- SYNC_ONLINE_TIMEOUT,
- handle_timeout,
- handle_update,
- )
- from synapse.storage.presence import UserPresenceState
- from tests import unittest
- class PresenceUpdateTestCase(unittest.TestCase):
- def test_offline_to_online(self):
- wheel_timer = Mock()
- user_id = "@foo:bar"
- now = 5000000
- prev_state = UserPresenceState.default(user_id)
- new_state = prev_state.copy_and_replace(
- state=PresenceState.ONLINE,
- last_active_ts=now,
- )
- state, persist_and_notify, federation_ping = handle_update(
- prev_state, new_state, is_mine=True, wheel_timer=wheel_timer, now=now
- )
- self.assertTrue(persist_and_notify)
- self.assertTrue(state.currently_active)
- self.assertEquals(new_state.state, state.state)
- self.assertEquals(new_state.status_msg, state.status_msg)
- self.assertEquals(state.last_federation_update_ts, now)
- self.assertEquals(wheel_timer.insert.call_count, 3)
- wheel_timer.insert.assert_has_calls([
- call(
- now=now,
- obj=user_id,
- then=new_state.last_active_ts + IDLE_TIMER
- ),
- call(
- now=now,
- obj=user_id,
- then=new_state.last_user_sync_ts + SYNC_ONLINE_TIMEOUT
- ),
- call(
- now=now,
- obj=user_id,
- then=new_state.last_active_ts + LAST_ACTIVE_GRANULARITY
- ),
- ], any_order=True)
- def test_online_to_online(self):
- wheel_timer = Mock()
- user_id = "@foo:bar"
- now = 5000000
- prev_state = UserPresenceState.default(user_id)
- prev_state = prev_state.copy_and_replace(
- state=PresenceState.ONLINE,
- last_active_ts=now,
- currently_active=True,
- )
- new_state = prev_state.copy_and_replace(
- state=PresenceState.ONLINE,
- last_active_ts=now,
- )
- state, persist_and_notify, federation_ping = handle_update(
- prev_state, new_state, is_mine=True, wheel_timer=wheel_timer, now=now
- )
- self.assertFalse(persist_and_notify)
- self.assertTrue(federation_ping)
- self.assertTrue(state.currently_active)
- self.assertEquals(new_state.state, state.state)
- self.assertEquals(new_state.status_msg, state.status_msg)
- self.assertEquals(state.last_federation_update_ts, now)
- self.assertEquals(wheel_timer.insert.call_count, 3)
- wheel_timer.insert.assert_has_calls([
- call(
- now=now,
- obj=user_id,
- then=new_state.last_active_ts + IDLE_TIMER
- ),
- call(
- now=now,
- obj=user_id,
- then=new_state.last_user_sync_ts + SYNC_ONLINE_TIMEOUT
- ),
- call(
- now=now,
- obj=user_id,
- then=new_state.last_active_ts + LAST_ACTIVE_GRANULARITY
- ),
- ], any_order=True)
- def test_online_to_online_last_active_noop(self):
- wheel_timer = Mock()
- user_id = "@foo:bar"
- now = 5000000
- prev_state = UserPresenceState.default(user_id)
- prev_state = prev_state.copy_and_replace(
- state=PresenceState.ONLINE,
- last_active_ts=now - LAST_ACTIVE_GRANULARITY - 10,
- currently_active=True,
- )
- new_state = prev_state.copy_and_replace(
- state=PresenceState.ONLINE,
- last_active_ts=now,
- )
- state, persist_and_notify, federation_ping = handle_update(
- prev_state, new_state, is_mine=True, wheel_timer=wheel_timer, now=now
- )
- self.assertFalse(persist_and_notify)
- self.assertTrue(federation_ping)
- self.assertTrue(state.currently_active)
- self.assertEquals(new_state.state, state.state)
- self.assertEquals(new_state.status_msg, state.status_msg)
- self.assertEquals(state.last_federation_update_ts, now)
- self.assertEquals(wheel_timer.insert.call_count, 3)
- wheel_timer.insert.assert_has_calls([
- call(
- now=now,
- obj=user_id,
- then=new_state.last_active_ts + IDLE_TIMER
- ),
- call(
- now=now,
- obj=user_id,
- then=new_state.last_user_sync_ts + SYNC_ONLINE_TIMEOUT
- ),
- call(
- now=now,
- obj=user_id,
- then=new_state.last_active_ts + LAST_ACTIVE_GRANULARITY
- ),
- ], any_order=True)
- def test_online_to_online_last_active(self):
- wheel_timer = Mock()
- user_id = "@foo:bar"
- now = 5000000
- prev_state = UserPresenceState.default(user_id)
- prev_state = prev_state.copy_and_replace(
- state=PresenceState.ONLINE,
- last_active_ts=now - LAST_ACTIVE_GRANULARITY - 1,
- currently_active=True,
- )
- new_state = prev_state.copy_and_replace(
- state=PresenceState.ONLINE,
- )
- state, persist_and_notify, federation_ping = handle_update(
- prev_state, new_state, is_mine=True, wheel_timer=wheel_timer, now=now
- )
- self.assertTrue(persist_and_notify)
- self.assertFalse(state.currently_active)
- self.assertEquals(new_state.state, state.state)
- self.assertEquals(new_state.status_msg, state.status_msg)
- self.assertEquals(state.last_federation_update_ts, now)
- self.assertEquals(wheel_timer.insert.call_count, 2)
- wheel_timer.insert.assert_has_calls([
- call(
- now=now,
- obj=user_id,
- then=new_state.last_active_ts + IDLE_TIMER
- ),
- call(
- now=now,
- obj=user_id,
- then=new_state.last_user_sync_ts + SYNC_ONLINE_TIMEOUT
- )
- ], any_order=True)
- def test_remote_ping_timer(self):
- wheel_timer = Mock()
- user_id = "@foo:bar"
- now = 5000000
- prev_state = UserPresenceState.default(user_id)
- prev_state = prev_state.copy_and_replace(
- state=PresenceState.ONLINE,
- last_active_ts=now,
- )
- new_state = prev_state.copy_and_replace(
- state=PresenceState.ONLINE,
- )
- state, persist_and_notify, federation_ping = handle_update(
- prev_state, new_state, is_mine=False, wheel_timer=wheel_timer, now=now
- )
- self.assertFalse(persist_and_notify)
- self.assertFalse(federation_ping)
- self.assertFalse(state.currently_active)
- self.assertEquals(new_state.state, state.state)
- self.assertEquals(new_state.status_msg, state.status_msg)
- self.assertEquals(wheel_timer.insert.call_count, 1)
- wheel_timer.insert.assert_has_calls([
- call(
- now=now,
- obj=user_id,
- then=new_state.last_federation_update_ts + FEDERATION_TIMEOUT
- ),
- ], any_order=True)
- def test_online_to_offline(self):
- wheel_timer = Mock()
- user_id = "@foo:bar"
- now = 5000000
- prev_state = UserPresenceState.default(user_id)
- prev_state = prev_state.copy_and_replace(
- state=PresenceState.ONLINE,
- last_active_ts=now,
- currently_active=True,
- )
- new_state = prev_state.copy_and_replace(
- state=PresenceState.OFFLINE,
- )
- state, persist_and_notify, federation_ping = handle_update(
- prev_state, new_state, is_mine=True, wheel_timer=wheel_timer, now=now
- )
- self.assertTrue(persist_and_notify)
- self.assertEquals(new_state.state, state.state)
- self.assertEquals(state.last_federation_update_ts, now)
- self.assertEquals(wheel_timer.insert.call_count, 0)
- def test_online_to_idle(self):
- wheel_timer = Mock()
- user_id = "@foo:bar"
- now = 5000000
- prev_state = UserPresenceState.default(user_id)
- prev_state = prev_state.copy_and_replace(
- state=PresenceState.ONLINE,
- last_active_ts=now,
- currently_active=True,
- )
- new_state = prev_state.copy_and_replace(
- state=PresenceState.UNAVAILABLE,
- )
- state, persist_and_notify, federation_ping = handle_update(
- prev_state, new_state, is_mine=True, wheel_timer=wheel_timer, now=now
- )
- self.assertTrue(persist_and_notify)
- self.assertEquals(new_state.state, state.state)
- self.assertEquals(state.last_federation_update_ts, now)
- self.assertEquals(new_state.state, state.state)
- self.assertEquals(new_state.status_msg, state.status_msg)
- self.assertEquals(wheel_timer.insert.call_count, 1)
- wheel_timer.insert.assert_has_calls([
- call(
- now=now,
- obj=user_id,
- then=new_state.last_user_sync_ts + SYNC_ONLINE_TIMEOUT
- )
- ], any_order=True)
- class PresenceTimeoutTestCase(unittest.TestCase):
- def test_idle_timer(self):
- user_id = "@foo:bar"
- now = 5000000
- state = UserPresenceState.default(user_id)
- state = state.copy_and_replace(
- state=PresenceState.ONLINE,
- last_active_ts=now - IDLE_TIMER - 1,
- last_user_sync_ts=now,
- )
- new_state = handle_timeout(
- state, is_mine=True, syncing_user_ids=set(), now=now
- )
- self.assertIsNotNone(new_state)
- self.assertEquals(new_state.state, PresenceState.UNAVAILABLE)
- def test_sync_timeout(self):
- user_id = "@foo:bar"
- now = 5000000
- state = UserPresenceState.default(user_id)
- state = state.copy_and_replace(
- state=PresenceState.ONLINE,
- last_active_ts=0,
- last_user_sync_ts=now - SYNC_ONLINE_TIMEOUT - 1,
- )
- new_state = handle_timeout(
- state, is_mine=True, syncing_user_ids=set(), now=now
- )
- self.assertIsNotNone(new_state)
- self.assertEquals(new_state.state, PresenceState.OFFLINE)
- def test_sync_online(self):
- user_id = "@foo:bar"
- now = 5000000
- state = UserPresenceState.default(user_id)
- state = state.copy_and_replace(
- state=PresenceState.ONLINE,
- last_active_ts=now - SYNC_ONLINE_TIMEOUT - 1,
- last_user_sync_ts=now - SYNC_ONLINE_TIMEOUT - 1,
- )
- new_state = handle_timeout(
- state, is_mine=True, syncing_user_ids=set([user_id]), now=now
- )
- self.assertIsNotNone(new_state)
- self.assertEquals(new_state.state, PresenceState.ONLINE)
- def test_federation_ping(self):
- user_id = "@foo:bar"
- now = 5000000
- state = UserPresenceState.default(user_id)
- state = state.copy_and_replace(
- state=PresenceState.ONLINE,
- last_active_ts=now,
- last_user_sync_ts=now,
- last_federation_update_ts=now - FEDERATION_PING_INTERVAL - 1,
- )
- new_state = handle_timeout(
- state, is_mine=True, syncing_user_ids=set(), now=now
- )
- self.assertIsNotNone(new_state)
- self.assertEquals(new_state, new_state)
- def test_no_timeout(self):
- user_id = "@foo:bar"
- now = 5000000
- state = UserPresenceState.default(user_id)
- state = state.copy_and_replace(
- state=PresenceState.ONLINE,
- last_active_ts=now,
- last_user_sync_ts=now,
- last_federation_update_ts=now,
- )
- new_state = handle_timeout(
- state, is_mine=True, syncing_user_ids=set(), now=now
- )
- self.assertIsNone(new_state)
- def test_federation_timeout(self):
- user_id = "@foo:bar"
- now = 5000000
- state = UserPresenceState.default(user_id)
- state = state.copy_and_replace(
- state=PresenceState.ONLINE,
- last_active_ts=now,
- last_user_sync_ts=now,
- last_federation_update_ts=now - FEDERATION_TIMEOUT - 1,
- )
- new_state = handle_timeout(
- state, is_mine=False, syncing_user_ids=set(), now=now
- )
- self.assertIsNotNone(new_state)
- self.assertEquals(new_state.state, PresenceState.OFFLINE)
- def test_last_active(self):
- user_id = "@foo:bar"
- now = 5000000
- state = UserPresenceState.default(user_id)
- state = state.copy_and_replace(
- state=PresenceState.ONLINE,
- last_active_ts=now - LAST_ACTIVE_GRANULARITY - 1,
- last_user_sync_ts=now,
- last_federation_update_ts=now,
- )
- new_state = handle_timeout(
- state, is_mine=True, syncing_user_ids=set(), now=now
- )
- self.assertIsNotNone(new_state)
- self.assertEquals(state, new_state)
|