From e782ed2f190b9bfa65c93f197852e483d20b010b Mon Sep 17 00:00:00 2001 From: Farhan Date: Thu, 23 Jan 2025 14:26:45 +0530 Subject: [PATCH] Run cargo tests on all platforms (#172) --- .github/workflows/ci.yml | 75 +++++++++++++++++++++------------------- Makefile | 2 +- src/compaction.rs | 66 +++++++++++++++++++++++++++++++++++ src/store.rs | 66 ----------------------------------- 4 files changed, 107 insertions(+), 102 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0878c40b..f7de459f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,24 +14,34 @@ defaults: shell: bash jobs: - build: - runs-on: ubuntu-latest + build-test: strategy: matrix: target: [ 'x86_64-unknown-linux-gnu', 'aarch64-unknown-linux-gnu', 'x86_64-pc-windows-msvc', 'x86_64-apple-darwin', 'aarch64-apple-darwin', 'wasm32-unknown-unknown' ] + include: + - target: x86_64-unknown-linux-gnu + os: ubuntu-latest + - target: aarch64-unknown-linux-gnu + os: ubuntu-latest + - target: x86_64-pc-windows-msvc + os: windows-latest + - target: x86_64-apple-darwin + os: macos-latest + - target: aarch64-apple-darwin + os: macos-latest + - target: wasm32-unknown-unknown + os: ubuntu-latest + + runs-on: ${{ matrix.os }} + steps: - - uses: actions/checkout@v2 - - name: Cache - id: rust-cache - uses: actions/cache@v3 + - name: Checkout sources + uses: actions/checkout@v4 + + - name: Setup cache + uses: Swatinem/rust-cache@v2 with: - path: | - ~/.cargo/bin/ - ~/.cargo/registry/index/ - ~/.cargo/registry/cache/ - ~/.cargo/git/db/ - target/ - key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml', '.github/workflows/*.yml') }} + save-if: ${{ github.ref == 'refs/heads/main' }} - uses: actions-rs/toolchain@v1 with: @@ -40,32 +50,27 @@ jobs: components: rustfmt, clippy default: true - - name: Compile + - name: Build run: cargo build --target=${{ matrix.target }} - ci: - strategy: - matrix: - os: ["ubuntu-latest"] - - runs-on: ${{ matrix.os }} + - name: Run tests + if: ${{ !contains(matrix.os, 'windows') }} + # Skip tests on Windows as compaction tests are not supported + run: make test + ci: + runs-on: ubuntu-latest env: RUSTFLAGS: --deny warnings steps: - - uses: actions/checkout@v2 - - name: Cache - id: rust-cache - uses: actions/cache@v3 + - name: Checkout sources + uses: actions/checkout@v4 + + - name: Setup cache + uses: Swatinem/rust-cache@v2 with: - path: | - ~/.cargo/bin/ - ~/.cargo/registry/index/ - ~/.cargo/registry/cache/ - ~/.cargo/git/db/ - target/ - key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml', '.github/workflows/*.yml') }} + save-if: ${{ github.ref == 'refs/heads/main' }} - uses: actions-rs/toolchain@v1 with: @@ -74,13 +79,13 @@ jobs: default: true - name: Install cargo-deny - run: cargo install --debug --locked cargo-deny@0.14.24 + run: which cargo-deny || cargo install --debug --locked cargo-deny@0.14.24 - - name: Run tests - run: make test + - name: License check + run: cargo deny --all-features check licenses - name: Clippy run: cargo clippy --all --all-targets -- -Dwarnings - name: Format - run: cargo fmt --all -- --check + run: cargo fmt --all -- --check \ No newline at end of file diff --git a/Makefile b/Makefile index e073b370..8ca82402 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ check: cargo clippy --all --all-targets # This command runs the tests with backtrace enabled. -test: check +test: RUST_BACKTRACE=1 cargo test .PHONY: build-cli diff --git a/src/compaction.rs b/src/compaction.rs index 752177e8..cb4bc443 100644 --- a/src/compaction.rs +++ b/src/compaction.rs @@ -1499,4 +1499,70 @@ mod tests { // Close the store store.close().unwrap(); } + + #[test] + fn compaction_preserves_key_deletion() { + // Create a temporary directory for testing + let temp_dir = create_temp_directory(); + + // Create store options with the test directory + let mut opts = Options::new(); + opts.dir = temp_dir.path().to_path_buf(); + opts.max_value_threshold = 0; + opts.max_value_cache_size = 0; + + // Create a new store instance with VariableKey as the key type + let store = Store::new(opts.clone()).expect("should create store"); + + // Number of keys to generate and write + let num_keys_to_write = 1; + + // Create a vector to store the generated keys + let mut keys: Vec = Vec::new(); + + for counter in 1usize..=num_keys_to_write { + // Convert the counter to Bytes + let key_bytes = Bytes::from(counter.to_le_bytes().to_vec()); + + // Add the key to the vector + keys.push(key_bytes); + } + + let default_value = Bytes::from("default_value".to_string()); + let default_value2 = Bytes::from("default_value2".to_string()); + + // Write the keys to the store + for key in keys.iter() { + let mut txn = store.begin().unwrap(); + txn.set(key, &default_value).unwrap(); + txn.commit().unwrap(); + } + + for key in keys.iter() { + let mut txn = store.begin().unwrap(); + txn.set(key, &default_value2).unwrap(); + txn.commit().unwrap(); + } + + let key_bytes = Bytes::from(2usize.to_le_bytes().to_vec()); + let mut txn = store.begin().unwrap(); + txn.set(&key_bytes, &default_value2).unwrap(); + txn.commit().unwrap(); + + // Delete the first 5 keys from the store + for key in keys.iter() { + let mut txn = store.begin().unwrap(); + txn.delete(key).unwrap(); + txn.commit().unwrap(); + } + + store.compact().unwrap(); + store.close().unwrap(); + + let reopened_store = Store::new(opts).expect("should reopen store"); + for key in keys.iter() { + let mut txn = reopened_store.begin().unwrap(); + assert!(txn.get(key).unwrap().is_none()); + } + } } diff --git a/src/store.rs b/src/store.rs index 82314eb4..77cffdb8 100644 --- a/src/store.rs +++ b/src/store.rs @@ -1102,72 +1102,6 @@ mod tests { } } - #[test] - fn basic_compaction1() { - // Create a temporary directory for testing - let temp_dir = create_temp_directory(); - - // Create store options with the test directory - let mut opts = Options::new(); - opts.dir = temp_dir.path().to_path_buf(); - opts.max_value_threshold = 0; - opts.max_value_cache_size = 0; - - // Create a new store instance with VariableKey as the key type - let store = Store::new(opts.clone()).expect("should create store"); - - // Number of keys to generate and write - let num_keys_to_write = 1; - - // Create a vector to store the generated keys - let mut keys: Vec = Vec::new(); - - for counter in 1usize..=num_keys_to_write { - // Convert the counter to Bytes - let key_bytes = Bytes::from(counter.to_le_bytes().to_vec()); - - // Add the key to the vector - keys.push(key_bytes); - } - - let default_value = Bytes::from("default_value".to_string()); - let default_value2 = Bytes::from("default_value2".to_string()); - - // Write the keys to the store - for key in keys.iter() { - let mut txn = store.begin().unwrap(); - txn.set(key, &default_value).unwrap(); - txn.commit().unwrap(); - } - - for key in keys.iter() { - let mut txn = store.begin().unwrap(); - txn.set(key, &default_value2).unwrap(); - txn.commit().unwrap(); - } - - let key_bytes = Bytes::from(2usize.to_le_bytes().to_vec()); - let mut txn = store.begin().unwrap(); - txn.set(&key_bytes, &default_value2).unwrap(); - txn.commit().unwrap(); - - // Delete the first 5 keys from the store - for key in keys.iter() { - let mut txn = store.begin().unwrap(); - txn.delete(key).unwrap(); - txn.commit().unwrap(); - } - - store.compact().unwrap(); - store.close().unwrap(); - - let reopened_store = Store::new(opts).expect("should reopen store"); - for key in keys.iter() { - let mut txn = reopened_store.begin().unwrap(); - assert!(txn.get(key).unwrap().is_none()); - } - } - #[test] fn test_store_with_varying_segment_sizes() { let temp_dir = create_temp_directory();