static Task[] TestRpcTimeout(CancellationTokenSource cts)
        {
            RPC.RpcTerminationDelay = TimeSpan.FromSeconds(3);

            var ts = Server.ListenAsync($"http://{address}", cts.Token, (c, wc) => c.Bind(new ServiceTimeoutAPI()));
            var tc = Client.ConnectAsync($"ws://{address}", cts.Token, c =>
            {
                c.Bind <IServiceTimeoutAPI>(); //should generate an exception: must be an interface
                c.OnOpen += async() =>
                {
                    try
                    {
                        var results = await RPC.For <IServiceTimeoutAPI>().CallAsync(x => x.LongRunningTask(1, 2));
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error while executing {0}: {1}", nameof(ServiceTimeoutAPI.LongRunningTask), ex.Message);
                    }
                };

                c.OnError += e => Task.Run(() => Console.WriteLine("Error: " + e.Message));
            },
                                         reconnectOnError: false);

            return(new Task[] { ts, tc });
        }
Exemple #2
0
        public NetSocketTests()
        {
            Server.OnConnections()
            .Subscribe(x => {
                x.AddInto(Clients);
                x.BeginSend(x.BeginReceive());
            })
            .AddInto(Disposes);

            Server.ListenAsync(Port).ConfigureAwait(false);
        }
        static Task[] TestConnectionTimeout(CancellationTokenSource cts)
        {
            var ts = Server.ListenAsync($"http://{address}", cts.Token, (c, wc) => c.Bind(new ServiceAPI()));
            var tc = Client.ConnectAsync($"ws://{address}", cts.Token, c =>
            {
                c.BindTimeout(TimeSpan.FromSeconds(5));

                c.OnError += e => Task.Run(() => Console.WriteLine("Error: " + e.Message));
                c.OnClose += (s, d) => Task.Run(() => Console.WriteLine("Close: " + d));
            },
                                         reconnectOnError: false);

            return(new Task[] { ts, tc });
        }
Exemple #4
0
        static Task[] TestConnectionUnhandledException(CancellationTokenSource cts)
        {
            var ts = Server.ListenAsync($"http://{address}", cts.Token, (c, wc) => c.Bind(new ServiceAPI()));
            var tc = Client.ConnectAsync($"ws://{address}", cts.Token, c =>
            {
                c.OnOpen += () =>
                {
                    throw new NotImplementedException();
                };

                c.OnError += e => Task.Run(() => Console.WriteLine("Error: " + e.Message));
            },
                                         reconnectOnError: false);

            return(new Task[] { ts, tc });
        }
Exemple #5
0
        static Task[] TestRpcUnhandledException(CancellationTokenSource cts)
        {
            var ts = Server.ListenAsync($"http://{address}", cts.Token, (c, wc) => c.Bind(new ServiceAPI()));
            var tc = Client.ConnectAsync($"ws://{address}", cts.Token, c =>
            {
                c.Bind <IServiceAPI>();
                c.OnOpen += async() =>
                {
                    var results = await RPC.For <IServiceAPI>().CallAsync(x => x.LongRunningTask(1, 2));
                };

                c.OnError += e => Task.Run(() => Console.WriteLine("Error: " + e.Message));
            },
                                         reconnectOnError: false);

            return(new Task[] { ts, tc });
        }
        static Task[] TestMaxMessageSize(CancellationTokenSource cts)
        {
            Connection.MaxMessageSize = Connection.Encoding.GetMaxByteCount(10);

            var ts = Server.ListenAsync($"http://{address}", cts.Token, (c, wc) => c.Bind(new ServiceAPI()));
            var tc = Client.ConnectAsync($"ws://{address}", cts.Token, c =>
            {
                c.OnOpen += async() =>
                {
                    await c.SendAsync("Long long message.");
                };

                c.OnError += e => Task.Run(() => Console.WriteLine("Error: " + e.Message));
                c.OnClose += (s, d) => Task.Run(() => Console.WriteLine("Close: " + d));
            },
                                         reconnectOnError: false);

            return(new Task[] { ts, tc });
        }
        static Task[] TestMultiClient(CancellationTokenSource cts)
        {
            var tasks = new List <Task>();

            var ts = Server.ListenAsync($"http://{address}", cts.Token, (c, wc) =>
            {
                c.OnReceive += msg => Task.Run(() => Console.WriteLine("Received: " + msg));
            });

            tasks.Add(ts);

            int id = 0;

            for (int i = 0; i < 10; i++)
            {
                var tc = Client.ConnectAsync($"ws://{address}", cts.Token, c =>
                {
                    Interlocked.Increment(ref id);

                    c.OnOpen += async() =>
                    {
                        int n = 10;
                        while (n > 0)
                        {
                            await c.SendAsync("Hello from: " + id);
                            await Task.Delay(10);
                            n--;
                        }
                    };

                    c.OnError += e => Task.Run(() => Console.WriteLine("Error: " + e.Message));
                    c.OnClose += (s, d) => Task.Run(() => Console.WriteLine("Close: " + s));
                },
                                             reconnectOnError: false);

                tasks.Add(tc);
            }


            return(tasks.ToArray());
        }