Пример #1
0
        public void TestExceptionInJsonRpcShouldReturnInternalError()
        {
            JsonRpcServer server = new JsonRpcServer("nats://127.0.0.1", 10000);

            using (server)
            {
                JsonRpcRequest request = new JsonRpcRequest
                {
                    Method     = "test:exception",
                    Id         = Guid.NewGuid().ToString(),
                    Parameters = new Dictionary <string, string>
                    {
                        { "data", "ping" }
                    }
                };
                var resp = server.ServeAsync(request).GetAwaiter().GetResult();

                Assert.IsNotNull(resp);
                Assert.AreEqual(request.Id.ToString(), resp.Id);
                Assert.IsNull(resp.Result);
                Assert.IsNotNull(resp.Error);
                Assert.AreEqual((int)ErrorCode.InternalError, resp.Error.Code);
                Assert.AreEqual("Error", resp.Error.Message);
            }
        }
Пример #2
0
        public void TestTimingThousandCalls()
        {
            JsonRpcServer server = new JsonRpcServer("nats://127.0.0.1", 5000);

            using (server)
            {
                JsonRpcRequest request = new JsonRpcRequest
                {
                    Method     = "test:channel",
                    Id         = Guid.NewGuid().ToString(),
                    Parameters = new Dictionary <string, string>
                    {
                        { "data", "ping" }
                    }
                };
                Stopwatch sw = new Stopwatch();
                sw.Start();
                for (int i = 0; i < 1000; i++)
                {
                    var resp = server.ServeAsync(request).GetAwaiter().GetResult();
                    Assert.IsNotNull(resp);
                    Assert.IsNull(resp.Error);
                    Assert.AreEqual(request.Id.ToString(), resp.Id);
                    Assert.AreEqual("ping", resp.Result);
                }
                sw.Stop();
                Console.WriteLine($"Elapsed Time:{sw.Elapsed}");
            }
        }
Пример #3
0
        public void TestNatSubscriptionShouldLogPublishingError()
        {
            var mockLogger = new Mock <Microsoft.Extensions.Logging.ILogger>();
            JsonRpcSubscription subscription = new JsonRpcSubscription("test:largepayload", "test::group", new LargePayloadService(), mockLogger.Object);

            subscription.Subscribe("nats://127.0.0.1");
            subscription.Start().GetAwaiter().GetResult();

            JsonRpcServer server = new JsonRpcServer("nats://127.0.0.1", 5000);

            using (server)
            {
                Assert.Throws <JsonRpcException>(() =>
                {
                    var resp = server.ServeAsync(new JsonRpcRequest
                    {
                        Method     = "test:largepayload",
                        Id         = Guid.NewGuid().ToString(),
                        Parameters = new Dictionary <string, string>
                        {
                            { "size", "10000000" }
                        }
                    }).GetAwaiter().GetResult();
                });
            }

            mockLogger.Verify(m => m.Log(LogLevel.Error, It.IsAny <EventId>(), It.IsAny <Object>(), It.IsAny <Exception>(), It.IsAny <Func <Object, Exception, string> >()));
        }
Пример #4
0
        public void TestNatSubscriptionShouldRecoverFromPublishingError()
        {
            JsonRpcSubscription subscription = new JsonRpcSubscription("test:largepayload", "test::group", new LargePayloadService(), null);

            subscription.Subscribe("nats://127.0.0.1");
            subscription.Start().GetAwaiter().GetResult();

            JsonRpcServer server = new JsonRpcServer("nats://127.0.0.1", 5000);

            using (server)
            {
                Assert.Throws <JsonRpcException>(() =>
                {
                    var resp = server.ServeAsync(new JsonRpcRequest
                    {
                        Method     = "test:largepayload",
                        Id         = Guid.NewGuid().ToString(),
                        Parameters = new Dictionary <string, string>
                        {
                            { "size", "10000000" }
                        }
                    }).GetAwaiter().GetResult();
                });

                var resp2 = server.ServeAsync(new JsonRpcRequest
                {
                    Method     = "test:largepayload",
                    Id         = Guid.NewGuid().ToString(),
                    Parameters = new Dictionary <string, string>
                    {
                        { "size", "100" }
                    }
                }).GetAwaiter().GetResult();

                Assert.IsNotNull(resp2);
                Assert.IsNull(resp2.Error);
                Assert.AreEqual(resp2.Id.ToString(), resp2.Id);
                Assert.AreEqual(new string('A', 100), resp2.Result);
            }
        }
Пример #5
0
        public void TestCallUnknownMethodCallTimeout()
        {
            JsonRpcServer server = new JsonRpcServer("nats://127.0.0.1", 5000);

            using (server)
            {
                JsonRpcRequest request = new JsonRpcRequest
                {
                    Method     = "test:channel-unknown",
                    Id         = Guid.NewGuid().ToString(),
                    Parameters = new Dictionary <string, string>
                    {
                        { "data", "ping" }
                    }
                };
                Stopwatch sw = Stopwatch.StartNew();
                Assert.Throws <JsonRpcException>(() => server.ServeAsync(request).GetAwaiter().GetResult());
                sw.Stop();
                Assert.IsTrue(sw.ElapsedMilliseconds >= 5000);
            }
        }
Пример #6
0
        public void TestCallMethodOverJsonRpc()
        {
            JsonRpcServer server = new JsonRpcServer("nats://127.0.0.1", 5000);

            using (server)
            {
                JsonRpcRequest request = new JsonRpcRequest
                {
                    Method     = "test:channel",
                    Id         = Guid.NewGuid().ToString(),
                    Parameters = new Dictionary <string, string>
                    {
                        { "data", "ping" }
                    }
                };
                var resp = server.ServeAsync(request).GetAwaiter().GetResult();
                Assert.IsNotNull(resp);
                Assert.IsNull(resp.Error);
                Assert.AreEqual(request.Id.ToString(), resp.Id);
                Assert.AreEqual("ping", resp.Result);
            }
        }