1
0
Fork 0
mirror of https://github.com/dragonflydb/dragonfly.git synced 2024-12-14 11:58:02 +00:00
dragonflydb-dragonfly/tests/dragonfly/snapshot_test.py

85 lines
2.8 KiB
Python
Raw Normal View History

import time
import pytest
import os
import glob
from pathlib import Path
from . import dfly_args
2023-01-09 20:31:15 +00:00
from .utility import DflySeeder, wait_available_async
BASIC_ARGS = {"dir": "{DRAGONFLY_TMP}/"}
2023-01-09 20:31:15 +00:00
SEEDER_ARGS = dict(keys=12_000, dbcount=5, multi_transaction_probability=0)
class SnapshotTestBase:
def setup(self, tmp_dir: Path):
self.tmp_dir = tmp_dir
def get_main_file(self, suffix):
def is_main(f): return "summary" in f if suffix == "dfs" else True
files = glob.glob(str(self.tmp_dir.absolute()) + '/test-*.'+suffix)
return next(f for f in sorted(files) if is_main(f))
2023-01-09 20:31:15 +00:00
@dfly_args({**BASIC_ARGS, "dbfilename": "test-rdb"})
class TestRdbSnapshot(SnapshotTestBase):
"""Test single file rdb snapshot"""
@pytest.fixture(autouse=True)
def setup(self, tmp_dir: Path):
super().setup(tmp_dir)
2023-01-09 20:31:15 +00:00
@pytest.mark.asyncio
async def test_snapshot(self, df_seeder_factory, async_client, df_server):
seeder = df_seeder_factory.create(port=df_server.port, **SEEDER_ARGS)
await seeder.run(target_deviation=0.1)
start_capture = await seeder.capture()
# save + flush + load
2023-02-15 08:15:38 +00:00
await async_client.execute_command("SAVE RDB")
2023-01-09 20:31:15 +00:00
assert await async_client.flushall()
await async_client.execute_command("DEBUG LOAD " + super().get_main_file("rdb"))
2023-01-09 20:31:15 +00:00
assert await seeder.compare(start_capture)
2023-01-09 20:31:15 +00:00
@dfly_args({**BASIC_ARGS, "dbfilename": "test-dfs"})
class TestDflySnapshot(SnapshotTestBase):
"""Test multi file snapshot"""
@pytest.fixture(autouse=True)
def setup(self, tmp_dir: Path):
self.tmp_dir = tmp_dir
2023-01-09 20:31:15 +00:00
@pytest.mark.asyncio
async def test_snapshot(self, df_seeder_factory, async_client, df_server):
seeder = df_seeder_factory.create(port=df_server.port, **SEEDER_ARGS)
await seeder.run(target_deviation=0.1)
start_capture = await seeder.capture()
# save + flush + load
2023-01-09 20:31:15 +00:00
await async_client.execute_command("SAVE DF")
assert await async_client.flushall()
await async_client.execute_command("DEBUG LOAD " + super().get_main_file("dfs"))
2023-01-09 20:31:15 +00:00
assert await seeder.compare(start_capture)
@dfly_args({**BASIC_ARGS, "dbfilename": "test-periodic.dfs", "save_schedule": "*:*"})
class TestPeriodicSnapshot(SnapshotTestBase):
"""Test periodic snapshotting"""
@pytest.fixture(autouse=True)
def setup(self, tmp_dir: Path):
super().setup(tmp_dir)
2023-01-09 20:31:15 +00:00
@pytest.mark.asyncio
async def test_snapshot(self, df_seeder_factory, df_server):
seeder = df_seeder_factory.create(port=df_server.port, keys=10, multi_transaction_probability=0)
2023-01-09 20:31:15 +00:00
await seeder.run(target_deviation=0.5)
time.sleep(60)
files = [f for f in os.listdir(self.tmp_dir) if f.startswith('test-periodic')]
assert len(files) > 0 and any(f.endswith('summary.dfs') for f in files)