示例#1
0
        public void SupportCancellation()
        {
            async Task ReceiveAsync()
            {
                using (var server = new RouterSocket("inproc://async"))
                {
                    var cts = new CancellationTokenSource();
                    cts.CancelAfter(1000);
                    await Assert.ThrowsAsync <TaskCanceledException>(
                        async() => await server.ReceiveMultipartMessageAsync(cancellationToken: cts.Token)
                        );

                    await Assert.ThrowsAsync <TaskCanceledException>(
                        async() => await server.ReceiveFrameBytesAsync(cts.Token)
                        );

                    await Assert.ThrowsAsync <TaskCanceledException>(
                        async() => await server.ReceiveFrameStringAsync(cts.Token)
                        );
                }
            }

            using (var runtime = new NetMQRuntime())
            {
                var t = ReceiveAsync();
                runtime.Run(t);

                if (t.IsFaulted && t.Exception is AggregateException exc)
                {
                    throw exc.GetBaseException();
                }
            }
        }
示例#2
0
        public void Receive()
        {
            async Task ReceiveAsync()
            {
                using (var server = new RouterSocket("inproc://async"))
                    using (var client = new DealerSocket("inproc://async"))
                    {
                        client.SendFrame("Hello");

                        var(routingKey, _) = await server.ReceiveRoutingKeyAsync();

                        var(message, _) = await server.ReceiveFrameStringAsync();

                        Assert.Equal(message, "Hello");

                        server.SendMoreFrame(routingKey);
                        server.SendFrame(new[] { (byte)0 });

                        var(bytes, _) = await client.ReceiveFrameBytesAsync();

                        Assert.Equal(bytes[0], 0);
                    }
            }

            using (var runtime = new NetMQRuntime())
            {
                runtime.Run(ReceiveAsync());
            }
        }
示例#3
0
        public void SendReceive()
        {
            async Task SendReceiveAsync()
            {
                using (var server = new RouterSocket("inproc://async"))
                    using (var client = new DealerSocket("inproc://async"))
                    {
                        await client.SendFrameAsync("Hello");

                        var(routingKey, _) = await server.ReceiveRoutingKeyAsync();

                        var(message, _) = await server.ReceiveFrameStringAsync();

                        Assert.Equal("Hello", message);

                        server.SendMoreFrame(routingKey);
                        server.SendFrame(new[] { (byte)0 });

                        var(bytes, _) = await client.ReceiveFrameBytesAsync();

                        Assert.Equal(new[] { (byte)0 }, bytes);
                    }
            }

            using (var runtime = new NetMQRuntime())
            {
                var t = SendReceiveAsync();
                runtime.Run(t);

                if (t.IsFaulted && t.Exception is AggregateException exc)
                {
                    throw exc.GetBaseException();
                }
            }
        }