|
| 1 | +from django.core.handlers.wsgi import WSGIHandler |
| 2 | +from django.core.signals import request_finished |
| 3 | +from django.http import HttpRequest |
| 4 | +from django.utils import timezone |
| 5 | + |
| 6 | +from sentry import app |
| 7 | +from sentry.testutils import TestCase |
| 8 | +from sentry.utils.compat.mock import patch |
| 9 | +from sentry.utils.request_cache import clear_cache, request_cache |
| 10 | + |
| 11 | + |
| 12 | +@request_cache |
| 13 | +def cached_fn(arg1, arg2=None): |
| 14 | + # call this so we can mock it and see how often it's called |
| 15 | + timezone.now() |
| 16 | + if arg2: |
| 17 | + return arg1 + arg2 |
| 18 | + return arg1 |
| 19 | + |
| 20 | + |
| 21 | +class RequestCacheTest(TestCase): |
| 22 | + def setUp(self): |
| 23 | + self.original_receivers = request_finished.receivers |
| 24 | + request_finished.receivers = [] |
| 25 | + request_finished.connect(clear_cache) |
| 26 | + super().setUp() |
| 27 | + |
| 28 | + def tearDown(self): |
| 29 | + # remove the request, trigger the signal to clear the cache, restore receivers |
| 30 | + app.env.request = None |
| 31 | + request_finished.send(sender=WSGIHandler) |
| 32 | + request_finished.receivers = self.original_receivers |
| 33 | + super().tearDown() |
| 34 | + |
| 35 | + @patch("django.utils.timezone.now") |
| 36 | + def test_basic_cache(self, mock_now): |
| 37 | + app.env.request = HttpRequest() |
| 38 | + assert cached_fn("cat", arg2="dog") == "catdog" |
| 39 | + assert cached_fn("cat", arg2="dog") == "catdog" |
| 40 | + assert mock_now.call_count == 1 |
| 41 | + |
| 42 | + @patch("django.utils.timezone.now") |
| 43 | + def test_cache_none(self, mock_now): |
| 44 | + app.env.request = HttpRequest() |
| 45 | + assert cached_fn(None) is None |
| 46 | + assert cached_fn(None) is None |
| 47 | + assert mock_now.call_count == 1 |
| 48 | + |
| 49 | + @patch("django.utils.timezone.now") |
| 50 | + def test_different_args(self, mock_now): |
| 51 | + app.env.request = HttpRequest() |
| 52 | + assert cached_fn("cat", arg2="dog") == "catdog" |
| 53 | + assert cached_fn("hey", arg2="dog") == "heydog" |
| 54 | + assert mock_now.call_count == 2 |
| 55 | + |
| 56 | + @patch("django.utils.timezone.now") |
| 57 | + def test_different_kwargs(self, mock_now): |
| 58 | + app.env.request = HttpRequest() |
| 59 | + assert cached_fn("cat", arg2="dog") == "catdog" |
| 60 | + assert cached_fn("cat", arg2="hat") == "cathat" |
| 61 | + assert mock_now.call_count == 2 |
| 62 | + |
| 63 | + @patch("django.utils.timezone.now") |
| 64 | + def test_different_request(self, mock_now): |
| 65 | + app.env.request = HttpRequest() |
| 66 | + assert cached_fn("cat") == "cat" |
| 67 | + request_finished.send(sender=WSGIHandler) |
| 68 | + app.env.request = HttpRequest() |
| 69 | + assert cached_fn("cat") == "cat" |
| 70 | + assert mock_now.call_count == 2 |
| 71 | + |
| 72 | + @patch("django.utils.timezone.now") |
| 73 | + def test_request_over(self, mock_now): |
| 74 | + app.env.request = HttpRequest() |
| 75 | + assert cached_fn("cat") == "cat" |
| 76 | + app.env.request = None |
| 77 | + assert cached_fn("cat") == "cat" |
| 78 | + assert mock_now.call_count == 2 |
0 commit comments