Skip to content

Commit

Permalink
Fix mysterious compilation issue on windows on CI or cross-compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
madadam committed Jan 16, 2024
1 parent dd8dba4 commit 18998fd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 38 deletions.
60 changes: 24 additions & 36 deletions lib/src/blob/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async fn write_and_read_case(
// Create the blob and write to it in chunks of `write_len` bytes.
let mut blob = Blob::create(branch.clone(), block_id);

let orig_content: Vec<u8> = rng.sample_iter(Standard).take(blob_len).collect();
let orig_content = random_bytes(rng, blob_len);

for chunk in orig_content.chunks(write_len) {
blob.write_all(&mut tx, &mut changeset, chunk)
Expand Down Expand Up @@ -116,7 +116,7 @@ fn len(
let mut tx = store.begin_write().await.unwrap();
let mut changeset = Changeset::new();

let content: Vec<u8> = rng.sample_iter(Standard).take(content_len).collect();
let content = random_bytes(rng, content_len);

let mut blob = Blob::create(branch.clone(), BlobId::ROOT);
blob.write_all(&mut tx, &mut changeset, &content[..])
Expand Down Expand Up @@ -174,7 +174,7 @@ async fn seek_from(content_len: usize, seek_from: SeekFrom, expected_pos: usize,
let mut tx = store.begin_write().await.unwrap();
let mut changeset = Changeset::new();

let content: Vec<u8> = rng.sample_iter(Standard).take(content_len).collect();
let content = random_bytes(rng, content_len);

let mut blob = Blob::create(branch.clone(), BlobId::ROOT);
blob.write_all(&mut tx, &mut changeset, &content[..])
Expand Down Expand Up @@ -207,7 +207,7 @@ fn seek_from_current(
let mut tx = store.begin_write().await.unwrap();
let mut changeset = Changeset::new();

let content: Vec<u8> = rng.sample_iter(Standard).take(content_len).collect();
let content = random_bytes(rng, content_len);

let mut blob = Blob::create(branch.clone(), BlobId::ROOT);
blob.write_all(&mut tx, &mut changeset, &content[..])
Expand Down Expand Up @@ -304,10 +304,7 @@ async fn truncate_to_empty() {

let id = rng.gen();

let content: Vec<_> = (&mut rng)
.sample_iter(Standard)
.take(2 * BLOCK_SIZE)
.collect();
let content = random_bytes(&mut rng, 2 * BLOCK_SIZE);

let mut changeset = Changeset::new();
let mut blob = Blob::create(branch.clone(), id);
Expand Down Expand Up @@ -345,10 +342,7 @@ async fn truncate_to_shorter() {

let id = rng.gen();

let content: Vec<_> = (&mut rng)
.sample_iter(Standard)
.take(3 * BLOCK_SIZE)
.collect();
let content = random_bytes(&mut rng, 3 * BLOCK_SIZE);

let mut changeset = Changeset::new();
let mut blob = Blob::create(branch.clone(), id);
Expand Down Expand Up @@ -390,10 +384,7 @@ async fn truncate_marks_as_dirty() {

let id = rng.gen();

let content: Vec<_> = (&mut rng)
.sample_iter(Standard)
.take(2 * BLOCK_SIZE)
.collect();
let content = random_bytes(&mut rng, 2 * BLOCK_SIZE);

let mut blob = Blob::create(branch.clone(), id);
blob.write_all(&mut tx, &mut changeset, &content)
Expand Down Expand Up @@ -565,7 +556,7 @@ async fn fork_and_write_case(
rng.gen()
};

let src_content: Vec<u8> = (&mut rng).sample_iter(Standard).take(src_len).collect();
let src_content = random_bytes(&mut rng, src_len);

let mut tx = store.begin_write().await.unwrap();
let mut changeset = Changeset::new();
Expand All @@ -588,7 +579,7 @@ async fn fork_and_write_case(
.await
.unwrap();

let write_content: Vec<u8> = rng.sample_iter(Standard).take(write_len).collect();
let write_content = random_bytes(rng, write_len);

blob.seek(SeekFrom::Start(seek_pos as u64));
blob.write_all(&mut tx, &mut changeset, &write_content[..])
Expand Down Expand Up @@ -631,7 +622,7 @@ async fn fork_is_idempotent() {
let (mut rng, _base_dir, store, [src_branch, dst_branch]) = setup(0).await;

let id = rng.gen();
let content: Vec<u8> = (&mut rng).sample_iter(Standard).take(512 * 1024).collect();
let content = random_bytes(&mut rng, 512 * 1024);

let mut tx = store.begin_write().await.unwrap();
let mut changeset = Changeset::new();
Expand Down Expand Up @@ -707,10 +698,7 @@ async fn block_ids_test() {
let blob_id: BlobId = rng.gen();
let mut blob = Blob::create(branch.clone(), blob_id);

let content: Vec<_> = rng
.sample_iter(Standard)
.take(BLOCK_SIZE * 3 - HEADER_SIZE)
.collect();
let content = random_bytes(rng, BLOCK_SIZE * 3 - HEADER_SIZE);
let mut tx = store.begin_write().await.unwrap();
let mut changeset = Changeset::new();
blob.write_all(&mut tx, &mut changeset, &content)
Expand Down Expand Up @@ -745,10 +733,7 @@ async fn block_ids_of_identical_blobs_in_the_same_branch() {
let blob_id_1: BlobId = rng.gen();
let mut blob_1 = Blob::create(branch.clone(), blob_id_1);

let content: Vec<_> = rng
.sample_iter(Standard)
.take(BLOCK_SIZE * 2 - HEADER_SIZE)
.collect();
let content = random_bytes(rng, BLOCK_SIZE * 2 - HEADER_SIZE);

for blob in [&mut blob_0, &mut blob_1] {
let mut tx = store.begin_write().await.unwrap();
Expand Down Expand Up @@ -794,10 +779,7 @@ async fn block_ids_of_identical_blobs_in_different_branches() {
let mut blob_0 = Blob::create(branch_0.clone(), blob_id);
let mut blob_1 = Blob::create(branch_1.clone(), blob_id);

let content: Vec<_> = rng
.sample_iter(Standard)
.take(BLOCK_SIZE * 2 - HEADER_SIZE)
.collect();
let content = random_bytes(rng, BLOCK_SIZE * 2 - HEADER_SIZE);

for blob in [&mut blob_0, &mut blob_1] {
let mut tx = store.begin_write().await.unwrap();
Expand Down Expand Up @@ -840,11 +822,8 @@ async fn block_ids_of_identical_blocks_in_the_same_blob() {
let blob_id: BlobId = rng.gen();
let mut blob = Blob::create(branch.clone(), blob_id);

let content_block_0: Vec<_> = (&mut rng)
.sample_iter(Standard)
.take(BLOCK_SIZE - HEADER_SIZE)
.collect();
let content_block_1: Vec<_> = (&mut rng).sample_iter(Standard).take(BLOCK_SIZE).collect();
let content_block_0 = random_bytes(&mut rng, BLOCK_SIZE - HEADER_SIZE);
let content_block_1 = random_bytes(&mut rng, BLOCK_SIZE);

let mut tx = store.begin_write().await.unwrap();
let mut changeset = Changeset::new();
Expand Down Expand Up @@ -901,3 +880,12 @@ async fn setup<const N: usize>(rng_seed: u64) -> (StdRng, TempDir, Store, [Branc

(rng, base_dir, store, branches)
}

fn random_bytes<R: Rng>(rng: R, size: usize) -> Vec<u8> {
rng
// The `u8` should be inferred but for some reason it doesn't work when compiling on
// windows, but only on the CI or cross-compilation ¯\_(ツ)_/¯
.sample_iter::<u8, _>(Standard)
.take(size)
.collect()
}
4 changes: 2 additions & 2 deletions lib/src/crypto/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ pub type SignatureError = ext::SignatureError;
#[cfg(test)]
mod tests {
use super::*;
use rand::{distributions::Standard, rngs::StdRng, SeedableRng};
use rand::{rngs::StdRng, SeedableRng};

// This test asserts that signatures from the same keys and input are identical between
// different versions.
Expand All @@ -270,7 +270,7 @@ mod tests {
);

assert_eq!(
dump_signature(&keypair.sign(&rng.sample_iter(Standard).take(32).collect::<Vec<_>>())),
dump_signature(&keypair.sign(&rng.gen::<[u8; 32]>())),
"3230b7f98529273c71f8af92b1581d290bf424fd7bd5015399c6213cbc461ca79ff932a7fcbb5e19d2ef6efa8ed9b833b6d17431793facf1b810c3b579570d0d"
);
}
Expand Down

0 comments on commit 18998fd

Please sign in to comment.