Skip to content

Commit

Permalink
Fixed Defer and Data tests (#5310)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Aug 17, 2022
1 parent c35a2fb commit aeef89e
Show file tree
Hide file tree
Showing 376 changed files with 3,922 additions and 2,641 deletions.
Expand Up @@ -40,19 +40,21 @@ public static class SnapshotExtensions
postFix,
formatter: SnapshotValueFormatters.PlainText);

public static Snapshot AddSqlFrom(
public static Snapshot Add(
this Snapshot snapshot,
IExecutionResult result,
string? name = null)
{
snapshot.SetPostFix(TestEnvironment.TargetFramework);
snapshot.Add(result.ToJson(), string.IsNullOrEmpty(name) ? "Result:" : $"{name} Result:");

if (result.ContextData is null)
{
snapshot.Add(result.ToJson(), name);
return snapshot;
}

snapshot.Add(result.ToJson(), string.IsNullOrEmpty(name) ? "Result:" : $"{name} Result:");
snapshot.SetPostFix(TestEnvironment.TargetFramework);

if (result.ContextData.TryGetValue("query", out var queryResult) &&
queryResult is string queryString &&
!string.IsNullOrWhiteSpace(queryString))
Expand All @@ -79,19 +81,11 @@ public static class SnapshotExtensions
SnapshotValueFormatters.PlainText);
}

return snapshot;
}

public static Snapshot AddExceptionFrom(
this Snapshot snapshot,
IExecutionResult result)
{
snapshot.Add(result, "Result:");
if (result.ContextData is { } &&
result.ContextData.TryGetValue("ex", out var queryResult))
if (result.ContextData.TryGetValue("ex", out var exception))
{
snapshot.Add(queryResult, "Exception:");
snapshot.Add(exception, "Exception:");
}

return snapshot;
}
}
Expand Up @@ -192,7 +192,10 @@ public async Task SingleRequest_GetHeroName_With_EnumVariable()
name
}
}",
Variables = new Dictionary<string, object> { { "episode", "NEW_HOPE" } }
Variables = new Dictionary<string, object?>
{
{ "episode", "NEW_HOPE" }
}
});

// assert
Expand All @@ -215,7 +218,10 @@ public async Task SingleRequest_GetHumanName_With_StringVariable()
name
}
}",
Variables = new Dictionary<string, object> { { "id", "1000" } }
Variables = new Dictionary<string, object?>
{
{ "id", "1000" }
}
});

// assert
Expand Down Expand Up @@ -258,7 +264,7 @@ public async Task Single_Diagnostic_Listener_Is_Triggered()
var server = CreateStarWarsServer(
configureServices: s => s
.AddGraphQLServer()
.AddDiagnosticEventListener(sp => listenerA));
.AddDiagnosticEventListener(_ => listenerA));

// act
await server.PostRawAsync(new ClientQueryRequest
Expand Down Expand Up @@ -290,8 +296,8 @@ public async Task Aggregate_Diagnostic_All_Listeners_Are_Triggered()
var server = CreateStarWarsServer(
configureServices: s => s
.AddGraphQLServer()
.AddDiagnosticEventListener(sp => listenerA)
.AddDiagnosticEventListener(sp => listenerB));
.AddDiagnosticEventListener(_ => listenerA)
.AddDiagnosticEventListener(_ => listenerB));

// act
await server.PostRawAsync(new ClientQueryRequest
Expand Down Expand Up @@ -362,7 +368,7 @@ public async Task Ensure_Multipart_Format_Is_Correct_With_Defer_If_Condition_Tru
}
}
}",
Variables = new Dictionary<string, object>
Variables = new Dictionary<string, object?>
{
["if"] = true
}
Expand Down Expand Up @@ -393,7 +399,7 @@ public async Task Ensure_JSON_Format_Is_Correct_With_Defer_If_Condition_False()
}
}
}",
Variables = new Dictionary<string, object>
Variables = new Dictionary<string, object?>
{
["if"] = false
}
Expand Down Expand Up @@ -450,16 +456,16 @@ public async Task SingleRequest_CreateReviewForEpisode_With_ObjectVariable()
commentary
}
}",
Variables = new Dictionary<string, object>
Variables = new Dictionary<string, object?>
{
{ "ep", "EMPIRE" },
{ "ep", "EMPIRE" },
{
"review",
new Dictionary<string, object>
{
"review",
new Dictionary<string, object>
{
{ "stars", 5 }, { "commentary", "This is a great movie!" },
}
{ "stars", 5 }, { "commentary", "This is a great movie!" },
}
}
}
});

Expand All @@ -486,15 +492,16 @@ public async Task SingleRequest_CreateReviewForEpisode_Omit_NonNull_Variable()
commentary
}
}",
Variables = new Dictionary<string, object>
Variables = new Dictionary<string, object?>
{
{
"review",
new Dictionary<string, object?>
{
"review",
new Dictionary<string, object>
{
{ "stars", 5 }, { "commentary", "This is a great movie!" },
}
{ "stars", 5 },
{ "commentary", "This is a great movie!" },
}
}
}
});

Expand Down Expand Up @@ -525,11 +532,11 @@ public async Task SingleRequest_CreateReviewForEpisode_Variables_In_ObjectValue(
commentary
}
}",
Variables = new Dictionary<string, object>
Variables = new Dictionary<string, object?>
{
{ "ep", "EMPIRE" },
{ "stars", 5 },
{ "commentary", "This is a great movie!" }
{ "ep", "EMPIRE" },
{ "stars", 5 },
{ "commentary", "This is a great movie!" }
}
});

