Skip to content

[Testing Platform] IDataConsumer is never invoked #4588

Open
@thomhurst

Description

@thomhurst

Heya, I'm trying to write an extension point, and using the IDataConsumer, but it never seems to be invoked.

Am I doing something wrong or is this a bug?

Repro:

using System.Text.Json;
using Microsoft.Testing.Platform.Builder;
using Microsoft.Testing.Platform.Capabilities.TestFramework;
using Microsoft.Testing.Platform.Extensions;
using Microsoft.Testing.Platform.Extensions.Messages;
using Microsoft.Testing.Platform.Extensions.TestFramework;
using Microsoft.Testing.Platform.Extensions.TestHost;
using Microsoft.Testing.Platform.TestHost;

var builder = await TestApplication.CreateBuilderAsync(args);

var testFramework = new DummyAdapter();

builder.RegisterTestFramework(_ => new TestFrameworkCapabilities(), (_, _) => testFramework);

var myExtension = new MyExtension(testFramework);

builder.TestHost.AddTestApplicationLifecycleCallbacks(_ => myExtension);
builder.TestHost.AddDataConsumer(_ => myExtension);

var app = await builder.BuildAsync();
return await app.RunAsync();

internal class DummyAdapter : ITestFramework, IDataProducer
{
    public string Uid => nameof(DummyAdapter);

    public string Version => string.Empty;

    public string DisplayName => string.Empty;

    public string Description => string.Empty;

    public Type[] DataTypesProduced => [typeof(TestNodeUpdateMessage)];

    public Task<CloseTestSessionResult> CloseTestSessionAsync(CloseTestSessionContext context) => Task.FromResult(new CloseTestSessionResult { IsSuccess = true });

    public Task<CreateTestSessionResult> CreateTestSessionAsync(CreateTestSessionContext context) => Task.FromResult(new CreateTestSessionResult { IsSuccess = true });

    public Task ExecuteRequestAsync(ExecuteRequestContext context)
    {
        context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(new SessionUid("1"), new TestNode
        {
            Uid = "1",
            DisplayName = "1",
            Properties = new PropertyBag(DiscoveredTestNodeStateProperty.CachedInstance)
        }));
        
        context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(new SessionUid("1"), new TestNode
        {
            Uid = "2",
            DisplayName = "2",
            Properties = new PropertyBag(DiscoveredTestNodeStateProperty.CachedInstance)
        }));
        
        context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(new SessionUid("1"), new TestNode
        {
            Uid = "3",
            DisplayName = "3",
            Properties = new PropertyBag(DiscoveredTestNodeStateProperty.CachedInstance)
        }));
        
        context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(new SessionUid("1"), new TestNode
        {
            Uid = "1",
            DisplayName = "1",
            Properties = new PropertyBag(PassedTestNodeStateProperty.CachedInstance)
        }));
        
        context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(new SessionUid("1"), new TestNode
        {
            Uid = "2",
            DisplayName = "2",
            Properties = new PropertyBag(new FailedTestNodeStateProperty("Oops"))
        }));
        
        context.MessageBus.PublishAsync(this, new TestNodeUpdateMessage(new SessionUid("1"), new TestNode
        {
            Uid = "3",
            DisplayName = "3",
            Properties = new PropertyBag(SkippedTestNodeStateProperty.CachedInstance)
        }));
     
        context.Complete();
        
        return Task.CompletedTask;
    }

    public Task<bool> IsEnabledAsync() => Task.FromResult(true);
}

public class MyExtension(IExtension extension) : IDataConsumer, ITestApplicationLifecycleCallbacks
{
    public Task ConsumeAsync(IDataProducer dataProducer, IData value, CancellationToken cancellationToken)
    {
        Console.WriteLine($"Consuming data: {JsonSerializer.Serialize(value)}");
        return Task.CompletedTask;
    }

    public Type[] DataTypesConsumed { get; } = [typeof(TestNodeUpdateMessage)];
    
    public Task<bool> IsEnabledAsync()
    {
        return extension.IsEnabledAsync();
    }

    public string Uid => extension.Uid;

    public string Version => extension.Version;

    public string DisplayName => extension.DisplayName;

    public string Description => extension.Description;
    
    public Task BeforeRunAsync(CancellationToken cancellationToken)
    {
        Console.WriteLine("Before Test App");
        return Task.CompletedTask;
    }

    public Task AfterRunAsync(int exitCode, CancellationToken cancellation)
    {
        Console.WriteLine("After Test App");
        return Task.CompletedTask;
    }
}

Stick a breakpoint in ConsumeAsync and you'll see it's never hit, or just see that it doesn't print any console statements.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions