コード例 #1
0
        static void TestWatchNonexistantFile()
        {
            WithTempFile(path => {
                try { File.Delete(path); } catch (IOException) { Inconclusive("Should've deleted temp file"); return; }

                try {
                    Watch.FileLines(path, text => {}, error => {});
                    Fail("Watch.FileLines(nonexistant, ...) should invoke only the error delegate");
                }
                catch (FileNotFoundException) {}
            });
        }
コード例 #2
0
        static void TestWatchExistingFile_ThenDelete()
        {
            WithTempFile(path => {
                var lines = RandomLines(PRNG);

                var result    = new TaskCompletionSource <string[]>();
                var errResult = new TaskCompletionSource <Exception>();
                File.WriteAllLines(path, lines);
                Watch.FileLines(path,
                                read => result.SetResult(read.ToArray()),
                                error => errResult.SetResult(error));
                Assert(EqualStringArrays(result.Task.Result, lines), "Watch.FileLines(existant, ...) should invoke the text delegate once");

                // Now delete
                try { File.Delete(path); } catch (IOException) { Inconclusive("Should've deleted temp file"); return; }
                var errTask = errResult.Task;
                Assert(errTask.Wait(1000) && (errTask.Result is FileNotFoundException), "Watch.FileLines(nonexistant, ...) should invoke only the error delegate");
            });
        }
コード例 #3
0
        static void TestWatchMutatingFile()
        {
            WithTempFile(path => {
                var finalLines = RandomLines(PRNG);
                var lines      = RandomLines(PRNG);

                var result = new TaskCompletionSource <bool>();
                File.WriteAllLines(path, lines);
                Watch.FileLines(path,
                                read => { if (EqualStringArrays(read.ToArray(), finalLines))
                                          {
                                              result.SetResult(true);
                                          }
                                },
                                error => { if (!(error is IOException))
                                           {
                                               result.SetResult(false);
                                           }
                                });                                                                          // Highly contested files are likely to throw IOException s.

                // Stress
                for (var i = 0; i < 100; ++i)
                {
                    try { File.WriteAllLines(path, RandomLines(PRNG)); } catch (IOException) { }
                }
                Thread.Sleep(10);
                for (var i = 0; i < 100; ++i)
                {
                    try { File.WriteAllLines(path, RandomLines(PRNG)); } catch (IOException) { }
                }
                for (;;)
                {
                    try { File.WriteAllLines(path, finalLines); break; } catch (IOException) { }
                }

                Assert(result.Task.Wait(1000) && result.Task.Result, "Watch.FileLines(existant, ...) should have ended up with the right final result.");
            });
        }