annotate tests/unit/test_pubsub-cache.py @ 4096:087902fbb77a

plugin XEP-0060: move setting of `pubsub_watching` to `profile_connecting` so it's available early
author Goffi <goffi@goffi.org>
date Mon, 12 Jun 2023 15:00:59 +0200
parents 4b842c1fb686
children f1d0cde61af7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3624
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
2
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia: an XMPP client
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
5
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
10
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
15
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
18
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from twisted.internet import defer
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from pytest_twisted import ensureDeferred as ed
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from unittest.mock import MagicMock, patch
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
22 from libervia.backend.memory.sqla import PubsubNode, SyncState
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
23 from libervia.backend.core.constants import Const as C
3624
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
24
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
25
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
26 class TestPubsubCache:
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
27
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
28 @ed
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
29 async def test_cache_is_used_transparently(self, host, client):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
30 """Cache is used when a pubsub get_items operation is done"""
3624
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
31 items_ret = defer.Deferred()
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
32 items_ret.callback(([], {}))
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
33 client.pubsub_client.items = MagicMock(return_value=items_ret)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
34 host.memory.storage.get_pubsub_node.return_value = None
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
35 pubsub_node = host.memory.storage.set_pubsub_node.return_value = PubsubNode(
3624
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
36 sync_state = None
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
37 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
38 with patch.object(host.plugins["PUBSUB_CACHE"], "cache_node") as cache_node:
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
39 await host.plugins["XEP-0060"].get_items(
3624
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
40 client,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
41 None,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
42 "urn:xmpp:microblog:0",
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
43 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
44 assert cache_node.call_count == 1
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
45 assert cache_node.call_args.args[-1] == pubsub_node
3624
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
46
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
47 @ed
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
48 async def test_cache_is_skipped_with_use_cache_false(self, host, client):
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
49 """Cache is skipped when 'use_cache' extra field is False"""
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
50 items_ret = defer.Deferred()
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
51 items_ret.callback(([], {}))
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
52 client.pubsub_client.items = MagicMock(return_value=items_ret)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
53 host.memory.storage.get_pubsub_node.return_value = None
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
54 host.memory.storage.set_pubsub_node.return_value = PubsubNode(
3624
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
55 sync_state = None
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
56 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
57 with patch.object(host.plugins["PUBSUB_CACHE"], "cache_node") as cache_node:
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
58 await host.plugins["XEP-0060"].get_items(
3624
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
59 client,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
60 None,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
61 "urn:xmpp:microblog:0",
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
62 extra = {C.KEY_USE_CACHE: False}
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
63 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
64 assert not cache_node.called
3624
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
65
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
66 @ed
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
67 async def test_cache_is_not_used_when_no_cache(self, host, client):
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
68 """Cache is skipped when 'pubsub_cache_strategy' is set to 'no_cache'"""
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
69 with host.use_option_and_reload(None, "pubsub_cache_strategy", "no_cache"):
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
70 items_ret = defer.Deferred()
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
71 items_ret.callback(([], {}))
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
72 client.pubsub_client.items = MagicMock(return_value=items_ret)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
73 host.memory.storage.get_pubsub_node.return_value = None
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
74 host.memory.storage.set_pubsub_node.return_value = PubsubNode(
3624
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
75 sync_state = None
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
76 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
77 with patch.object(host.plugins["PUBSUB_CACHE"], "cache_node") as cache_node:
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
78 await host.plugins["XEP-0060"].get_items(
3624
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
79 client,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
80 None,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
81 "urn:xmpp:microblog:0",
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
82 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
83 assert not cache_node.called
3624
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
84
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
85
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
86 @ed
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
87 async def test_no_pubsub_get_when_cache_completed(self, host, client):
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
88 """No pubsub get is emitted when items are fully cached"""
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
89 items_ret = defer.Deferred()
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
90 items_ret.callback(([], {}))
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
91 client.pubsub_client.items = MagicMock(return_value=items_ret)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
92 host.memory.storage.get_pubsub_node.return_value = PubsubNode(
3624
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
93 sync_state = SyncState.COMPLETED
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
94 )
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
95 with patch.object(
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
96 host.plugins["PUBSUB_CACHE"],
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
97 "get_items_from_cache"
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
98 ) as get_items_from_cache:
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
99 get_items_from_cache.return_value = ([], {})
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
100 await host.plugins["XEP-0060"].get_items(
3624
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
101 client,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
102 None,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
103 "urn:xmpp:microblog:0",
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
104 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
105 assert get_items_from_cache.call_count == 1
3624
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
106 assert not client.pubsub_client.items.called
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
107
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
108 @ed
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
109 async def test_pubsub_get_when_cache_in_progress(self, host, client):
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
110 """Pubsub get is emitted when items are currently being cached"""
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
111 items_ret = defer.Deferred()
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
112 items_ret.callback(([], {}))
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
113 client.pubsub_client.items = MagicMock(return_value=items_ret)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
114 host.memory.storage.get_pubsub_node.return_value = PubsubNode(
3624
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
115 sync_state = SyncState.IN_PROGRESS
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
116 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
117 with patch.object(host.plugins["PUBSUB_CACHE"], "analyse_node") as analyse_node:
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
118 analyse_node.return_value = {"to_sync": True}
3624
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
119 with patch.object(
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
120 host.plugins["PUBSUB_CACHE"],
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
121 "get_items_from_cache"
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
122 ) as get_items_from_cache:
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
123 get_items_from_cache.return_value = ([], {})
3624
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
124 assert client.pubsub_client.items.call_count == 0
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
125 await host.plugins["XEP-0060"].get_items(
3624
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
126 client,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
127 None,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
128 "urn:xmpp:microblog:0",
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
129 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3624
diff changeset
130 assert not get_items_from_cache.called
3624
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
131 assert client.pubsub_client.items.call_count == 1