private void ConfigureTest(WaitFile wf, bool startWithJob = false) { CancellationTokenSource cts = startWithJob?job.TokenSource: new CancellationTokenSource(); var token = cts.Token; token.ThrowIfCancellationRequested(); //wf.StopOnFirst = true; var disp = wf.Output.Subscribe(file => { Debug.WriteLine($"{ file.EventArgs.Name} has {file.EventArgs.ChangeType.ToString()}"); cts.Cancel();//stop on first }, () => { //System.Environment.Exit(1); Debug.WriteLine("Stop listening"); //completed = true; }); var cpt = 0; var filename = $@"d:\{ ETLString.GetAsciiRandomString(6)}.txt"; var delayed = Task.Delay(500).ContinueWith(t => { //if (File.Exists(filename)) File.Delete(filename); Debug.WriteLine($"Create {filename}"); File.WriteAllText(filename, ETLString.GetAsciiRandomString(50)); Debug.WriteLine($"modify {filename}"); File.AppendAllLines(filename, new string[] { ETLString.GetAsciiRandomString(50) }); Debug.WriteLine($"{filename} modified"); Assert.IsTrue(true, "Test passed"); cts.Cancel(); }); var checker = Task.Delay(3000).ContinueWith(t => { cts.Cancel(); Assert.IsTrue(false, "Checker cancelled test"); }); Task.Run(() => { Debug.WriteLine("Start delayed"); return(delayed); }); Task.Run(() => checker); if (startWithJob) { Task.Run(() => Start()); } else { wf.Start(cts.Token); } while (!token.IsCancellationRequested) { Thread.Sleep(100); Debug.Write("."); if (++cpt > 20) { Debug.WriteLine(""); cpt = 0; } } File.Delete(filename); Assert.IsFalse(File.Exists(filename)); }
public void TestWaitfile() { // bool completed = false; Debug.WriteLine("Start wait file Test"); WaitFile wf = new WaitFile(options); ConfigureTest(wf); }