Skip to content

Commit

Permalink
Check concurrent batches conflict, add extra impl
Browse files Browse the repository at this point in the history
  • Loading branch information
erickt committed Sep 21, 2022
1 parent d812261 commit 6229fc6
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
45 changes: 45 additions & 0 deletions tuf/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,29 @@ where
}
}

impl<T, D> RepositoryStorage<D> for &T
where
T: RepositoryStorage<D>,
D: DataInterchange + Sync,
{
fn store_metadata<'a>(
&'a self,
meta_path: &MetadataPath,
version: MetadataVersion,
metadata: &'a mut (dyn AsyncRead + Send + Unpin),
) -> BoxFuture<'a, Result<()>> {
(**self).store_metadata(meta_path, version, metadata)
}

fn store_target<'a>(
&'a self,
target_path: &TargetPath,
target: &'a mut (dyn AsyncRead + Send + Unpin),
) -> BoxFuture<'a, Result<()>> {
(**self).store_target(target_path, target)
}
}

impl<T, D> RepositoryStorage<D> for &mut T
where
T: RepositoryStorage<D>,
Expand Down Expand Up @@ -317,6 +340,28 @@ where
}
}

impl<D> RepositoryStorage<D> for &dyn RepositoryStorage<D>
where
D: DataInterchange + Sync,
{
fn store_metadata<'a>(
&'a self,
meta_path: &MetadataPath,
version: MetadataVersion,
metadata: &'a mut (dyn AsyncRead + Send + Unpin),
) -> BoxFuture<'a, Result<()>> {
(**self).store_metadata(meta_path, version, metadata)
}

fn store_target<'a>(
&'a self,
target_path: &TargetPath,
target: &'a mut (dyn AsyncRead + Send + Unpin),
) -> BoxFuture<'a, Result<()>> {
(**self).store_target(target_path, target)
}
}

impl<D> RepositoryStorage<D> for &mut dyn RepositoryStorage<D>
where
D: DataInterchange + Sync,
Expand Down
23 changes: 23 additions & 0 deletions tuf/src/repository/ephemeral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,29 @@ mod test {
.unwrap();

assert_matches!(batch.commit().await, Err(CommitError::Conflict));

// multiple batches should conflict.
let batch1 = repo.batch_update();
let batch2 = repo.batch_update();

batch1
.store_target(
&TargetPath::new("target4").unwrap(),
&mut "target4".as_bytes(),
)
.await
.unwrap();

batch2
.store_target(
&TargetPath::new("target5").unwrap(),
&mut "target5".as_bytes(),
)
.await
.unwrap();

assert_matches!(batch1.commit().await, Ok(()));
assert_matches!(batch2.commit().await, Err(CommitError::Conflict));
})
}
}
23 changes: 23 additions & 0 deletions tuf/src/repository/file_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,29 @@ mod test {
.unwrap();

assert_matches!(batch.commit().await, Err(CommitError::Conflict));

// multiple batches should conflict.
let batch1 = repo.batch_update();
let batch2 = repo.batch_update();

batch1
.store_target(
&TargetPath::new("target4").unwrap(),
&mut "target4".as_bytes(),
)
.await
.unwrap();

batch2
.store_target(
&TargetPath::new("target5").unwrap(),
&mut "target5".as_bytes(),
)
.await
.unwrap();

assert_matches!(batch1.commit().await, Ok(()));
assert_matches!(batch2.commit().await, Err(CommitError::Conflict));
})
}
}

0 comments on commit 6229fc6

Please sign in to comment.