Skip to content

Commit ae7f76c

Browse files
committed
refactor + reformat
1 parent c16e51e commit ae7f76c

File tree

3 files changed

+13
-32
lines changed

3 files changed

+13
-32
lines changed

tests/coordination/test_coordination_client.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ def test_coordination_lock_full_lifecycle_sync(self, driver_sync):
191191
client = CoordinationClient(driver_sync)
192192
node_path = "/local/test_lock_full_lifecycle"
193193

194-
# --- cleanup ---
195194
try:
196195
client.delete_node(node_path)
197196
except ydb.SchemeError:
@@ -210,7 +209,6 @@ def test_coordination_lock_full_lifecycle_sync(self, driver_sync):
210209

211210
lock = client.lock("test_lock", node_path)
212211

213-
# --- create/update/describe ---
214212
create_resp: CreateSemaphoreResult = lock.create(init_limit=1, init_data=b"init-data")
215213
assert create_resp.status == StatusCode.SUCCESS
216214

@@ -222,61 +220,49 @@ def test_coordination_lock_full_lifecycle_sync(self, driver_sync):
222220
assert update_resp.status == StatusCode.SUCCESS
223221
assert lock.describe().data == b"updated-data"
224222

225-
# --- threading coordination ---
226223
lock2_ready = threading.Event()
227224
lock2_acquired = threading.Event()
228225
thread_exc = {"err": None}
229226

230227
def second_lock_task():
231228
try:
232-
lock2_ready.set() # сигнал, что поток готов
229+
lock2_ready.set()
233230
with client.lock("test_lock", node_path):
234-
lock2_acquired.set() # сигнал, что захватил lock
231+
lock2_acquired.set()
235232
logger.info("Second thread acquired lock")
236233
except Exception as e:
237234
logger.exception("second_lock_task failed")
238235
thread_exc["err"] = e
239236

240237
t2 = threading.Thread(target=second_lock_task)
241238

242-
# --- main thread acquires first lock ---
243239
with client.lock("test_lock", node_path) as lock1:
244240
resp = lock1.describe()
245241
assert resp.status == StatusCode.SUCCESS
246242
assert resp.count == 1
247243

248-
# запускаем второй поток
249244
t2.start()
250245
started = lock2_ready.wait(timeout=2.0)
251246
assert started, "Second thread did not signal readiness to acquire lock"
252247

253-
# --- main thread released lock, второй поток должен захватить ---
254-
acquired = lock2_acquired.wait(timeout=10.0) # увеличенный таймаут
248+
acquired = lock2_acquired.wait(timeout=10.0)
255249
t2.join(timeout=5.0)
256250

257251
if not acquired:
258252
if thread_exc["err"]:
259253
raise AssertionError(f"Second thread raised exception: {thread_exc['err']!r}") from thread_exc["err"]
260254
else:
261-
raise AssertionError(
262-
"Second thread did not acquire the lock in time. Check logs for details."
263-
)
255+
raise AssertionError("Second thread did not acquire the lock in time. Check logs for details.")
264256

265257
assert not t2.is_alive(), "Second thread did not finish after acquiring lock"
266258

267-
# --- проверяем, что lock можно снова взять в главном потоке ---
268259
with client.lock("test_lock", node_path) as lock3:
269260
resp3: DescribeLockResult = lock3.describe()
270261
assert resp3.status == StatusCode.SUCCESS
271262
assert resp3.count == 1
272263

273-
# --- cleanup ---
274264
delete_resp = lock.delete()
275265
assert delete_resp.status == StatusCode.SUCCESS
276-
# небольшая пауза для удаления на сервере
277266
time.sleep(0.1)
278267
describe_after_delete: DescribeLockResult = lock.describe()
279268
assert describe_after_delete.status == StatusCode.NOT_FOUND
280-
281-
282-

ydb/_grpc/grpcwrapper/ydb_coordination.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
# ---------- CRUD для узлов ----------
1212

13+
1314
@dataclass
1415
class CreateNodeRequest(IToProto):
1516
path: str
@@ -55,8 +56,10 @@ def to_proto(self) -> "ydb_coordination_pb2.DropNodeRequest":
5556
path=self.path,
5657
)
5758

59+
5860
# ---------- Сессии и семафоры ----------
5961

62+
6063
@dataclass
6164
class SessionStart(IToProto):
6265
path: str
@@ -82,9 +85,7 @@ def to_proto(self) -> "ydb_coordination_pb2.SessionRequest":
8285
@dataclass
8386
class SessionStop(IToProto):
8487
def to_proto(self) -> "ydb_coordination_pb2.SessionRequest":
85-
return ydb_coordination_pb2.SessionRequest(
86-
session_stop=ydb_coordination_pb2.SessionRequest.SessionStop()
87-
)
88+
return ydb_coordination_pb2.SessionRequest(session_stop=ydb_coordination_pb2.SessionRequest.SessionStop())
8889

8990

9091
@dataclass
@@ -141,9 +142,7 @@ class ReleaseSemaphore(IToProto):
141142

142143
def to_proto(self) -> "ydb_coordination_pb2.SessionRequest":
143144
return ydb_coordination_pb2.SessionRequest(
144-
release_semaphore=ydb_coordination_pb2.SessionRequest.ReleaseSemaphore(
145-
req_id=self.req_id, name=self.name
146-
)
145+
release_semaphore=ydb_coordination_pb2.SessionRequest.ReleaseSemaphore(req_id=self.req_id, name=self.name)
147146
)
148147

149148

@@ -168,6 +167,7 @@ def to_proto(self) -> "ydb_coordination_pb2.SessionRequest":
168167
)
169168
)
170169

170+
171171
@dataclass
172172
class UpdateSemaphore(IToProto):
173173
name: str

ydb/coordination/lock_sync.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def __init__(
2727
self._count = count
2828
self._timeout_millis = timeout_millis
2929

30-
3130
async def _make_lock():
3231
return CoordinationLock(
3332
client=self._client,
@@ -37,9 +36,7 @@ async def _make_lock():
3736
timeout_millis=self._timeout_millis,
3837
)
3938

40-
self._async_lock: CoordinationLock = self._caller.safe_call_with_result(
41-
_make_lock(), self._timeout_sec
42-
)
39+
self._async_lock: CoordinationLock = self._caller.safe_call_with_result(_make_lock(), self._timeout_sec)
4340

4441
def _check_closed(self):
4542
if self._closed:
@@ -69,14 +66,12 @@ def release(self, timeout: Optional[float] = None):
6966
def create(self, init_limit: int, init_data: bytes, timeout: Optional[float] = None):
7067
self._check_closed()
7168
t = timeout or self._timeout_sec
72-
return self._caller.safe_call_with_result(
73-
self._async_lock.create(init_limit, init_data), t
74-
)
69+
return self._caller.safe_call_with_result(self._async_lock.create(init_limit, init_data), t)
7570

7671
def delete(self, timeout: Optional[float] = None):
7772
self._check_closed()
7873
t = timeout or self._timeout_sec
79-
return self._caller.safe_call_with_result( self._async_lock.delete(), t)
74+
return self._caller.safe_call_with_result(self._async_lock.delete(), t)
8075

8176
def describe(self, timeout: Optional[float] = None):
8277
self._check_closed()

0 commit comments

Comments
 (0)