test_presence.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from tests import unittest
  16. from mock import Mock, call
  17. from synapse.api.constants import PresenceState
  18. from synapse.handlers.presence import (
  19. handle_update, handle_timeout,
  20. IDLE_TIMER, SYNC_ONLINE_TIMEOUT, LAST_ACTIVE_GRANULARITY, FEDERATION_TIMEOUT,
  21. FEDERATION_PING_INTERVAL,
  22. )
  23. from synapse.storage.presence import UserPresenceState
  24. class PresenceUpdateTestCase(unittest.TestCase):
  25. def test_offline_to_online(self):
  26. wheel_timer = Mock()
  27. user_id = "@foo:bar"
  28. now = 5000000
  29. prev_state = UserPresenceState.default(user_id)
  30. new_state = prev_state.copy_and_replace(
  31. state=PresenceState.ONLINE,
  32. last_active_ts=now,
  33. )
  34. state, persist_and_notify, federation_ping = handle_update(
  35. prev_state, new_state, is_mine=True, wheel_timer=wheel_timer, now=now
  36. )
  37. self.assertTrue(persist_and_notify)
  38. self.assertTrue(state.currently_active)
  39. self.assertEquals(new_state.state, state.state)
  40. self.assertEquals(new_state.status_msg, state.status_msg)
  41. self.assertEquals(state.last_federation_update_ts, now)
  42. self.assertEquals(wheel_timer.insert.call_count, 3)
  43. wheel_timer.insert.assert_has_calls([
  44. call(
  45. now=now,
  46. obj=user_id,
  47. then=new_state.last_active_ts + IDLE_TIMER
  48. ),
  49. call(
  50. now=now,
  51. obj=user_id,
  52. then=new_state.last_user_sync_ts + SYNC_ONLINE_TIMEOUT
  53. ),
  54. call(
  55. now=now,
  56. obj=user_id,
  57. then=new_state.last_active_ts + LAST_ACTIVE_GRANULARITY
  58. ),
  59. ], any_order=True)
  60. def test_online_to_online(self):
  61. wheel_timer = Mock()
  62. user_id = "@foo:bar"
  63. now = 5000000
  64. prev_state = UserPresenceState.default(user_id)
  65. prev_state = prev_state.copy_and_replace(
  66. state=PresenceState.ONLINE,
  67. last_active_ts=now,
  68. currently_active=True,
  69. )
  70. new_state = prev_state.copy_and_replace(
  71. state=PresenceState.ONLINE,
  72. last_active_ts=now,
  73. )
  74. state, persist_and_notify, federation_ping = handle_update(
  75. prev_state, new_state, is_mine=True, wheel_timer=wheel_timer, now=now
  76. )
  77. self.assertFalse(persist_and_notify)
  78. self.assertTrue(federation_ping)
  79. self.assertTrue(state.currently_active)
  80. self.assertEquals(new_state.state, state.state)
  81. self.assertEquals(new_state.status_msg, state.status_msg)
  82. self.assertEquals(state.last_federation_update_ts, now)
  83. self.assertEquals(wheel_timer.insert.call_count, 3)
  84. wheel_timer.insert.assert_has_calls([
  85. call(
  86. now=now,
  87. obj=user_id,
  88. then=new_state.last_active_ts + IDLE_TIMER
  89. ),
  90. call(
  91. now=now,
  92. obj=user_id,
  93. then=new_state.last_user_sync_ts + SYNC_ONLINE_TIMEOUT
  94. ),
  95. call(
  96. now=now,
  97. obj=user_id,
  98. then=new_state.last_active_ts + LAST_ACTIVE_GRANULARITY
  99. ),
  100. ], any_order=True)
  101. def test_online_to_online_last_active_noop(self):
  102. wheel_timer = Mock()
  103. user_id = "@foo:bar"
  104. now = 5000000
  105. prev_state = UserPresenceState.default(user_id)
  106. prev_state = prev_state.copy_and_replace(
  107. state=PresenceState.ONLINE,
  108. last_active_ts=now - LAST_ACTIVE_GRANULARITY - 10,
  109. currently_active=True,
  110. )
  111. new_state = prev_state.copy_and_replace(
  112. state=PresenceState.ONLINE,
  113. last_active_ts=now,
  114. )
  115. state, persist_and_notify, federation_ping = handle_update(
  116. prev_state, new_state, is_mine=True, wheel_timer=wheel_timer, now=now
  117. )
  118. self.assertFalse(persist_and_notify)
  119. self.assertTrue(federation_ping)
  120. self.assertTrue(state.currently_active)
  121. self.assertEquals(new_state.state, state.state)
  122. self.assertEquals(new_state.status_msg, state.status_msg)
  123. self.assertEquals(state.last_federation_update_ts, now)
  124. self.assertEquals(wheel_timer.insert.call_count, 3)
  125. wheel_timer.insert.assert_has_calls([
  126. call(
  127. now=now,
  128. obj=user_id,
  129. then=new_state.last_active_ts + IDLE_TIMER
  130. ),
  131. call(
  132. now=now,
  133. obj=user_id,
  134. then=new_state.last_user_sync_ts + SYNC_ONLINE_TIMEOUT
  135. ),
  136. call(
  137. now=now,
  138. obj=user_id,
  139. then=new_state.last_active_ts + LAST_ACTIVE_GRANULARITY
  140. ),
  141. ], any_order=True)
  142. def test_online_to_online_last_active(self):
  143. wheel_timer = Mock()
  144. user_id = "@foo:bar"
  145. now = 5000000
  146. prev_state = UserPresenceState.default(user_id)
  147. prev_state = prev_state.copy_and_replace(
  148. state=PresenceState.ONLINE,
  149. last_active_ts=now - LAST_ACTIVE_GRANULARITY - 1,
  150. currently_active=True,
  151. )
  152. new_state = prev_state.copy_and_replace(
  153. state=PresenceState.ONLINE,
  154. )
  155. state, persist_and_notify, federation_ping = handle_update(
  156. prev_state, new_state, is_mine=True, wheel_timer=wheel_timer, now=now
  157. )
  158. self.assertTrue(persist_and_notify)
  159. self.assertFalse(state.currently_active)
  160. self.assertEquals(new_state.state, state.state)
  161. self.assertEquals(new_state.status_msg, state.status_msg)
  162. self.assertEquals(state.last_federation_update_ts, now)
  163. self.assertEquals(wheel_timer.insert.call_count, 2)
  164. wheel_timer.insert.assert_has_calls([
  165. call(
  166. now=now,
  167. obj=user_id,
  168. then=new_state.last_active_ts + IDLE_TIMER
  169. ),
  170. call(
  171. now=now,
  172. obj=user_id,
  173. then=new_state.last_user_sync_ts + SYNC_ONLINE_TIMEOUT
  174. )
  175. ], any_order=True)
  176. def test_remote_ping_timer(self):
  177. wheel_timer = Mock()
  178. user_id = "@foo:bar"
  179. now = 5000000
  180. prev_state = UserPresenceState.default(user_id)
  181. prev_state = prev_state.copy_and_replace(
  182. state=PresenceState.ONLINE,
  183. last_active_ts=now,
  184. )
  185. new_state = prev_state.copy_and_replace(
  186. state=PresenceState.ONLINE,
  187. )
  188. state, persist_and_notify, federation_ping = handle_update(
  189. prev_state, new_state, is_mine=False, wheel_timer=wheel_timer, now=now
  190. )
  191. self.assertFalse(persist_and_notify)
  192. self.assertFalse(federation_ping)
  193. self.assertFalse(state.currently_active)
  194. self.assertEquals(new_state.state, state.state)
  195. self.assertEquals(new_state.status_msg, state.status_msg)
  196. self.assertEquals(wheel_timer.insert.call_count, 1)
  197. wheel_timer.insert.assert_has_calls([
  198. call(
  199. now=now,
  200. obj=user_id,
  201. then=new_state.last_federation_update_ts + FEDERATION_TIMEOUT
  202. ),
  203. ], any_order=True)
  204. def test_online_to_offline(self):
  205. wheel_timer = Mock()
  206. user_id = "@foo:bar"
  207. now = 5000000
  208. prev_state = UserPresenceState.default(user_id)
  209. prev_state = prev_state.copy_and_replace(
  210. state=PresenceState.ONLINE,
  211. last_active_ts=now,
  212. currently_active=True,
  213. )
  214. new_state = prev_state.copy_and_replace(
  215. state=PresenceState.OFFLINE,
  216. )
  217. state, persist_and_notify, federation_ping = handle_update(
  218. prev_state, new_state, is_mine=True, wheel_timer=wheel_timer, now=now
  219. )
  220. self.assertTrue(persist_and_notify)
  221. self.assertEquals(new_state.state, state.state)
  222. self.assertEquals(state.last_federation_update_ts, now)
  223. self.assertEquals(wheel_timer.insert.call_count, 0)
  224. def test_online_to_idle(self):
  225. wheel_timer = Mock()
  226. user_id = "@foo:bar"
  227. now = 5000000
  228. prev_state = UserPresenceState.default(user_id)
  229. prev_state = prev_state.copy_and_replace(
  230. state=PresenceState.ONLINE,
  231. last_active_ts=now,
  232. currently_active=True,
  233. )
  234. new_state = prev_state.copy_and_replace(
  235. state=PresenceState.UNAVAILABLE,
  236. )
  237. state, persist_and_notify, federation_ping = handle_update(
  238. prev_state, new_state, is_mine=True, wheel_timer=wheel_timer, now=now
  239. )
  240. self.assertTrue(persist_and_notify)
  241. self.assertEquals(new_state.state, state.state)
  242. self.assertEquals(state.last_federation_update_ts, now)
  243. self.assertEquals(new_state.state, state.state)
  244. self.assertEquals(new_state.status_msg, state.status_msg)
  245. self.assertEquals(wheel_timer.insert.call_count, 1)
  246. wheel_timer.insert.assert_has_calls([
  247. call(
  248. now=now,
  249. obj=user_id,
  250. then=new_state.last_user_sync_ts + SYNC_ONLINE_TIMEOUT
  251. )
  252. ], any_order=True)
  253. class PresenceTimeoutTestCase(unittest.TestCase):
  254. def test_idle_timer(self):
  255. user_id = "@foo:bar"
  256. now = 5000000
  257. state = UserPresenceState.default(user_id)
  258. state = state.copy_and_replace(
  259. state=PresenceState.ONLINE,
  260. last_active_ts=now - IDLE_TIMER - 1,
  261. last_user_sync_ts=now,
  262. )
  263. new_state = handle_timeout(
  264. state, is_mine=True, syncing_user_ids=set(), now=now
  265. )
  266. self.assertIsNotNone(new_state)
  267. self.assertEquals(new_state.state, PresenceState.UNAVAILABLE)
  268. def test_sync_timeout(self):
  269. user_id = "@foo:bar"
  270. now = 5000000
  271. state = UserPresenceState.default(user_id)
  272. state = state.copy_and_replace(
  273. state=PresenceState.ONLINE,
  274. last_active_ts=0,
  275. last_user_sync_ts=now - SYNC_ONLINE_TIMEOUT - 1,
  276. )
  277. new_state = handle_timeout(
  278. state, is_mine=True, syncing_user_ids=set(), now=now
  279. )
  280. self.assertIsNotNone(new_state)
  281. self.assertEquals(new_state.state, PresenceState.OFFLINE)
  282. def test_sync_online(self):
  283. user_id = "@foo:bar"
  284. now = 5000000
  285. state = UserPresenceState.default(user_id)
  286. state = state.copy_and_replace(
  287. state=PresenceState.ONLINE,
  288. last_active_ts=now - SYNC_ONLINE_TIMEOUT - 1,
  289. last_user_sync_ts=now - SYNC_ONLINE_TIMEOUT - 1,
  290. )
  291. new_state = handle_timeout(
  292. state, is_mine=True, syncing_user_ids=set([user_id]), now=now
  293. )
  294. self.assertIsNotNone(new_state)
  295. self.assertEquals(new_state.state, PresenceState.ONLINE)
  296. def test_federation_ping(self):
  297. user_id = "@foo:bar"
  298. now = 5000000
  299. state = UserPresenceState.default(user_id)
  300. state = state.copy_and_replace(
  301. state=PresenceState.ONLINE,
  302. last_active_ts=now,
  303. last_user_sync_ts=now,
  304. last_federation_update_ts=now - FEDERATION_PING_INTERVAL - 1,
  305. )
  306. new_state = handle_timeout(
  307. state, is_mine=True, syncing_user_ids=set(), now=now
  308. )
  309. self.assertIsNotNone(new_state)
  310. self.assertEquals(new_state, new_state)
  311. def test_no_timeout(self):
  312. user_id = "@foo:bar"
  313. now = 5000000
  314. state = UserPresenceState.default(user_id)
  315. state = state.copy_and_replace(
  316. state=PresenceState.ONLINE,
  317. last_active_ts=now,
  318. last_user_sync_ts=now,
  319. last_federation_update_ts=now,
  320. )
  321. new_state = handle_timeout(
  322. state, is_mine=True, syncing_user_ids=set(), now=now
  323. )
  324. self.assertIsNone(new_state)
  325. def test_federation_timeout(self):
  326. user_id = "@foo:bar"
  327. now = 5000000
  328. state = UserPresenceState.default(user_id)
  329. state = state.copy_and_replace(
  330. state=PresenceState.ONLINE,
  331. last_active_ts=now,
  332. last_user_sync_ts=now,
  333. last_federation_update_ts=now - FEDERATION_TIMEOUT - 1,
  334. )
  335. new_state = handle_timeout(
  336. state, is_mine=False, syncing_user_ids=set(), now=now
  337. )
  338. self.assertIsNotNone(new_state)
  339. self.assertEquals(new_state.state, PresenceState.OFFLINE)
  340. def test_last_active(self):
  341. user_id = "@foo:bar"
  342. now = 5000000
  343. state = UserPresenceState.default(user_id)
  344. state = state.copy_and_replace(
  345. state=PresenceState.ONLINE,
  346. last_active_ts=now - LAST_ACTIVE_GRANULARITY - 1,
  347. last_user_sync_ts=now,
  348. last_federation_update_ts=now,
  349. )
  350. new_state = handle_timeout(
  351. state, is_mine=True, syncing_user_ids=set(), now=now
  352. )
  353. self.assertIsNotNone(new_state)
  354. self.assertEquals(state, new_state)