test_presence.py 14 KB

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