forked from ChilliCream/graphql-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f3b74a
commit d51b71a
Showing
74 changed files
with
873 additions
and
462 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using HotChocolate.Language; | ||
using HotChocolate.Resolvers; | ||
using Microsoft.Extensions.DiagnosticAdapter; | ||
using Xunit; | ||
|
||
namespace HotChocolate.Execution | ||
{ | ||
public class DiagnosticTests | ||
{ | ||
[Fact] | ||
public async Task ResolverEvents() | ||
{ | ||
// arrange | ||
var listener = new TestDiagnosticListener(); | ||
using (DiagnosticListener.AllListeners.Subscribe( | ||
new DiagnosticObserver(listener))) | ||
{ | ||
ISchema schema = CreateSchema(); | ||
|
||
// act | ||
await schema.ExecuteAsync("{ foo }"); | ||
|
||
// assert | ||
Assert.True(listener.ResolveFieldStart); | ||
Assert.True(listener.ResolveFieldStop); | ||
Assert.Equal("foo", listener.FieldSelection.Name.Value); | ||
Assert.InRange(listener.Duration, | ||
TimeSpan.FromMilliseconds(50), | ||
TimeSpan.FromMilliseconds(2000)); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task QueryEvents() | ||
{ | ||
// arrange | ||
var listener = new TestDiagnosticListener(); | ||
using (DiagnosticListener.AllListeners.Subscribe( | ||
new DiagnosticObserver(listener))) | ||
{ | ||
ISchema schema = CreateSchema(); | ||
|
||
// act | ||
await schema.ExecuteAsync("{ foo }"); | ||
|
||
// assert | ||
Assert.True(listener.QueryStart); | ||
Assert.True(listener.QueryStop); | ||
} | ||
} | ||
|
||
private ISchema CreateSchema() | ||
{ | ||
return Schema.Create( | ||
"type Query { foo: String }", | ||
c => c.BindResolver(() => | ||
{ | ||
Thread.Sleep(50); | ||
return "bar"; | ||
}).To("Query", "foo")); | ||
} | ||
|
||
private class TestDiagnosticListener | ||
{ | ||
public bool ResolveFieldStart { get; private set; } | ||
|
||
public bool ResolveFieldStop { get; private set; } | ||
|
||
public TimeSpan Duration { get; private set; } | ||
|
||
public FieldNode FieldSelection { get; private set; } | ||
|
||
public bool QueryStart { get; private set; } | ||
|
||
public bool QueryStop { get; private set; } | ||
|
||
|
||
[DiagnosticName("HotChocolate.Execution.Resolver.Start")] | ||
public virtual void OnResolveFieldStart() | ||
{ | ||
ResolveFieldStart = true; | ||
} | ||
|
||
[DiagnosticName("HotChocolate.Execution.Resolver.Stop")] | ||
public virtual void OnResolveFieldStop(IResolverContext context) | ||
{ | ||
ResolveFieldStop = true; | ||
FieldSelection = context.FieldSelection; | ||
Duration = Activity.Current.Duration; | ||
} | ||
|
||
[DiagnosticName("HotChocolate.Execution.Query.Start")] | ||
public virtual void OnQueryStart() | ||
{ | ||
QueryStart = true; | ||
} | ||
|
||
[DiagnosticName("HotChocolate.Execution.Query.Stop")] | ||
public virtual void OnQueryStop(IResolverContext context) | ||
{ | ||
QueryStop = true; | ||
} | ||
} | ||
|
||
private class DiagnosticObserver | ||
: IObserver<DiagnosticListener> | ||
{ | ||
private readonly TestDiagnosticListener _listener; | ||
|
||
public DiagnosticObserver(TestDiagnosticListener listener) | ||
{ | ||
_listener = listener; | ||
} | ||
|
||
public void OnCompleted() | ||
{ | ||
} | ||
|
||
public void OnError(Exception error) | ||
{ | ||
} | ||
|
||
public void OnNext(DiagnosticListener value) | ||
{ | ||
if (value.Name == "HotChocolate.Execution") | ||
{ | ||
value.SubscribeWithAdapter(_listener, s => | ||
{ | ||
return true; | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.