test_monthly_active_users.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 New Vector
  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 twisted.internet import defer
  16. import tests.unittest
  17. import tests.utils
  18. from tests.utils import setup_test_homeserver
  19. FORTY_DAYS = 40 * 24 * 60 * 60
  20. class MonthlyActiveUsersTestCase(tests.unittest.TestCase):
  21. def __init__(self, *args, **kwargs):
  22. super(MonthlyActiveUsersTestCase, self).__init__(*args, **kwargs)
  23. @defer.inlineCallbacks
  24. def setUp(self):
  25. self.hs = yield setup_test_homeserver(self.addCleanup)
  26. self.store = self.hs.get_datastore()
  27. @defer.inlineCallbacks
  28. def test_initialise_reserved_users(self):
  29. self.hs.config.max_mau_value = 5
  30. user1 = "@user1:server"
  31. user1_email = "user1@matrix.org"
  32. user2 = "@user2:server"
  33. user2_email = "user2@matrix.org"
  34. threepids = [
  35. {'medium': 'email', 'address': user1_email},
  36. {'medium': 'email', 'address': user2_email},
  37. ]
  38. user_num = len(threepids)
  39. yield self.store.register(user_id=user1, token="123", password_hash=None)
  40. yield self.store.register(user_id=user2, token="456", password_hash=None)
  41. now = int(self.hs.get_clock().time_msec())
  42. yield self.store.user_add_threepid(user1, "email", user1_email, now, now)
  43. yield self.store.user_add_threepid(user2, "email", user2_email, now, now)
  44. yield self.store.initialise_reserved_users(threepids)
  45. active_count = yield self.store.get_monthly_active_count()
  46. # Test total counts
  47. self.assertEquals(active_count, user_num)
  48. # Test user is marked as active
  49. timestamp = yield self.store.user_last_seen_monthly_active(user1)
  50. self.assertTrue(timestamp)
  51. timestamp = yield self.store.user_last_seen_monthly_active(user2)
  52. self.assertTrue(timestamp)
  53. # Test that users are never removed from the db.
  54. self.hs.config.max_mau_value = 0
  55. self.hs.get_clock().advance_time(FORTY_DAYS)
  56. yield self.store.reap_monthly_active_users()
  57. active_count = yield self.store.get_monthly_active_count()
  58. self.assertEquals(active_count, user_num)
  59. @defer.inlineCallbacks
  60. def test_can_insert_and_count_mau(self):
  61. count = yield self.store.get_monthly_active_count()
  62. self.assertEqual(0, count)
  63. yield self.store.upsert_monthly_active_user("@user:server")
  64. count = yield self.store.get_monthly_active_count()
  65. self.assertEqual(1, count)
  66. @defer.inlineCallbacks
  67. def test_user_last_seen_monthly_active(self):
  68. user_id1 = "@user1:server"
  69. user_id2 = "@user2:server"
  70. user_id3 = "@user3:server"
  71. result = yield self.store.user_last_seen_monthly_active(user_id1)
  72. self.assertFalse(result == 0)
  73. yield self.store.upsert_monthly_active_user(user_id1)
  74. yield self.store.upsert_monthly_active_user(user_id2)
  75. result = yield self.store.user_last_seen_monthly_active(user_id1)
  76. self.assertTrue(result > 0)
  77. result = yield self.store.user_last_seen_monthly_active(user_id3)
  78. self.assertFalse(result == 0)
  79. @defer.inlineCallbacks
  80. def test_reap_monthly_active_users(self):
  81. self.hs.config.max_mau_value = 5
  82. initial_users = 10
  83. for i in range(initial_users):
  84. yield self.store.upsert_monthly_active_user("@user%d:server" % i)
  85. count = yield self.store.get_monthly_active_count()
  86. self.assertTrue(count, initial_users)
  87. yield self.store.reap_monthly_active_users()
  88. count = yield self.store.get_monthly_active_count()
  89. self.assertEquals(count, initial_users - self.hs.config.max_mau_value)
  90. self.hs.get_clock().advance_time(FORTY_DAYS)
  91. yield self.store.reap_monthly_active_users()
  92. count = yield self.store.get_monthly_active_count()
  93. self.assertEquals(count, 0)