Skip to content

Commit

Permalink
Locking; add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mdwint committed May 20, 2021
1 parent a21e5f1 commit 41ee42f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
21 changes: 17 additions & 4 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from contextlib import contextmanager

import boto3
import moto
import pytest
from moto import mock_s3


@pytest.fixture(scope="session")
Expand All @@ -30,13 +30,26 @@ def aws_credentials():

@pytest.fixture
def s3_bucket(aws_credentials):
with mock_s3():
conn = boto3.resource("s3", region_name="us-east-1")
bucket = conn.Bucket("s3pypi-test")
with moto.mock_s3():
s3 = boto3.resource("s3", region_name="us-east-1")
bucket = s3.Bucket("s3pypi-test")
bucket.create()
yield bucket


@pytest.fixture
def dynamodb_table(s3_bucket):
name = f"{s3_bucket.name}-locks"
with moto.mock_dynamodb2(), moto.mock_sts():
db = boto3.resource("dynamodb")
db.create_table(
TableName=name,
AttributeDefinitions=[{"AttributeName": "LockID", "AttributeType": "S"}],
KeySchema=[{"AttributeName": "LockID", "KeyType": "HASH"}],
)
yield db.Table(name)


@pytest.fixture
def boto3_session(s3_bucket):
return boto3.session.Session()
18 changes: 18 additions & 0 deletions tests/integration/test_locking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest

from s3pypi.locking import DynamoDBLocker, DynamoDBLockTimeoutError


def test_dynamodb_lock_timeout(boto3_session, dynamodb_table):
lock = DynamoDBLocker(
boto3_session,
dynamodb_table.name,
poll_interval=0.01,
max_attempts=3,
)
key = "example"

with lock(key):
with pytest.raises(DynamoDBLockTimeoutError):
with lock(key):
pass
4 changes: 2 additions & 2 deletions tests/integration/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ def test_string_dict(text, expected):
assert string_dict(text) == expected


def test_main_upload_package(chdir, data_dir, s3_bucket):
def test_main_upload_package(chdir, data_dir, s3_bucket, dynamodb_table):
with chdir(data_dir):
dist = sorted(glob.glob("dists/*"))
s3pypi(*dist, "--bucket", s3_bucket.name, "--put-root-index")
s3pypi(*dist, "--bucket", s3_bucket.name, "--lock-indexes", "--put-root-index")

def read(key: str) -> bytes:
return s3_bucket.Object(key).get()["Body"].read()
Expand Down

0 comments on commit 41ee42f

Please sign in to comment.