mod common;
use anyhow::Result;
use common::TestRepoSetup;
use git_sync_rs::{watch_with_periodic_sync, SyncConfig, WatchConfig};
use std::fs;
use std::path::Path;
use std::time::{Duration, Instant};
use tokio::time::timeout;
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn continuous_changes_starve_debounce() -> Result<()> {
let setup = TestRepoSetup::new()?;
setup.commit_file("README.md", "# Initial\n", "Initial commit")?;
setup.push()?;
let second_clone = setup.create_second_clone("second")?;
let sync_config = SyncConfig {
sync_new_files: true,
skip_hooks: false,
commit_message: Some("Auto-sync: {hostname} at {timestamp}".to_string()),
remote_name: "origin".to_string(),
branch_name: "master".to_string(),
conflict_branch: false,
target_branch: None,
};
let watch_config = WatchConfig {
debounce_ms: 100, min_interval_ms: 200,
sync_on_start: false,
dry_run: false,
..Default::default()
};
let local_path = setup.local_path.clone();
let watch_handle = tokio::spawn(async move {
watch_with_periodic_sync(&local_path, sync_config, watch_config, Some(200)).await
});
tokio::time::sleep(Duration::from_millis(500)).await;
let burst_file = setup.local_path.join("burst.txt");
let writer = tokio::spawn({
let burst_file = burst_file.clone();
async move {
let start = Instant::now();
while start.elapsed() < Duration::from_secs(3) {
fs::write(&burst_file, format!("tick at {:?}\n", Instant::now()))?;
tokio::time::sleep(Duration::from_millis(40)).await; }
Result::<()>::Ok(())
}
});
tokio::time::sleep(Duration::from_millis(800)).await;
timeout(Duration::from_secs(8), async {
loop {
setup.pull_in(&second_clone)?;
if Path::new(&second_clone).join("burst.txt").exists() {
break;
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
Result::<()>::Ok(())
})
.await
.map_err(|_| {
anyhow::anyhow!("Timed out waiting for burst.txt to be synced during continuous changes")
})??;
writer.await??;
watch_handle.abort();
Ok(())
}