Skip to content

Commit

Permalink
Addressing flakiness of a couple of tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
zlatanov committed Apr 26, 2021
1 parent 5a0e435 commit 935969a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
26 changes: 17 additions & 9 deletions src/libraries/System.Net.WebSockets/tests/WebSocketDeflateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -493,19 +494,23 @@ public async Task ReceiveInvalidCompressedData()
Assert.Equal(WebSocketState.Aborted, client.State);
}

[Fact]
public async Task PayloadShouldHaveSimilarSizeWhenSplitIntoSegments()
[Theory]
[MemberData(nameof(SupportedWindowBits))]
public async Task PayloadShouldHaveSimilarSizeWhenSplitIntoSegments(int windowBits)
{
WebSocketTestStream stream = new();
MemoryStream stream = new();
WebSocket client = WebSocket.CreateFromStream(stream, new WebSocketCreationOptions
{
DangerousDeflateOptions = new WebSocketDeflateOptions()
{
ClientMaxWindowBits = windowBits
}
});

// We're using a frame size that is close to the sliding window size for the deflate
const int frameSize = 32_000;
int frameSize = 2 << windowBits;

byte[] message = new byte[frameSize * 100];
byte[] message = new byte[frameSize * 10];
Random random = new(0);

for (int i = 0; i < message.Length; ++i)
Expand All @@ -515,15 +520,18 @@ public async Task PayloadShouldHaveSimilarSizeWhenSplitIntoSegments()

await client.SendAsync(message, WebSocketMessageType.Binary, true, CancellationToken);

int payloadLength = stream.Remote.Available;
stream.Remote.Clear();
long payloadLength = stream.Length;
stream.SetLength(0);

for (var i = 0; i < message.Length; i += frameSize)
for (int i = 0; i < message.Length; i += frameSize)
{
await client.SendAsync(message.AsMemory(i, frameSize), WebSocketMessageType.Binary, i + frameSize == message.Length, CancellationToken);
}

Assert.Equal(0.999, Math.Round(payloadLength * 1.0 / stream.Remote.Available, 3));
double difference = Math.Round(1 - payloadLength * 1.0 / stream.Length, 3);

// The difference should not be more than 10% in either direction
Assert.InRange(difference, -0.1, 0.1);
}

[Theory]
Expand Down
8 changes: 4 additions & 4 deletions src/libraries/System.Net.WebSockets/tests/ZLibStreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ZLibStreamTests
[Fact]
public async Task PoolShouldReuseTheSameInstance()
{
var pool = new Pool(timeoutMilliseconds: 100);
var pool = new Pool(timeoutMilliseconds: 25);

object inflater = pool.GetInflater();
for ( var i = 0; i < 10_000; ++i)
Expand All @@ -29,7 +29,7 @@ public async Task PoolShouldReuseTheSameInstance()
pool.ReturnInflater(inflater);

Assert.Equal(1, pool.ActiveCount);
await Task.Delay(250);
await Task.Delay(200);

// After timeout elapses we should not have any active instances
Assert.Equal(0, pool.ActiveCount);
Expand All @@ -39,7 +39,7 @@ public async Task PoolShouldReuseTheSameInstance()
[PlatformSpecific(~TestPlatforms.Browser)] // There is no concurrency in browser
public async Task PoolingConcurrently()
{
var pool = new Pool(timeoutMilliseconds: 100);
var pool = new Pool(timeoutMilliseconds: 25);
var parallelOptions = new ParallelOptions
{
MaxDegreeOfParallelism = 16
Expand All @@ -60,7 +60,7 @@ public async Task PoolingConcurrently()

Assert.True(pool.ActiveCount >= 2);
Assert.True(pool.ActiveCount <= parallelOptions.MaxDegreeOfParallelism * 2);
await Task.Delay(250);
await Task.Delay(200);
Assert.Equal(0, pool.ActiveCount);
}

Expand Down

0 comments on commit 935969a

Please sign in to comment.