Skip to content

Commit

Permalink
Removes start/stop
Browse files Browse the repository at this point in the history
  • Loading branch information
hbcarlos committed Oct 10, 2023
1 parent 50e03b4 commit dd72571
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 103 deletions.
10 changes: 0 additions & 10 deletions tests/test_file_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ async def _inner(path: str, doc_path: str, version: int, data: bytes | None = No
async def test_initialization(tmp_path):
path = tmp_path / "tmp"
store = FileYStore(str(path))
await store.start()
await store.initialize()

assert store.initialized
Expand All @@ -69,7 +68,6 @@ async def test_initialization_with_old_store(tmp_path, create_store):
await create_store(path, 1)

store = FileYStore(str(path))
await store.start()
await store.initialize()

assert store.initialized
Expand All @@ -90,7 +88,6 @@ async def test_initialization_with_existing_store(tmp_path, create_store, add_do
await add_document(path, doc_path, 0)

store = FileYStore(str(path))
await store.start()
await store.initialize()

assert store.initialized
Expand All @@ -114,7 +111,6 @@ async def test_exists(tmp_path, create_store, add_document):
await add_document(path, doc_path, 0)

store = FileYStore(str(path))
await store.start()
await store.initialize()

assert store.initialized
Expand All @@ -136,7 +132,6 @@ async def test_list(tmp_path, create_store, add_document):
await add_document(path, doc2, 0)

store = FileYStore(str(path))
await store.start()
await store.initialize()

assert store.initialized
Expand All @@ -159,7 +154,6 @@ async def test_get(tmp_path, create_store, add_document):
await add_document(path, doc_path, 0)

store = FileYStore(str(path))
await store.start()
await store.initialize()

assert store.initialized
Expand All @@ -182,7 +176,6 @@ async def test_create(tmp_path, create_store, add_document):
await add_document(path, doc_path, 0)

store = FileYStore(str(path))
await store.start()
await store.initialize()

assert store.initialized
Expand Down Expand Up @@ -213,7 +206,6 @@ async def test_remove(tmp_path, create_store, add_document):
await add_document(path, doc_path, 0)

store = FileYStore(str(path))
await store.start()
await store.initialize()

assert store.initialized
Expand Down Expand Up @@ -241,7 +233,6 @@ async def test_read(tmp_path, create_store, add_document):
await add_document(path, doc_path, 0, update)

store = FileYStore(str(path))
await store.start()
await store.initialize()

assert store.initialized
Expand All @@ -264,7 +255,6 @@ async def test_write(tmp_path, create_store, add_document):
await add_document(path, doc_path, 0)

store = FileYStore(str(path))
await store.start()
await store.initialize()

assert store.initialized
Expand Down
21 changes: 7 additions & 14 deletions tests/test_sqlite_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ async def _inner(path: str, doc_path: str, version: int, data: bytes | None = No
async def test_initialization(tmp_path):
path = tmp_path / "tmp.db"
store = SQLiteYStore(str(path))
await store.start()
await store.initialize()

assert store.initialized
Expand All @@ -66,7 +65,6 @@ async def test_initialization_with_old_database(tmp_path, create_database):
await create_database(path, 1)

store = SQLiteYStore(str(path))
await store.start()
await store.initialize()

assert store.initialized
Expand All @@ -82,7 +80,6 @@ async def test_initialization_with_empty_database(tmp_path, create_database):
await create_database(path, SQLiteYStore.version, False)

store = SQLiteYStore(str(path))
await store.start()
await store.initialize()

assert store.initialized
Expand All @@ -100,7 +97,6 @@ async def test_initialization_with_existing_database(tmp_path, create_database,
await add_document(path, doc_path, 0)

store = SQLiteYStore(str(path))
await store.start()
await store.initialize()

assert store.initialized
Expand All @@ -118,7 +114,6 @@ async def test_exists(tmp_path, create_database, add_document):
await add_document(path, doc_path, 0)

store = SQLiteYStore(str(path))
await store.start()
await store.initialize()

assert store.initialized
Expand All @@ -140,7 +135,6 @@ async def test_list(tmp_path, create_database, add_document):
await add_document(path, doc2, 0)

store = SQLiteYStore(str(path))
await store.start()
await store.initialize()

assert store.initialized
Expand All @@ -163,7 +157,6 @@ async def test_get(tmp_path, create_database, add_document):
await add_document(path, doc_path, 0)

store = SQLiteYStore(str(path))
await store.start()
await store.initialize()

assert store.initialized
Expand All @@ -186,7 +179,6 @@ async def test_create(tmp_path, create_database, add_document):
await add_document(path, doc_path, 0)

store = SQLiteYStore(str(path))
await store.start()
await store.initialize()

assert store.initialized
Expand Down Expand Up @@ -217,7 +209,6 @@ async def test_remove(tmp_path, create_database, add_document):
await add_document(path, doc_path, 0)

store = SQLiteYStore(str(path))
await store.start()
await store.initialize()

assert store.initialized
Expand Down Expand Up @@ -245,7 +236,6 @@ async def test_read(tmp_path, create_database, add_document):
await add_document(path, doc_path, 0, update)

store = SQLiteYStore(str(path))
await store.start()
await store.initialize()

assert store.initialized
Expand All @@ -268,7 +258,6 @@ async def test_write(tmp_path, create_database, add_document):
await add_document(path, doc_path, 0)

store = SQLiteYStore(str(path))
await store.start()
await store.initialize()

assert store.initialized
Expand All @@ -288,26 +277,30 @@ async def test_write(tmp_path, create_database, add_document):
async def _check_db(path: str, store: SQLiteYStore, doc_path: str | None = None):
async with aiosqlite.connect(path) as db:
cursor = await db.execute("pragma user_version")
version = (await cursor.fetchone())[0]
assert store.version == version
res = await cursor.fetchone()
assert res
assert store.version == res[0]

cursor = await db.execute(
"SELECT count(*) FROM sqlite_master WHERE type='table' AND name='documents'"
)
res = await cursor.fetchone()
assert res
assert res[0] == 1

cursor = await db.execute(
"SELECT count(*) FROM sqlite_master WHERE type='table' AND name='yupdates'"
)
res = await cursor.fetchone()
assert res[0] == 1
assert res
assert res and res[0] == 1

if doc_path is not None:
cursor = await db.execute(
"SELECT path, version FROM documents WHERE path = ?",
(doc_path,),
)
res = await cursor.fetchone()
assert res
assert res[0] == doc_path
assert res[1] == 0
2 changes: 0 additions & 2 deletions tests/test_ystore.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ async def test_ystore(tmp_path, YStore):
doc_name = "my_doc.txt"

ystore = YStore(str(store_path), metadata_callback=MetadataCallback())
await ystore.start()
await ystore.initialize()

await ystore.create(doc_name, 0)
Expand Down Expand Up @@ -61,7 +60,6 @@ async def test_document_ttl_sqlite_ystore(tmp_path, test_ydoc):
doc_name = "my_doc.txt"

ystore = MySQLiteYStore(str(store_path))
await ystore.start()
await ystore.initialize()

await ystore.create(doc_name, 0)
Expand Down
59 changes: 1 addition & 58 deletions ypy_websocket/stores/base_store.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from contextlib import AsyncExitStack
from inspect import isawaitable
from typing import AsyncIterator, Awaitable, Callable, cast

import y_py as Y
from anyio import TASK_STATUS_IGNORED, Event, create_task_group
from anyio.abc import TaskGroup, TaskStatus
from anyio import Event


class BaseYStore(ABC):
Expand All @@ -20,9 +18,6 @@ class BaseYStore(ABC):

_store_path: str
_initialized: Event | None = None
_started: Event | None = None
_starting: bool = False
_task_group: TaskGroup | None = None

@abstractmethod
def __init__(
Expand Down Expand Up @@ -124,58 +119,6 @@ def initialized(self) -> bool:
return self._initialized.is_set()
return False

@property
def started(self) -> Event:
if self._started is None:
self._started = Event()
return self._started

async def __aenter__(self) -> BaseYStore:
if self._task_group is not None:
raise RuntimeError("YStore already running")

async with AsyncExitStack() as exit_stack:
tg = create_task_group()
self._task_group = await exit_stack.enter_async_context(tg)
self._exit_stack = exit_stack.pop_all()
tg.start_soon(self.start)

return self

async def __aexit__(self, exc_type, exc_value, exc_tb):
if self._task_group is None:
raise RuntimeError("YStore not running")

self._task_group.cancel_scope.cancel()
self._task_group = None
return await self._exit_stack.__aexit__(exc_type, exc_value, exc_tb)

async def start(self, *, task_status: TaskStatus[None] = TASK_STATUS_IGNORED):
"""Start the store.
Arguments:
task_status: The status to set when the task has started.
"""
if self._starting:
return
else:
self._staring = True

if self._task_group is not None:
raise RuntimeError("YStore already running")

self.started.set()
self._starting = False
task_status.started()

def stop(self) -> None:
"""Stop the store."""
if self._task_group is None:
raise RuntimeError("YStore not running")

self._task_group.cancel_scope.cancel()
self._task_group = None

async def get_metadata(self) -> bytes:
"""
Returns:
Expand Down
19 changes: 0 additions & 19 deletions ypy_websocket/stores/sqlite_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,6 @@ def __init__(
self.metadata_callback = metadata_callback
self.log = log or getLogger(__name__)

async def start(self, *, task_status: TaskStatus[None] = TASK_STATUS_IGNORED):
"""Start the SQLiteYStore.
Arguments:
task_status: The status to set when the task has started.
"""
if self._starting:
return
else:
self._starting = True

if self._task_group is not None:
raise RuntimeError("YStore already running")

self._task_group = create_task_group()
self.started.set()
self._starting = False
task_status.started()

async def initialize(self) -> None:
"""
Initializes the store.
Expand Down

0 comments on commit dd72571

Please sign in to comment.