Repository: tikv/tikv
File: tests/failpoints/cases/test_import_service.rs (Rust)

Code before:

    let temp_path = temp_dir.path().to_owned();
    let local_backend = external_storage::make_local_backend(&temp_path);
    let inject_err_fn = |i| i % 2 == 0;
    let inject_mismatch_fn = |i| i == 3;

    let metas: Arc<Mutex<Vec<_>>> = Arc::new(Mutex::new(
        (0..10)
            .map(|i| {
                let file_name: String = format!("test_{}.sst", i);
                let sst_path = temp_path.clone().join(&file_name);
                let sst_range: (u8, u8) = (i, (i + 1) * 2);
                let (mut meta, _) = gen_sst_file(sst_path, sst_range);
                meta.set_region_id(ctx.get_region_id());
                meta.set_region_epoch(ctx.get_region_epoch().clone());
                if inject_err_fn(i) {
                    // random inject the wrong length to make download fail
                    meta.set_length(1);
                }
                (meta, file_name, sst_range)
            })
            .collect(),
    ));

    let threads: Vec<_> = (0..10)
        .flat_map(|i: u8| vec![i, i, i]) // duplciate sst file
        .map(|i| {
            let import = import.clone();
            let metas = Arc::clone(&metas);
            let local_backend = local_backend.clone();

            std::thread::spawn(move || {
                // Run multiple concurrent downloads
                let mut download = DownloadRequest::default();
                let (mut meta, file_name, sst_range) = metas.lock().unwrap()[i as usize].clone();
                if inject_mismatch_fn(i) {
                    // make the partial download request broken
                    meta.crc32 += rand::random::<u32>() % 1000 + 1; // Add a random value between 1 and 1000 to corrupt the checksum
                }
                download.set_sst(meta);
                download.set_storage_backend(local_backend);
                download.set_name(file_name);
                download.mut_sst().mut_range().set_start(vec![sst_range.1]);
                download
                    .mut_sst()
                    .mut_range()
                    .set_end(vec![sst_range.1 + 1]);
                download.mut_sst().mut_range().set_start(Vec::new());
                download.mut_sst().mut_range().set_end(Vec::new());
                let download = download.clone();


Review comment (refactor):
Typo in comment: 'duplciate' should be corrected to 'duplicate' for clarity.
```suggestion
        .flat_map(|i: u8| vec![i, i, i]) // duplicate sst file
```

Code after:

    let temp_path = temp_dir.path().to_owned();
    let local_backend = external_storage::make_local_backend(&temp_path);
    let inject_err_fn = |i| i % 2 == 0;
    let inject_mismatch_fn = |i| i == 3;

    let metas: Arc<Mutex<Vec<_>>> = Arc::new(Mutex::new(
        (0..10)
            .map(|i| {
                let file_name: String = format!("test_{}.sst", i);
                let sst_path = temp_path.clone().join(&file_name);
                let sst_range: (u8, u8) = (i, (i + 1) * 2);
                let (mut meta, _) = gen_sst_file(sst_path, sst_range);
                meta.set_region_id(ctx.get_region_id());
                meta.set_region_epoch(ctx.get_region_epoch().clone());
                if inject_err_fn(i) {
                    // random inject the wrong length to make download fail
                    meta.set_length(1);
                }
                (meta, file_name, sst_range)
            })
            .collect(),
    ));

    let threads: Vec<_> = (0..10)
        .flat_map(|i: u8| vec![i, i, i]) // duplicate sst file
        .map(|i| {
            let import = import.clone();
            let metas = Arc::clone(&metas);
            let local_backend = local_backend.clone();

            std::thread::spawn(move || {
                // Run multiple concurrent downloads
                let mut download = DownloadRequest::default();
                let (mut meta, file_name, sst_range) = metas.lock().unwrap()[i as usize].clone();
                if inject_mismatch_fn(i) {
                    // make the partial download request broken
                    meta.crc32 += rand::random::<u32>() % 1000 + 1; // Add a random value between 1 and 1000 to corrupt the checksum
                }
                download.set_sst(meta);
                download.set_storage_backend(local_backend);
                download.set_name(file_name);
                download.mut_sst().mut_range().set_start(vec![sst_range.1]);
                download
                    .mut_sst()
                    .mut_range()
                    .set_end(vec![sst_range.1 + 1]);
                download.mut_sst().mut_range().set_start(Vec::new());
                download.mut_sst().mut_range().set_end(Vec::new());
                let download = download.clone();