Files
2026-10-09 16:11:06 +03:00

136 lines
6.4 KiB
Python

import os
import subprocess
import sys
import time
import unittest
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from unittest.mock import patch
from urllib.parse import quote
from uuid import uuid4
from redis.exceptions import ConnectionError
from sqlalchemy import select
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
from client import Client
from scripts.cache_experiment import make_store, stale_race
from src.auth import sessions
from src.cache import HistoryCache, history_cache
from src.database import create_db_engine
from src.errors import APIError
from src.models import Chat
class CacheTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.engine = create_db_engine()
@classmethod
def tearDownClass(cls):
cls.engine.dispose()
def setUp(self):
self.client = Client('http://127.0.0.1:8080')
self.actor = self.client.user('cache')
self.chat = self.client.call('POST', '/api/v1/chats', 201, actor=self.actor,
body={'title': 'Cache', 'member_ids': [self.actor['id']]}, schema='Chat')['json']
self.prefix = 'test-history:' + uuid4().hex
self.cache = HistoryCache(sessions.client, prefix=self.prefix)
self.reader = make_store(self.engine, self.cache)
self.writer = make_store(self.engine, history_cache)
def tearDown(self):
for key in sessions.client.scan_iter(match=self.prefix + ':*'):
sessions.client.delete(key)
def test_late_fill_cannot_restore_old_page(self):
result = stale_race(self.reader, self.writer, self.actor['id'], self.chat['id'])
self.assertFalse(result['stale'])
def test_hit_still_checks_membership_and_cache_failure_falls_back(self):
args = (self.chat['id'], self.actor['id'], 100, None)
self.writer.create_message(self.chat['id'], self.actor['id'], 'hello')
page = self.reader.list_messages(*args)
with patch.object(self.reader, '_load_messages', side_effect=AssertionError('Cache miss')):
self.assertEqual(self.reader.list_messages(*args), page)
with self.assertRaises(APIError) as error:
self.reader.list_messages(self.chat['id'], str(uuid4()), 100, None)
self.assertEqual(error.exception.status_code, 404)
with patch.object(self.cache.client, 'get', side_effect=ConnectionError('unavailable')):
self.assertEqual(self.reader.list_messages(*args), page)
self.cache.client.delete(self.cache.key(self.chat['id'], 1, 100, None))
with patch.object(self.cache.client, 'set', side_effect=ConnectionError('unavailable')):
self.assertEqual(self.reader.list_messages(*args), page)
def test_version_rolls_back_with_failed_message(self):
with self.assertRaises(IntegrityError) as error:
self.writer.create_message(self.chat['id'], self.actor['id'], '')
self.assertEqual(error.exception.orig.sqlstate, '23514')
with Session(self.engine) as session:
self.assertEqual(session.get(Chat, self.chat['id']).history_version, 0)
def test_concurrent_writers_do_not_lose_version_increments(self):
self.assertEqual(self.reader.list_messages(self.chat['id'], self.actor['id'], 100, None)['items'], [])
def write(n):
return self.writer.create_message(self.chat['id'], self.actor['id'], f'message {n}')
with ThreadPoolExecutor(max_workers=4) as executor:
messages = list(executor.map(write, range(8)))
with Session(self.engine) as session:
self.assertEqual(session.get(Chat, self.chat['id']).history_version, 8)
page = self.reader.list_messages(self.chat['id'], self.actor['id'], 100, None)
self.assertEqual({item['id'] for item in page['items']}, {item['id'] for item in messages})
def test_http_read_after_write_and_cursor_pages(self):
path = f"/api/v1/chats/{self.chat['id']}/messages"
def read(query=''):
return self.client.call('GET', path + query, actor=self.actor, schema='MessagePage')['json']
self.assertEqual(read()['items'], [])
first = self.client.message(self.actor, self.chat, 'one')['json']
self.assertEqual(read()['items'], [first])
self.client.message(self.actor, self.chat, 'two')
page = read('?limit=1')
cursor = quote(page['next_cursor'], safe='')
tail = read('?limit=1&cursor=' + cursor)
self.assertIsNone(tail['next_cursor'])
third = self.client.message(self.actor, self.chat, 'three')['json']
tail = read('?limit=1&cursor=' + cursor)
self.assertIsNotNone(tail['next_cursor'])
self.assertEqual(read('?limit=1&cursor=' + quote(tail['next_cursor'], safe=''))['items'], [third])
self.assertEqual(len(read()['items']), 3)
with Session(self.engine) as session:
version = session.scalar(select(Chat.history_version).where(Chat.id == self.chat['id']))
key = history_cache.key(self.chat['id'], version, 50, None)
self.assertGreater(sessions.client.ttl(key), 0)
self.assertLessEqual(sessions.client.ttl(key), history_cache.ttl)
def test_read_after_write_through_another_api_process(self):
process = subprocess.Popen([sys.executable, '-m', 'src'],
env={**os.environ, 'APP_HOST': '127.0.0.1', 'APP_PORT': '8082'},
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
try:
replica = Client('http://127.0.0.1:8082')
for _ in range(100):
try:
replica.call('GET', '/health/ready')
break
except AssertionError:
self.assertIsNone(process.poll())
time.sleep(0.1)
else:
self.fail('Second API did not become ready')
path = f"/api/v1/chats/{self.chat['id']}/messages"
self.assertEqual(replica.call('GET', path, actor=self.actor, schema='MessagePage')['json']['items'], [])
message = self.client.message(self.actor, self.chat, 'written through first API')['json']
self.assertEqual(replica.call('GET', path, actor=self.actor, schema='MessagePage')['json']['items'], [message])
finally:
process.terminate()
process.wait(timeout=10)
if __name__ == '__main__':
unittest.main()