test_stats.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 New Vector 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 synapse.rest import admin
  16. from synapse.rest.client.v1 import login, room
  17. from synapse.storage.data_stores.main import stats
  18. from tests import unittest
  19. # The expected number of state events in a fresh public room.
  20. EXPT_NUM_STATE_EVTS_IN_FRESH_PUBLIC_ROOM = 5
  21. # The expected number of state events in a fresh private room.
  22. EXPT_NUM_STATE_EVTS_IN_FRESH_PRIVATE_ROOM = 6
  23. class StatsRoomTests(unittest.HomeserverTestCase):
  24. servlets = [
  25. admin.register_servlets_for_client_rest_resource,
  26. room.register_servlets,
  27. login.register_servlets,
  28. ]
  29. def prepare(self, reactor, clock, hs):
  30. self.store = hs.get_datastore()
  31. self.handler = self.hs.get_stats_handler()
  32. def _add_background_updates(self):
  33. """
  34. Add the background updates we need to run.
  35. """
  36. # Ugh, have to reset this flag
  37. self.store._all_done = False
  38. self.get_success(
  39. self.store._simple_insert(
  40. "background_updates",
  41. {"update_name": "populate_stats_prepare", "progress_json": "{}"},
  42. )
  43. )
  44. self.get_success(
  45. self.store._simple_insert(
  46. "background_updates",
  47. {
  48. "update_name": "populate_stats_process_rooms",
  49. "progress_json": "{}",
  50. "depends_on": "populate_stats_prepare",
  51. },
  52. )
  53. )
  54. self.get_success(
  55. self.store._simple_insert(
  56. "background_updates",
  57. {
  58. "update_name": "populate_stats_process_users",
  59. "progress_json": "{}",
  60. "depends_on": "populate_stats_process_rooms",
  61. },
  62. )
  63. )
  64. self.get_success(
  65. self.store._simple_insert(
  66. "background_updates",
  67. {
  68. "update_name": "populate_stats_cleanup",
  69. "progress_json": "{}",
  70. "depends_on": "populate_stats_process_users",
  71. },
  72. )
  73. )
  74. def get_all_room_state(self):
  75. return self.store._simple_select_list(
  76. "room_stats_state", None, retcols=("name", "topic", "canonical_alias")
  77. )
  78. def _get_current_stats(self, stats_type, stat_id):
  79. table, id_col = stats.TYPE_TO_TABLE[stats_type]
  80. cols = list(stats.ABSOLUTE_STATS_FIELDS[stats_type]) + list(
  81. stats.PER_SLICE_FIELDS[stats_type]
  82. )
  83. end_ts = self.store.quantise_stats_time(self.reactor.seconds() * 1000)
  84. return self.get_success(
  85. self.store._simple_select_one(
  86. table + "_historical",
  87. {id_col: stat_id, end_ts: end_ts},
  88. cols,
  89. allow_none=True,
  90. )
  91. )
  92. def _perform_background_initial_update(self):
  93. # Do the initial population of the stats via the background update
  94. self._add_background_updates()
  95. while not self.get_success(self.store.has_completed_background_updates()):
  96. self.get_success(self.store.do_next_background_update(100), by=0.1)
  97. def test_initial_room(self):
  98. """
  99. The background updates will build the table from scratch.
  100. """
  101. r = self.get_success(self.get_all_room_state())
  102. self.assertEqual(len(r), 0)
  103. # Disable stats
  104. self.hs.config.stats_enabled = False
  105. self.handler.stats_enabled = False
  106. u1 = self.register_user("u1", "pass")
  107. u1_token = self.login("u1", "pass")
  108. room_1 = self.helper.create_room_as(u1, tok=u1_token)
  109. self.helper.send_state(
  110. room_1, event_type="m.room.topic", body={"topic": "foo"}, tok=u1_token
  111. )
  112. # Stats disabled, shouldn't have done anything
  113. r = self.get_success(self.get_all_room_state())
  114. self.assertEqual(len(r), 0)
  115. # Enable stats
  116. self.hs.config.stats_enabled = True
  117. self.handler.stats_enabled = True
  118. # Do the initial population of the user directory via the background update
  119. self._add_background_updates()
  120. while not self.get_success(self.store.has_completed_background_updates()):
  121. self.get_success(self.store.do_next_background_update(100), by=0.1)
  122. r = self.get_success(self.get_all_room_state())
  123. self.assertEqual(len(r), 1)
  124. self.assertEqual(r[0]["topic"], "foo")
  125. def test_initial_earliest_token(self):
  126. """
  127. Ingestion via notify_new_event will ignore tokens that the background
  128. update have already processed.
  129. """
  130. self.reactor.advance(86401)
  131. self.hs.config.stats_enabled = False
  132. self.handler.stats_enabled = False
  133. u1 = self.register_user("u1", "pass")
  134. u1_token = self.login("u1", "pass")
  135. u2 = self.register_user("u2", "pass")
  136. u2_token = self.login("u2", "pass")
  137. u3 = self.register_user("u3", "pass")
  138. u3_token = self.login("u3", "pass")
  139. room_1 = self.helper.create_room_as(u1, tok=u1_token)
  140. self.helper.send_state(
  141. room_1, event_type="m.room.topic", body={"topic": "foo"}, tok=u1_token
  142. )
  143. # Begin the ingestion by creating the temp tables. This will also store
  144. # the position that the deltas should begin at, once they take over.
  145. self.hs.config.stats_enabled = True
  146. self.handler.stats_enabled = True
  147. self.store._all_done = False
  148. self.get_success(
  149. self.store._simple_update_one(
  150. table="stats_incremental_position",
  151. keyvalues={},
  152. updatevalues={"stream_id": 0},
  153. )
  154. )
  155. self.get_success(
  156. self.store._simple_insert(
  157. "background_updates",
  158. {"update_name": "populate_stats_prepare", "progress_json": "{}"},
  159. )
  160. )
  161. while not self.get_success(self.store.has_completed_background_updates()):
  162. self.get_success(self.store.do_next_background_update(100), by=0.1)
  163. # Now, before the table is actually ingested, add some more events.
  164. self.helper.invite(room=room_1, src=u1, targ=u2, tok=u1_token)
  165. self.helper.join(room=room_1, user=u2, tok=u2_token)
  166. # orig_delta_processor = self.store.
  167. # Now do the initial ingestion.
  168. self.get_success(
  169. self.store._simple_insert(
  170. "background_updates",
  171. {"update_name": "populate_stats_process_rooms", "progress_json": "{}"},
  172. )
  173. )
  174. self.get_success(
  175. self.store._simple_insert(
  176. "background_updates",
  177. {
  178. "update_name": "populate_stats_cleanup",
  179. "progress_json": "{}",
  180. "depends_on": "populate_stats_process_rooms",
  181. },
  182. )
  183. )
  184. self.store._all_done = False
  185. while not self.get_success(self.store.has_completed_background_updates()):
  186. self.get_success(self.store.do_next_background_update(100), by=0.1)
  187. self.reactor.advance(86401)
  188. # Now add some more events, triggering ingestion. Because of the stream
  189. # position being set to before the events sent in the middle, a simpler
  190. # implementation would reprocess those events, and say there were four
  191. # users, not three.
  192. self.helper.invite(room=room_1, src=u1, targ=u3, tok=u1_token)
  193. self.helper.join(room=room_1, user=u3, tok=u3_token)
  194. # self.handler.notify_new_event()
  195. # We need to let the delta processor advance…
  196. self.pump(10 * 60)
  197. # Get the slices! There should be two -- day 1, and day 2.
  198. r = self.get_success(self.store.get_statistics_for_subject("room", room_1, 0))
  199. self.assertEqual(len(r), 2)
  200. # The oldest has 2 joined members
  201. self.assertEqual(r[-1]["joined_members"], 2)
  202. # The newest has 3
  203. self.assertEqual(r[0]["joined_members"], 3)
  204. def test_create_user(self):
  205. """
  206. When we create a user, it should have statistics already ready.
  207. """
  208. u1 = self.register_user("u1", "pass")
  209. u1stats = self._get_current_stats("user", u1)
  210. self.assertIsNotNone(u1stats)
  211. # not in any rooms by default
  212. self.assertEqual(u1stats["joined_rooms"], 0)
  213. def test_create_room(self):
  214. """
  215. When we create a room, it should have statistics already ready.
  216. """
  217. self._perform_background_initial_update()
  218. u1 = self.register_user("u1", "pass")
  219. u1token = self.login("u1", "pass")
  220. r1 = self.helper.create_room_as(u1, tok=u1token)
  221. r1stats = self._get_current_stats("room", r1)
  222. r2 = self.helper.create_room_as(u1, tok=u1token, is_public=False)
  223. r2stats = self._get_current_stats("room", r2)
  224. self.assertIsNotNone(r1stats)
  225. self.assertIsNotNone(r2stats)
  226. # contains the default things you'd expect in a fresh room
  227. self.assertEqual(
  228. r1stats["total_events"],
  229. EXPT_NUM_STATE_EVTS_IN_FRESH_PUBLIC_ROOM,
  230. "Wrong number of total_events in new room's stats!"
  231. " You may need to update this if more state events are added to"
  232. " the room creation process.",
  233. )
  234. self.assertEqual(
  235. r2stats["total_events"],
  236. EXPT_NUM_STATE_EVTS_IN_FRESH_PRIVATE_ROOM,
  237. "Wrong number of total_events in new room's stats!"
  238. " You may need to update this if more state events are added to"
  239. " the room creation process.",
  240. )
  241. self.assertEqual(
  242. r1stats["current_state_events"], EXPT_NUM_STATE_EVTS_IN_FRESH_PUBLIC_ROOM
  243. )
  244. self.assertEqual(
  245. r2stats["current_state_events"], EXPT_NUM_STATE_EVTS_IN_FRESH_PRIVATE_ROOM
  246. )
  247. self.assertEqual(r1stats["joined_members"], 1)
  248. self.assertEqual(r1stats["invited_members"], 0)
  249. self.assertEqual(r1stats["banned_members"], 0)
  250. self.assertEqual(r2stats["joined_members"], 1)
  251. self.assertEqual(r2stats["invited_members"], 0)
  252. self.assertEqual(r2stats["banned_members"], 0)
  253. def test_send_message_increments_total_events(self):
  254. """
  255. When we send a message, it increments total_events.
  256. """
  257. self._perform_background_initial_update()
  258. u1 = self.register_user("u1", "pass")
  259. u1token = self.login("u1", "pass")
  260. r1 = self.helper.create_room_as(u1, tok=u1token)
  261. r1stats_ante = self._get_current_stats("room", r1)
  262. self.helper.send(r1, "hiss", tok=u1token)
  263. r1stats_post = self._get_current_stats("room", r1)
  264. self.assertEqual(r1stats_post["total_events"] - r1stats_ante["total_events"], 1)
  265. def test_send_state_event_nonoverwriting(self):
  266. """
  267. When we send a non-overwriting state event, it increments total_events AND current_state_events
  268. """
  269. self._perform_background_initial_update()
  270. u1 = self.register_user("u1", "pass")
  271. u1token = self.login("u1", "pass")
  272. r1 = self.helper.create_room_as(u1, tok=u1token)
  273. self.helper.send_state(
  274. r1, "cat.hissing", {"value": True}, tok=u1token, state_key="tabby"
  275. )
  276. r1stats_ante = self._get_current_stats("room", r1)
  277. self.helper.send_state(
  278. r1, "cat.hissing", {"value": False}, tok=u1token, state_key="moggy"
  279. )
  280. r1stats_post = self._get_current_stats("room", r1)
  281. self.assertEqual(r1stats_post["total_events"] - r1stats_ante["total_events"], 1)
  282. self.assertEqual(
  283. r1stats_post["current_state_events"] - r1stats_ante["current_state_events"],
  284. 1,
  285. )
  286. def test_send_state_event_overwriting(self):
  287. """
  288. When we send an overwriting state event, it increments total_events ONLY
  289. """
  290. self._perform_background_initial_update()
  291. u1 = self.register_user("u1", "pass")
  292. u1token = self.login("u1", "pass")
  293. r1 = self.helper.create_room_as(u1, tok=u1token)
  294. self.helper.send_state(
  295. r1, "cat.hissing", {"value": True}, tok=u1token, state_key="tabby"
  296. )
  297. r1stats_ante = self._get_current_stats("room", r1)
  298. self.helper.send_state(
  299. r1, "cat.hissing", {"value": False}, tok=u1token, state_key="tabby"
  300. )
  301. r1stats_post = self._get_current_stats("room", r1)
  302. self.assertEqual(r1stats_post["total_events"] - r1stats_ante["total_events"], 1)
  303. self.assertEqual(
  304. r1stats_post["current_state_events"] - r1stats_ante["current_state_events"],
  305. 0,
  306. )
  307. def test_join_first_time(self):
  308. """
  309. When a user joins a room for the first time, total_events, current_state_events and
  310. joined_members should increase by exactly 1.
  311. """
  312. self._perform_background_initial_update()
  313. u1 = self.register_user("u1", "pass")
  314. u1token = self.login("u1", "pass")
  315. r1 = self.helper.create_room_as(u1, tok=u1token)
  316. u2 = self.register_user("u2", "pass")
  317. u2token = self.login("u2", "pass")
  318. r1stats_ante = self._get_current_stats("room", r1)
  319. self.helper.join(r1, u2, tok=u2token)
  320. r1stats_post = self._get_current_stats("room", r1)
  321. self.assertEqual(r1stats_post["total_events"] - r1stats_ante["total_events"], 1)
  322. self.assertEqual(
  323. r1stats_post["current_state_events"] - r1stats_ante["current_state_events"],
  324. 1,
  325. )
  326. self.assertEqual(
  327. r1stats_post["joined_members"] - r1stats_ante["joined_members"], 1
  328. )
  329. def test_join_after_leave(self):
  330. """
  331. When a user joins a room after being previously left, total_events and
  332. joined_members should increase by exactly 1.
  333. current_state_events should not increase.
  334. left_members should decrease by exactly 1.
  335. """
  336. self._perform_background_initial_update()
  337. u1 = self.register_user("u1", "pass")
  338. u1token = self.login("u1", "pass")
  339. r1 = self.helper.create_room_as(u1, tok=u1token)
  340. u2 = self.register_user("u2", "pass")
  341. u2token = self.login("u2", "pass")
  342. self.helper.join(r1, u2, tok=u2token)
  343. self.helper.leave(r1, u2, tok=u2token)
  344. r1stats_ante = self._get_current_stats("room", r1)
  345. self.helper.join(r1, u2, tok=u2token)
  346. r1stats_post = self._get_current_stats("room", r1)
  347. self.assertEqual(r1stats_post["total_events"] - r1stats_ante["total_events"], 1)
  348. self.assertEqual(
  349. r1stats_post["current_state_events"] - r1stats_ante["current_state_events"],
  350. 0,
  351. )
  352. self.assertEqual(
  353. r1stats_post["joined_members"] - r1stats_ante["joined_members"], +1
  354. )
  355. self.assertEqual(
  356. r1stats_post["left_members"] - r1stats_ante["left_members"], -1
  357. )
  358. def test_invited(self):
  359. """
  360. When a user invites another user, current_state_events, total_events and
  361. invited_members should increase by exactly 1.
  362. """
  363. self._perform_background_initial_update()
  364. u1 = self.register_user("u1", "pass")
  365. u1token = self.login("u1", "pass")
  366. r1 = self.helper.create_room_as(u1, tok=u1token)
  367. u2 = self.register_user("u2", "pass")
  368. r1stats_ante = self._get_current_stats("room", r1)
  369. self.helper.invite(r1, u1, u2, tok=u1token)
  370. r1stats_post = self._get_current_stats("room", r1)
  371. self.assertEqual(r1stats_post["total_events"] - r1stats_ante["total_events"], 1)
  372. self.assertEqual(
  373. r1stats_post["current_state_events"] - r1stats_ante["current_state_events"],
  374. 1,
  375. )
  376. self.assertEqual(
  377. r1stats_post["invited_members"] - r1stats_ante["invited_members"], +1
  378. )
  379. def test_join_after_invite(self):
  380. """
  381. When a user joins a room after being invited, total_events and
  382. joined_members should increase by exactly 1.
  383. current_state_events should not increase.
  384. invited_members should decrease by exactly 1.
  385. """
  386. self._perform_background_initial_update()
  387. u1 = self.register_user("u1", "pass")
  388. u1token = self.login("u1", "pass")
  389. r1 = self.helper.create_room_as(u1, tok=u1token)
  390. u2 = self.register_user("u2", "pass")
  391. u2token = self.login("u2", "pass")
  392. self.helper.invite(r1, u1, u2, tok=u1token)
  393. r1stats_ante = self._get_current_stats("room", r1)
  394. self.helper.join(r1, u2, tok=u2token)
  395. r1stats_post = self._get_current_stats("room", r1)
  396. self.assertEqual(r1stats_post["total_events"] - r1stats_ante["total_events"], 1)
  397. self.assertEqual(
  398. r1stats_post["current_state_events"] - r1stats_ante["current_state_events"],
  399. 0,
  400. )
  401. self.assertEqual(
  402. r1stats_post["joined_members"] - r1stats_ante["joined_members"], +1
  403. )
  404. self.assertEqual(
  405. r1stats_post["invited_members"] - r1stats_ante["invited_members"], -1
  406. )
  407. def test_left(self):
  408. """
  409. When a user leaves a room after joining, total_events and
  410. left_members should increase by exactly 1.
  411. current_state_events should not increase.
  412. joined_members should decrease by exactly 1.
  413. """
  414. self._perform_background_initial_update()
  415. u1 = self.register_user("u1", "pass")
  416. u1token = self.login("u1", "pass")
  417. r1 = self.helper.create_room_as(u1, tok=u1token)
  418. u2 = self.register_user("u2", "pass")
  419. u2token = self.login("u2", "pass")
  420. self.helper.join(r1, u2, tok=u2token)
  421. r1stats_ante = self._get_current_stats("room", r1)
  422. self.helper.leave(r1, u2, tok=u2token)
  423. r1stats_post = self._get_current_stats("room", r1)
  424. self.assertEqual(r1stats_post["total_events"] - r1stats_ante["total_events"], 1)
  425. self.assertEqual(
  426. r1stats_post["current_state_events"] - r1stats_ante["current_state_events"],
  427. 0,
  428. )
  429. self.assertEqual(
  430. r1stats_post["left_members"] - r1stats_ante["left_members"], +1
  431. )
  432. self.assertEqual(
  433. r1stats_post["joined_members"] - r1stats_ante["joined_members"], -1
  434. )
  435. def test_banned(self):
  436. """
  437. When a user is banned from a room after joining, total_events and
  438. left_members should increase by exactly 1.
  439. current_state_events should not increase.
  440. banned_members should decrease by exactly 1.
  441. """
  442. self._perform_background_initial_update()
  443. u1 = self.register_user("u1", "pass")
  444. u1token = self.login("u1", "pass")
  445. r1 = self.helper.create_room_as(u1, tok=u1token)
  446. u2 = self.register_user("u2", "pass")
  447. u2token = self.login("u2", "pass")
  448. self.helper.join(r1, u2, tok=u2token)
  449. r1stats_ante = self._get_current_stats("room", r1)
  450. self.helper.change_membership(r1, u1, u2, "ban", tok=u1token)
  451. r1stats_post = self._get_current_stats("room", r1)
  452. self.assertEqual(r1stats_post["total_events"] - r1stats_ante["total_events"], 1)
  453. self.assertEqual(
  454. r1stats_post["current_state_events"] - r1stats_ante["current_state_events"],
  455. 0,
  456. )
  457. self.assertEqual(
  458. r1stats_post["banned_members"] - r1stats_ante["banned_members"], +1
  459. )
  460. self.assertEqual(
  461. r1stats_post["joined_members"] - r1stats_ante["joined_members"], -1
  462. )
  463. def test_initial_background_update(self):
  464. """
  465. Test that statistics can be generated by the initial background update
  466. handler.
  467. This test also tests that stats rows are not created for new subjects
  468. when stats are disabled. However, it may be desirable to change this
  469. behaviour eventually to still keep current rows.
  470. """
  471. self.hs.config.stats_enabled = False
  472. u1 = self.register_user("u1", "pass")
  473. u1token = self.login("u1", "pass")
  474. r1 = self.helper.create_room_as(u1, tok=u1token)
  475. # test that these subjects, which were created during a time of disabled
  476. # stats, do not have stats.
  477. self.assertIsNone(self._get_current_stats("room", r1))
  478. self.assertIsNone(self._get_current_stats("user", u1))
  479. self.hs.config.stats_enabled = True
  480. self._perform_background_initial_update()
  481. r1stats = self._get_current_stats("room", r1)
  482. u1stats = self._get_current_stats("user", u1)
  483. self.assertEqual(r1stats["joined_members"], 1)
  484. self.assertEqual(
  485. r1stats["current_state_events"], EXPT_NUM_STATE_EVTS_IN_FRESH_PUBLIC_ROOM
  486. )
  487. self.assertEqual(u1stats["joined_rooms"], 1)
  488. def test_incomplete_stats(self):
  489. """
  490. This tests that we track incomplete statistics.
  491. We first test that incomplete stats are incrementally generated,
  492. following the preparation of a background regen.
  493. We then test that these incomplete rows are completed by the background
  494. regen.
  495. """
  496. u1 = self.register_user("u1", "pass")
  497. u1token = self.login("u1", "pass")
  498. u2 = self.register_user("u2", "pass")
  499. u2token = self.login("u2", "pass")
  500. u3 = self.register_user("u3", "pass")
  501. r1 = self.helper.create_room_as(u1, tok=u1token, is_public=False)
  502. # preparation stage of the initial background update
  503. # Ugh, have to reset this flag
  504. self.store._all_done = False
  505. self.get_success(
  506. self.store._simple_delete(
  507. "room_stats_current", {"1": 1}, "test_delete_stats"
  508. )
  509. )
  510. self.get_success(
  511. self.store._simple_delete(
  512. "user_stats_current", {"1": 1}, "test_delete_stats"
  513. )
  514. )
  515. self.helper.invite(r1, u1, u2, tok=u1token)
  516. self.helper.join(r1, u2, tok=u2token)
  517. self.helper.invite(r1, u1, u3, tok=u1token)
  518. self.helper.send(r1, "thou shalt yield", tok=u1token)
  519. # now do the background updates
  520. self.store._all_done = False
  521. self.get_success(
  522. self.store._simple_insert(
  523. "background_updates",
  524. {
  525. "update_name": "populate_stats_process_rooms",
  526. "progress_json": "{}",
  527. "depends_on": "populate_stats_prepare",
  528. },
  529. )
  530. )
  531. self.get_success(
  532. self.store._simple_insert(
  533. "background_updates",
  534. {
  535. "update_name": "populate_stats_process_users",
  536. "progress_json": "{}",
  537. "depends_on": "populate_stats_process_rooms",
  538. },
  539. )
  540. )
  541. self.get_success(
  542. self.store._simple_insert(
  543. "background_updates",
  544. {
  545. "update_name": "populate_stats_cleanup",
  546. "progress_json": "{}",
  547. "depends_on": "populate_stats_process_users",
  548. },
  549. )
  550. )
  551. while not self.get_success(self.store.has_completed_background_updates()):
  552. self.get_success(self.store.do_next_background_update(100), by=0.1)
  553. r1stats_complete = self._get_current_stats("room", r1)
  554. u1stats_complete = self._get_current_stats("user", u1)
  555. u2stats_complete = self._get_current_stats("user", u2)
  556. # now we make our assertions
  557. # check that _complete rows are complete and correct
  558. self.assertEqual(r1stats_complete["joined_members"], 2)
  559. self.assertEqual(r1stats_complete["invited_members"], 1)
  560. self.assertEqual(
  561. r1stats_complete["current_state_events"],
  562. 2 + EXPT_NUM_STATE_EVTS_IN_FRESH_PRIVATE_ROOM,
  563. )
  564. self.assertEqual(u1stats_complete["joined_rooms"], 1)
  565. self.assertEqual(u2stats_complete["joined_rooms"], 1)