示例#1
0
        public static IAmbientValuesRegistration RegisterValue <T>(this IAmbientValuesRegistration @this, string name, T value) where T : IComparable
        {
            var directProvider = new DirectProvider(typeof(T), value);

            @this.AddProvider(name, (services) => directProvider);
            return(@this);
        }
示例#2
0
        public void ApiGenerationTest()
        {
            var system = ActorSystem.Create("test");
            var api    = new ApiProvider(system, new TestRepository());

            foreach (var error in api.GenerationErrors)
            {
                this.output.WriteLine($"Error: {error}");
            }

            Assert.Equal(0, api.GenerationErrors.Count);

            var webApiProvider = new DirectProvider(api, this.output.WriteLine)
            {
                UseJsonRepack = true
            };
            var schema = SchemaGenerator.Generate(new List <Web.GraphQL.Publisher.ApiProvider> {
                webApiProvider
            });

            var hasSchemaErrors = false;

            foreach (var error in SchemaGenerator.CheckSchema(schema))
            {
                hasSchemaErrors = true;
                this.output.WriteLine($"Schema error: {error}");
            }

            using (var printer = new SchemaPrinter(schema))
            {
                var description = printer.Print();
                this.output.WriteLine("-------- Schema -----------");
                this.output.WriteLine(description);
                Assert.False(string.IsNullOrWhiteSpace(description));
            }

            Assert.False(hasSchemaErrors);

            hasSchemaErrors = false;
            foreach (var error in SchemaGenerator.CheckSchemaIntrospection(schema))
            {
                hasSchemaErrors = true;
                this.output.WriteLine($"Schema introspection error: {error}");
            }

            Assert.False(hasSchemaErrors);
        }
