synapse/tests/storage/test_monthly_active_users.py

182 lines
6.3 KiB
Python
Raw Normal View History

2018-08-03 12:49:53 +00:00
# -*- coding: utf-8 -*-
# Copyright 2018 New Vector
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from mock import Mock
from twisted.internet import defer
2018-07-31 15:36:24 +00:00
2018-09-03 16:21:48 +00:00
from tests.unittest import HomeserverTestCase
2018-07-31 15:36:24 +00:00
FORTY_DAYS = 40 * 24 * 60 * 60
2018-07-31 15:36:24 +00:00
2018-09-03 16:21:48 +00:00
class MonthlyActiveUsersTestCase(HomeserverTestCase):
def make_homeserver(self, reactor, clock):
hs = self.setup_test_homeserver()
self.store = hs.get_datastore()
hs.config.limit_usage_by_mau = True
hs.config.max_mau_value = 50
2018-09-03 16:21:48 +00:00
# Advance the clock a bit
reactor.advance(FORTY_DAYS)
2018-07-31 15:36:24 +00:00
2018-09-03 16:21:48 +00:00
return hs
2018-07-31 15:36:24 +00:00
def test_initialise_reserved_users(self):
2018-08-14 15:52:30 +00:00
self.hs.config.max_mau_value = 5
user1 = "@user1:server"
user1_email = "user1@matrix.org"
user2 = "@user2:server"
user2_email = "user2@matrix.org"
threepids = [
{'medium': 'email', 'address': user1_email},
2018-08-10 13:54:09 +00:00
{'medium': 'email', 'address': user2_email},
]
user_num = len(threepids)
2018-09-03 16:21:48 +00:00
self.store.register(user_id=user1, token="123", password_hash=None)
self.store.register(user_id=user2, token="456", password_hash=None)
self.pump()
now = int(self.hs.get_clock().time_msec())
2018-09-03 16:21:48 +00:00
self.store.user_add_threepid(user1, "email", user1_email, now, now)
self.store.user_add_threepid(user2, "email", user2_email, now, now)
self.store.initialise_reserved_users(threepids)
self.pump()
2018-09-03 16:21:48 +00:00
active_count = self.store.get_monthly_active_count()
# Test total counts
2018-09-03 16:21:48 +00:00
self.assertEquals(self.get_success(active_count), user_num)
# Test user is marked as active
2018-09-03 16:21:48 +00:00
timestamp = self.store.user_last_seen_monthly_active(user1)
self.assertTrue(self.get_success(timestamp))
timestamp = self.store.user_last_seen_monthly_active(user2)
self.assertTrue(self.get_success(timestamp))
# Test that users are never removed from the db.
self.hs.config.max_mau_value = 0
2018-09-03 16:21:48 +00:00
self.reactor.advance(FORTY_DAYS)
2018-09-03 16:21:48 +00:00
self.store.reap_monthly_active_users()
self.pump()
2018-09-03 16:21:48 +00:00
active_count = self.store.get_monthly_active_count()
self.assertEquals(self.get_success(active_count), user_num)
# Test that regular users are removed from the db
ru_count = 2
2018-09-03 16:21:48 +00:00
self.store.upsert_monthly_active_user("@ru1:server")
self.store.upsert_monthly_active_user("@ru2:server")
self.pump()
2018-09-03 16:21:48 +00:00
active_count = self.store.get_monthly_active_count()
self.assertEqual(self.get_success(active_count), user_num + ru_count)
self.hs.config.max_mau_value = user_num
2018-09-03 16:21:48 +00:00
self.store.reap_monthly_active_users()
self.pump()
2018-09-03 16:21:48 +00:00
active_count = self.store.get_monthly_active_count()
self.assertEquals(self.get_success(active_count), user_num)
2018-07-31 15:36:24 +00:00
def test_can_insert_and_count_mau(self):
2018-09-03 16:21:48 +00:00
count = self.store.get_monthly_active_count()
self.assertEqual(0, self.get_success(count))
2018-07-31 15:36:24 +00:00
2018-09-03 16:21:48 +00:00
self.store.upsert_monthly_active_user("@user:server")
self.pump()
2018-07-31 15:36:24 +00:00
2018-09-03 16:21:48 +00:00
count = self.store.get_monthly_active_count()
self.assertEqual(1, self.get_success(count))
2018-07-31 15:36:24 +00:00
2018-08-09 17:02:12 +00:00
def test_user_last_seen_monthly_active(self):
2018-07-31 15:36:24 +00:00
user_id1 = "@user1:server"
user_id2 = "@user2:server"
user_id3 = "@user3:server"
2018-08-09 17:02:12 +00:00
2018-09-03 16:21:48 +00:00
result = self.store.user_last_seen_monthly_active(user_id1)
self.assertFalse(self.get_success(result) == 0)
self.store.upsert_monthly_active_user(user_id1)
self.store.upsert_monthly_active_user(user_id2)
self.pump()
result = self.store.user_last_seen_monthly_active(user_id1)
self.assertGreater(self.get_success(result), 0)
result = self.store.user_last_seen_monthly_active(user_id3)
self.assertNotEqual(self.get_success(result), 0)
2018-08-03 12:49:53 +00:00
def test_reap_monthly_active_users(self):
self.hs.config.max_mau_value = 5
initial_users = 10
for i in range(initial_users):
2018-09-03 16:21:48 +00:00
self.store.upsert_monthly_active_user("@user%d:server" % i)
self.pump()
count = self.store.get_monthly_active_count()
self.assertTrue(self.get_success(count), initial_users)
self.store.reap_monthly_active_users()
self.pump()
count = self.store.get_monthly_active_count()
self.assertEquals(
self.get_success(count), initial_users - self.hs.config.max_mau_value
)
self.reactor.advance(FORTY_DAYS)
self.store.reap_monthly_active_users()
self.pump()
count = self.store.get_monthly_active_count()
self.assertEquals(self.get_success(count), 0)
def test_populate_monthly_users_is_guest(self):
# Test that guest users are not added to mau list
self.store.upsert_monthly_active_user = Mock()
self.store.populate_monthly_active_users('user_id', True)
self.pump()
self.store.upsert_monthly_active_user.assert_not_called()
def test_populate_monthly_users_should_update(self):
self.store.upsert_monthly_active_user = Mock()
self.store.is_trial_user = Mock(
return_value=defer.succeed(False)
)
self.store.user_last_seen_monthly_active = Mock(
return_value=defer.succeed(None)
)
self.store.populate_monthly_active_users('user_id')
self.pump()
self.store.upsert_monthly_active_user.assert_called_once()
def test_populate_monthly_users_should_not_update(self):
self.store.upsert_monthly_active_user = Mock()
self.store.is_trial_user = Mock(
return_value=defer.succeed(False)
)
self.store.user_last_seen_monthly_active = Mock(
return_value=defer.succeed(
self.hs.get_clock().time_msec()
)
)
self.store.populate_monthly_active_users('user_id')
self.pump()
self.store.upsert_monthly_active_user.assert_not_called()