Files
upstream-aw-client/tests/test_requestqueue.py
upstream-mirror 96755ecd21
Some checks failed
Build / Test on ubuntu-latest (push) Successful in 2m4s
Examples / Test examples (ubuntu-latest, 3.10) (push) Successful in 2m16s
Lint / lint (push) Successful in 1m25s
Build / Test on macOS-latest (push) Has been cancelled
Build / Test on windows-latest (push) Has been cancelled
CodeQL / Analyze (python) (push) Failing after 1s
snapshot of https://github.com/ActivityWatch/aw-client.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 00:20:39 +05:00

91 lines
2.2 KiB
Python

"""
Tests the often-buggy request queue.
WARNING: A shitload of mocking ahead
It is said about testing that it makes you able to refactorize
with confidence, and I need some of that right now.
"""
from time import sleep
from logging import basicConfig, DEBUG
basicConfig(level=DEBUG)
import requests
from aw_client.client import RequestQueue
class MockClient:
client_name = "Mock"
def __init__(self):
self.testing = True
self.create_bucket_calls = []
def get_buckets(self, *args, **kwargs):
print("Called get_buckets")
return [{"id": "test", "name": "Test"}]
def create_bucket(self, *args, **kwargs):
self.create_bucket_calls.append((args, kwargs))
print("Called create_bucket")
def _post(self, *args, **kwargs):
print(args, kwargs)
return requests.Response()
def test_basic():
client = MockClient()
rq = RequestQueue(client) # type: ignore
# Mockeypatching
rq._try_connect = lambda: True # type: ignore
rq.connected = True
rq.start()
rq.add_request("/api/0/buckets/test/heartbeat", {})
sleep(1)
rq.stop()
rq.join()
def test_complex():
client = MockClient()
rq = RequestQueue(client) # type: ignore
# Mockeypatching
rq._try_connect = lambda: False # type: ignore
rq.start()
sleep(1)
rq.stop()
rq.join()
def test_register_bucket_creates_immediately_when_connected():
client = MockClient()
rq = RequestQueue(client) # type: ignore
rq.connected = True
rq.register_bucket("test-bucket", "test-type")
assert client.create_bucket_calls == [(("test-bucket", "test-type"), {})]
def test_register_bucket_marks_queue_disconnected_on_create_failure():
class FailingClient(MockClient):
def create_bucket(self, *args, **kwargs):
super().create_bucket(*args, **kwargs)
raise requests.exceptions.ConnectionError()
client = FailingClient()
rq = RequestQueue(client) # type: ignore
rq.connected = True
rq.register_bucket("test-bucket", "test-type")
assert rq.connected is False
assert client.create_bucket_calls == [(("test-bucket", "test-type"), {})]