Mercurial > libervia-backend
annotate tests/unit/test_pubsub-cache.py @ 4242:8acf46ed7f36
frontends: remote control implementation:
This is the frontends common part of remote control implementation. It handle the creation
of WebRTC session, and management of inputs. For now the reception use freedesktop.org
Desktop portal, and works mostly with Wayland based Desktop Environments.
rel 436
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 11 May 2024 13:52:43 +0200 |
parents | 4b842c1fb686 |
children | f1d0cde61af7 |
rev | line source |
---|---|
3624 | 1 #!/usr/bin/env python3 |
2 | |
3 # Libervia: an XMPP client | |
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) | |
5 | |
6 # This program is free software: you can redistribute it and/or modify | |
7 # it under the terms of the GNU Affero General Public License as published by | |
8 # the Free Software Foundation, either version 3 of the License, or | |
9 # (at your option) any later version. | |
10 | |
11 # This program is distributed in the hope that it will be useful, | |
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 # GNU Affero General Public License for more details. | |
15 | |
16 # You should have received a copy of the GNU Affero General Public License | |
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | |
19 from twisted.internet import defer | |
20 from pytest_twisted import ensureDeferred as ed | |
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 | 24 |
25 | |
26 class TestPubsubCache: | |
27 | |
28 @ed | |
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 | 31 items_ret = defer.Deferred() |
32 items_ret.callback(([], {})) | |
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 | 36 sync_state = None |
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 | 40 client, |
41 None, | |
42 "urn:xmpp:microblog:0", | |
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 | 46 |
47 @ed | |
48 async def test_cache_is_skipped_with_use_cache_false(self, host, client): | |
49 """Cache is skipped when 'use_cache' extra field is False""" | |
50 items_ret = defer.Deferred() | |
51 items_ret.callback(([], {})) | |
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 | 55 sync_state = None |
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 | 59 client, |
60 None, | |
61 "urn:xmpp:microblog:0", | |
62 extra = {C.KEY_USE_CACHE: False} | |
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 | 65 |
66 @ed | |
67 async def test_cache_is_not_used_when_no_cache(self, host, client): | |
68 """Cache is skipped when 'pubsub_cache_strategy' is set to 'no_cache'""" | |
69 with host.use_option_and_reload(None, "pubsub_cache_strategy", "no_cache"): | |
70 items_ret = defer.Deferred() | |
71 items_ret.callback(([], {})) | |
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 | 75 sync_state = None |
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 | 79 client, |
80 None, | |
81 "urn:xmpp:microblog:0", | |
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 | 84 |
85 | |
86 @ed | |
87 async def test_no_pubsub_get_when_cache_completed(self, host, client): | |
88 """No pubsub get is emitted when items are fully cached""" | |
89 items_ret = defer.Deferred() | |
90 items_ret.callback(([], {})) | |
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 | 93 sync_state = SyncState.COMPLETED |
94 ) | |
95 with patch.object( | |
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 | 101 client, |
102 None, | |
103 "urn:xmpp:microblog:0", | |
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 | 106 assert not client.pubsub_client.items.called |
107 | |
108 @ed | |
109 async def test_pubsub_get_when_cache_in_progress(self, host, client): | |
110 """Pubsub get is emitted when items are currently being cached""" | |
111 items_ret = defer.Deferred() | |
112 items_ret.callback(([], {})) | |
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 | 115 sync_state = SyncState.IN_PROGRESS |
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 | 119 with patch.object( |
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 | 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 | 126 client, |
127 None, | |
128 "urn:xmpp:microblog:0", | |
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 | 131 assert client.pubsub_client.items.call_count == 1 |