示例#3
0
        public async Task EmptyVariablesTest(string variables)
        {
            this.ExpectNoMsg();
            var internalApiProvider = new TestProvider();
            var publishingProvider  = new DirectProvider(internalApiProvider, this.output.WriteLine)
            {
                UseJsonRepack = true
            };
            var schemaProvider = this.Container.Resolve <SchemaProvider>();

            schemaProvider.CurrentSchema = SchemaGenerator.Generate(new List <ApiProvider> {
                publishingProvider
            });

            var client = new RestClient($"http://*****:*****@"
            {                
                api {
                    syncScalarField
                }
            }";

            var body = $"{{\"query\": {JsonConvert.SerializeObject(query)}, \"variables\": {variables}}}";

            request.AddParameter("application/json", body, ParameterType.RequestBody);
            var result = await client.ExecuteTaskAsync(request);

            Assert.Equal(ResponseStatus.Completed, result.ResponseStatus);
            Assert.Equal(HttpStatusCode.OK, result.StatusCode);
            this.Sys.Log.Info("Response {Response}", result.Content);
        }
示例#4
0
        public async Task SchemaInitializedTest()
        {
            this.ExpectNoMsg();
            var internalApiProvider = new TestProvider();
            var publishingProvider  = new DirectProvider(internalApiProvider, this.output.WriteLine)
            {
                UseJsonRepack = true
            };
            var schemaProvider = this.Container.Resolve <SchemaProvider>();

            schemaProvider.CurrentSchema = SchemaGenerator.Generate(new List <ApiProvider> {
                publishingProvider
            });

            var client = new RestClient($"http://*****:*****@"
            {                
                api {
                    asyncArrayOfScalarField,
                    asyncForwardedScalar,
                    nestedAsync {
                        asyncScalarField,
                        syncScalarField                        
                    },
                    asyncScalarField,
                    faultedSyncField,
                    forwardedArray,
                    syncArrayOfScalarField,
                    nestedSync {
                        asyncScalarField,
                        syncScalarField  
                    },
                    syncScalarField,
                    faultedASyncMethod {
                        asyncScalarField,
                        syncScalarField 
                    }
                }
            }";

            request.AddJsonBody(new EndpointController.QueryRequest {
                Query = query
            });
            var result = await client.ExecuteTaskAsync(request);

            this.Sys.Log.Info("Response {Response}", result.Content);
            Assert.Equal(ResponseStatus.Completed, result.ResponseStatus);
            Assert.Equal(HttpStatusCode.OK, result.StatusCode);

            var expectedResult = @"
                        {
                          ""data"": {
                            ""api"": {
                              ""asyncArrayOfScalarField"": [
                                4.0,
                                5.0
                              ],
                              ""asyncForwardedScalar"": ""AsyncForwardedScalar"",
                              ""nestedAsync"": {
                                ""asyncScalarField"": ""AsyncScalarField"",
                                ""syncScalarField"": ""SyncScalarField""
                              },
                              ""asyncScalarField"": ""AsyncScalarField"",
                              ""faultedSyncField"": null,
                              ""forwardedArray"": [
                                5,
                                6,
                                7
                              ],
                              ""syncArrayOfScalarField"": [
                                1,
                                2,
                                3
                              ],
                              ""nestedSync"": {
                                ""asyncScalarField"": ""AsyncScalarField"",
                                ""syncScalarField"": ""SyncScalarField""
                              },
                              ""syncScalarField"": ""SyncScalarField"",
                              ""faultedASyncMethod"": null
                            }
                          }
                        }
                        ";

            Assert.Equal(
                ApiProviderPublishResolveIntegration.CleanResponse(expectedResult),
                ApiProviderPublishResolveIntegration.CleanResponse(result.Content));
        }
示例#5
0
        public async Task LoadTest()
        {
            this.ExpectNoMsg();
            var initialObjects = new List <TestObject>
            {
                new TestObject
                {
                    Id =
                        Guid.Parse(
                            "{3BEEE369-11DF-4A30-BF11-1D8465C87110}"),
                    Name  = "1-test",
                    Value = 100m
                },
                new TestObject
                {
                    Id =
                        Guid.Parse(
                            "{B500CA20-F649-4DCD-BDA8-1FA5031ECDD3}"),
                    Name  = "2-test",
                    Value = 50m
                },
                new TestObject
                {
                    Id =
                        Guid.Parse(
                            "{67885BA0-B284-438F-8393-EE9A9EB299D1}"),
                    Name  = "3-test",
                    Value = 50m
                },
                new TestObject
                {
                    Id =
                        Guid.Parse(
                            "{3AF2C973-D985-4F95-A0C7-AA928D276881}"),
                    Name  = "4-test",
                    Value = 70m
                },
                new TestObject
                {
                    Id =
                        Guid.Parse(
                            "{F0607502-5B77-4A3C-9142-E6197A7EE61E}"),
                    Name  = "5-test",
                    Value = 6m
                },
            };

            var internalApiProvider = new TestProvider(initialObjects);
            var publishingProvider  = new DirectProvider(internalApiProvider, this.output.WriteLine)
            {
                UseJsonRepack = true
            };
            var schemaProvider = this.Container.Resolve <SchemaProvider>();

            schemaProvider.CurrentSchema = SchemaGenerator.Generate(new List <ApiProvider> {
                publishingProvider
            });

            var client = new RestClient($"http://*****:*****@"
            {                
                api {
                    asyncArrayOfScalarField,
                    asyncForwardedScalar,
                    nestedAsync {
                        asyncScalarField,
                        syncScalarField                        
                    },
                    asyncScalarField,
                    forwardedArray,
                    syncArrayOfScalarField,
                    nestedSync {
                        asyncScalarField,
                        syncScalarField  
                    },
                    syncScalarField,
                    syncEnumField,
                    syncFlagsField,
                    connection(sort: [value_asc, name_asc], filter: {value_gt: 10}, offset: 1, limit: 2) {
                            count,
                            edges {
                                cursor,
                                node {
                                    id,
                                    __id,
                                    name,
                                    value
                                }                    
                            }
                        }
                }
            }                
            ";

            var stopwatch = new Stopwatch();
            var request   = new RestRequest {
                Method = Method.POST
            };

            request.AddHeader("Accept", "application/json, text/json");
            var body = $"{{\"query\": {JsonConvert.SerializeObject(query)}, \"variables\": null}}";

            request.AddParameter("application/json", body, ParameterType.RequestBody);
            stopwatch.Start();
            var result = client.ExecuteTaskAsync(request).Result;

            stopwatch.Stop();
            Assert.Equal(ResponseStatus.Completed, result.ResponseStatus);
            Assert.Equal(HttpStatusCode.OK, result.StatusCode);
            this.Sys.Log.Info("!!!!!!!!!! First request served in {StopWatch}ms", (double)stopwatch.ElapsedTicks * 1000 / Stopwatch.Frequency);
            stopwatch.Reset();
            stopwatch.Start();
            client.ExecuteTaskAsync(request).Wait();
            stopwatch.Stop();
            this.Sys.Log.Info("!!!!!!!!!! Second request served in {StopWatch}ms", (double)stopwatch.ElapsedTicks * 1000 / Stopwatch.Frequency);
            stopwatch.Reset();
            stopwatch.Start();

            var numberOfRequests = 3000;
            await Task.WhenAll(Enumerable.Range(1, numberOfRequests).Select(i => client.ExecuteTaskAsync(request)));

            stopwatch.Stop();
            this.Sys.Log.Info(
                "!!!!!!!!!! Served {numberOfRequests} requests in {StopWatch}ms ({StopWatchRequest}ms/req)",
                numberOfRequests,
                (double)stopwatch.ElapsedTicks * 1000 / Stopwatch.Frequency,
                (double)stopwatch.ElapsedTicks * 1000 / numberOfRequests / Stopwatch.Frequency);

            this.Sys.Log.Info("Response {Response}", result.Content);
        }