54 lines
3.1 KiB
Python
54 lines
3.1 KiB
Python
"""Ownership and FIFO routing: the economic and Cube consumers share one queue."""
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
|
|
|
|
class SQLFenceRouter(unittest.TestCase):
|
|
def test_tokens_ownership_capacity_reset_and_mixed_consumers(self):
|
|
source = r'''
|
|
#include <cassert>
|
|
#include <cstdint>
|
|
#include <deque>
|
|
#include <string>
|
|
struct SQLMsg {void* pvUserData;static int alive;explicit SQLMsg(uintptr_t n):pvUserData(reinterpret_cast<void*>(n)){++alive;}~SQLMsg(){--alive;}};
|
|
int SQLMsg::alive=0;
|
|
struct CAsyncSQL {std::deque<SQLMsg*> results;void ReturnQuery(const char* sql,void* token){assert(std::string(sql)=="SELECT 1");results.push_back(new SQLMsg(reinterpret_cast<uintptr_t>(token)));}bool PopResult(SQLMsg** out){if(results.empty())return false;*out=results.front();results.pop_front();return true;}};
|
|
#include "src/server/db/src/SQLFences.h"
|
|
using namespace PortSQLFence;
|
|
int main(){
|
|
{Router<SQLMsg> r(4,2);assert(!r.Reserve(Cube,0)&&!r.Reserve(Kind(2),1));
|
|
auto a=r.Reserve(Economy,55),b=r.Reserve(Cube,55);assert(a&&b&&a!=b&&!r.Reserve(Cube,66));
|
|
assert(r.Complete(new SQLMsg(b)));assert(!r.Pop(Economy));assert(!r.Reserve(Cube,66)); // ready messages still own capacity
|
|
assert(r.Complete(new SQLMsg(a)));auto* m=r.Pop(Economy);assert(m&&reinterpret_cast<uintptr_t>(m->pvUserData)==55);delete m;
|
|
auto c=r.Reserve(Economy,66);assert(c>b);assert(!r.Complete(new SQLMsg(a))&&r.Failures()==1);
|
|
m=r.Pop(Cube);assert(m&&reinterpret_cast<uintptr_t>(m->pvUserData)==55);delete m;
|
|
r.Clear();assert(r.Failures()==2&&!r.Pop(Cube));assert(!r.Complete(new SQLMsg(c)));
|
|
auto d=r.Reserve(Cube,77);assert(d>c);assert(!r.Reserve(Economy,88)); // no token wrap/reuse, even across clear
|
|
assert(!r.Complete(nullptr));assert(r.Complete(new SQLMsg(d))); // destructor owns undelivered ready result
|
|
}assert(SQLMsg::alive==0);
|
|
CAsyncSQL sql;assert(!Queue(nullptr,Cube,1));assert(Queue(&sql,Cube,7)&&Queue(&sql,Economy,7)&&Queue(&sql,Cube,8));
|
|
auto* m=Pop(&sql,Economy);assert(m&&reinterpret_cast<uintptr_t>(m->pvUserData)==7);delete m;
|
|
assert(!Pop(&sql,Economy));m=Pop(&sql,Cube);assert(m&&reinterpret_cast<uintptr_t>(m->pvUserData)==7);delete m;
|
|
m=Pop(&sql,Cube);assert(m&&reinterpret_cast<uintptr_t>(m->pvUserData)==8);delete m;
|
|
sql.results.push_back(new SQLMsg(UINT32_MAX));assert(!Pop(&sql,Cube)&&PlayerRouter().Failures()==1);
|
|
// Pump budget must not lose results, including when the requested kind has none.
|
|
for(unsigned i=1;i<=2050;++i)assert(Queue(&sql,Cube,i));
|
|
assert(!Pop(&sql,Economy));unsigned count=0;while((m=Pop(&sql,Cube))){++count;delete m;}assert(count==2050);
|
|
PlayerRouter().Clear();assert(SQLMsg::alive==0);
|
|
}
|
|
'''
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
cpp = Path(folder)/'router.cpp'; exe = Path(folder)/'router'
|
|
cpp.write_text(source)
|
|
subprocess.run(['c++','-std=c++17','-fsanitize=address,undefined',
|
|
'-fno-sanitize-recover=all','-I'+str(ROOT),str(cpp),'-o',str(exe)],check=True)
|
|
subprocess.run([str(exe)],check=True)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|