test_events.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket 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 mock import Mock
  16. from synapse.types import RoomID, UserID
  17. from tests import unittest
  18. from twisted.internet import defer
  19. from tests.storage.event_injector import EventInjector
  20. from tests.utils import setup_test_homeserver
  21. class EventsStoreTestCase(unittest.TestCase):
  22. @defer.inlineCallbacks
  23. def setUp(self):
  24. self.hs = yield setup_test_homeserver(
  25. resource_for_federation=Mock(),
  26. http_client=None,
  27. )
  28. self.store = self.hs.get_datastore()
  29. self.db_pool = self.hs.get_db_pool()
  30. self.message_handler = self.hs.get_handlers().message_handler
  31. self.event_injector = EventInjector(self.hs)
  32. @defer.inlineCallbacks
  33. def test_count_daily_messages(self):
  34. self.db_pool.runQuery("DELETE FROM stats_reporting")
  35. self.hs.clock.now = 100
  36. # Never reported before, and nothing which could be reported
  37. count = yield self.store.count_daily_messages()
  38. self.assertIsNone(count)
  39. count = yield self.db_pool.runQuery("SELECT COUNT(*) FROM stats_reporting")
  40. self.assertEqual([(0,)], count)
  41. # Create something to report
  42. room = RoomID.from_string("!abc123:test")
  43. user = UserID.from_string("@raccoonlover:test")
  44. yield self.event_injector.create_room(room)
  45. self.base_event = yield self._get_last_stream_token()
  46. yield self.event_injector.inject_message(room, user, "Raccoons are really cute")
  47. # Never reported before, something could be reported, but isn't because
  48. # it isn't old enough.
  49. count = yield self.store.count_daily_messages()
  50. self.assertIsNone(count)
  51. self._assert_stats_reporting(1, self.hs.clock.now)
  52. # Already reported yesterday, two new events from today.
  53. yield self.event_injector.inject_message(room, user, "Yeah they are!")
  54. yield self.event_injector.inject_message(room, user, "Incredibly!")
  55. self.hs.clock.now += 60 * 60 * 24
  56. count = yield self.store.count_daily_messages()
  57. self.assertEqual(2, count) # 2 since yesterday
  58. self._assert_stats_reporting(3, self.hs.clock.now) # 3 ever
  59. # Last reported too recently.
  60. yield self.event_injector.inject_message(room, user, "Who could disagree?")
  61. self.hs.clock.now += 60 * 60 * 22
  62. count = yield self.store.count_daily_messages()
  63. self.assertIsNone(count)
  64. self._assert_stats_reporting(4, self.hs.clock.now)
  65. # Last reported too long ago
  66. yield self.event_injector.inject_message(room, user, "No one.")
  67. self.hs.clock.now += 60 * 60 * 26
  68. count = yield self.store.count_daily_messages()
  69. self.assertIsNone(count)
  70. self._assert_stats_reporting(5, self.hs.clock.now)
  71. # And now let's actually report something
  72. yield self.event_injector.inject_message(room, user, "Indeed.")
  73. yield self.event_injector.inject_message(room, user, "Indeed.")
  74. yield self.event_injector.inject_message(room, user, "Indeed.")
  75. # A little over 24 hours is fine :)
  76. self.hs.clock.now += (60 * 60 * 24) + 50
  77. count = yield self.store.count_daily_messages()
  78. self.assertEqual(3, count)
  79. self._assert_stats_reporting(8, self.hs.clock.now)
  80. @defer.inlineCallbacks
  81. def _get_last_stream_token(self):
  82. rows = yield self.db_pool.runQuery(
  83. "SELECT stream_ordering"
  84. " FROM events"
  85. " ORDER BY stream_ordering DESC"
  86. " LIMIT 1"
  87. )
  88. if not rows:
  89. defer.returnValue(0)
  90. else:
  91. defer.returnValue(rows[0][0])
  92. @defer.inlineCallbacks
  93. def _assert_stats_reporting(self, messages, time):
  94. rows = yield self.db_pool.runQuery(
  95. "SELECT reported_stream_token, reported_time FROM stats_reporting"
  96. )
  97. self.assertEqual([(self.base_event + messages, time,)], rows)