|
| 1 | +/* |
| 2 | + * This file is part of the CatLib package. |
| 3 | + * |
| 4 | + |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + * |
| 9 | + * Document: http://catlib.io/ |
| 10 | + */ |
| 11 | + |
| 12 | +using System; |
| 13 | +using System.IO; |
| 14 | +using System.Text; |
| 15 | +using System.Threading; |
| 16 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 17 | + |
| 18 | +namespace CatLib.Core.Tests.Support.Stream |
| 19 | +{ |
| 20 | + [TestClass] |
| 21 | + public class PipelineStreamTests |
| 22 | + { |
| 23 | + private PipelineStream stream; |
| 24 | + |
| 25 | + [TestMethod] |
| 26 | + public void TestPipeline() |
| 27 | + { |
| 28 | + stream = new PipelineStream(256); |
| 29 | + ThreadPool.QueueUserWorkItem(WriteThread); |
| 30 | + |
| 31 | + var wrote = false; |
| 32 | + stream.OnWrote += (_) => |
| 33 | + { |
| 34 | + wrote = true; |
| 35 | + }; |
| 36 | + var data = new byte[100]; |
| 37 | + int read; |
| 38 | + var actual = new StringBuilder(); |
| 39 | + var rand = new Random(); |
| 40 | + while ((read = stream.Read(data, 0, data.Length)) != 0) |
| 41 | + { |
| 42 | + var str = Encoding.UTF8.GetString(data, 0, read); |
| 43 | + actual.Append(str); |
| 44 | + Thread.Sleep(rand.Next(1, 5)); |
| 45 | + } |
| 46 | + stream.Dispose(); |
| 47 | + |
| 48 | + var expected = new StringBuilder(); |
| 49 | + for (var i = 0; i < 1000; i++) |
| 50 | + { |
| 51 | + expected.Append("0123456789"); |
| 52 | + } |
| 53 | + |
| 54 | + Assert.AreEqual(expected.ToString(), actual.ToString()); |
| 55 | + Assert.AreEqual(true, wrote); |
| 56 | + } |
| 57 | + |
| 58 | + public void WriteThread(object obj) |
| 59 | + { |
| 60 | + var data = Encoding.UTF8.GetBytes("0123456789"); |
| 61 | + for (var i = 0; i < 1000; i++) |
| 62 | + { |
| 63 | + stream.Write(data, 0, data.Length); |
| 64 | + } |
| 65 | + stream.Close(); |
| 66 | + } |
| 67 | + |
| 68 | + [TestMethod] |
| 69 | + [ExpectedException(typeof(ObjectDisposedException))] |
| 70 | + public void TestClosedAndWrite() |
| 71 | + { |
| 72 | + var stream = new PipelineStream(256); |
| 73 | + Assert.AreEqual(true, stream.CanWrite); |
| 74 | + stream.Write(Encoding.UTF8.GetBytes("0123456789"), 0, 10); |
| 75 | + stream.Close(); |
| 76 | + Assert.AreEqual(false, stream.CanWrite); |
| 77 | + Assert.AreEqual(true, stream.CanRead); |
| 78 | + Assert.AreEqual(true, stream.Closed); |
| 79 | + stream.Write(Encoding.UTF8.GetBytes("0123456789"), 0, 10); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + [TestMethod] |
| 84 | + [ExpectedException(typeof(ObjectDisposedException))] |
| 85 | + public void TestDisposeAndWrite() |
| 86 | + { |
| 87 | + var stream = new PipelineStream(256); |
| 88 | + Assert.AreEqual(true, stream.CanWrite); |
| 89 | + Assert.AreEqual(false, stream.CanRead); |
| 90 | + stream.Dispose(); |
| 91 | + Assert.AreEqual(false, stream.CanWrite); |
| 92 | + Assert.AreEqual(false, stream.CanRead); |
| 93 | + stream.Write(Encoding.UTF8.GetBytes("0123456789"), 0, 10); |
| 94 | + } |
| 95 | + |
| 96 | + [TestMethod] |
| 97 | + public void TestCanReadCanWrite() |
| 98 | + { |
| 99 | + var stream = new PipelineStream(256); |
| 100 | + stream.Write(Encoding.UTF8.GetBytes("0123456789"), 0, 10); |
| 101 | + Assert.AreEqual(true, stream.CanWrite); |
| 102 | + Assert.AreEqual(true, stream.CanRead); |
| 103 | + } |
| 104 | + |
| 105 | + [TestMethod] |
| 106 | + public void TestCanSeek() |
| 107 | + { |
| 108 | + var stream = new PipelineStream(256); |
| 109 | + Assert.AreEqual(false, stream.CanSeek); |
| 110 | + } |
| 111 | + |
| 112 | + [TestMethod] |
| 113 | + [ExpectedException(typeof(NotSupportedException))] |
| 114 | + public void TestSeek() |
| 115 | + { |
| 116 | + var stream = new PipelineStream(256); |
| 117 | + stream.Seek(0, SeekOrigin.Begin); |
| 118 | + } |
| 119 | + |
| 120 | + [TestMethod] |
| 121 | + [ExpectedException(typeof(NotSupportedException))] |
| 122 | + public void TestSetLength() |
| 123 | + { |
| 124 | + var stream = new PipelineStream(256); |
| 125 | + stream.SetLength(0); |
| 126 | + } |
| 127 | + |
| 128 | + [TestMethod] |
| 129 | + [ExpectedException(typeof(NotSupportedException))] |
| 130 | + public void TestSetPosition() |
| 131 | + { |
| 132 | + var stream = new PipelineStream(256); |
| 133 | + stream.Position = 10; |
| 134 | + } |
| 135 | + |
| 136 | + [TestMethod] |
| 137 | + [ExpectedException(typeof(NotSupportedException))] |
| 138 | + public void TestGetPosition() |
| 139 | + { |
| 140 | + var stream = new PipelineStream(256); |
| 141 | + var pos = stream.Position; |
| 142 | + } |
| 143 | + |
| 144 | + [TestMethod] |
| 145 | + [ExpectedException(typeof(NotSupportedException))] |
| 146 | + public void TestGetLength() |
| 147 | + { |
| 148 | + var stream = new PipelineStream(256); |
| 149 | + var length = stream.Length; |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments