Ejemplo n.º 1
0
        public async Task Request_With_ConnectionPayload()
        {
            Snapshot.FullName();

            await TryTest(async ct =>
            {
                // arrange
                var payload = new Dictionary <string, object> {
                    ["Key"] = "Value"
                };
                var sessionInterceptor = new StubSessionInterceptor();
                using IWebHost host    = TestServerHelper.CreateServer(
                          x => x
                          .AddTypeExtension <StringSubscriptionExtensions>()
                          .AddSocketSessionInterceptor <ISocketSessionInterceptor>(
                              _ => sessionInterceptor),
                          out var port);

                var serviceCollection = new ServiceCollection();
                serviceCollection
                .AddProtocol <GraphQLWebSocketProtocolFactory>()
                .AddWebSocketClient(
                    "Foo",
                    c => c.Uri = new Uri("ws://localhost:" + port + "/graphql"))
                .ConfigureConnectionInterceptor(new StubConnectionInterceptor(payload));
                IServiceProvider services =
                    serviceCollection.BuildServiceProvider();

                ISessionPool sessionPool =
                    services.GetRequiredService <ISessionPool>();

                List <JsonDocument> results = new();
                MockDocument document       = new("subscription Test { onTest(id:1) }");
                OperationRequest request    = new("Test", document);

                // act
                var connection =
                    new WebSocketConnection(async _ => await sessionPool.CreateAsync("Foo", _));
                await foreach (var response in connection.ExecuteAsync(request, ct))
                {
                    if (response.Body is not null)
                    {
                        results.Add(response.Body);
                    }
                }

                // assert
                Dictionary <string, object> message =
                    Assert.IsType <Dictionary <string, object> >(
                        sessionInterceptor.InitializeConnectionMessage?.Payload);

                Assert.Equal(payload["Key"], message["Key"]);
            });
        }
Ejemplo n.º 2
0
        public async Task Simple_Request()
        {
            Snapshot.FullName();

            await TryTest(async ct =>
            {
                // arrange
                using IWebHost host = TestServerHelper.CreateServer(
                          x => x.AddTypeExtension <StringSubscriptionExtensions>(),
                          out var port);
                var serviceCollection = new ServiceCollection();
                serviceCollection
                .AddProtocol <GraphQLWebSocketProtocolFactory>()
                .AddWebSocketClient(
                    "Foo",
                    c => c.Uri            = new Uri("ws://localhost:" + port + "/graphql"));
                IServiceProvider services =
                    serviceCollection.BuildServiceProvider();

                ISessionPool sessionPool =
                    services.GetRequiredService <ISessionPool>();

                List <JsonDocument> results = new();
                MockDocument document       = new("subscription Test { onTest(id:1) }");
                OperationRequest request    = new("Test", document);

                // act
                var connection =
                    new WebSocketConnection(async _ => await sessionPool.CreateAsync("Foo", _));
                await foreach (var response in connection.ExecuteAsync(request, ct))
                {
                    if (response.Body is not null)
                    {
                        results.Add(response.Body);
                    }
                }


                // assert
                results.Select(x => x.RootElement.ToString()).ToList().MatchSnapshot();
            });
        }
Ejemplo n.º 3
0
        public async Task Parallel_Request_SameSocket()
        {
            Snapshot.FullName();

            await TryTest(async ct =>
            {
                // arrange
                using IWebHost host = TestServerHelper
                                      .CreateServer(
                          x => x.AddTypeExtension <StringSubscriptionExtensions>(),
                          out var port);

                ServiceCollection serviceCollection = new();
                serviceCollection
                .AddProtocol <GraphQLWebSocketProtocolFactory>()
                .AddWebSocketClient(
                    "Foo",
                    c => c.Uri            = new Uri("ws://localhost:" + port + "/graphql"));
                IServiceProvider services = serviceCollection.BuildServiceProvider();

                ISessionPool sessionPool = services.GetRequiredService <ISessionPool>();
                ConcurrentDictionary <int, List <JsonDocument> > results = new();

                async Task CreateSubscription(int id)
                {
                    var connection = new WebSocketConnection(
                        async cancellationToken =>
                        await sessionPool.CreateAsync("Foo", cancellationToken));
                    var document = new MockDocument(
                        $"subscription Test {{ onTest(id:{id.ToString()}) }}");
                    var request = new OperationRequest("Test", document);
                    await foreach (var response in connection.ExecuteAsync(request, ct))
                    {
                        if (response.Body is not null)
                        {
                            results.AddOrUpdate(id,
                                                _ => new List <JsonDocument> {
                                response.Body
                            },
                                                (_, l) =>
                            {
                                l.Add(response.Body);
                                return(l);
                            });
                        }
                    }
                }

                // act
                var list = new List <Task>();
                for (var i = 0; i < 15; i++)
                {
                    list.Add(CreateSubscription(i));
                }

                await Task.WhenAll(list);

                // assert
                var str = "";
                foreach (KeyValuePair <int, List <JsonDocument> > sub in results.OrderBy(x => x.Key))
                {
                    JsonDocument[] jsonDocuments = sub.Value.ToArray();

                    str += "Operation " + sub.Key + "\n";
                    for (var index = 0; index < jsonDocuments.Length; index++)
                    {
                        str += "Operation " + jsonDocuments[index].RootElement + "\n";
                    }
                }

                str.MatchSnapshot();
            });
        }
Ejemplo n.º 4
0
        public async Task LoadTest_MessagesReceivedInCorrectOrder()
        {
            // arrange
            CancellationToken ct = new CancellationTokenSource(20_000).Token;

            using IWebHost host = TestServerHelper
                                  .CreateServer(
                      x => x.AddTypeExtension <StringSubscriptionExtensions>(),
                      out var port);

            ServiceCollection serviceCollection = new();

            serviceCollection
            .AddProtocol <GraphQLWebSocketProtocolFactory>();


            for (var i = 0; i < 10; i++)
            {
                serviceCollection.AddWebSocketClient(
                    "Foo" + i,
                    c => c.Uri = new Uri("ws://localhost:" + port + "/graphql"));
            }

            IServiceProvider services = serviceCollection.BuildServiceProvider();

            ISessionPool sessionPool = services.GetRequiredService <ISessionPool>();

            var globalCounter = 0;

            async Task?CreateSubscription(int client, int id)
            {
                var connection =
                    new WebSocketConnection(async ct =>
                                            await sessionPool.CreateAsync("Foo" + client, ct));
                var document =
                    new MockDocument($"subscription Test {{ countUp }}");
                var request = new OperationRequest("Test", document);
                var counter = 0;

                await foreach (var response in connection.ExecuteAsync(request, ct))
                {
                    if (response.Body is not null)
                    {
                        Interlocked.Increment(ref globalCounter);
                        var received = response.Body.RootElement
                                       .GetProperty("data")
                                       .GetProperty("countUp")
                                       .GetInt32();

                        if (counter != received)
                        {
                            throw new InvalidOperationException();
                        }

                        counter++;
                    }
                }
            }

            // act
            var list = new List <Task>();

            for (var i = 0; i < 10; i++)
            {
                for (var j = 0; j < 10; j++)
                {
                    list.Add(CreateSubscription(i, j));
                }
            }

            await Task.WhenAll(list);

            // assert
            Assert.Equal(10000, globalCounter);
        }