203 lines
6.8 KiB
C#
203 lines
6.8 KiB
C#
using System;
|
|
using System.Runtime.InteropServices;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using MxGateway.Contracts.Proto;
|
|
using MxGateway.Worker.MxAccess;
|
|
using MxGateway.Worker.Sta;
|
|
|
|
namespace MxGateway.Worker.Tests.MxAccess;
|
|
|
|
/// <summary>
|
|
/// Tests for <see cref="MxAccessStaSession"/>.
|
|
/// </summary>
|
|
public sealed class MxAccessStaSessionTests
|
|
{
|
|
/// <summary>
|
|
/// Verifies that StartAsync creates the MXAccess COM object and attaches the event sink on the STA thread.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task StartAsync_CreatesComObjectAndAttachesEventSinkOnStaThread()
|
|
{
|
|
FakeMxAccessComObjectFactory factory = new();
|
|
FakeMxAccessEventSink eventSink = new();
|
|
using StaRuntime runtime = CreateRuntime();
|
|
using MxAccessStaSession session = new(runtime, factory, eventSink);
|
|
|
|
WorkerReady ready = await session.StartAsync("session-1", workerProcessId: 1234);
|
|
|
|
Assert.Equal(1234, ready.WorkerProcessId);
|
|
Assert.Equal(MxAccessInteropInfo.ProgId, ready.MxaccessProgid);
|
|
Assert.Equal(MxAccessInteropInfo.Clsid, ready.MxaccessClsid);
|
|
Assert.NotNull(ready.ReadyTimestamp);
|
|
Assert.Equal(runtime.StaThreadId, factory.CreateThreadId);
|
|
Assert.Equal(runtime.StaThreadId, eventSink.AttachThreadId);
|
|
Assert.Equal(ApartmentState.STA, factory.CreateApartmentState);
|
|
Assert.Same(factory.CreatedObject, eventSink.AttachedObject);
|
|
Assert.Equal("session-1", eventSink.SessionId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifies that StartAsync maps creation exceptions with HResult when the factory fails.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task StartAsync_WhenFactoryFails_MapsCreationExceptionWithHResult()
|
|
{
|
|
const int hresult = unchecked((int)0x80040154);
|
|
FakeMxAccessComObjectFactory factory = new(new COMException("Class not registered.", hresult));
|
|
FakeMxAccessEventSink eventSink = new();
|
|
using StaRuntime runtime = CreateRuntime();
|
|
using MxAccessStaSession session = new(runtime, factory, eventSink);
|
|
|
|
MxAccessCreationException exception = await Assert.ThrowsAsync<MxAccessCreationException>(
|
|
() => session.StartAsync(workerProcessId: 1234));
|
|
|
|
Assert.Equal(hresult, exception.CapturedHResult);
|
|
Assert.Equal(MxAccessInteropInfo.ProgId, exception.AttemptedProgId);
|
|
Assert.Equal(MxAccessInteropInfo.Clsid, exception.AttemptedClsid);
|
|
Assert.Null(eventSink.AttachedObject);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Verifies that Dispose detaches the event sink on the STA thread.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task Dispose_DetachesEventSinkOnStaThread()
|
|
{
|
|
FakeMxAccessComObjectFactory factory = new();
|
|
FakeMxAccessEventSink eventSink = new();
|
|
using StaRuntime runtime = CreateRuntime();
|
|
MxAccessStaSession session = new(runtime, factory, eventSink);
|
|
await session.StartAsync(workerProcessId: 1234);
|
|
|
|
session.Dispose();
|
|
|
|
Assert.Equal(runtime.StaThreadId, eventSink.DetachThreadId);
|
|
}
|
|
|
|
private static StaRuntime CreateRuntime()
|
|
{
|
|
return new StaRuntime(
|
|
new NoopComApartmentInitializer(),
|
|
new StaMessagePump(),
|
|
TimeSpan.FromMilliseconds(25));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fake MXAccess COM object factory for testing.
|
|
/// </summary>
|
|
private sealed class FakeMxAccessComObjectFactory : IMxAccessComObjectFactory
|
|
{
|
|
private readonly Exception? exception;
|
|
|
|
/// <summary>
|
|
/// Initializes a fake factory that optionally throws an exception.
|
|
/// </summary>
|
|
/// <param name="exception">Exception to throw when Create is called; null to succeed.</param>
|
|
public FakeMxAccessComObjectFactory(Exception? exception = null)
|
|
{
|
|
this.exception = exception;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the COM object created by this factory.
|
|
/// </summary>
|
|
public object CreatedObject { get; } = new();
|
|
|
|
/// <summary>
|
|
/// Gets the managed thread ID when Create was called.
|
|
/// </summary>
|
|
public int? CreateThreadId { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets the apartment state when Create was called.
|
|
/// </summary>
|
|
public ApartmentState? CreateApartmentState { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Creates the COM object or throws the configured exception.
|
|
/// </summary>
|
|
public object Create()
|
|
{
|
|
CreateThreadId = Thread.CurrentThread.ManagedThreadId;
|
|
CreateApartmentState = Thread.CurrentThread.GetApartmentState();
|
|
|
|
if (exception is not null)
|
|
{
|
|
throw exception;
|
|
}
|
|
|
|
return CreatedObject;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fake MXAccess event sink for testing.
|
|
/// </summary>
|
|
private sealed class FakeMxAccessEventSink : IMxAccessEventSink
|
|
{
|
|
/// <summary>
|
|
/// Gets the attached MXAccess COM object.
|
|
/// </summary>
|
|
public object? AttachedObject { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets the managed thread ID when Attach was called.
|
|
/// </summary>
|
|
public int? AttachThreadId { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets the managed thread ID when Detach was called.
|
|
/// </summary>
|
|
public int? DetachThreadId { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets the session identifier.
|
|
/// </summary>
|
|
public string? SessionId { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Attaches the MXAccess COM object and records thread context.
|
|
/// </summary>
|
|
/// <param name="mxAccessComObject">MXAccess COM object to attach.</param>
|
|
/// <param name="sessionId">Identifier of the session.</param>
|
|
public void Attach(
|
|
object mxAccessComObject,
|
|
string sessionId)
|
|
{
|
|
AttachedObject = mxAccessComObject;
|
|
AttachThreadId = Thread.CurrentThread.ManagedThreadId;
|
|
SessionId = sessionId;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Detaches the MXAccess COM object and records thread context.
|
|
/// </summary>
|
|
public void Detach()
|
|
{
|
|
DetachThreadId = Thread.CurrentThread.ManagedThreadId;
|
|
AttachedObject = null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Noop STA COM apartment initializer for testing.
|
|
/// </summary>
|
|
private sealed class NoopComApartmentInitializer : IStaComApartmentInitializer
|
|
{
|
|
/// <summary>
|
|
/// Initializes the COM apartment (no-op).
|
|
/// </summary>
|
|
public void Initialize()
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Uninitializes the COM apartment (no-op).
|
|
/// </summary>
|
|
public void Uninitialize()
|
|
{
|
|
}
|
|
}
|
|
}
|