示例#1
0
 static void TestAsyncFileWriter(int boundedCapacity = -1)
 {
     Console.WriteLine("{0:#,##0} bounded capacity.", boundedCapacity);
     Test((filePath, handler) =>
     {
         using (var writer = new AsyncFileWriter(filePath, boundedCapacity))
             handler(s => writer.SendAsync(s).Wait());
     });
 }
示例#2
0
        static void Main(string[] args)
        {
            var dir      = Environment.CurrentDirectory;
            var fileName = args.FirstOrDefault() ?? "AsyncFileWriterTest.txt";
            var filePath = Path.Combine(dir, fileName);

            File.Delete(filePath);             // Start from scratch. (comment out for further appends.)

            Console.WriteLine($"Writing to file: {filePath}");
            using (var writer = new AsyncFileWriter(filePath))
            {
                // Test to ensure blocks are linkable and properly pass messages.
                var buffer = new TransformBlock <string, byte[]>(
                    value => Encoding.UTF8.GetBytes(value + '\n'),
                    new ExecutionDataflowBlockOptions {
                    BoundedCapacity = 200000                             // Test a max pre-buffered amount here.
                });

                buffer.LinkTo(writer, new DataflowLinkOptions {
                    PropagateCompletion = true
                });
                writer.Completion.ContinueWith(t => {
                    buffer.Complete();
                    buffer.LinkTo(DataflowBlock.NullTarget <byte[]>());                    // Empty the buffer and allow the buffer to complete.
                });

                void write(int i)
                {
                    var message = $"{i}) {DateTime.Now}";

                    if (!buffer.Post(message) && !buffer.Completion.IsCompleted)
                    {
                        buffer.SendAsync(message).Wait();
                    }
                }

                Parallel.For(0, 10000, write);
                Parallel.For(10000, 20000, write);

                //writer.Fault(new Exception("Stop!"));

                Task.Delay(1).Wait();
                Parallel.For(20000, 100000, write);

                Task.Delay(1000).Wait();                 // Demonstrate that when nothing buffered the active stream closes.
                Parallel.For(100000, 1000000, write);

                buffer.Complete();
                buffer.Completion.Wait();

                if (writer.Completion.IsFaulted)
                {
                    throw writer.Completion.Exception;
                }
            }
        }
 public static Task TestAsyncFileWriter(int boundedCapacity, bool asyncFileWrite)
 {
     Console.WriteLine("{0:#,##0} bounded capacity.", boundedCapacity);
     return(RunAndReportToConsole(async(filePath, handler) =>
     {
         var writer = new AsyncFileWriter(filePath, boundedCapacity, asyncFileStream: true, asyncFileWrite: asyncFileWrite);
         await handler(s => writer.AddAsync(s));
         await writer.DisposeAsync();
     }));
 }
 public void SetUp()
 {
     pathToFile      = Environment.CurrentDirectory + "\\Result\\Test.cs";
     pathToDirectory = Environment.CurrentDirectory + "\\Result";
     asyncReader     = new AsyncFileReader();
     asyncWriter     = new AsyncFileWriter(pathToDirectory);
     fileName        = "Test.cs";
     testString      = "Write and read test!";
     template        = new TestClassTemplate(fileName, testString);
 }
示例#5
0
        private async void btnGenerate_Click(object sender, EventArgs e)
        {
            if (tbMaxRead.Text.Length == 0 || tbMaxProcess.Text.Length == 0 || tbMaxWrite.Text.Length == 0)
            {
                MessageBox.Show("Please, input initial parameters");
                return;
            }

            int maxReadFilesCount    = Convert.ToInt32(tbMaxRead.Text);
            int maxProcessTasksCount = Convert.ToInt32(tbMaxProcess.Text);
            int maxWriteFilesCount   = Convert.ToInt32(tbMaxWrite.Text);

            if (!(maxReadFilesCount > 0 && maxProcessTasksCount > 0 && maxWriteFilesCount > 0))
            {
                MessageBox.Show("Bad initial parameters");
                return;
            }

            if (files.Count == 0)
            {
                MessageBox.Show("List of files is empty. Choose minimum one class.");
                return;
            }

            TestsGenerator.TestGenerator generator = new TestsGenerator.TestGenerator(files, maxReadFilesCount, maxProcessTasksCount, maxWriteFilesCount);

            string folderPath;

            using (FolderBrowserDialog fbd = new FolderBrowserDialog())
            {
                fbd.SelectedPath = @"C:\Users\angel\OneDrive\Рабочий стол\BSUIR\5 сем\СПП\TestsGenerator\Output\";
                DialogResult result = fbd.ShowDialog();
                if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
                {
                    folderPath = fbd.SelectedPath;
                }
                else
                {
                    return;
                }
            }
            if (!string.IsNullOrEmpty(folderPath))
            {
                AsyncFileWriter asyncWriter = new AsyncFileWriter(folderPath);
                await generator.Generate(asyncWriter);
            }
            else
            {
                return;
            }
        }
示例#6
0
        public void SetUp()
        {
            resultPath  = Environment.CurrentDirectory + "\\ResultTests";
            asyncWriter = new AsyncFileWriter(resultPath);

            files = new List <string>();
            string pathToFile1 = Environment.CurrentDirectory + "\\SourceCodeFiles\\Class.cs";

            files.Add(pathToFile1);

            maxReadableCount    = 3;
            maxProcessableCount = 3;
            maxWritableCount    = 3;
            testsGenerator      = new TestsGenerator(files, maxReadableCount, maxProcessableCount, maxWritableCount);
        }
示例#7
0
        private static void Main(string[] args)
        {
            string path = "ExampleFile.txt";
            Random random = new Random();
            byte[] writtenBytes = new byte[1000000];
            for (int i = 0; i < writtenBytes.Length; ++i)
            {
                writtenBytes[i] = (byte)('A' + (i % 7));
            }

            AsyncFileWriter writer = new AsyncFileWriter(99999);
            writer.WriteAllBytesAsync(path, writtenBytes).Wait();
            Console.WriteLine("Wrote {0} bytes.", writtenBytes.Length);

            AsyncFileReader reader = new AsyncFileReader(100001);
            Task<byte[]> task = reader.ReadAllBytesAsync(path);
            byte[] readBytes = task.Result;
            Console.WriteLine("Read {0} bytes.", readBytes.Length);
            Console.WriteLine("First 30 bytes: " + Encoding.ASCII.GetString(readBytes, 0, 30));
            Console.WriteLine("Last 30 bytes: " + Encoding.ASCII.GetString(readBytes, readBytes.Length - 30, 30));

            if (writtenBytes.Length != readBytes.Length)
            {
                throw new InvalidOperationException("Lengths do not match.");
            }

            for (int i = 0; i < readBytes.Length; ++i)
            {
                if (writtenBytes[i] != readBytes[i])
                {
                    throw new InvalidOperationException("Mismatching byte values at index " + i);
                }
            }

            Console.WriteLine("All byte values match!");
        }
示例#8
0
 // ReSharper disable once MemberCanBeProtected.Global
 public ConsoleEmitterBase(uint sampleMinimum = 50, string logFilePath = null)
 {
     LogFile       = logFilePath == null ? null : new AsyncFileWriter(logFilePath, 1000);
     SampleMinimum = sampleMinimum;
 }