Skip to content

Commit

Permalink
Merge pull request dotnet#3323 from davidsh/disable_leak_tests
Browse files Browse the repository at this point in the history
Disable SafeHandle leak checking in WinHttpHandler Unit Tests
  • Loading branch information
davidsh committed Sep 19, 2015
2 parents dbbc96a + b30b397 commit f8de53d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Net.Http;
using System.Diagnostics;
using System.Threading;

namespace System.Net.Http.WinHttpHandlerUnitTests
{
internal class FakeSafeWinHttpHandle : Interop.WinHttp.SafeWinHttpHandle
{
private static int s_HandlesOpen = 0;

public FakeSafeWinHttpHandle(bool markAsValid)
{
if (markAsValid)
{
SetHandle(Marshal.AllocHGlobal(1));
Interlocked.Increment(ref s_HandlesOpen);
Debug.WriteLine(
"FakeSafeWinHttpHandle.cctor, handle=#{0}, s_HandlesOpen={1}",
handle.GetHashCode(),
s_HandlesOpen);
}
else
{
Expand All @@ -32,19 +36,13 @@ public static int HandlesOpen
}
}

public static void ForceGarbageCollection()
{
// Make several passes through the FReachable list.
for (int i = 0; i < 10; i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
}

protected override bool ReleaseHandle()
{
Interlocked.Decrement(ref s_HandlesOpen);
Debug.WriteLine(
"FakeSafeWinHttpHandle.ReleaseHandle, handle=#{0}, s_HandlesOpen={1}",
handle.GetHashCode(),
s_HandlesOpen);

return base.ReleaseHandle();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,8 @@

namespace System.Net.Http.WinHttpHandlerUnitTests
{
public class SafeWinHttpHandleTest : IDisposable
public class SafeWinHttpHandleTest
{
public SafeWinHttpHandleTest()
{
}

public void Dispose()
{
// This runs after every test and makes sure that we run any finalizers to free all eligible handles.
FakeSafeWinHttpHandle.ForceGarbageCollection();
Assert.Equal(0, FakeSafeWinHttpHandle.HandlesOpen);
}

[Fact]
public void CreateAddRefDispose_HandleIsNotClosed()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ public WinHttpHandlerTest()
public void Dispose()
{
TestControl.ResponseDelayCompletedEvent.WaitOne();

FakeSafeWinHttpHandle.ForceGarbageCollection();
Assert.Equal(0, FakeSafeWinHttpHandle.HandlesOpen);
}

[Fact]
Expand Down Expand Up @@ -859,7 +856,7 @@ public async Task SendAsync_WinHttpOpenReturnsError_ExpectHttpRequestException()
}

[Fact]
public void SendAsync_MultipleCallsWithDispose_NoHandleLeaks()
public void SendAsync_MultipleCallsWithDispose_NoHandleLeaksManuallyVerifiedUsingLogging()
{
WinHttpHandler handler;
HttpResponseMessage response;
Expand All @@ -870,14 +867,10 @@ public void SendAsync_MultipleCallsWithDispose_NoHandleLeaks()
response.Dispose();
handler.Dispose();
}

FakeSafeWinHttpHandle.ForceGarbageCollection();

Assert.Equal(0, FakeSafeWinHttpHandle.HandlesOpen);
}

[Fact]
public void SendAsync_MultipleCallsWithoutDispose_NoHandleLeaks()
public void SendAsync_MultipleCallsWithoutDispose_NoHandleLeaksManuallyVerifiedUsingLogging()
{
WinHttpHandler handler;
HttpResponseMessage response;
Expand All @@ -886,12 +879,6 @@ public void SendAsync_MultipleCallsWithoutDispose_NoHandleLeaks()
handler = new WinHttpHandler();
response = SendRequestHelper(handler, () => { });
}

handler = null;
response = null;
FakeSafeWinHttpHandle.ForceGarbageCollection();

Assert.Equal(0, FakeSafeWinHttpHandle.HandlesOpen);
}

private HttpResponseMessage SendRequestHelper(WinHttpHandler handler, Action setup)
Expand Down

0 comments on commit f8de53d

Please sign in to comment.