Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aspire.Hosting.Mongodb hangs / freezes in testing. #6871

Open
1 task done
huberttrueselftrueme opened this issue Dec 5, 2024 · 8 comments
Open
1 task done

Aspire.Hosting.Mongodb hangs / freezes in testing. #6871

huberttrueselftrueme opened this issue Dec 5, 2024 · 8 comments
Labels
area-app-testing Issues pertaining to the APIs in Aspire.Hosting.Testing needs-author-action An issue or pull request that requires more info or actions from the author.

Comments

@huberttrueselftrueme
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Env.
.NET 9
MacOs 15.1
Rider 2024.3

Application Stack
.net aspire orchestration
asp.net core api.
mongodb database.
Nunit Test project

As documentation My .net aspire project do not work properly in test. Test are hanging every time in app.StartAsync(). And i found exactly when.

This is my test

public class HostApi
{
    [Test]
    public async Task HostApiTest()
    {
        var builder = await DistributedApplicationTestingBuilder.CreateAsync<Projects.AppHost1>();

        var app = await builder.BuildAsync();
        await app.StartAsync();
        
        Assert.Pass();
    }
}

And this is my AppHost

using Projects;

var builder = DistributedApplication.CreateBuilder(args);

var mongo = builder.AddMongoDB("mongo", 50020)
    .WithExternalHttpEndpoints();

var mongodatabase = mongo
    .AddDatabase("mongodb");

builder.AddProject<AICoach_UI_Api>("api")
    .WithReference(mongodatabase)
    .WaitFor(mongodatabase)
    .WithExternalHttpEndpoints();

builder.Build().Run();

but when i remove .WaitFor(mongodatabase) line it start to working correctly.

Expected Behavior

Distributed application start in tests

Steps To Reproduce

Create AppHost with Api
Install Aspire.Hosting.MongoDb.
Add reference to mongodb and invoce WaitFor(mongodb) in AppHost config

Create test project and create test NUnit
var builder = await DistributedApplicationTestingBuilder.CreateAsync<Projects.AppHost1>();
var app = await builder.BuildAsync();
await app.StartAsync();
Assert.Pass();

Exceptions (if any)

Test just hanging forever on this await app.StartAsync();

.NET Version info

.NET SDK:
Version: 9.0.100
Commit: 59db016f11
Workload version: 9.0.100-manifests.c6f19616
MSBuild version: 17.12.7+5b8665660

Runtime Environment:
OS Name: Mac OS X
OS Version: 15.1
OS Platform: Darwin
RID: osx-arm64
Base Path: /usr/local/share/dotnet/sdk/9.0.100/

.NET workloads installed:
[maui]
Installation Source: SDK 9.0.100
Manifest Version: 9.0.0/9.0.100
Manifest Path: /usr/local/share/dotnet/sdk-manifests/9.0.100/microsoft.net.sdk.maui/9.0.0/WorkloadManifest.json
Install Type: FileBased

Configured to use loose manifests when installing new manifests.

Host:
Version: 9.0.0
Architecture: arm64
Commit: 9d5a6a9aa4

.NET SDKs installed:
9.0.100 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 9.0.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 9.0.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
None

Environment variables:
Not set

global.json file:
Not found

Learn more:
https://aka.ms/dotnet/info

Download .NET:
https://aka.ms/dotnet/download

Anything else?

No response

@davidfowl
Copy link
Member

Are you using rider?

@huberttrueselftrueme
Copy link
Author

Are you using rider?

Yes

@davidfowl
Copy link
Member

Same as this #6695? Make sure your tooling is up to date and if it still persists file an issue on the rider aspire plugin.

@filipbekic01
Copy link

filipbekic01 commented Dec 9, 2024

I have similar issue but not mongodb related. I use JetBrains Rider and .WaitForCompletion() never (sometime, randomly it does) completes. Project properly returns 0 but completition never triggers. It happens randomly, sometimes it passes sometimes not. I can't catch the bug.

@joperezr joperezr added the untriaged New issue has not been triaged label Dec 9, 2024
@joperezr joperezr added the area-app-testing Issues pertaining to the APIs in Aspire.Hosting.Testing label Dec 18, 2024
@davidfowl davidfowl added needs-author-action An issue or pull request that requires more info or actions from the author. and removed untriaged New issue has not been triaged labels Jan 9, 2025
@davidfowl
Copy link
Member

@filipbekic01 can you share the application? It's unclear what you are running into.

@filipbekic01
Copy link

filipbekic01 commented Jan 10, 2025

Our DbMigrations app runs first in Aspire, ensuring migrations are applied before the Web app starts. However, ~50% of the time, the Web app waits indefinitely for migrations to finish. Keep in mind that migrations always return 0 when this bug happens, which means it executes successfully.

Migrations app:

using Sample;

namespace Sample.DatabaseMigrationsProject;

public class Program
{
    public static async Task<int> Main(string[] args)
    {
        try
        {
            WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

           // We run migrations here, they execute successfully.

            using IHost app = builder.Build();

            await app.StartAsync();
            await app.StopAsync();

            return 0; // It does return 0, checked several times.
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Application encountered an error: {ex.Message}");
            return 1;
        }
    }
}

Aspire app:

using Projects;
using Shared.Configuration;

namespace Dam.Aspire;

public class Program
{
    public static async Task Main(string[] args)
    {
        IDistributedApplicationBuilder builder = DistributedApplication.CreateBuilder(args);

        await builder.Configuration.SetupConfigurationAsync(builder.Environment);

        // Postgre database
        IResourceBuilder<ParameterResource> dbUser = builder.AddParameter("dbUser", "our_username", secret: true);
        IResourceBuilder<ParameterResource> dbPass = builder.AddParameter("dbPass", "our_password", secret: true);
        IResourceBuilder<PostgresServerResource> postgresEngine = builder.AddPostgres(
                name: "postgres",
                userName: dbUser,
                password: dbPass
            )
            .WithDataVolume(name: "pgsql-volume")
            .WithEndpoint(name: "postgres-endpoint", port: 5432, isProxied: false)
            .WithLifetime(ContainerLifetime.Persistent);

        IResourceBuilder<PostgresDatabaseResource> postgresDb = postgresEngine.AddDatabase("PostgresDb", "db_connection_string");

        // Migrations
        IResourceBuilder<ProjectResource> migrateAndSeed = builder.AddProject<Dam_MigrateAndSeed>("migrate-and-seed")
            .WaitFor(postgresDb);

        // Start web app
        builder
            .AddProject<Dam_UI_Web>("dam-ui-web")
            .WaitForCompletion(migrateAndSeed);

        await builder.Build().RunAsync();
    }
}

@davidfowl
Copy link
Member

Its unclear if this is a related issue, I think you need to file a new one, with the entire runnable code sample. There might be a problem in SetupConfigurationAsync and that't not aspire logic.

@filipbekic01
Copy link

Method SetupConfigurationAsync reads JSON configuration files, quite trivial logic there. We can consider it as optional. Okay I'll try to provide runnable code here soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-app-testing Issues pertaining to the APIs in Aspire.Hosting.Testing needs-author-action An issue or pull request that requires more info or actions from the author.
Projects
None yet
Development

No branches or pull requests

4 participants