Skip to content

Commit

Permalink
Fixed Tests Issues (#5221)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Jul 7, 2022
1 parent 0477a36 commit 1cd2215
Show file tree
Hide file tree
Showing 67 changed files with 1,041 additions and 1,111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ public IDisposable ExecuteOperation(IRequestContext context)
return new AggregateActivityScope(scopes);
}

public IDisposable ExecuteStream(IRequestContext context)
public IDisposable ExecuteStream(IOperation operation)
{
var scopes = new IDisposable[_listeners.Length];

for (var i = 0; i < _listeners.Length; i++)
{
scopes[i] = _listeners[i].ExecuteStream(context);
scopes[i] = _listeners[i].ExecuteStream(operation);
}

return new AggregateActivityScope(scopes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public virtual IDisposable ExecuteOperation(IRequestContext context)
=> EmptyScope;

/// <inheritdoc />
public virtual IDisposable ExecuteStream(IRequestContext context)
public virtual IDisposable ExecuteStream(IOperation operation)
=> EmptyScope;

public virtual IDisposable ExecuteDeferredTask()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,14 @@ public interface IExecutionDiagnosticEvents
/// The ExecuteStream scope will run longer then the ExecuteOperation scope.
/// The ExecuteOperation scope is completed once the initial operation is executed.
/// All deferred elements will be executed and delivered within the ExecuteStream scope.
/// The passed in context is polled and can only be used at the initialization of the scope.
/// </summary>
/// <param name="context">
/// The request context encapsulates all GraphQL-specific information about an
/// individual GraphQL request.
/// <param name="operation">
/// The operation that is being streamed.
/// </param>
/// <returns>
/// A scope that will be disposed when the execution has finished.
/// </returns>
IDisposable ExecuteStream(IRequestContext context);
IDisposable ExecuteStream(IOperation operation);

/// <summary>
/// Called when starting to execute a deferred part an operation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ public void OperationComplexityAnalyzerCompiled(IRequestContext context)

public IDisposable CompileOperation(IRequestContext context) => this;

public IDisposable BuildQueryPlan(IRequestContext context) => this;

public IDisposable ExecuteOperation(IRequestContext context) => this;

public IDisposable ExecuteStream(IRequestContext context) => this;
public IDisposable ExecuteStream(IOperation operation) => this;

public IDisposable ExecuteDeferredTask() => this;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Threading;
using HotChocolate.Execution.DependencyInjection;
using HotChocolate.Execution.Instrumentation;
using static HotChocolate.Execution.QueryResultBuilder;

namespace HotChocolate.Execution.Processing;
Expand Down Expand Up @@ -64,22 +66,36 @@ public void Complete(DeferredExecutionTaskResult result)
=> StateOwner.State.Complete(result);

public IAsyncEnumerable<IQueryResult> CreateResultStream(IQueryResult initialResult)
=> new DeferResultStream(initialResult, StateOwner);
=> new DeferResultStream(
initialResult,
StateOwner,
_parentContext.Operation,
_parentContext.DiagnosticEvents);

private class DeferResultStream : IAsyncEnumerable<IQueryResult>
{
private readonly IQueryResult _initialResult;
private readonly DeferredWorkStateOwner _stateOwner;

public DeferResultStream(IQueryResult initialResult, DeferredWorkStateOwner stateOwner)
private readonly IOperation _operation;
private readonly IExecutionDiagnosticEvents _diagnosticEvents;

public DeferResultStream(
IQueryResult initialResult,
DeferredWorkStateOwner stateOwner,
IOperation operation,
IExecutionDiagnosticEvents diagnosticEvents)
{
_initialResult = FromResult(initialResult).SetHasNext(true).Create();
_stateOwner = stateOwner;
_operation = operation;
_diagnosticEvents = diagnosticEvents;
}

public async IAsyncEnumerator<IQueryResult> GetAsyncEnumerator(
CancellationToken cancellationToken = default)
{
var span = _diagnosticEvents.ExecuteStream(_operation);

try
{

Expand All @@ -102,6 +118,7 @@ public DeferResultStream(IQueryResult initialResult, DeferredWorkStateOwner stat
finally
{
_stateOwner.Dispose();
span.Dispose();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ internal static class ResolverTaskFactory
Path path,
IImmutableDictionary<string, object?> scopedContext)
{
var responseIndex = 0;
var selectionsCount = selectionSet.Selections.Count;
var responseIndex = selectionsCount;
var parentResult = operationContext.Result.RentObject(selectionsCount);
var scheduler = operationContext.Scheduler;
var pathFactory = operationContext.PathFactory;
Expand All @@ -35,7 +35,13 @@ internal static class ResolverTaskFactory
{
ref var selectionSpace = ref ((SelectionSet)selectionSet).GetSelectionsReference();

for (var i = 0; i < selectionsCount; i++)
// we are iterating reverse so that in the case of a mutation the first
// synchronous root selection is executed first, since the work scheduler
// is using two stacks one for parallel work an one for synchronous work.
// the scheduler this tries to schedule new work first.
// coincidentally we can use that to schedule a mutation so that we honor the spec
// guarantees while executing efficient.
for (var i = selectionsCount - 1; i >= 0; i--)
{
ref var selection = ref Unsafe.Add(ref selectionSpace, i);

Expand All @@ -46,7 +52,7 @@ internal static class ResolverTaskFactory
selection,
parent,
parentResult,
responseIndex++,
--responseIndex,
pathFactory.Append(path, selection.ResponseName),
scopedContext));
}
Expand Down
28 changes: 14 additions & 14 deletions src/HotChocolate/Data/test/Data.AutoMapper.Tests/AutomapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ public class ProjectToTests
public async Task Execute_ManyToOne()
{
// arrange
IRequestExecutor tester = await CreateSchema();
var tester = await CreateSchema();

// act
// assert
IExecutionResult res1 = await tester.ExecuteAsync(
var res1 = await tester.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery(
@"
Expand All @@ -121,11 +121,11 @@ public async Task Execute_ManyToOne()
public async Task Execute_ManyToOne_Deep()
{
// arrange
IRequestExecutor tester = await CreateSchema();
var tester = await CreateSchema();

// act
// assert
IExecutionResult res1 = await tester.ExecuteAsync(
var res1 = await tester.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery(
@"
Expand Down Expand Up @@ -156,11 +156,11 @@ public async Task Execute_ManyToOne_Deep()
public async Task Execute_OneToOne()
{
// arrange
IRequestExecutor tester = await CreateSchema();
var tester = await CreateSchema();

// act
// assert
IExecutionResult res1 = await tester.ExecuteAsync(
var res1 = await tester.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery(
@"
Expand All @@ -181,11 +181,11 @@ public async Task Execute_OneToOne()
public async Task Execute_OneToOne_Deep()
{
// arrange
IRequestExecutor tester = await CreateSchema();
var tester = await CreateSchema();

// act
// assert
IExecutionResult res1 = await tester.ExecuteAsync(
var res1 = await tester.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery(
@"
Expand All @@ -210,11 +210,11 @@ public async Task Execute_OneToOne_Deep()
public async Task Execute_Derived_CompleteSelectionSet()
{
// arrange
IRequestExecutor tester = await CreateSchema();
var tester = await CreateSchema();

// act
// assert
IExecutionResult res1 = await tester.ExecuteAsync(
var res1 = await tester.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery(
@"
Expand All @@ -234,11 +234,11 @@ public async Task Execute_Derived_CompleteSelectionSet()
public async Task Execute_Derived_PartialSelectionSet()
{
// arrange
IRequestExecutor tester = await CreateSchema();
var tester = await CreateSchema();

// act
// assert
IExecutionResult res1 = await tester.ExecuteAsync(
var res1 = await tester.ExecuteAsync(
QueryRequestBuilder.New()
.SetQuery(
@"
Expand Down Expand Up @@ -267,11 +267,11 @@ public async ValueTask<IRequestExecutor> CreateSchema()
mc.AddProfile(new AuthorProfile());
});

IMapper mapper = mapperConfig.CreateMapper();
var mapper = mapperConfig.CreateMapper();
services.AddSingleton(sp =>
{
// abusing the mapper factory to add to the database. You didnt see this.
BloggingContext context =
var context =
sp.GetRequiredService<IDbContextFactory<BloggingContext>>().CreateDbContext();
context.Database.EnsureCreated();
context.Blogs.AddRange(_blogEntries);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class AuthorFixture : IDisposable
public AuthorFixture()
{
_fileName = Guid.NewGuid().ToString("N") + ".db";
BookContext context = new ServiceCollection()
var context = new ServiceCollection()
.AddDbContext<BookContext>(
b => b.UseSqlite("Data Source=" + _fileName))
.AddGraphQL()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void Extensions_Should_ReturnEntityFrameworkExecutable_When_DBSet()
{
// arrange
// act
IExecutable<Author> executable = _context.Authors.AsExecutable();
var executable = _context.Authors.AsExecutable();

// assert
Assert.IsType<EntityFrameworkExecutable<Author>>(executable);
Expand All @@ -32,7 +32,7 @@ public void Extensions_Should_ReturnEntityFrameworkExecutable_When_Queryable()
{
// arrange
// act
IExecutable<Author> executable = _context
var executable = _context
.Authors
.AsQueryable()
.AsEntityFrameworkExecutable();
Expand All @@ -47,7 +47,7 @@ public void Extensions_Should_ReturnEntityFrameworkExecutable_When_Queryable()
public async Task ExecuteAsync_Should_ReturnAllItems_When_ToListAsync()
{
// arrange
IExecutable<Author> executable = _context
var executable = _context
.Authors
.AsExecutable();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task Resolver_Pipeline_With_Request_DbContext_Is_Created()

using AuthorFixture authorFixture = new();

using IServiceScope scope = new ServiceCollection()
using var scope = new ServiceCollection()
.AddScoped(_ => authorFixture.Context)
.AddGraphQL()
.AddQueryType<Query>()
Expand All @@ -58,7 +58,7 @@ public async Task Resolver_Pipeline_With_Request_DbContext_Is_Created()
.BuildServiceProvider()
.CreateScope();

IExecutionResult result = await scope.ServiceProvider.ExecuteRequestAsync(
var result = await scope.ServiceProvider.ExecuteRequestAsync(
QueryRequestBuilder.New()
.SetQuery("{ books { title } }")
.SetServices(scope.ServiceProvider)
Expand All @@ -74,7 +74,7 @@ public async Task Resolver_Pipeline_With_Field_DbContext_Is_Created()

using AuthorFixture authorFixture = new();

await using ServiceProvider service = new ServiceCollection()
await using var service = new ServiceCollection()
.AddScoped(_ => authorFixture.Context)
.AddGraphQL()
.AddQueryType<Query>()
Expand Down

0 comments on commit 1cd2215

Please sign in to comment.