anonymousland-synapse/tests/storage/test_monthly_active_users.py

132 lines
4.9 KiB
Python
Raw Normal View History

2018-08-03 08:49:53 -04: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.
2018-07-31 11:36:24 -04:00
2018-08-03 08:49:53 -04:00
from twisted.internet import defer
2018-07-31 11:36:24 -04:00
import tests.unittest
import tests.utils
from tests.utils import setup_test_homeserver
FORTY_DAYS = 40 * 24 * 60 * 60
2018-07-31 11:36:24 -04:00
class MonthlyActiveUsersTestCase(tests.unittest.TestCase):
def __init__(self, *args, **kwargs):
super(MonthlyActiveUsersTestCase, self).__init__(*args, **kwargs)
@defer.inlineCallbacks
def setUp(self):
2018-08-13 02:47:46 -04:00
self.hs = yield setup_test_homeserver(self.addCleanup)
2018-08-03 08:49:53 -04:00
self.store = self.hs.get_datastore()
2018-07-31 11:36:24 -04:00
@defer.inlineCallbacks
def test_initialise_reserved_users(self):
2018-08-14 11:52:30 -04: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 09:54:09 -04:00
{'medium': 'email', 'address': user2_email},
]
user_num = len(threepids)
2018-08-10 09:54:09 -04:00
yield self.store.register(user_id=user1, token="123", password_hash=None)
2018-08-10 09:54:09 -04:00
yield self.store.register(user_id=user2, token="456", password_hash=None)
now = int(self.hs.get_clock().time_msec())
yield self.store.user_add_threepid(user1, "email", user1_email, now, now)
yield self.store.user_add_threepid(user2, "email", user2_email, now, now)
yield self.store.initialise_reserved_users(threepids)
active_count = yield self.store.get_monthly_active_count()
# Test total counts
self.assertEquals(active_count, user_num)
# Test user is marked as active
2018-08-09 13:02:12 -04:00
timestamp = yield self.store.user_last_seen_monthly_active(user1)
self.assertTrue(timestamp)
2018-08-09 13:02:12 -04:00
timestamp = yield self.store.user_last_seen_monthly_active(user2)
self.assertTrue(timestamp)
# Test that users are never removed from the db.
self.hs.config.max_mau_value = 0
self.hs.get_clock().advance_time(FORTY_DAYS)
yield self.store.reap_monthly_active_users()
active_count = yield self.store.get_monthly_active_count()
self.assertEquals(active_count, user_num)
# Test that regalar users are removed from the db
ru_count = 2
yield self.store.upsert_monthly_active_user("@ru1:server")
yield self.store.upsert_monthly_active_user("@ru2:server")
active_count = yield self.store.get_monthly_active_count()
self.assertEqual(active_count, user_num + ru_count)
self.hs.config.max_mau_value = user_num
yield self.store.reap_monthly_active_users()
active_count = yield self.store.get_monthly_active_count()
self.assertEquals(active_count, user_num)
2018-07-31 11:36:24 -04:00
@defer.inlineCallbacks
def test_can_insert_and_count_mau(self):
2018-08-03 08:49:53 -04:00
count = yield self.store.get_monthly_active_count()
2018-07-31 11:36:24 -04:00
self.assertEqual(0, count)
2018-08-03 08:49:53 -04:00
yield self.store.upsert_monthly_active_user("@user:server")
count = yield self.store.get_monthly_active_count()
2018-07-31 11:36:24 -04:00
self.assertEqual(1, count)
@defer.inlineCallbacks
2018-08-09 13:02:12 -04:00
def test_user_last_seen_monthly_active(self):
2018-07-31 11:36:24 -04:00
user_id1 = "@user1:server"
user_id2 = "@user2:server"
user_id3 = "@user3:server"
2018-08-09 13:02:12 -04:00
result = yield self.store.user_last_seen_monthly_active(user_id1)
2018-08-03 12:55:50 -04:00
self.assertFalse(result == 0)
2018-08-03 08:49:53 -04:00
yield self.store.upsert_monthly_active_user(user_id1)
yield self.store.upsert_monthly_active_user(user_id2)
2018-08-09 13:02:12 -04:00
result = yield self.store.user_last_seen_monthly_active(user_id1)
2018-08-03 12:55:50 -04:00
self.assertTrue(result > 0)
2018-08-09 13:02:12 -04:00
result = yield self.store.user_last_seen_monthly_active(user_id3)
2018-08-03 12:55:50 -04:00
self.assertFalse(result == 0)
2018-08-03 08:49:53 -04:00
@defer.inlineCallbacks
def test_reap_monthly_active_users(self):
self.hs.config.max_mau_value = 5
initial_users = 10
for i in range(initial_users):
yield self.store.upsert_monthly_active_user("@user%d:server" % i)
count = yield self.store.get_monthly_active_count()
self.assertTrue(count, initial_users)
yield self.store.reap_monthly_active_users()
count = yield self.store.get_monthly_active_count()
self.assertEquals(count, initial_users - self.hs.config.max_mau_value)
self.hs.get_clock().advance_time(FORTY_DAYS)
yield self.store.reap_monthly_active_users()
count = yield self.store.get_monthly_active_count()
self.assertEquals(count, 0)