test_events.py 4.5 KB

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