1
0

test_stats.py 25 KB

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