|
| 1 | +from utils import dojo_run |
| 2 | + |
| 3 | + |
| 4 | +def test_dojo_schema_creation_serializes_first_start(): |
| 5 | + result = dojo_run("dojo", "flask", input=""" |
| 6 | +import threading |
| 7 | +import time |
| 8 | +import uuid |
| 9 | +
|
| 10 | +from flask import current_app |
| 11 | +from sqlalchemy import Column, Integer, Table, inspect, text |
| 12 | +
|
| 13 | +from CTFd.models import db |
| 14 | +from CTFd.plugins import dojo_plugin |
| 15 | +
|
| 16 | +app = current_app._get_current_object() |
| 17 | +table = Table( |
| 18 | + f"test_dojo_schema_{uuid.uuid4().hex}", |
| 19 | + db.metadata, |
| 20 | + Column("id", Integer, primary_key=True), |
| 21 | +) |
| 22 | +real_create_all = db.metadata.create_all |
| 23 | +create_calls = [] |
| 24 | +create_calls_lock = threading.Lock() |
| 25 | +first_create_entered = threading.Event() |
| 26 | +release_first_create = threading.Event() |
| 27 | +errors = [] |
| 28 | +
|
| 29 | +def tracked_create_all(*args, **kwargs): |
| 30 | + with create_calls_lock: |
| 31 | + create_calls.append(threading.get_ident()) |
| 32 | + call_number = len(create_calls) |
| 33 | + if call_number == 1: |
| 34 | + first_create_entered.set() |
| 35 | + assert release_first_create.wait(10) |
| 36 | + return real_create_all(*args, **kwargs) |
| 37 | +
|
| 38 | +def create_tables(): |
| 39 | + try: |
| 40 | + with app.app_context(): |
| 41 | + dojo_plugin.create_dojo_tables() |
| 42 | + except BaseException as error: |
| 43 | + errors.append(error) |
| 44 | +
|
| 45 | +db.metadata.create_all = tracked_create_all |
| 46 | +first_thread = threading.Thread(target=create_tables) |
| 47 | +second_thread = threading.Thread(target=create_tables) |
| 48 | +try: |
| 49 | + first_thread.start() |
| 50 | + assert first_create_entered.wait(10) |
| 51 | + second_thread.start() |
| 52 | + deadline = time.time() + 10 |
| 53 | + waiting_for_lock = False |
| 54 | + while time.time() < deadline: |
| 55 | + with db.engine.connect() as connection: |
| 56 | + waiting_for_lock = bool(connection.execute( |
| 57 | + text( |
| 58 | + "SELECT COUNT(*) FROM pg_locks " |
| 59 | + "WHERE locktype = 'advisory' AND NOT granted " |
| 60 | + "AND classid = :namespace AND objid = :lock_id " |
| 61 | + "AND objsubid = 2" |
| 62 | + ), |
| 63 | + { |
| 64 | + "namespace": dojo_plugin.DOJO_SCHEMA_LOCK_NAMESPACE, |
| 65 | + "lock_id": dojo_plugin.DOJO_SCHEMA_LOCK_ID, |
| 66 | + }, |
| 67 | + ).scalar()) |
| 68 | + if waiting_for_lock: |
| 69 | + break |
| 70 | + time.sleep(0.01) |
| 71 | + assert waiting_for_lock |
| 72 | + assert len(create_calls) == 1 |
| 73 | + release_first_create.set() |
| 74 | + first_thread.join(10) |
| 75 | + second_thread.join(10) |
| 76 | + assert not first_thread.is_alive() |
| 77 | + assert not second_thread.is_alive() |
| 78 | + assert errors == [] |
| 79 | + assert len(create_calls) == 2 |
| 80 | + assert inspect(db.engine).has_table(table.name) |
| 81 | +finally: |
| 82 | + release_first_create.set() |
| 83 | + for thread in (first_thread, second_thread): |
| 84 | + if thread.ident is not None: |
| 85 | + thread.join(10) |
| 86 | + db.metadata.create_all = real_create_all |
| 87 | + table.drop(db.engine, checkfirst=True) |
| 88 | + db.metadata.remove(table) |
| 89 | +
|
| 90 | +print("DOJO_SCHEMA_CREATION_SERIALIZED") |
| 91 | +""", check=False) |
| 92 | + assert result.returncode == 0, result.stderr |
| 93 | + assert "DOJO_SCHEMA_CREATION_SERIALIZED" in result.stdout, ( |
| 94 | + result.stdout + result.stderr |
| 95 | + ) |
0 commit comments