Expand Down Expand Up @@ -561,11 +568,11 @@ public async Task SingleRequest_CreateReviewForEpisode_Variables_Unused()
commentary
}
}",
Variables = new Dictionary<string, object>
Variables = new Dictionary<string, object?>
{
{ "ep", "EMPIRE" },
{ "stars", 5 },
{ "commentary", "This is a great movie!" }
{ "ep", "EMPIRE" },
{ "stars", 5 },
{ "commentary", "This is a great movie!" }
}
});

Expand Down Expand Up @@ -621,7 +628,10 @@ public async Task SingleRequest_ValidationError()
name
}
}",
Variables = new Dictionary<string, object> { { "episode", "NEW_HOPE" } }
Variables = new Dictionary<string, object?>
{
{ "episode", "NEW_HOPE" }
}
});

// assert
Expand Down Expand Up @@ -658,14 +668,18 @@ public async Task SingleRequest_Double_Variable()

// act
var result =
await server.PostAsync(new ClientQueryRequest
{
Query = @"
query ($d: Float) {
double_arg(d: $d)
}",
Variables = new Dictionary<string, object> { { "d", 1.539 } }
},
await server.PostAsync(
new ClientQueryRequest
{
Query = @"
query ($d: Float) {
double_arg(d: $d)
}",
Variables = new Dictionary<string, object?>
{
{ "d", 1.539 }
}
},
"/arguments");

// assert
Expand All @@ -680,14 +694,18 @@ public async Task SingleRequest_Double_Max_Variable()

// act
var result =
await server.PostAsync(new ClientQueryRequest
{
Query = @"
query ($d: Float) {
double_arg(d: $d)
}",
Variables = new Dictionary<string, object> { { "d", double.MaxValue } }
},
await server.PostAsync(
new ClientQueryRequest
{
Query = @"
query ($d: Float) {
double_arg(d: $d)
}",
Variables = new Dictionary<string, object?>
{
{ "d", double.MaxValue }
}
},
"/arguments");

// assert
Expand All @@ -708,7 +726,10 @@ public async Task SingleRequest_Double_Min_Variable()
query ($d: Float) {
double_arg(d: $d)
}",
Variables = new Dictionary<string, object> { { "d", double.MinValue } }
Variables = new Dictionary<string, object?>
{
{ "d", double.MinValue }
}
},
"/arguments");

Expand All @@ -724,14 +745,18 @@ public async Task SingleRequest_Decimal_Max_Variable()

// act
var result =
await server.PostAsync(new ClientQueryRequest
{
Query = @"
query ($d: Decimal) {
decimal_arg(d: $d)
}",
Variables = new Dictionary<string, object> { { "d", decimal.MaxValue } }
},
await server.PostAsync(
new ClientQueryRequest
{
Query = @"
query ($d: Decimal) {
decimal_arg(d: $d)
}",
Variables = new Dictionary<string, object?>
{
{ "d", decimal.MaxValue }
}
},
"/arguments");

// assert
Expand All @@ -746,14 +771,18 @@ public async Task SingleRequest_Decimal_Min_Variable()

// act
var result =
await server.PostAsync(new ClientQueryRequest
{
Query = @"
query ($d: Decimal) {
decimal_arg(d: $d)
}",
Variables = new Dictionary<string, object> { { "d", decimal.MinValue } }
},
await server.PostAsync(
new ClientQueryRequest
{
Query = @"
query ($d: Decimal) {
decimal_arg(d: $d)
}",
Variables = new Dictionary<string, object?>
{
{ "d", decimal.MinValue }
}
},
"/arguments");

// assert
Expand Down
Expand Up @@ -39,23 +39,26 @@ private DeferredWorkStateOwner StateOwner
public void Initialize(OperationContext operationContext)
{
var services = operationContext.Services;

_parentContext = operationContext;
_operationContextFactory = services.GetRequiredService<IFactory<OperationContextOwner>>();
_deferredWorkStateFactory = services.GetRequiredService<IFactory<DeferredWorkStateOwner>>();
}

public void InitializeFrom(OperationContext operationContext, DeferredWorkScheduler scheduler)
{
_stateOwner = scheduler.StateOwner;
_parentContext = operationContext;
_stateOwner = scheduler._stateOwner;
_operationContextFactory = scheduler._operationContextFactory;
_deferredWorkStateFactory = scheduler._deferredWorkStateFactory;
}

public void Register(DeferredExecutionTask task)
{
var resultId = StateOwner.State.CreateId();
var taskContextOwner = _operationContextFactory.Create();
taskContextOwner.OperationContext.InitializeFrom(_parentContext);
task.Begin(taskContextOwner, StateOwner.State.CreateId());
task.Begin(taskContextOwner, resultId);
}

public void Complete(DeferredExecutionTaskResult result)
Expand Down
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.ObjectPool;

namespace HotChocolate.Execution.Processing;

Expand Down Expand Up @@ -131,25 +129,3 @@ public void Reset()
_delivered = 0;
}
}

internal sealed class DeferredWorkStateOwner : IDisposable
{
private readonly ObjectPool<DeferredWorkState> _statePool;
private int _disposed = 0;

public DeferredWorkStateOwner(DeferredWorkState state, ObjectPool<DeferredWorkState> statePool)
{
State = state ?? throw new ArgumentNullException(nameof(state));
_statePool = statePool ?? throw new ArgumentNullException(nameof(statePool));
}

public DeferredWorkState State { get; }

public void Dispose()
{
if (_disposed == 0 && Interlocked.CompareExchange(ref _disposed, 0, 1) == 0)
{
_statePool.Return(State);
}
}
}

0 comments on commit aeef89e

Please sign in to comment.