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

BeforeEachAsync is somehow parallel with pipeline execution #75

Closed
LodewijkSioen opened this issue Apr 29, 2020 · 2 comments
Closed

BeforeEachAsync is somehow parallel with pipeline execution #75

LodewijkSioen opened this issue Apr 29, 2020 · 2 comments

Comments

@LodewijkSioen
Copy link
Contributor

I have the following BeforeEachAsync code:

.BeforeEachAsync(async c =>
{
    using var scope = Server.Services.CreateScope();          //Breakpoint A
    var userManager = scope.ServiceProvider.GetService<UserManager<User>>();
    var signInManager = scope.ServiceProvider.GetService<SignInManager<User>>();

    var user = new User("TestUser", Guid.NewGuid().ToString());
    await userManager.CreateAsync(user);      // This actually goes to the database, so it takes a while
    c.User = await signInManager.CreateUserPrincipalAsync(user);        //Breakpoint B
});

I also have the following code in my StartUp:

app.Use(async (context, next) =>
{
    if (!context.User.Identity.IsAuthenticated)      //Breakpoint C
    {
        await context.ChallengeAsync();
        return;
    }
    await next();
});

When I run my tests, I keep getting status 302 with a redirect to my loginpage. When I debug my tests the order in which my breakpoints are hit is A - C - B. This would mean that my BeforeEach code is run in parallel with the actual HttpRequest. Either I'm doing something wrong, or something seems very wrong here.

@LodewijkSioen
Copy link
Contributor Author

I've made some failing tests for this issue. It's really weird. If I run the tests separately, I get the following output:

authentication_with_before passed
    In BeforeEach
    In app.Run
authentication_with_long_running_before failed
    Start BeforeEach
    In app.Run

When I run both tests together, I get the following result:

authentication_with_before failed
    In app.Run
    In BeforeEach
authentication_with_long_running_before failed
    Start BeforeEach
    In app.Run

@LodewijkSioen
Copy link
Contributor Author

I committed a fix on the pull request. Asp.net Core's internal void Configure(Action<HttpContext> configureContext) is not async, so you everything you put in BeforeEachAsync is never awaited :(

jeremydmiller pushed a commit that referenced this issue May 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant