aspnetcore/test/Kestrel.Transport.Libuv.Tests/LibuvThreadTests.cs

78 lines
2.3 KiB
C#

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal;
using Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests.TestHelpers;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using Xunit;
namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Tests
{
public class LibuvThreadTests
{
[Fact]
public async Task LibuvThreadDoesNotThrowIfPostingWorkAfterDispose()
{
var mockConnectionDispatcher = new MockConnectionDispatcher();
var mockLibuv = new MockLibuv();
var transportContext = new TestLibuvTransportContext() { ConnectionDispatcher = mockConnectionDispatcher };
var transport = new LibuvTransport(mockLibuv, transportContext, null);
var thread = new LibuvThread(transport);
var ranOne = false;
var ranTwo = false;
var ranThree = false;
var ranFour = false;
await thread.StartAsync();
await thread.PostAsync<object>(_ =>
{
ranOne = true;
},
null);
Assert.Equal(1, mockLibuv.PostCount);
// Shutdown the libuv thread
await thread.StopAsync(TimeSpan.FromSeconds(5));
Assert.Equal(2, mockLibuv.PostCount);
var task = thread.PostAsync<object>(_ =>
{
ranTwo = true;
},
null);
Assert.Equal(2, mockLibuv.PostCount);
thread.Post<object>(_ =>
{
ranThree = true;
},
null);
Assert.Equal(2, mockLibuv.PostCount);
thread.Schedule(_ =>
{
ranFour = true;
},
(object)null);
Assert.Equal(2, mockLibuv.PostCount);
Assert.True(task.IsCompleted);
Assert.True(ranOne);
Assert.False(ranTwo);
Assert.False(ranThree);
Assert.False(ranFour);
}
}
}