Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Foundation/NSUrlSessionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ protected override void Dispose (bool disposing)

inflightRequests.Clear ();
}
session.InvalidateAndCancel ();
Comment thread
rolfbjarne marked this conversation as resolved.
base.Dispose (disposing);
}

Expand Down
63 changes: 63 additions & 0 deletions tests/monotouch-test/System.Net.Http/NSUrlSessionHandlerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// NSUrlSessionHandlerTest.cs
//

using System;
using System.Net.Http;
using System.Threading.Tasks;
using Foundation;
using NUnit.Framework;
using Xamarin.Utils;

namespace MonoTests.System.Net.Http {
[TestFixture]
[Preserve (AllMembers = true)]
public class NSUrlSessionHandlerTest {

// https://github.com/dotnet/macios/issues/24376
[Test]
public void DisposeAndRecreateBackgroundSessionHandler ()
{
bool firstRequestSucceeded = false;

// First request - should succeed
var done = TestRuntime.TryRunAsync (TimeSpan.FromSeconds (30), async () => {
using (var handler = new NSUrlSessionHandler (NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration ("test-id"))) {
using (var client = new HttpClient (handler)) {
var response = await client.GetByteArrayAsync (NetworkResources.MicrosoftUrl);
Assert.IsNotNull (response, "First request response");
Assert.IsTrue (response.Length > 0, "First request response length");
firstRequestSucceeded = true;
}
}
}, out var ex);

if (!done || !firstRequestSucceeded) {
TestRuntime.IgnoreInCI ("Transient network failure - ignore in CI");
Assert.Inconclusive ("First request failed or timed out - cannot verify the bug.");
}

TestRuntime.IgnoreInCIIfBadNetwork (ex);
Assert.IsNull (ex, "First request exception");

// Second request with new handler using same background session ID - should not timeout
done = TestRuntime.TryRunAsync (TimeSpan.FromSeconds (30), async () => {
using (var handler = new NSUrlSessionHandler (NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration ("test-id"))) {
using (var client = new HttpClient (handler)) {
var response = await client.GetByteArrayAsync (NetworkResources.MicrosoftUrl);
Assert.IsNotNull (response, "Second request response");
Assert.IsTrue (response.Length > 0, "Second request response length");
}
}
}, out ex);

if (!done) {
TestRuntime.IgnoreInCI ("Transient network failure - ignore in CI");
Assert.Fail ("Second request timedout - this indicates the bug is present.");
Comment thread
rolfbjarne marked this conversation as resolved.
}

TestRuntime.IgnoreInCIIfBadNetwork (ex);
Assert.IsNull (ex, "Second request exception");
}
}
}
Loading