# -*- coding: utf-8 -*-
"""只操作本程序创建的临时对象；全部数据为合成样本。"""
import os, sys, json, sqlite3, tempfile, pathlib, subprocess, time, socket, stat
from pathlib import Path
def emit(**kw):
    print(json.dumps(kw, ensure_ascii=False, sort_keys=True))
with tempfile.TemporaryDirectory(prefix='ywebs-next132-b2-lock-') as d:
    p=str(Path(d)/'demo.db'); a=sqlite3.connect(p); b=sqlite3.connect(p,timeout=.05)
    a.executescript('CREATE TABLE t(id PRIMARY KEY,n); INSERT INTO t VALUES(1,0),(2,0);')
    a.execute('BEGIN IMMEDIATE'); a.execute('UPDATE t SET n=1 WHERE id=1')
    try: b.execute('UPDATE t SET n=1 WHERE id=2'); blocked=False
    except sqlite3.OperationalError as e: blocked='locked' in str(e); b.rollback()
    a.commit(); b.execute('UPDATE t SET n=1 WHERE id=2'); b.commit()
    rows=b.execute('SELECT * FROM t ORDER BY id').fetchall(); assert blocked and rows==[(1,1),(2,1)]
    emit(other_row_write_blocked=blocked,after_commit=rows)
    a.close(); b.close()
