| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
from datetime import datetime, timedelta, timezone |
| 29 |
import gzip |
| 30 |
import json |
| 31 |
|
| 32 |
|
| 33 |
import logging |
| 34 |
import os |
| 35 |
import shutil |
| 36 |
import sys |
| 37 |
import tempfile |
| 38 |
import time |
| 39 |
from unittest import mock |
| 40 |
import zlib |
| 41 |
|
| 42 |
import pytest |
| 43 |
|
| 44 |
from apprise import exception |
| 45 |
from apprise.asset import AppriseAsset |
| 46 |
from apprise.persistent_store import ( |
| 47 |
CacheJSONEncoder, |
| 48 |
CacheObject, |
| 49 |
PersistentStore, |
| 50 |
PersistentStoreMode, |
| 51 |
) |
| 52 |
|
| 53 |
logging.disable(logging.CRITICAL) |
| 54 |
|
| 55 |
|
| 56 |
TEST_VAR_DIR = os.path.join(os.path.dirname(__file__), "var") |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
_real_ntf_close = tempfile._TemporaryFileWrapper.close |
| 61 |
|
| 62 |
|
| 63 |
def _flaky_close(always=False): |
| 64 |
"""Mock close errors while still releasing the real file handle. |
| 65 |
|
| 66 |
Raise on every call when ``always`` is true; otherwise, raise only once. |
| 67 |
""" |
| 68 |
state = {"calls": 0} |
| 69 |
|
| 70 |
def _close(self, *args, **kwargs): |
| 71 |
|
| 72 |
_real_ntf_close(self) |
| 73 |
state["calls"] += 1 |
| 74 |
if always or state["calls"] == 1: |
| 75 |
raise OSError() |
| 76 |
|
| 77 |
return _close |
| 78 |
|
| 79 |
|
| 80 |
def test_persistent_storage_asset(tmpdir): |
| 81 |
"""Tests the Apprise Asset Object when setting the Persistent Store.""" |
| 82 |
|
| 83 |
asset = AppriseAsset(storage_path=str(tmpdir)) |
| 84 |
assert asset.storage_path == str(tmpdir) |
| 85 |
assert asset.storage_mode is PersistentStoreMode.AUTO |
| 86 |
|
| 87 |
|
| 88 |
asset = AppriseAsset( |
| 89 |
storage_path=None, storage_mode=PersistentStoreMode.MEMORY |
| 90 |
) |
| 91 |
assert asset.storage_path is None |
| 92 |
assert asset.storage_mode is PersistentStoreMode.MEMORY |
| 93 |
|
| 94 |
|
| 95 |
def test_persistent_storage_bad_mode(tmpdir): |
| 96 |
"""Persistent Storage Bad Mode Testing.""" |
| 97 |
|
| 98 |
with pytest.raises(AttributeError): |
| 99 |
PersistentStore(namespace="abc", path=str(tmpdir), mode="invalid") |
| 100 |
|
| 101 |
with pytest.raises(AttributeError): |
| 102 |
AppriseAsset(storage_mode="invalid") |
| 103 |
|
| 104 |
|
| 105 |
def test_disabled_persistent_storage(tmpdir): |
| 106 |
"""Persistent Storage General Testing.""" |
| 107 |
|
| 108 |
pc = PersistentStore( |
| 109 |
namespace="abc", path=str(tmpdir), mode=PersistentStoreMode.MEMORY |
| 110 |
) |
| 111 |
assert pc.read() is None |
| 112 |
assert pc.read("mykey") is None |
| 113 |
with pytest.raises(AttributeError): |
| 114 |
|
| 115 |
pc.read("!invalid") |
| 116 |
assert pc.write("data") is False |
| 117 |
assert pc.get("key") is None |
| 118 |
assert pc.set("key", "value") |
| 119 |
assert pc.get("key") == "value" |
| 120 |
|
| 121 |
assert pc.set("key2", "value") |
| 122 |
pc.clear("key", "key-not-previously-set") |
| 123 |
assert pc.get("key2") == "value" |
| 124 |
assert pc.get("key") is None |
| 125 |
|
| 126 |
|
| 127 |
assert pc.set("key", "another-value") |
| 128 |
|
| 129 |
pc.clear() |
| 130 |
assert pc.get("key2") is None |
| 131 |
assert pc.get("key") is None |
| 132 |
|
| 133 |
pc.clear() |
| 134 |
|
| 135 |
|
| 136 |
pc.set("not-persistent", "value", persistent=False) |
| 137 |
del pc["not-persistent"] |
| 138 |
with pytest.raises(KeyError): |
| 139 |
|
| 140 |
del pc["not-persistent"] |
| 141 |
|
| 142 |
|
| 143 |
pc.set("persistent", "value") |
| 144 |
|
| 145 |
del pc["persistent"] |
| 146 |
|
| 147 |
|
| 148 |
assert len(os.listdir(str(tmpdir))) == 0 |
| 149 |
|
| 150 |
with pytest.raises(AttributeError): |
| 151 |
|
| 152 |
PersistentStore(namespace="abc", path=str(tmpdir), mode="garbage") |
| 153 |
|
| 154 |
|
| 155 |
def test_persistent_storage_init(tmpdir): |
| 156 |
"""Test storage initialization.""" |
| 157 |
with pytest.raises(AttributeError): |
| 158 |
PersistentStore(namespace="", path=str(tmpdir)) |
| 159 |
with pytest.raises(AttributeError): |
| 160 |
PersistentStore(namespace=None, path=str(tmpdir)) |
| 161 |
|
| 162 |
with pytest.raises(AttributeError): |
| 163 |
PersistentStore(namespace="_", path=str(tmpdir)) |
| 164 |
with pytest.raises(AttributeError): |
| 165 |
PersistentStore(namespace=".", path=str(tmpdir)) |
| 166 |
with pytest.raises(AttributeError): |
| 167 |
PersistentStore(namespace="-", path=str(tmpdir)) |
| 168 |
|
| 169 |
with pytest.raises(AttributeError): |
| 170 |
PersistentStore(namespace="_abc", path=str(tmpdir)) |
| 171 |
with pytest.raises(AttributeError): |
| 172 |
PersistentStore(namespace=".abc", path=str(tmpdir)) |
| 173 |
with pytest.raises(AttributeError): |
| 174 |
PersistentStore(namespace="-abc", path=str(tmpdir)) |
| 175 |
|
| 176 |
with pytest.raises(AttributeError): |
| 177 |
PersistentStore(namespace="%", path=str(tmpdir)) |
| 178 |
|
| 179 |
|
| 180 |
def test_persistent_storage_general(tmpdir): |
| 181 |
"""Persistent Storage General Testing.""" |
| 182 |
namespace = "abc" |
| 183 |
|
| 184 |
pc = PersistentStore() |
| 185 |
|
| 186 |
|
| 187 |
assert pc.mode == PersistentStoreMode.MEMORY |
| 188 |
|
| 189 |
assert pc.size() == 0 |
| 190 |
assert pc.files() == [] |
| 191 |
assert pc.files(exclude=True, lazy=False) == [] |
| 192 |
assert pc.files(exclude=False, lazy=False) == [] |
| 193 |
pc.set("key", "value") |
| 194 |
|
| 195 |
assert pc.size() == 0 |
| 196 |
assert pc.files(exclude=True, lazy=False) == [] |
| 197 |
assert pc.files(exclude=False, lazy=False) == [] |
| 198 |
|
| 199 |
|
| 200 |
pc = PersistentStore(namespace=namespace, path=str(tmpdir)) |
| 201 |
|
| 202 |
|
| 203 |
assert pc.mode == PersistentStoreMode.AUTO |
| 204 |
|
| 205 |
|
| 206 |
assert pc.path == os.path.join(str(tmpdir), "abc") |
| 207 |
|
| 208 |
|
| 209 |
assert pc.set("key", "value", datetime.now() + timedelta(hours=1)) |
| 210 |
|
| 211 |
assert pc.set("key", "value", 60) |
| 212 |
|
| 213 |
with pytest.raises(AttributeError): |
| 214 |
assert pc.set("key", "value", "invalid") |
| 215 |
|
| 216 |
pc = PersistentStore(namespace=namespace, path=str(tmpdir)) |
| 217 |
|
| 218 |
|
| 219 |
assert pc.get("key") == "value" |
| 220 |
assert pc["key"] == "value" |
| 221 |
|
| 222 |
pc = PersistentStore(namespace=namespace, path=str(tmpdir)) |
| 223 |
assert pc.keys() |
| 224 |
|
| 225 |
assert pc.keys() |
| 226 |
|
| 227 |
pc = PersistentStore(namespace=namespace, path=str(tmpdir)) |
| 228 |
|
| 229 |
with pytest.raises(KeyError): |
| 230 |
|
| 231 |
pc["unassigned_key"] |
| 232 |
|
| 233 |
|
| 234 |
def test_persistent_storage_auto_mode(tmpdir): |
| 235 |
"""Persistent Storage Auto Write Testing.""" |
| 236 |
namespace = "abc" |
| 237 |
|
| 238 |
pc = PersistentStore( |
| 239 |
namespace=namespace, path=str(tmpdir), mode=PersistentStoreMode.AUTO |
| 240 |
) |
| 241 |
|
| 242 |
pc.write(b"test") |
| 243 |
with mock.patch("os.unlink", side_effect=FileNotFoundError()): |
| 244 |
assert pc.delete(all=True) is True |
| 245 |
|
| 246 |
|
| 247 |
with open(os.path.join(pc.path, pc.temp_dir, "test.file"), "wb") as fd: |
| 248 |
fd.write(b"data") |
| 249 |
|
| 250 |
|
| 251 |
assert pc.delete(temp=True) is True |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
assert pc.set("key", "value") is True |
| 256 |
pc.write(b"test") |
| 257 |
assert pc.delete(cache=True) is True |
| 258 |
|
| 259 |
assert pc.read() == b"test" |
| 260 |
|
| 261 |
assert pc.get("key") is None |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
assert pc.set("key", "value") is True |
| 266 |
assert pc.write(b"test", key="iokey") is True |
| 267 |
assert pc.delete("iokey") is True |
| 268 |
assert pc.get("key") == "value" |
| 269 |
assert pc.read("iokey") is None |
| 270 |
|
| 271 |
|
| 272 |
def test_persistent_storage_flush_mode(tmpdir): |
| 273 |
"""Persistent Storage Forced Write Testing.""" |
| 274 |
namespace = "abc" |
| 275 |
|
| 276 |
pc = PersistentStore( |
| 277 |
namespace=namespace, path=str(tmpdir), mode=PersistentStoreMode.FLUSH |
| 278 |
) |
| 279 |
|
| 280 |
|
| 281 |
path = os.path.join(str(tmpdir), namespace) |
| 282 |
|
| 283 |
assert pc.size() == 0 |
| 284 |
assert list(pc.files()) == [] |
| 285 |
|
| 286 |
|
| 287 |
assert pc.get("key") is None |
| 288 |
assert len(pc.keys()) == 0 |
| 289 |
assert "key" not in pc |
| 290 |
|
| 291 |
|
| 292 |
assert pc.set("key", "value") |
| 293 |
assert len(pc.keys()) == 1 |
| 294 |
assert "key" in list(pc.keys()) |
| 295 |
|
| 296 |
assert pc.size() > 0 |
| 297 |
assert len(pc.files()) == 1 |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
assert len(pc.files()) == 1 |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
assert pc.set("key", "value") |
| 306 |
|
| 307 |
path_content = os.listdir(path) |
| 308 |
|
| 309 |
assert len(path_content) == 3 |
| 310 |
|
| 311 |
|
| 312 |
pc["key"] = "value2" |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
pc.set("key-xx", "abc123", persistent=False) |
| 317 |
|
| 318 |
pc["key-xx"] = "def678" |
| 319 |
|
| 320 |
pc["key-xx"] = "def678" |
| 321 |
|
| 322 |
|
| 323 |
assert pc["key-xx"] == "def678" |
| 324 |
assert pc.get("key-xx") == "def678" |
| 325 |
|
| 326 |
|
| 327 |
del pc |
| 328 |
|
| 329 |
pc = PersistentStore( |
| 330 |
namespace=namespace, path=str(tmpdir), mode=PersistentStoreMode.FLUSH |
| 331 |
) |
| 332 |
|
| 333 |
assert pc.get("key-xx") is None |
| 334 |
with pytest.raises(KeyError): |
| 335 |
pc["key-xx"] |
| 336 |
|
| 337 |
|
| 338 |
assert "key" in pc |
| 339 |
assert pc.get("key") == "value2" |
| 340 |
|
| 341 |
|
| 342 |
assert len(os.listdir(str(tmpdir))) == 1 |
| 343 |
assert namespace in os.listdir(str(tmpdir)) |
| 344 |
|
| 345 |
path_content = os.listdir(path) |
| 346 |
assert len(path_content) == 4 |
| 347 |
|
| 348 |
|
| 349 |
pc["key"] = "value3" |
| 350 |
path_content = os.listdir(path) |
| 351 |
assert len(path_content) == 4 |
| 352 |
|
| 353 |
|
| 354 |
assert pc.temp_dir in path_content |
| 355 |
|
| 356 |
assert os.path.basename(pc.cache_file) in path_content |
| 357 |
|
| 358 |
path = os.path.join(pc.path, pc.temp_dir) |
| 359 |
path_content = os.listdir(path) |
| 360 |
|
| 361 |
|
| 362 |
assert len(path_content) == 0 |
| 363 |
|
| 364 |
|
| 365 |
del pc |
| 366 |
|
| 367 |
|
| 368 |
pc = PersistentStore( |
| 369 |
namespace=namespace, path=str(tmpdir), mode=PersistentStoreMode.FLUSH |
| 370 |
) |
| 371 |
|
| 372 |
|
| 373 |
assert pc.get("key") == "value3" |
| 374 |
assert "key" in pc |
| 375 |
|
| 376 |
|
| 377 |
del pc["key"] |
| 378 |
assert pc.size() == 0 |
| 379 |
assert "key" not in pc |
| 380 |
|
| 381 |
assert pc.write("data") is True |
| 382 |
assert pc.read() == b"data" |
| 383 |
assert pc.write(b"data") is True |
| 384 |
assert pc.read() == b"data" |
| 385 |
|
| 386 |
assert pc.read("default") == b"data" |
| 387 |
assert pc.write("data2", key="mykey") is True |
| 388 |
assert pc.read("mykey") == b"data2" |
| 389 |
|
| 390 |
|
| 391 |
assert pc.delete("mykey") |
| 392 |
assert pc.read("mykey") is None |
| 393 |
|
| 394 |
assert pc.read("default") == b"data" |
| 395 |
assert pc.read() == b"data" |
| 396 |
|
| 397 |
assert pc.delete() |
| 398 |
assert pc.read("mykey") is None |
| 399 |
assert pc.read() is None |
| 400 |
|
| 401 |
|
| 402 |
with open(os.path.join(TEST_VAR_DIR, "apprise-test.gif"), "rb") as fd: |
| 403 |
assert pc.write(fd, key="mykey", compress=False) is True |
| 404 |
|
| 405 |
|
| 406 |
fd.seek(0) |
| 407 |
assert pc.read("mykey", compress=False) == fd.read() |
| 408 |
|
| 409 |
with open(os.path.join(TEST_VAR_DIR, "apprise-test.gif"), "rb") as fd: |
| 410 |
assert pc.write(fd, key="mykey", compress=True) is True |
| 411 |
|
| 412 |
|
| 413 |
fd.seek(0) |
| 414 |
assert pc.read("mykey", compress=True) == fd.read() |
| 415 |
|
| 416 |
class Foobar: |
| 417 |
def read(*args, **kwargs): |
| 418 |
return 42 |
| 419 |
|
| 420 |
foobar = Foobar() |
| 421 |
|
| 422 |
with pytest.raises(exception.AppriseDiskIOError): |
| 423 |
pc.write(foobar, key="foobar", compress=True) |
| 424 |
assert pc.read("foobar") is None |
| 425 |
|
| 426 |
class Foobar: |
| 427 |
def read(*args, **kwargs): |
| 428 |
return "good" |
| 429 |
|
| 430 |
foobar = Foobar() |
| 431 |
|
| 432 |
assert pc.write(foobar, key="foobar", compress=True) |
| 433 |
assert pc.read("foobar") == b"good" |
| 434 |
pc.delete() |
| 435 |
|
| 436 |
class Foobar: |
| 437 |
def read(*args, **kwargs): |
| 438 |
|
| 439 |
raise TypeError() |
| 440 |
|
| 441 |
foobar = Foobar() |
| 442 |
|
| 443 |
with pytest.raises(exception.AppriseDiskIOError): |
| 444 |
pc.write(foobar, key="foobar", compress=True) |
| 445 |
assert pc.read("foobar") is None |
| 446 |
|
| 447 |
|
| 448 |
prev_max_file_size = pc.max_file_size |
| 449 |
pc.max_file_size = 1 |
| 450 |
assert pc.delete() |
| 451 |
|
| 452 |
assert pc.write("data") is False |
| 453 |
assert pc.read() is None |
| 454 |
|
| 455 |
|
| 456 |
pc.max_file_size = prev_max_file_size |
| 457 |
|
| 458 |
|
| 459 |
pc.delete() |
| 460 |
|
| 461 |
assert pc.write("data") |
| 462 |
|
| 463 |
data = pc.read(compress=False)[:20] + pc.read(compress=False)[:10] |
| 464 |
pc.write(data, compress=False) |
| 465 |
|
| 466 |
|
| 467 |
assert pc.read() is None |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
assert pc.read(compress=False) == data |
| 474 |
|
| 475 |
|
| 476 |
assert pc.read("no-key-exists") is None |
| 477 |
|
| 478 |
pc.write(b"test") |
| 479 |
pc["key"] = "value" |
| 480 |
with mock.patch("os.unlink", side_effect=FileNotFoundError()): |
| 481 |
assert pc.delete(all=True) is True |
| 482 |
with mock.patch("os.unlink", side_effect=OSError()): |
| 483 |
assert pc.delete(all=True) is False |
| 484 |
|
| 485 |
|
| 486 |
tmp_file = os.path.join(pc.path, pc.temp_dir, "test.file") |
| 487 |
with open(tmp_file, "wb") as fd: |
| 488 |
fd.write(b"data") |
| 489 |
|
| 490 |
assert pc.set("key", "value") is True |
| 491 |
assert pc.write(b"test", key="iokey") is True |
| 492 |
|
| 493 |
assert pc.delete(temp=True) is True |
| 494 |
assert os.path.exists(tmp_file) is False |
| 495 |
|
| 496 |
assert pc.get("key") == "value" |
| 497 |
assert pc.read("iokey") == b"test" |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
assert pc.set("key", "value") is True |
| 502 |
assert pc.write(b"test") is True |
| 503 |
assert pc.delete(cache=True) is True |
| 504 |
|
| 505 |
assert pc.read() == b"test" |
| 506 |
|
| 507 |
assert pc.get("key") is None |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
assert pc.set("key", "value") is True |
| 512 |
assert pc.write(b"test", key="iokey") is True |
| 513 |
assert pc.delete("iokey") is True |
| 514 |
assert pc.get("key") == "value" |
| 515 |
assert pc.read("iokey") is None |
| 516 |
|
| 517 |
|
| 518 |
cust1_file = os.path.join(pc.path, "test.file") |
| 519 |
cust2_file = os.path.join(pc.path, pc.data_dir, "test.file") |
| 520 |
with open(cust1_file, "wb") as fd: |
| 521 |
fd.write(b"data") |
| 522 |
with open(cust2_file, "wb") as fd: |
| 523 |
fd.write(b"data") |
| 524 |
|
| 525 |
|
| 526 |
assert pc.delete() |
| 527 |
assert os.path.exists(cust1_file) is True |
| 528 |
assert os.path.exists(cust2_file) is True |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
assert pc.delete(validate=False) |
| 533 |
assert os.path.exists(cust1_file) is False |
| 534 |
assert os.path.exists(cust2_file) is False |
| 535 |
|
| 536 |
pc["key"] = "value" |
| 537 |
pc["key2"] = "value2" |
| 538 |
assert "key" in pc |
| 539 |
assert "key2" in pc |
| 540 |
pc.clear("key") |
| 541 |
assert "key" not in pc |
| 542 |
assert "key2" in pc |
| 543 |
|
| 544 |
|
| 545 |
pc.set( |
| 546 |
"expired", |
| 547 |
"expired-content", |
| 548 |
expires=datetime.now() - timedelta(days=1), |
| 549 |
) |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
assert "expired" not in pc |
| 554 |
assert pc.get("expired") is None |
| 555 |
|
| 556 |
pc.prune() |
| 557 |
|
| 558 |
|
| 559 |
def test_persistent_storage_corruption_handling(tmpdir): |
| 560 |
"""Test corrupting handling of storage.""" |
| 561 |
|
| 562 |
|
| 563 |
namespace = "def456" |
| 564 |
|
| 565 |
|
| 566 |
pc = PersistentStore( |
| 567 |
namespace=namespace, path=str(tmpdir), mode=PersistentStoreMode.FLUSH |
| 568 |
) |
| 569 |
|
| 570 |
cache_file = pc.cache_file |
| 571 |
assert not os.path.isfile(cache_file) |
| 572 |
|
| 573 |
|
| 574 |
pc["mykey"] = 42 |
| 575 |
assert os.path.isfile(cache_file) |
| 576 |
|
| 577 |
with gzip.open(cache_file, "rb") as f: |
| 578 |
|
| 579 |
json.loads(f.read().decode("utf-8")) |
| 580 |
|
| 581 |
|
| 582 |
del pc |
| 583 |
|
| 584 |
|
| 585 |
with open(cache_file, "wb") as f: |
| 586 |
f.write(b"{") |
| 587 |
|
| 588 |
pc = PersistentStore( |
| 589 |
namespace=namespace, path=str(tmpdir), mode=PersistentStoreMode.FLUSH |
| 590 |
) |
| 591 |
|
| 592 |
|
| 593 |
assert "mykey" not in pc |
| 594 |
pc["mykey"] = 42 |
| 595 |
del pc |
| 596 |
|
| 597 |
|
| 598 |
pc = PersistentStore( |
| 599 |
namespace=namespace, path=str(tmpdir), mode=PersistentStoreMode.FLUSH |
| 600 |
) |
| 601 |
|
| 602 |
assert "mykey" in pc |
| 603 |
|
| 604 |
|
| 605 |
with gzip.open(cache_file, "wb") as f: |
| 606 |
|
| 607 |
f.write(b"{") |
| 608 |
|
| 609 |
pc = PersistentStore( |
| 610 |
namespace=namespace, path=str(tmpdir), mode=PersistentStoreMode.FLUSH |
| 611 |
) |
| 612 |
|
| 613 |
|
| 614 |
assert "mykey" not in pc |
| 615 |
pc["mykey"] = 42 |
| 616 |
del pc |
| 617 |
|
| 618 |
pc = PersistentStore( |
| 619 |
namespace=namespace, path=str(tmpdir), mode=PersistentStoreMode.FLUSH |
| 620 |
) |
| 621 |
|
| 622 |
|
| 623 |
assert pc.flush(force=True) is True |
| 624 |
|
| 625 |
assert pc.flush(force=True) is True |
| 626 |
|
| 627 |
|
| 628 |
with ( |
| 629 |
mock.patch("gzip.open", side_effect=OSError()), |
| 630 |
pytest.raises(KeyError), |
| 631 |
): |
| 632 |
pc["mykey"] = 43 |
| 633 |
|
| 634 |
pc = PersistentStore( |
| 635 |
namespace=namespace, path=str(tmpdir), mode=PersistentStoreMode.FLUSH |
| 636 |
) |
| 637 |
|
| 638 |
|
| 639 |
with mock.patch("gzip.open", side_effect=OSError()): |
| 640 |
|
| 641 |
assert not pc.keys() |
| 642 |
|
| 643 |
pc = PersistentStore( |
| 644 |
namespace=namespace, path=str(tmpdir), mode=PersistentStoreMode.FLUSH |
| 645 |
) |
| 646 |
|
| 647 |
with ( |
| 648 |
mock.patch("json.loads", side_effect=TypeError()), |
| 649 |
mock.patch("os.unlink", side_effect=FileNotFoundError()), |
| 650 |
pytest.raises(KeyError), |
| 651 |
): |
| 652 |
pc["mykey"] = 44 |
| 653 |
|
| 654 |
pc = PersistentStore( |
| 655 |
namespace=namespace, path=str(tmpdir), mode=PersistentStoreMode.FLUSH |
| 656 |
) |
| 657 |
|
| 658 |
with ( |
| 659 |
mock.patch("json.loads", side_effect=TypeError()), |
| 660 |
mock.patch("os.unlink", side_effect=OSError()), |
| 661 |
pytest.raises(KeyError), |
| 662 |
): |
| 663 |
pc["mykey"] = 45 |
| 664 |
|
| 665 |
pc["my-new-key"] = 43 |
| 666 |
with mock.patch("gzip.open", side_effect=OSError()): |
| 667 |
|
| 668 |
assert pc.flush(force=True) is False |
| 669 |
|
| 670 |
with mock.patch("json.dumps", side_effect=TypeError()): |
| 671 |
|
| 672 |
assert pc.flush(force=True) is False |
| 673 |
|
| 674 |
with mock.patch("os.makedirs", side_effect=OSError()): |
| 675 |
pc = PersistentStore( |
| 676 |
namespace=namespace, |
| 677 |
path=str(tmpdir), |
| 678 |
mode=PersistentStoreMode.FLUSH, |
| 679 |
) |
| 680 |
|
| 681 |
|
| 682 |
assert pc.mode == PersistentStoreMode.MEMORY |
| 683 |
|
| 684 |
|
| 685 |
pc = PersistentStore( |
| 686 |
namespace="file-time-refresh", |
| 687 |
path=str(tmpdir), |
| 688 |
mode=PersistentStoreMode.AUTO, |
| 689 |
) |
| 690 |
|
| 691 |
pc["test"] = "abcd" |
| 692 |
assert pc.write(b"data", key="abcd") is True |
| 693 |
assert pc.read("abcd", expires=True) == b"data" |
| 694 |
assert pc.write(b"data2", key="defg") is True |
| 695 |
assert pc.read("defg", expires=False) == b"data2" |
| 696 |
assert pc.write(b"data3", key="hijk") is True |
| 697 |
assert pc.read("hijk", expires=False) == b"data3" |
| 698 |
assert pc["test"] == "abcd" |
| 699 |
|
| 700 |
with mock.patch("os.utime", side_effect=(OSError(), FileNotFoundError())): |
| 701 |
pc.flush() |
| 702 |
|
| 703 |
|
| 704 |
pc = PersistentStore( |
| 705 |
namespace=namespace, path=str(tmpdir), mode=PersistentStoreMode.FLUSH |
| 706 |
) |
| 707 |
|
| 708 |
assert "mykey" not in pc |
| 709 |
pc["mykey"] = 42 |
| 710 |
del pc |
| 711 |
|
| 712 |
pc = PersistentStore( |
| 713 |
namespace=namespace, path=str(tmpdir), mode=PersistentStoreMode.FLUSH |
| 714 |
) |
| 715 |
assert "mykey" in pc |
| 716 |
|
| 717 |
|
| 718 |
del pc["mykey"] |
| 719 |
with ( |
| 720 |
mock.patch("os.rename", side_effect=OSError()), |
| 721 |
mock.patch("os.unlink", side_effect=OSError()), |
| 722 |
): |
| 723 |
assert not pc.flush(force=True) |
| 724 |
|
| 725 |
|
| 726 |
pc["mykey"] = 42 |
| 727 |
with mock.patch("tempfile.NamedTemporaryFile", side_effect=OSError()): |
| 728 |
assert not pc.flush(force=True) |
| 729 |
|
| 730 |
|
| 731 |
with mock.patch( |
| 732 |
"tempfile._TemporaryFileWrapper.close", side_effect=OSError() |
| 733 |
): |
| 734 |
assert not pc.flush(force=True) |
| 735 |
|
| 736 |
|
| 737 |
pc["mykey"] = 43 |
| 738 |
mock_ntf = mock.MagicMock() |
| 739 |
mock_ntf.name = os.path.join(tmpdir, "file") |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
with mock.patch( |
| 745 |
"tempfile.NamedTemporaryFile", |
| 746 |
side_effect=[FileNotFoundError(), FileNotFoundError(), mock_ntf], |
| 747 |
): |
| 748 |
|
| 749 |
assert not pc.flush(force=True, _recovery=True) |
| 750 |
|
| 751 |
with mock.patch( |
| 752 |
"tempfile.NamedTemporaryFile", |
| 753 |
side_effect=[FileNotFoundError(), FileNotFoundError(), mock_ntf], |
| 754 |
): |
| 755 |
|
| 756 |
assert not pc.flush(force=False, _recovery=True) |
| 757 |
|
| 758 |
with mock.patch( |
| 759 |
"tempfile.NamedTemporaryFile", |
| 760 |
side_effect=[FileNotFoundError(), FileNotFoundError(), mock_ntf], |
| 761 |
): |
| 762 |
|
| 763 |
assert not pc.flush(force=False, _recovery=False) |
| 764 |
|
| 765 |
with mock.patch( |
| 766 |
"tempfile.NamedTemporaryFile", |
| 767 |
side_effect=[FileNotFoundError(), FileNotFoundError(), mock_ntf], |
| 768 |
): |
| 769 |
|
| 770 |
assert not pc.flush(force=True, _recovery=False) |
| 771 |
|
| 772 |
with ( |
| 773 |
mock.patch( |
| 774 |
"tempfile._TemporaryFileWrapper.close", |
| 775 |
autospec=True, |
| 776 |
side_effect=_flaky_close(), |
| 777 |
), |
| 778 |
mock.patch("os.unlink", side_effect=(OSError())), |
| 779 |
): |
| 780 |
assert not pc.flush(force=True) |
| 781 |
|
| 782 |
with mock.patch( |
| 783 |
"tempfile._TemporaryFileWrapper.close", |
| 784 |
autospec=True, |
| 785 |
side_effect=_flaky_close(always=True), |
| 786 |
): |
| 787 |
assert not pc.flush(force=True) |
| 788 |
|
| 789 |
with ( |
| 790 |
mock.patch( |
| 791 |
"tempfile._TemporaryFileWrapper.close", |
| 792 |
autospec=True, |
| 793 |
side_effect=_flaky_close(), |
| 794 |
), |
| 795 |
mock.patch("os.unlink", side_effect=OSError()), |
| 796 |
): |
| 797 |
assert not pc.flush(force=True) |
| 798 |
|
| 799 |
with ( |
| 800 |
mock.patch( |
| 801 |
"tempfile._TemporaryFileWrapper.close", |
| 802 |
autospec=True, |
| 803 |
side_effect=_flaky_close(), |
| 804 |
), |
| 805 |
mock.patch("os.unlink", side_effect=FileNotFoundError()), |
| 806 |
): |
| 807 |
assert not pc.flush(force=True) |
| 808 |
|
| 809 |
del pc |
| 810 |
|
| 811 |
|
| 812 |
pc = PersistentStore( |
| 813 |
namespace=namespace, path=str(tmpdir), mode=PersistentStoreMode.FLUSH |
| 814 |
) |
| 815 |
|
| 816 |
|
| 817 |
pc.encoding = "ascii" |
| 818 |
|
| 819 |
|
| 820 |
with mock.patch("os.stat", side_effect=OSError()): |
| 821 |
|
| 822 |
assert pc.write("abcd") is False |
| 823 |
|
| 824 |
here) :) |
| 825 |
data = "ボールト" |
| 826 |
|
| 827 |
|
| 828 |
assert pc.write(data) is False |
| 829 |
|
| 830 |
with mock.patch("gzip.open", side_effect=FileNotFoundError()): |
| 831 |
pc = PersistentStore(namespace=namespace, path=str(tmpdir)) |
| 832 |
|
| 833 |
|
| 834 |
assert pc.write(b"key") is False |
| 835 |
|
| 836 |
with mock.patch("gzip.open", side_effect=OSError()): |
| 837 |
pc = PersistentStore(namespace=namespace, path=str(tmpdir)) |
| 838 |
|
| 839 |
|
| 840 |
assert pc.get("key") is None |
| 841 |
|
| 842 |
pc = PersistentStore(namespace=namespace, path=str(tmpdir)) |
| 843 |
with pytest.raises(KeyError): |
| 844 |
pc["key"] = "value" |
| 845 |
|
| 846 |
pc = PersistentStore(namespace=namespace, path=str(tmpdir)) |
| 847 |
with pytest.raises(KeyError): |
| 848 |
pc["key"] |
| 849 |
|
| 850 |
pc = PersistentStore(namespace=namespace, path=str(tmpdir)) |
| 851 |
with pytest.raises(KeyError): |
| 852 |
del pc["key"] |
| 853 |
|
| 854 |
pc = PersistentStore(namespace=namespace, path=str(tmpdir)) |
| 855 |
|
| 856 |
assert pc.set("key", "value") is False |
| 857 |
|
| 858 |
pc = PersistentStore(namespace=namespace, path=str(tmpdir)) |
| 859 |
|
| 860 |
assert pc.clear() is False |
| 861 |
|
| 862 |
pc = PersistentStore(namespace=namespace, path=str(tmpdir)) |
| 863 |
|
| 864 |
assert pc.prune() is False |
| 865 |
|
| 866 |
|
| 867 |
pc.set( |
| 868 |
"key", |
| 869 |
"value", |
| 870 |
persistent=False, |
| 871 |
expires=datetime.now() - timedelta(days=1), |
| 872 |
) |
| 873 |
pc.set( |
| 874 |
"key2", |
| 875 |
"value2", |
| 876 |
persistent=True, |
| 877 |
expires=datetime.now() - timedelta(days=1), |
| 878 |
) |
| 879 |
|
| 880 |
|
| 881 |
pc.set("key3", "value3", persistent=True) |
| 882 |
pc.set("key4", "value4", persistent=False) |
| 883 |
assert pc.prune() is True |
| 884 |
|
| 885 |
|
| 886 |
assert pc.prune() is False |
| 887 |
|
| 888 |
|
| 889 |
pc.delete() |
| 890 |
|
| 891 |
|
| 892 |
pc = PersistentStore( |
| 893 |
namespace=namespace, path=str(tmpdir), mode=PersistentStoreMode.FLUSH |
| 894 |
) |
| 895 |
|
| 896 |
|
| 897 |
pc.set( |
| 898 |
"key1", |
| 899 |
"value", |
| 900 |
persistent=True, |
| 901 |
expires=datetime.now() + timedelta(seconds=1), |
| 902 |
) |
| 903 |
pc.set( |
| 904 |
"key2", |
| 905 |
"value", |
| 906 |
persistent=True, |
| 907 |
expires=datetime.now() + timedelta(seconds=1), |
| 908 |
) |
| 909 |
pc.set( |
| 910 |
"key3", |
| 911 |
"value", |
| 912 |
persistent=True, |
| 913 |
expires=datetime.now() + timedelta(seconds=1), |
| 914 |
) |
| 915 |
pc.flush() |
| 916 |
|
| 917 |
|
| 918 |
time.sleep(1.3) |
| 919 |
|
| 920 |
|
| 921 |
pc = PersistentStore( |
| 922 |
namespace=namespace, path=str(tmpdir), mode=PersistentStoreMode.FLUSH |
| 923 |
) |
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
assert "key1" not in pc |
| 928 |
assert "key2" not in pc |
| 929 |
assert "key3" not in pc |
| 930 |
|
| 931 |
|
| 932 |
pc.delete() |
| 933 |
pc.set("key", "value") |
| 934 |
pc.set("key2", "value2") |
| 935 |
pc.write("more-content") |
| 936 |
|
| 937 |
pc.flush() |
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
with mock.patch("builtins.sum", side_effect=OSError()): |
| 943 |
assert pc.size(exclude=True, lazy=False) == 0 |
| 944 |
assert pc.size(exclude=False, lazy=False) == 0 |
| 945 |
|
| 946 |
pc = PersistentStore(namespace=namespace, path=str(tmpdir)) |
| 947 |
with mock.patch("glob.glob", side_effect=OSError()): |
| 948 |
assert pc.files(exclude=True, lazy=False) == [] |
| 949 |
assert pc.files(exclude=False, lazy=False) == [] |
| 950 |
|
| 951 |
pc = PersistentStore( |
| 952 |
namespace=namespace, path=str(tmpdir), mode=PersistentStoreMode.FLUSH |
| 953 |
) |
| 954 |
|
| 955 |
|
| 956 |
pc["abc"] = 1 |
| 957 |
with mock.patch("os.unlink", side_effect=OSError()): |
| 958 |
|
| 959 |
with pytest.raises(KeyError): |
| 960 |
pc["new-key"] = "value" |
| 961 |
|
| 962 |
|
| 963 |
pc["abc"] = "value" |
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
pc.clear() |
| 969 |
with mock.patch("os.unlink", side_effect=OSError()): |
| 970 |
|
| 971 |
assert pc.flush(force=True) is False |
| 972 |
|
| 973 |
with mock.patch("os.unlink", side_effect=FileNotFoundError()): |
| 974 |
|
| 975 |
assert pc.flush(force=True) is True |
| 976 |
|
| 977 |
with mock.patch("os.rename", side_effect=OSError()): |
| 978 |
|
| 979 |
assert pc.flush(force=True) is False |
| 980 |
|
| 981 |
with mock.patch("os.rename", side_effect=FileNotFoundError()): |
| 982 |
|
| 983 |
assert pc.flush(force=True) is True |
| 984 |
|
| 985 |
|
| 986 |
pc.delete() |
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
pc.set("abc", "a-test-value") |
| 994 |
with mock.patch("os.unlink", side_effect=(OSError(), None)): |
| 995 |
|
| 996 |
assert pc.flush(force=True) is False |
| 997 |
|
| 998 |
with mock.patch("os.unlink", side_effect=(OSError(), FileNotFoundError())): |
| 999 |
|
| 1000 |
assert pc.flush(force=True) is False |
| 1001 |
|
| 1002 |
with mock.patch("os.unlink", side_effect=(OSError(), OSError())): |
| 1003 |
|
| 1004 |
assert pc.flush(force=True) is False |
| 1005 |
|
| 1006 |
|
| 1007 |
def test_persistent_custom_io(tmpdir): |
| 1008 |
"""Test reading and writing custom files.""" |
| 1009 |
|
| 1010 |
|
| 1011 |
pc = PersistentStore(path=str(tmpdir)) |
| 1012 |
|
| 1013 |
with pytest.raises(AttributeError): |
| 1014 |
pc.open("!invalid#-Key") |
| 1015 |
|
| 1016 |
|
| 1017 |
with pytest.raises(FileNotFoundError): |
| 1018 |
pc.open("valid-key") |
| 1019 |
|
| 1020 |
with pytest.raises(AttributeError): |
| 1021 |
|
| 1022 |
pc.open(1234) |
| 1023 |
|
| 1024 |
with pytest.raises(FileNotFoundError), pc.open("key") as fd: |
| 1025 |
pass |
| 1026 |
|
| 1027 |
|
| 1028 |
with pytest.raises(exception.AppriseFileNotFound), pc.open("key") as fd: |
| 1029 |
pass |
| 1030 |
|
| 1031 |
|
| 1032 |
with pc.open("new-key", "wb") as fd: |
| 1033 |
fd.write(b"data") |
| 1034 |
|
| 1035 |
with mock.patch( |
| 1036 |
"builtins.open", |
| 1037 |
new_callable=mock.mock_open, |
| 1038 |
read_data="mocked file content", |
| 1039 |
) as mock_file: |
| 1040 |
mock_file.side_effect = OSError |
| 1041 |
with ( |
| 1042 |
pytest.raises(exception.AppriseDiskIOError), |
| 1043 |
pc.open("new-key", compress=True) as fd, |
| 1044 |
): |
| 1045 |
pass |
| 1046 |
|
| 1047 |
|
| 1048 |
with mock.patch( |
| 1049 |
"gzip.open", |
| 1050 |
new_callable=mock.mock_open, |
| 1051 |
read_data="mocked file content", |
| 1052 |
) as mock_file: |
| 1053 |
mock_file.side_effect = OSError |
| 1054 |
with ( |
| 1055 |
pytest.raises(exception.AppriseDiskIOError), |
| 1056 |
pc.open("new-key", compress=True) as fd, |
| 1057 |
): |
| 1058 |
pass |
| 1059 |
|
| 1060 |
|
| 1061 |
with mock.patch( |
| 1062 |
"gzip.open", |
| 1063 |
new_callable=mock.mock_open, |
| 1064 |
read_data="mocked file content", |
| 1065 |
) as mock_file: |
| 1066 |
mock_file.side_effect = zlib.error |
| 1067 |
with ( |
| 1068 |
pytest.raises(exception.AppriseDiskIOError), |
| 1069 |
pc.open("new-key", compress=True) as fd, |
| 1070 |
): |
| 1071 |
pass |
| 1072 |
|
| 1073 |
|
| 1074 |
with pytest.raises(AttributeError): |
| 1075 |
pc.write(1234) |
| 1076 |
|
| 1077 |
with pytest.raises(AttributeError): |
| 1078 |
pc.write(None) |
| 1079 |
|
| 1080 |
with pytest.raises(AttributeError): |
| 1081 |
pc.write(True) |
| 1082 |
|
| 1083 |
pc = PersistentStore(str(tmpdir)) |
| 1084 |
with pc.open("key", "wb") as fd: |
| 1085 |
fd.write(b"test") |
| 1086 |
fd.close() |
| 1087 |
|
| 1088 |
|
| 1089 |
with mock.patch( |
| 1090 |
"gzip.open", |
| 1091 |
new_callable=mock.mock_open, |
| 1092 |
read_data="mocked file content", |
| 1093 |
) as mock_file: |
| 1094 |
mock_file.side_effect = zlib.error |
| 1095 |
|
| 1096 |
|
| 1097 |
assert pc.write(b"test") is False |
| 1098 |
|
| 1099 |
|
| 1100 |
mock_file.side_effect = OSError |
| 1101 |
assert pc.write(b"test") is False |
| 1102 |
|
| 1103 |
with pytest.raises(AttributeError): |
| 1104 |
pc.write(b"data", key="!invalid#-Key") |
| 1105 |
|
| 1106 |
pc.delete() |
| 1107 |
with mock.patch("os.unlink", side_effect=OSError()): |
| 1108 |
|
| 1109 |
assert pc.write(b"test") is False |
| 1110 |
|
| 1111 |
pc.delete() |
| 1112 |
with mock.patch("os.rename", side_effect=OSError()): |
| 1113 |
|
| 1114 |
assert pc.write(b"test") is False |
| 1115 |
|
| 1116 |
pc.delete() |
| 1117 |
with mock.patch("os.unlink", side_effect=(OSError(), FileNotFoundError())): |
| 1118 |
|
| 1119 |
assert pc.write(b"test") is False |
| 1120 |
|
| 1121 |
pc.delete() |
| 1122 |
with mock.patch("os.unlink", side_effect=(OSError(), None)): |
| 1123 |
|
| 1124 |
assert pc.write(b"test") is False |
| 1125 |
|
| 1126 |
pc.delete() |
| 1127 |
with mock.patch("os.unlink", side_effect=(OSError(), OSError())): |
| 1128 |
|
| 1129 |
assert pc.write(b"test") is False |
| 1130 |
|
| 1131 |
pc.delete() |
| 1132 |
with mock.patch("os.rename", side_effect=(None, OSError(), None)): |
| 1133 |
assert pc.write(b"test") is False |
| 1134 |
|
| 1135 |
with mock.patch("os.rename", side_effect=(None, OSError(), OSError())): |
| 1136 |
assert pc.write(b"test") is False |
| 1137 |
|
| 1138 |
with mock.patch( |
| 1139 |
"os.rename", side_effect=(None, OSError(), FileNotFoundError()) |
| 1140 |
): |
| 1141 |
assert pc.write(b"test") is False |
| 1142 |
|
| 1143 |
pc.delete() |
| 1144 |
with mock.patch("os.rename", side_effect=(None, None, None, OSError())): |
| 1145 |
|
| 1146 |
assert pc.write(b"test") is True |
| 1147 |
|
| 1148 |
with ( |
| 1149 |
mock.patch("os.stat", side_effect=OSError()), |
| 1150 |
mock.patch("os.close", side_effect=(None, OSError())), |
| 1151 |
): |
| 1152 |
assert pc.write(b"test") is False |
| 1153 |
|
| 1154 |
pc.delete() |
| 1155 |
with mock.patch( |
| 1156 |
"tempfile._TemporaryFileWrapper.close", |
| 1157 |
autospec=True, |
| 1158 |
side_effect=_flaky_close(always=True), |
| 1159 |
): |
| 1160 |
assert pc.write(b"test") is False |
| 1161 |
|
| 1162 |
pc.delete() |
| 1163 |
with ( |
| 1164 |
mock.patch( |
| 1165 |
"tempfile._TemporaryFileWrapper.close", |
| 1166 |
autospec=True, |
| 1167 |
side_effect=_flaky_close(), |
| 1168 |
), |
| 1169 |
mock.patch("os.unlink", side_effect=OSError()), |
| 1170 |
): |
| 1171 |
assert pc.write(b"test") is False |
| 1172 |
|
| 1173 |
pc.delete() |
| 1174 |
with ( |
| 1175 |
mock.patch( |
| 1176 |
"tempfile._TemporaryFileWrapper.close", |
| 1177 |
autospec=True, |
| 1178 |
side_effect=_flaky_close(), |
| 1179 |
), |
| 1180 |
mock.patch("os.unlink", side_effect=FileNotFoundError()), |
| 1181 |
): |
| 1182 |
assert pc.write(b"test") is False |
| 1183 |
|
| 1184 |
|
| 1185 |
def test_persistent_storage_cache_object(tmpdir): |
| 1186 |
"""General testing of a CacheObject.""" |
| 1187 |
|
| 1188 |
c = CacheObject(123) |
| 1189 |
|
| 1190 |
ref = datetime.now(tz=timezone.utc) |
| 1191 |
expires = ref + timedelta(days=1) |
| 1192 |
|
| 1193 |
c = CacheObject("abcd", expires=expires) |
| 1194 |
assert c.expires == expires |
| 1195 |
assert c.expires_sec > 86390.0 and c.expires_sec <= 86400.0 |
| 1196 |
assert bool(c) is True |
| 1197 |
assert "never" not in str(c) |
| 1198 |
assert "str:+:abcd" in str(c) |
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
c.set(123) |
| 1204 |
assert "never" not in str(c) |
| 1205 |
assert "int:+:123" in str(c) |
| 1206 |
hash_value = c.hash() |
| 1207 |
assert isinstance(hash_value, str) |
| 1208 |
|
| 1209 |
c.set(124) |
| 1210 |
assert "never" not in str(c) |
| 1211 |
assert "int:+:124" in str(c) |
| 1212 |
assert c.hash() != hash_value |
| 1213 |
|
| 1214 |
c.set(123) |
| 1215 |
|
| 1216 |
assert c.hash() == hash_value |
| 1217 |
|
| 1218 |
c.set(124) |
| 1219 |
assert isinstance(c.hash(), str) |
| 1220 |
assert c.value == 124 |
| 1221 |
assert bool(c) is True |
| 1222 |
c.set(124, expires=False, persistent=False) |
| 1223 |
assert bool(c) is True |
| 1224 |
assert c.expires is None |
| 1225 |
assert c.expires_sec is None |
| 1226 |
c.set(124, expires=True) |
| 1227 |
|
| 1228 |
assert bool(c) is False |
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
a = CacheObject("abc") |
| 1234 |
b = CacheObject("abc") |
| 1235 |
|
| 1236 |
assert a == b |
| 1237 |
assert a == "abc" |
| 1238 |
assert b == "abc" |
| 1239 |
|
| 1240 |
|
| 1241 |
b = CacheObject("abc", 30) |
| 1242 |
assert a != b |
| 1243 |
|
| 1244 |
assert a == b.value |
| 1245 |
|
| 1246 |
b = CacheObject("abc", persistent=False) |
| 1247 |
a = CacheObject("abc", persistent=True) |
| 1248 |
|
| 1249 |
assert a != b |
| 1250 |
|
| 1251 |
assert a == b.value |
| 1252 |
b = CacheObject("abc", persistent=True) |
| 1253 |
assert a == b |
| 1254 |
|
| 1255 |
|
| 1256 |
EPOCH = datetime(1970, 1, 1) |
| 1257 |
|
| 1258 |
|
| 1259 |
for entry in ( |
| 1260 |
"string", |
| 1261 |
123, |
| 1262 |
1.2222, |
| 1263 |
datetime.now(), |
| 1264 |
datetime.now(tz=timezone.utc), |
| 1265 |
None, |
| 1266 |
False, |
| 1267 |
True, |
| 1268 |
b"\0", |
| 1269 |
): |
| 1270 |
|
| 1271 |
c = CacheObject(entry, datetime.now() + timedelta(days=1)) |
| 1272 |
|
| 1273 |
|
| 1274 |
assert c |
| 1275 |
|
| 1276 |
|
| 1277 |
result = json.loads( |
| 1278 |
json.dumps(c, separators=(",", ":"), cls=CacheJSONEncoder) |
| 1279 |
) |
| 1280 |
|
| 1281 |
|
| 1282 |
cc = CacheObject.instantiate(result) |
| 1283 |
assert cc.json() == c.json() |
| 1284 |
|
| 1285 |
|
| 1286 |
with pytest.raises(TypeError): |
| 1287 |
json.loads( |
| 1288 |
json.dumps(object(), separators=(",", ":"), cls=CacheJSONEncoder) |
| 1289 |
) |
| 1290 |
|
| 1291 |
assert CacheObject.instantiate(None) is None |
| 1292 |
assert CacheObject.instantiate({}) is None |
| 1293 |
|
| 1294 |
|
| 1295 |
assert ( |
| 1296 |
CacheObject.instantiate({"v": 123, "x": datetime.now(), "c": "int"}) |
| 1297 |
is None |
| 1298 |
) |
| 1299 |
|
| 1300 |
|
| 1301 |
assert ( |
| 1302 |
CacheObject.instantiate( |
| 1303 |
{ |
| 1304 |
"v": 123, |
| 1305 |
"x": (datetime.now() - EPOCH).total_seconds(), |
| 1306 |
"c": object, |
| 1307 |
} |
| 1308 |
) |
| 1309 |
is None |
| 1310 |
) |
| 1311 |
|
| 1312 |
obj = CacheObject.instantiate( |
| 1313 |
{"v": 123, "x": (datetime.now() - EPOCH).total_seconds(), "c": "int"}, |
| 1314 |
verify=False, |
| 1315 |
) |
| 1316 |
assert isinstance(obj, CacheObject) |
| 1317 |
assert obj.value == 123 |
| 1318 |
|
| 1319 |
|
| 1320 |
assert ( |
| 1321 |
CacheObject.instantiate( |
| 1322 |
{ |
| 1323 |
"v": 123, |
| 1324 |
"x": (datetime.now() - EPOCH).total_seconds(), |
| 1325 |
"c": "int", |
| 1326 |
}, |
| 1327 |
verify=True, |
| 1328 |
) |
| 1329 |
is None |
| 1330 |
) |
| 1331 |
|
| 1332 |
|
| 1333 |
assert ( |
| 1334 |
CacheObject.instantiate( |
| 1335 |
{"v": 123, "x": "garbage", "c": "int"}, verify=False |
| 1336 |
) |
| 1337 |
is None |
| 1338 |
) |
| 1339 |
|
| 1340 |
|
| 1341 |
assert ( |
| 1342 |
CacheObject.instantiate( |
| 1343 |
{ |
| 1344 |
"v": 123, |
| 1345 |
"x": (datetime.now() - EPOCH).total_seconds(), |
| 1346 |
"c": "int", |
| 1347 |
|
| 1348 |
"!": 1.0, |
| 1349 |
}, |
| 1350 |
verify=False, |
| 1351 |
) |
| 1352 |
is None |
| 1353 |
) |
| 1354 |
|
| 1355 |
|
| 1356 |
assert ( |
| 1357 |
CacheObject.instantiate( |
| 1358 |
{ |
| 1359 |
"v": "garbage", |
| 1360 |
"x": (datetime.now() - EPOCH).total_seconds(), |
| 1361 |
"c": "bytes", |
| 1362 |
}, |
| 1363 |
verify=False, |
| 1364 |
) |
| 1365 |
is None |
| 1366 |
) |
| 1367 |
|
| 1368 |
obj = CacheObject.instantiate( |
| 1369 |
{ |
| 1370 |
"v": "AA==", |
| 1371 |
"x": (datetime.now() - EPOCH).total_seconds(), |
| 1372 |
"c": "bytes", |
| 1373 |
}, |
| 1374 |
verify=False, |
| 1375 |
) |
| 1376 |
assert isinstance(obj, CacheObject) |
| 1377 |
assert obj.value == b"\0" |
| 1378 |
|
| 1379 |
|
| 1380 |
obj = CacheObject.instantiate( |
| 1381 |
{ |
| 1382 |
"v": "2024-06-08T01:50:01.587267", |
| 1383 |
"x": (datetime.now() - EPOCH).total_seconds(), |
| 1384 |
"c": "datetime", |
| 1385 |
}, |
| 1386 |
verify=False, |
| 1387 |
) |
| 1388 |
assert isinstance(obj, CacheObject) |
| 1389 |
assert obj.value == datetime(2024, 6, 8, 1, 50, 1, 587267) |
| 1390 |
|
| 1391 |
|
| 1392 |
assert ( |
| 1393 |
CacheObject.instantiate( |
| 1394 |
{ |
| 1395 |
"v": "garbage", |
| 1396 |
"x": (datetime.now() - EPOCH).total_seconds(), |
| 1397 |
"c": "datetime", |
| 1398 |
}, |
| 1399 |
verify=False, |
| 1400 |
) |
| 1401 |
is None |
| 1402 |
) |
| 1403 |
|
| 1404 |
|
| 1405 |
@pytest.mark.skipif( |
| 1406 |
sys.platform == "win32", reason="Unreliable results to be determined" |
| 1407 |
) |
| 1408 |
def test_persistent_storage_disk_prune(tmpdir): |
| 1409 |
"""General testing of a Persistent Store prune calls.""" |
| 1410 |
|
| 1411 |
|
| 1412 |
pc = PersistentStore( |
| 1413 |
path=str(tmpdir), namespace="t01", mode=PersistentStoreMode.FLUSH |
| 1414 |
) |
| 1415 |
|
| 1416 |
assert pc.write(b"data-t01") is True |
| 1417 |
assert pc.set("key-t01", "value") |
| 1418 |
|
| 1419 |
pc = PersistentStore( |
| 1420 |
path=str(tmpdir), namespace="t02", mode=PersistentStoreMode.FLUSH |
| 1421 |
) |
| 1422 |
|
| 1423 |
assert pc.write(b"data-t02") is True |
| 1424 |
assert pc.set("key-t02", "value") |
| 1425 |
|
| 1426 |
|
| 1427 |
results = PersistentStore.disk_prune(path=str(tmpdir), expires=30) |
| 1428 |
|
| 1429 |
assert isinstance(results, dict) |
| 1430 |
assert "t01" in results |
| 1431 |
assert "t02" in results |
| 1432 |
assert len(results["t01"]) == 0 |
| 1433 |
assert len(results["t02"]) == 0 |
| 1434 |
|
| 1435 |
pc = PersistentStore( |
| 1436 |
path=str(tmpdir), namespace="t01", mode=PersistentStoreMode.FLUSH |
| 1437 |
) |
| 1438 |
|
| 1439 |
|
| 1440 |
assert pc.get("key-t01") == "value" |
| 1441 |
assert pc.read() == b"data-t01" |
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
results = PersistentStore.disk_prune(path=str(tmpdir), expires=0) |
| 1447 |
|
| 1448 |
assert isinstance(results, dict) |
| 1449 |
assert "t01" in results |
| 1450 |
assert "t02" in results |
| 1451 |
assert len(results["t01"]) == 2 |
| 1452 |
assert len(results["t02"]) == 2 |
| 1453 |
|
| 1454 |
|
| 1455 |
pc = PersistentStore( |
| 1456 |
path=str(tmpdir), namespace="t02", mode=PersistentStoreMode.FLUSH |
| 1457 |
) |
| 1458 |
|
| 1459 |
assert pc.get("key-t02") == "value" |
| 1460 |
assert pc.read() == b"data-t02" |
| 1461 |
pc = PersistentStore( |
| 1462 |
path=str(tmpdir), namespace="t01", mode=PersistentStoreMode.FLUSH |
| 1463 |
) |
| 1464 |
|
| 1465 |
assert pc.get("key-t01") == "value" |
| 1466 |
assert pc.read() == b"data-t01" |
| 1467 |
|
| 1468 |
with mock.patch("os.listdir", side_effect=OSError()): |
| 1469 |
results = PersistentStore.disk_scan( |
| 1470 |
namespace="t01", path=str(tmpdir), closest=True |
| 1471 |
) |
| 1472 |
assert isinstance(results, list) |
| 1473 |
assert len(results) == 0 |
| 1474 |
|
| 1475 |
with mock.patch("os.listdir", side_effect=FileNotFoundError()): |
| 1476 |
results = PersistentStore.disk_scan( |
| 1477 |
namespace="t01", path=str(tmpdir), closest=True |
| 1478 |
) |
| 1479 |
assert isinstance(results, list) |
| 1480 |
assert len(results) == 0 |
| 1481 |
|
| 1482 |
|
| 1483 |
results = PersistentStore.disk_scan( |
| 1484 |
namespace="t01", path=str(tmpdir), closest=False |
| 1485 |
) |
| 1486 |
assert isinstance(results, list) |
| 1487 |
assert len(results) == 0 |
| 1488 |
|
| 1489 |
|
| 1490 |
results = PersistentStore.disk_prune( |
| 1491 |
namespace="notfound", path=str(tmpdir), expires=0, action=True |
| 1492 |
) |
| 1493 |
|
| 1494 |
|
| 1495 |
assert isinstance(results, dict) |
| 1496 |
assert len(results) == 0 |
| 1497 |
|
| 1498 |
results = PersistentStore.disk_prune( |
| 1499 |
namespace=("t01", "invalid", "-garbag!"), |
| 1500 |
path=str(tmpdir), |
| 1501 |
expires=0, |
| 1502 |
action=True, |
| 1503 |
) |
| 1504 |
|
| 1505 |
|
| 1506 |
assert isinstance(results, dict) |
| 1507 |
assert len(results) == 1 |
| 1508 |
assert len(results["t01"]) == 2 |
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
results = PersistentStore.disk_prune( |
| 1513 |
namespace="t01", path=str(tmpdir), expires=0, action=True |
| 1514 |
) |
| 1515 |
assert isinstance(results, dict) |
| 1516 |
assert len(results) == 0 |
| 1517 |
|
| 1518 |
|
| 1519 |
pc = PersistentStore( |
| 1520 |
path=str(tmpdir), namespace="t02", mode=PersistentStoreMode.FLUSH |
| 1521 |
) |
| 1522 |
|
| 1523 |
assert pc.get("key-t02") == "value" |
| 1524 |
assert pc.read() == b"data-t02" |
| 1525 |
|
| 1526 |
|
| 1527 |
pc = PersistentStore( |
| 1528 |
path=str(tmpdir), namespace="t01", mode=PersistentStoreMode.FLUSH |
| 1529 |
) |
| 1530 |
|
| 1531 |
assert pc.get("key-t01") is None |
| 1532 |
assert pc.read() is None |
| 1533 |
|
| 1534 |
with pytest.raises(AttributeError): |
| 1535 |
|
| 1536 |
PersistentStore.disk_prune( |
| 1537 |
namespace=object, path=str(tmpdir), expires=0, action=True |
| 1538 |
) |
| 1539 |
|
| 1540 |
|
| 1541 |
with mock.patch("os.path.getmtime", side_effect=FileNotFoundError()): |
| 1542 |
results = PersistentStore.disk_prune( |
| 1543 |
namespace="t02", path=str(tmpdir), expires=0, action=True |
| 1544 |
) |
| 1545 |
assert isinstance(results, dict) |
| 1546 |
assert len(results) == 1 |
| 1547 |
assert len(results["t02"]) == 0 |
| 1548 |
|
| 1549 |
|
| 1550 |
pc = PersistentStore( |
| 1551 |
path=str(tmpdir), namespace="t02", mode=PersistentStoreMode.FLUSH |
| 1552 |
) |
| 1553 |
|
| 1554 |
assert pc.get("key-t02") == "value" |
| 1555 |
assert pc.read() == b"data-t02" |
| 1556 |
|
| 1557 |
with mock.patch("os.path.getmtime", side_effect=OSError()): |
| 1558 |
results = PersistentStore.disk_prune( |
| 1559 |
namespace="t02", path=str(tmpdir), expires=0, action=True |
| 1560 |
) |
| 1561 |
assert isinstance(results, dict) |
| 1562 |
assert len(results) == 1 |
| 1563 |
assert len(results["t02"]) == 0 |
| 1564 |
|
| 1565 |
|
| 1566 |
pc = PersistentStore( |
| 1567 |
path=str(tmpdir), namespace="t02", mode=PersistentStoreMode.FLUSH |
| 1568 |
) |
| 1569 |
|
| 1570 |
assert pc.get("key-t02") == "value" |
| 1571 |
assert pc.read() == b"data-t02" |
| 1572 |
|
| 1573 |
with mock.patch("os.unlink", side_effect=FileNotFoundError()): |
| 1574 |
results = PersistentStore.disk_prune( |
| 1575 |
namespace="t02", path=str(tmpdir), expires=0, action=True |
| 1576 |
) |
| 1577 |
assert isinstance(results, dict) |
| 1578 |
assert len(results) == 1 |
| 1579 |
assert len(results["t02"]) == 2 |
| 1580 |
|
| 1581 |
|
| 1582 |
pc = PersistentStore( |
| 1583 |
path=str(tmpdir), namespace="t02", mode=PersistentStoreMode.FLUSH |
| 1584 |
) |
| 1585 |
|
| 1586 |
assert pc.get("key-t02") == "value" |
| 1587 |
assert pc.read() == b"data-t02" |
| 1588 |
|
| 1589 |
with mock.patch("os.unlink", side_effect=OSError()): |
| 1590 |
results = PersistentStore.disk_prune( |
| 1591 |
namespace="t02", path=str(tmpdir), expires=0, action=True |
| 1592 |
) |
| 1593 |
assert isinstance(results, dict) |
| 1594 |
assert len(results) == 1 |
| 1595 |
assert len(results["t02"]) == 2 |
| 1596 |
|
| 1597 |
|
| 1598 |
pc = PersistentStore( |
| 1599 |
path=str(tmpdir), namespace="t02", mode=PersistentStoreMode.FLUSH |
| 1600 |
) |
| 1601 |
|
| 1602 |
assert pc.get("key-t02") == "value" |
| 1603 |
assert pc.read() == b"data-t02" |
| 1604 |
|
| 1605 |
with mock.patch("os.rmdir", side_effect=OSError()): |
| 1606 |
results = PersistentStore.disk_prune( |
| 1607 |
namespace="t02", path=str(tmpdir), expires=0, action=True |
| 1608 |
) |
| 1609 |
assert isinstance(results, dict) |
| 1610 |
assert len(results) == 1 |
| 1611 |
assert len(results["t02"]) == 2 |
| 1612 |
|
| 1613 |
|
| 1614 |
pc = PersistentStore( |
| 1615 |
path=str(tmpdir), namespace="t02", mode=PersistentStoreMode.FLUSH |
| 1616 |
) |
| 1617 |
|
| 1618 |
assert pc.get("key-t02") is None |
| 1619 |
assert pc.read() is None |
| 1620 |
|
| 1621 |
|
| 1622 |
def test_persistent_storage_disk_changes(tmpdir): |
| 1623 |
"""General testing of a Persistent Store with underlining disk changes.""" |
| 1624 |
|
| 1625 |
|
| 1626 |
tmpdir.join("t01").write("0" * 1024) |
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
pc = PersistentStore( |
| 1631 |
path=str(tmpdir), namespace="t01", mode=PersistentStoreMode.FLUSH |
| 1632 |
) |
| 1633 |
|
| 1634 |
|
| 1635 |
assert pc.write(b"data-t01") is False |
| 1636 |
|
| 1637 |
assert pc.mode == PersistentStoreMode.MEMORY |
| 1638 |
|
| 1639 |
|
| 1640 |
assert pc.set("key-t01", "value") |
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
pc = PersistentStore( |
| 1645 |
path=str(tmpdir), namespace="t01", mode=PersistentStoreMode.FLUSH |
| 1646 |
) |
| 1647 |
|
| 1648 |
assert pc.get("key-t01") is None |
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
pc = PersistentStore( |
| 1654 |
path=str(tmpdir), namespace="t02", mode=PersistentStoreMode.FLUSH |
| 1655 |
) |
| 1656 |
|
| 1657 |
assert pc.mode == PersistentStoreMode.FLUSH |
| 1658 |
assert os.path.isdir(pc.path) |
| 1659 |
|
| 1660 |
shutil.rmtree(pc.path) |
| 1661 |
assert not os.path.isdir(pc.path) |
| 1662 |
assert pc.set("key-t02", "value") |
| 1663 |
|
| 1664 |
assert os.path.isdir(pc.path) |
| 1665 |
|
| 1666 |
|
| 1667 |
pc = PersistentStore( |
| 1668 |
path=str(tmpdir), namespace="t02", mode=PersistentStoreMode.AUTO |
| 1669 |
) |
| 1670 |
|
| 1671 |
assert pc.mode == PersistentStoreMode.AUTO |
| 1672 |
assert os.path.isdir(pc.path) |
| 1673 |
|
| 1674 |
shutil.rmtree(pc.path) |
| 1675 |
assert not os.path.isdir(pc.path) |
| 1676 |
assert pc.set("key-t02", "value") |
| 1677 |
|
| 1678 |
assert not os.path.isdir(pc.path) |
| 1679 |
path = pc.path |
| 1680 |
del pc |
| 1681 |
|
| 1682 |
assert os.path.isdir(path) |
| 1683 |
|
| 1684 |
pc = PersistentStore( |
| 1685 |
path=str(tmpdir), namespace="t02", mode=PersistentStoreMode.FLUSH |
| 1686 |
) |
| 1687 |
|
| 1688 |
assert pc.get("key-t02") == "value" |
| 1689 |
|
| 1690 |
|
| 1691 |
shutil.rmtree(os.path.join(pc.path, pc.temp_dir)) |
| 1692 |
|
| 1693 |
|
| 1694 |
assert pc.write(b"data-t02") is True |
| 1695 |
assert os.path.isdir(pc.path) |
| 1696 |
|
| 1697 |
shutil.rmtree(pc.path) |
| 1698 |
assert not os.path.isdir(pc.path) |
| 1699 |
assert pc.set("key-t01", "value") |