Example #1
0
        public void TestPipeline()
        {
            stream = new PipelineStream(256);
            ThreadPool.QueueUserWorkItem(WriteThread);

            var readed = false;

            stream.OnRead += (_) =>
            {
                readed = true;
            };
            var data = new byte[100];
            int read;
            var actual = new StringBuilder();
            var rand   = new Random();

            while ((read = stream.Read(data, 0, data.Length)) != 0)
            {
                var str = Encoding.UTF8.GetString(data, 0, read);
                actual.Append(str);
                Thread.Sleep(rand.Next(1, 5));
            }
            stream.Dispose();

            var expected = new StringBuilder();

            for (var i = 0; i < 1000; i++)
            {
                expected.Append("0123456789");
            }

            Assert.AreEqual(expected.ToString(), actual.ToString());
            Assert.AreEqual(true, readed);
        }
        public void TestLargeWithVaryingChunkSizes()
        {
            FakeRandomGenerator frg = new FakeRandomGenerator();

            byte[] source = frg.Generate(500000);

            int[] chunkSizes = { 1, 2, 3, 5, 7, 11, 13, 17, 19, 64, 128, 256, 257, };

            using (MemoryStream destination = new MemoryStream())
            {
                using (PipelineStream pipeline = new PipelineStream(new CancellationToken()))
                {
                    Task producerTask = Task.Run(() =>
                    {
                        int i     = 0;
                        int total = 0;
                        while (total < source.Length)
                        {
                            int count = chunkSizes[i++ % chunkSizes.Length];
                            count     = total + count > source.Length ? source.Length - total : count;
                            pipeline.Write(source, total, count);
                            total += count;
                        }
                        pipeline.Complete();
                    });

                    Task consumerTask = Task.Run(() =>
                    {
                        byte[] read = new byte[17];
                        int count;
                        while ((count = pipeline.Read(read, 0, read.Length)) > 0)
                        {
                            destination.Write(read, 0, count);
                        }
                    });

                    Task.WaitAll(producerTask, consumerTask);
                }

                byte[] result = destination.ToArray();

                Assert.That(source.IsEquivalentTo(result), "The source and result should be the same after passing through the pipeline.");
            }
        }
        public void CompleteTest()
        {
            using var Stream = new PipelineStream();
            Assert.AreEqual(0, Stream.Length);
            var WriteMessage1 = "HELLO";

            Stream.Write(Encoding.GetBytes(WriteMessage1));
            Stream.Flush();
            var ReadCount = Encoding.GetByteCount(WriteMessage1);

            Assert.AreEqual(ReadCount, Stream.Length);
            var ReadBytes = new byte[ReadCount];

            ReadCount = Stream.Read(ReadBytes);
            Assert.AreEqual(0, Stream.Length);
            var ReadMessage1 = Encoding.GetString(ReadBytes.AsSpan().Slice(0, ReadCount));

            Assert.AreEqual(WriteMessage1, ReadMessage1);

            Stream.Complete();
            var WriteMessage2 = "HI!";

            Assert.ThrowsException <InvalidOperationException>(() => Stream.Write(Encoding.GetBytes(WriteMessage2)));
        }