Skip to content

Race condition when building test server #134

@Fgruntjes

Description

@Fgruntjes

You will run into a race condition when you build multiple test servers with the same controller. You will run into this exception:

GraphQL.AspNet.Execution.Exceptions.GraphTypeDeclarationException : The item, 'GraphQlAspnetBuildRaceCondition.ParamController.Get.id', declares an invalid name, ''. Graph names can only contain letters A-Z, numbers 0-9 and an underscore. They must also not start with a double underscore.

I could refactor to ensure I do not have different tests with the same controller. However, it is sometimes helpful to do this, especially when you are using type extensions.

It can be reproduced with a simple test case. Only the test that creates multiple servers will fail (most of the time).

public class UnitTest
{
    [Fact]
    public void FailedWithParams()
    {
        Task.WaitAll(
            Task.Run(() => BuildServer<ParamController>()),
            Task.Run(() => BuildServer<ParamController>()),
            Task.Run(() => BuildServer<ParamController>())
        );
    }

    [Fact]
    public void SuccessWithParams()
    {
        BuildServer<ParamController>()
    }

    [Fact]
    public void FailedWithoutParams()
    {
        Task.WaitAll(
            Task.Run(() => BuildServer<WithoutParamController>()),
            Task.Run(() => BuildServer<WithoutParamController>()),
            Task.Run(() => BuildServer<WithoutParamController>())
        );
    }

    private void BuildServer<T>()
        where T : GraphController
    {
        var builder = new TestServerBuilder<GraphSchema>();

        builder.AddGraphQL(options =>
        {
            options.AddController<T>();
        });

        builder.Build();
    }
}

And the controllers:

[GraphRoute("without-param")]
public class WithoutParamController : GraphController
{
    [Query("get")]
    public string Get()
    {
        return "somestring";
    }
}
[GraphRoute("with-param")]
public class ParamController : GraphController
{
    [Query("get")]
    public string Get(string id)
    {
        return id;
    }
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions