Skip to content

Commit

Permalink
Add regression test for #2709
Browse files Browse the repository at this point in the history
Adds a new "slow_test" decorator and enables slow tests to be run during
CI.
  • Loading branch information
coleifer committed Apr 19, 2023
1 parent 3d52139 commit 9cabb4a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ jobs:
PGUSER: postgres
PGHOST: 127.0.0.1
PGPASSWORD: peewee
run: python runtests.py --mysql-user=root --mysql-password=peewee
run: python runtests.py --mysql-user=root --mysql-password=peewee -s
4 changes: 4 additions & 0 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def get_option_parser():
type='int', help='Verbosity of output')
basic.add_option('-f', '--failfast', action='store_true', default=False,
dest='failfast', help='Exit on first failure/error.')
basic.add_option('-s', '--slow-tests', action='store_true', default=False,
dest='slow_tests', help='Run tests that may be slow.')
parser.add_option_group(basic)

db_param_map = (
Expand Down Expand Up @@ -90,6 +92,8 @@ def collect_tests(args):
os.environ['PEEWEE_%s' % att_name.upper()] = value

os.environ['PEEWEE_TEST_VERBOSITY'] = str(options.verbosity)
if options.slow_tests:
os.environ['PEEWEE_SLOW_TESTS'] = '1'

suite = collect_tests(args)
failures, errors = runtests(suite, options.verbosity, options.failfast)
Expand Down
7 changes: 6 additions & 1 deletion tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def get_in_memory_db(**params):

BACKEND = os.environ.get('PEEWEE_TEST_BACKEND') or 'sqlite'
VERBOSITY = int(os.environ.get('PEEWEE_TEST_VERBOSITY') or 1)
SLOW_TESTS = bool(os.environ.get('PEEWEE_SLOW_TESTS'))

IS_SQLITE = BACKEND.startswith('sqlite')
IS_MYSQL = BACKEND.startswith(('mysql', 'maria'))
Expand Down Expand Up @@ -270,12 +271,16 @@ def decorator(method):
return unittest.skipIf(expr, reason)(method)
return decorator


def skip_unless(expr, reason='n/a'):
def decorator(method):
return unittest.skipUnless(expr, reason)(method)
return decorator

def slow_test():
def decorator(method):
return unittest.skipUnless(SLOW_TESTS, 'skipping slow test')(method)
return decorator

def requires_sqlite(method):
return skip_unless(IS_SQLITE, 'requires sqlite')(method)

Expand Down
23 changes: 23 additions & 0 deletions tests/regressions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import datetime
import json
import random
import sys
import threading
import time
import uuid

from peewee import *
Expand All @@ -21,6 +24,7 @@
from .base import requires_postgresql
from .base import skip_if
from .base import skip_unless
from .base import slow_test
from .base_models import Sample
from .base_models import Tweet
from .base_models import User
Expand Down Expand Up @@ -1777,3 +1781,22 @@ def test_join_prefetch_multiple_fks(self):
('s1', [t1, t2], [], ['s2a', 's2b'], []),
('s2a', [t3], [t1], ['s3'], ['s1']),
('s2b', [t4], [t2], ['s3'], ['s1'])])


@slow_test()
class TestThreadSafetyDecorators(ModelTestCase):
requires = [User]

def test_thread_safety_atomic(self):
@self.database.atomic()
def get_one(n):
time.sleep(n)
return User.select().first()
def run(n):
with self.database.atomic():
get_one(n)
User.create(username='u')
threads = [threading.Thread(target=run, args=(i,))
for i in (0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.5)]
for t in threads: t.start()
for t in threads: t.join()

0 comments on commit 9cabb4a

Please sign in to comment.