|
| 1 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 2 | +using ParallelTreeWalker.Elements; |
| 3 | +using System; |
| 4 | +using System.Collections.Concurrent; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Diagnostics; |
| 7 | +using System.IO; |
| 8 | +using System.Linq; |
| 9 | +using System.Text; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +namespace ParallelTreeWalker.Tests |
| 13 | +{ |
| 14 | + [TestClass] |
| 15 | + public class FileSystemTests |
| 16 | + { |
| 17 | + private readonly string ROOTNAME = "TestRoot"; |
| 18 | + |
| 19 | + [TestMethod] |
| 20 | + public async Task TwoLevels_Balanced() |
| 21 | + { |
| 22 | + var rootDir = CreateTestStructure(); |
| 23 | + |
| 24 | + var allVisitedPath = await TestWalk(new FileSystemElement(rootDir, true), 5); |
| 25 | + var actual = string.Join(Environment.NewLine, allVisitedPath); |
| 26 | + |
| 27 | + // expected results: enumerate manually, add root dir and sort |
| 28 | + var expected = string.Join(Environment.NewLine, |
| 29 | + Enumerable.Union(new List<string> { rootDir }, Directory.EnumerateFileSystemEntries(rootDir, "*", SearchOption.AllDirectories)).OrderBy(p => p)); |
| 30 | + |
| 31 | + Assert.AreEqual(expected, actual); |
| 32 | + } |
| 33 | + |
| 34 | + public async Task<string[]> TestWalk(ITreeElement root, int maxParallel) |
| 35 | + { |
| 36 | + var allPaths = new ConcurrentBag<string>(); |
| 37 | + |
| 38 | + await TreeWalker.WalkAsync(root, new TreeWalkerOptions |
| 39 | + { |
| 40 | + MaxDegreeOfParallelism = maxParallel, |
| 41 | + ProcessElementAsync = (element) => |
| 42 | + { |
| 43 | + var el = element as FileSystemElement; |
| 44 | + |
| 45 | + Trace.WriteLine(string.Format("##PTW> {0} Path: {1}", DateTime.UtcNow.ToLongTimeString(), el.Path)); |
| 46 | + |
| 47 | + allPaths.Add(el.Path); |
| 48 | + |
| 49 | + return Task.FromResult<object>(null); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + return allPaths.OrderBy(p => p).ToArray(); |
| 54 | + } |
| 55 | + |
| 56 | + private string CreateTestStructure() |
| 57 | + { |
| 58 | + var currentDir = Directory.GetCurrentDirectory(); |
| 59 | + var rootDir = Path.Combine(currentDir, ROOTNAME); |
| 60 | + |
| 61 | + // cleanup |
| 62 | + if (Directory.Exists(rootDir)) |
| 63 | + Directory.Delete(rootDir, true); |
| 64 | + |
| 65 | + var subDirs = new string[] |
| 66 | + { |
| 67 | + Path.Combine(rootDir, @"F1\F11"), |
| 68 | + Path.Combine(rootDir, @"F1\F12"), |
| 69 | + Path.Combine(rootDir, @"F2\F21"), |
| 70 | + Path.Combine(rootDir, @"F2\F22"), |
| 71 | + Path.Combine(rootDir, @"F3\F31"), |
| 72 | + Path.Combine(rootDir, @"F3\F32"), |
| 73 | + }; |
| 74 | + |
| 75 | + foreach (var subDir in subDirs) |
| 76 | + { |
| 77 | + Directory.CreateDirectory(subDir); |
| 78 | + |
| 79 | + using (var writer = File.CreateText(Path.Combine(subDir, "F1.txt"))) |
| 80 | + { |
| 81 | + writer.Write("TEST - " + DateTime.UtcNow); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return rootDir; |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments