本文整理汇总了C#中Pipe.SetFixedLength方法的典型用法代码示例。如果您正苦于以下问题:C# Pipe.SetFixedLength方法的具体用法?C# Pipe.SetFixedLength怎么用?C# Pipe.SetFixedLength使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Pipe
的用法示例。
在下文中一共展示了Pipe.SetFixedLength方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestWriteTimeout
public void TestWriteTimeout()
{
var pipe = new Pipe {InputStream = {WriteTimeout = 0}};
pipe.SetFixedLength();
pipe.WriteText(new string('a', 2*Pipe.ByteBufferSize));
var asyncWrite = pipe.InputStream.WriteAsync(new byte[1], 0, 1);
Assert.AreEqual(true, asyncWrite.ContinueWith(_ => { }).Wait(TimeSpan.FromSeconds(.01)));
Assert.AreEqual(true, asyncWrite.IsFaulted);
Assert.IsInstanceOf<TimeoutException>(asyncWrite.Exception.InnerException);
}
示例2: TestLargeStreamWithFixedLength
public void TestLargeStreamWithFixedLength()
{
var pipe = new Pipe();
pipe.SetFixedLength();
var bytes = Enumerable.Range(0, 5*Pipe.ByteBufferSize)
.Select(b => (byte) (b%256))
.ToArray();
var asyncWrite = pipe.InputStream.WriteAsync(bytes, 0, bytes.Length)
.ContinueWith(_ => pipe.InputStream.Close());
var memoryStream = new MemoryStream();
var buffer = new byte[777];
int bytesRead;
while ((bytesRead = pipe.OutputStream.Read(buffer, 0, buffer.Length)) > 0)
{
memoryStream.Write(buffer, 0, bytesRead);
}
Assert.AreEqual(true, asyncWrite.Wait(TimeSpan.FromSeconds(1)));
Assert.That(memoryStream.ToArray().SequenceEqual(bytes));
}
示例3: TestPartialWriteDoesNotTimeout
public void TestPartialWriteDoesNotTimeout()
{
var pipe = new Pipe {InputStream = {WriteTimeout = 0}, OutputStream = {ReadTimeout = 1000}};
pipe.SetFixedLength();
var text = Enumerable.Repeat((byte) 't', 3*Pipe.ByteBufferSize).ToArray();
var asyncWrite = pipe.InputStream.WriteAsync(text, 0, text.Length);
Assert.AreEqual(false, asyncWrite.Wait(TimeSpan.FromSeconds(.01)));
Assert.AreEqual(new string(text.Select(b => (char) b).ToArray()), pipe.ReadTextAsync(text.Length).Result);
}
示例4: FuzzTest
public void FuzzTest()
{
const int ByteCount = 100000;
var pipe = new Pipe();
var writeTask = Task.Run(async () =>
{
var memoryStream = new MemoryStream();
var random = new Random(1234);
var bytesWritten = 0;
while (bytesWritten < ByteCount)
{
//Console.WriteLine("Writing " + memoryStream.Length);
switch (random.Next(10))
{
case 1:
//Console.WriteLine("SETTING FIXED LENGTH");
pipe.SetFixedLength();
break;
case 2:
case 3:
await Task.Delay(1);
break;
default:
var bufferLength = random.Next(0, 5000);
var offset = random.Next(0, bufferLength + 1);
var count = random.Next(0, bufferLength - offset + 1);
var buffer = new byte[bufferLength];
random.NextBytes(buffer);
memoryStream.Write(buffer, offset, count);
//Console.WriteLine("WRITE START");
await pipe.InputStream.WriteAsync(buffer, offset, count);
//Console.WriteLine("WRITE END");
bytesWritten += count;
break;
}
}
//Console.WriteLine("WRITER ALL DONE");
pipe.InputStream.Close();
return memoryStream;
});
var readTask = Task.Run(async () =>
{
var memoryStream = new MemoryStream();
var random = new Random(5678);
while (true)
{
//Console.WriteLine("Reading " + memoryStream.Length);
if (random.Next(10) == 1)
{
await Task.Delay(1);
}
var bufferLength = random.Next(0, 5000);
var offset = random.Next(0, bufferLength + 1);
var count = random.Next(0, bufferLength - offset + 1);
var buffer = new byte[bufferLength];
//Console.WriteLine("READ START");
var bytesRead = await pipe.OutputStream.ReadAsync(buffer, offset, count);
//Console.WriteLine("READ END");
if (bytesRead == 0 && count > 0)
{
// if we tried to read more than 1 byte and we got 0, the pipe is done
//Console.WriteLine("READER ALL DONE");
return memoryStream;
}
memoryStream.Write(buffer, offset, bytesRead);
}
});
Assert.AreEqual(true, Task.WhenAll(writeTask, readTask).Wait(TimeSpan.FromSeconds(5)));
CollectionAssert.AreEqual(writeTask.Result.ToArray(), readTask.Result.ToArray());
}
示例5: TestConcurrentWrites
public void TestConcurrentWrites()
{
var pipe = new Pipe();
pipe.SetFixedLength();
var longText = new string('x', (2*Pipe.ByteBufferSize) + 1);
var asyncWrite = pipe.WriteTextAsync(longText);
Assert.AreEqual(false, asyncWrite.Wait(TimeSpan.FromSeconds(.01)));
Assert.Throws<InvalidOperationException>(() => pipe.InputStream.WriteByte(101));
pipe.OutputStream.Close();
Assert.AreEqual(true, asyncWrite.Wait(TimeSpan.FromSeconds(5)));
}
示例6: TestPartialWriteDoesNotCancel
public void TestPartialWriteDoesNotCancel()
{
var pipe = new Pipe();
var cancellationTokenSource = new CancellationTokenSource();
pipe.WriteText(new string('a', 2*Pipe.ByteBufferSize));
var asyncRead = pipe.ReadTextAsync(1);
Assert.AreEqual(true, asyncRead.Wait(TimeSpan.FromSeconds(5)));
Assert.AreEqual("a", asyncRead.Result);
pipe.SetFixedLength();
var bytes = Enumerable.Repeat((byte) 'b', Pipe.ByteBufferSize).ToArray();
var asyncWrite = pipe.InputStream.WriteAsync(bytes, 0, bytes.Length, cancellationTokenSource.Token);
Assert.AreEqual(false, asyncWrite.Wait(TimeSpan.FromSeconds(.01)));
cancellationTokenSource.Cancel();
Assert.AreEqual(false, asyncWrite.Wait(TimeSpan.FromSeconds(.01)));
asyncRead = pipe.ReadTextAsync((3*Pipe.ByteBufferSize) - 1);
Assert.AreEqual(true, asyncRead.Wait(TimeSpan.FromSeconds(5)));
Assert.AreEqual(
new string('a', (2*Pipe.ByteBufferSize) - 1) + new string('b', Pipe.ByteBufferSize),
asyncRead.Result);
}
示例7: TestCancelWrite
public void TestCancelWrite()
{
var pipe = new Pipe();
var cancellationTokenSource = new CancellationTokenSource();
pipe.WriteText(new string('a', 2*Pipe.ByteBufferSize));
pipe.SetFixedLength();
var bytes = Enumerable.Repeat((byte) 'b', Pipe.ByteBufferSize).ToArray();
var asyncWrite = pipe.InputStream.WriteAsync(bytes, 0, bytes.Length, cancellationTokenSource.Token);
Assert.AreEqual(false, asyncWrite.Wait(TimeSpan.FromSeconds(.01)));
cancellationTokenSource.Cancel();
Assert.AreEqual(true, asyncWrite.ContinueWith(_ => { }).Wait(TimeSpan.FromSeconds(5)));
Assert.AreEqual(true, asyncWrite.IsCanceled);
}