Skip to content

Commit

Permalink
Add test against race conditions in commit_and_push
Browse files Browse the repository at this point in the history
  • Loading branch information
sersorrel committed Feb 1, 2024
1 parent 26e7aed commit f7db003
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
34 changes: 34 additions & 0 deletions tests/integration/test_artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import os
import shutil
import threading
from pathlib import Path

import pygit2
Expand Down Expand Up @@ -226,3 +227,36 @@ def test_iter() -> None:
assert pkgs[1].version == "v2.0.1"
assert pkgs[2].name == "pck3"
assert pkgs[2].version is None


# pygit2 protects us against producing diverging histories anyway:
# _pygit2.GitError: failed to create commit: current tip is not the first
# parent
# but the test is nice reassurance.
def test_simultaneous_commit():
parallelism = 100
ad = new_test_artifacts()
artifacts: Artifacts = ad["artifacts"]
initial_commit_oid = ad["initial_commit_oid"]

new_tree, _ = add_test_file_to_repo(artifacts)

e = threading.Event()

def fn(i: int):
e.wait()
artifacts.commit_and_push(new_tree, f"I am thread {i}")

threads = [
threading.Thread(target=fn, args=[i]) for i in range(parallelism)
]
for thread in threads:
thread.start()
e.set()
for thread in threads:
thread.join()

commit = artifacts.repo.head.peel(pygit2.Commit)
for _ in range(parallelism):
commit = commit.parents[0]
assert commit.oid == initial_commit_oid
6 changes: 4 additions & 2 deletions tests/integration/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from softpack_core.artifacts import Artifacts, app
from softpack_core.schemas.environment import (
BuilderError,
CreateEnvironmentSuccess,
DeleteEnvironmentSuccess,
Environment,
Expand All @@ -26,7 +27,6 @@
State,
UpdateEnvironmentSuccess,
WriteArtifactSuccess,
BuilderError,
)
from tests.integration.utils import file_in_remote

Expand Down Expand Up @@ -127,7 +127,9 @@ def test_create_path_invalid_disallowed(httpx_post, testable_env_input):
assert isinstance(result, InvalidInputError)


def test_create_cleans_up_after_builder_failure(httpx_post, testable_env_input):
def test_create_cleans_up_after_builder_failure(
httpx_post, testable_env_input
):
httpx_post.side_effect = Exception('could not contact builder')
result = Environment.create(testable_env_input)
assert isinstance(result, BuilderError)
Expand Down

0 comments on commit f7db003

Please sign in to comment.