protected override void Given(IServiceContainer container)
            {
                var testServer = new AmqpTestFramework(Test.It.With.Amqp091.Protocol.Amqp091.ProtocolResolver);

                testServer
                .WithDefaultProtocolHeaderNegotiation()
                .WithDefaultSecurityNegotiation(heartbeatInterval: TimeSpan.FromSeconds(1))
                .WithDefaultConnectionOpenNegotiation()
                .WithHeartbeats(interval: TimeSpan.FromSeconds(1))
                .WithDefaultConnectionCloseNegotiation();

                testServer.On <Heartbeat>((connectionId, frame) =>
                {
                    _heartbeatCancelationToken.Cancel(true);
                    _heartbeats.Add(frame);
                    _heartbeatCancelationToken = new CancellationTokenSource();
                    Task.Delay(4000)
                    .ContinueWith(task =>
                    {
                        _missingHeartbeat = true;
                        ServiceController.Stop();
                    }, _heartbeatCancelationToken.Token);
                });

                Task.Delay(TimeSpan.FromSeconds(10)).ContinueWith(task =>
                {
                    ServiceController.Stop();
                });

                container.RegisterSingleton(() => testServer.ConnectionFactory.ToRabbitMqConnectionFactory());
            }
Beispiel #2
0
            protected override void Given(IServiceContainer container)
            {
                var channelClosed = false;

                void TryStop()
                {
                    if (channelClosed && _basicPublish.Count == 2)
                    {
                        ServiceController.Stop();
                    }
                }

                var testServer = new AmqpTestFramework(RabbitMq091.ProtocolResolver);

                testServer
                .WithDefaultProtocolHeaderNegotiation()
                .WithDefaultSecurityNegotiation(heartbeatInterval: TimeSpan.FromSeconds(5))
                .WithDefaultConnectionOpenNegotiation()
                .WithHeartbeats(interval: TimeSpan.FromSeconds(5))
                .WithDefaultConnectionCloseNegotiation();

                testServer.On <Channel.Open, Channel.OpenOk>((connectionId, frame) => new Channel.OpenOk());
                testServer.On <Channel.Close, Channel.CloseOk>((connectionId, frame) => new Channel.CloseOk());
                testServer.On <Exchange.Declare, Exchange.DeclareOk>((connectionId, frame) =>
                {
                    _exchangeDeclare.Add(frame);
                    return(new Exchange.DeclareOk());
                });
                testServer.On <Channel.Close>((id, frame) =>
                {
                    channelClosed = true;
                    TryStop();
                });
                testServer.On <Amqp091.Protocol.Basic.Publish>((connectionId, frame) =>
                {
                    _basicPublish.Add(frame);
                    testServer.Send(connectionId,
                                    new MethodFrame <Basic.Ack>(frame.Channel,
                                                                new Basic.Ack
                    {
                        DeliveryTag =
                            DeliveryTag.From(_basicPublish.Count(methodFrame =>
                                                                 methodFrame.Channel == frame.Channel))
                    }));
                });
                testServer.On <Confirm.Select, Confirm.SelectOk>((connectionId, frame) =>
                {
                    _selectOkSent = true;
                    return(new Confirm.SelectOk());
                });

                container.RegisterSingleton(() => testServer.ConnectionFactory.ToRabbitMqConnectionFactory());
            }
            protected override void Given(IServiceContainer container)
            {
                var closedChannels = new ConcurrentBag <short>();

                void TryStop()
                {
                    if (closedChannels.Count == NumberOfPublishes && _basicPublish.Count == NumberOfPublishes)
                    {
                        ServiceController.Stop();
                    }
                }

                var testServer = new AmqpTestFramework(Amqp091.Protocol.Amqp091.ProtocolResolver);

                testServer
                .WithDefaultProtocolHeaderNegotiation()
                .WithDefaultSecurityNegotiation(heartbeatInterval: TimeSpan.FromSeconds(5))
                .WithDefaultConnectionOpenNegotiation()
                .WithHeartbeats(interval: TimeSpan.FromSeconds(5))
                .WithDefaultConnectionCloseNegotiation();

                testServer.On <Channel.Open, Channel.OpenOk>((connectionId, frame) => new Channel.OpenOk());
                testServer.On <Channel.Close, Channel.CloseOk>((connectionId, frame) => new Channel.CloseOk());
                testServer.On <Exchange.Declare, Exchange.DeclareOk>((connectionId, frame) =>
                {
                    _exchangeDeclare.Add(frame);
                    return(new Exchange.DeclareOk());
                });
                testServer.On <Channel.Close>((id, frame) =>
                {
                    closedChannels.Add(frame.Channel);
                    TryStop();
                });
                testServer.On <Basic.Publish>((connectionId, frame) =>
                {
                    _basicPublish.Add(frame);
                });

                container.RegisterSingleton(() => testServer.ConnectionFactory.ToRabbitMqConnectionFactory());
            }
            protected override void Given(IServiceContainer container)
            {
                var closedChannels = new ConcurrentBag <short>();

                void TryStop()
                {
                    if (closedChannels.Count == Parallelism && _basicPublishes.Count == Parallelism && _acks.Count == Parallelism)
                    {
                        ServiceController.Stop();
                    }
                }

                var testServer = new AmqpTestFramework(Amqp091.Protocol.Amqp091.ProtocolResolver);

                testServer
                .WithDefaultProtocolHeaderNegotiation()
                .WithDefaultSecurityNegotiation(heartbeatInterval: TimeSpan.FromSeconds(5))
                .WithDefaultConnectionOpenNegotiation()
                .WithHeartbeats(interval: TimeSpan.FromSeconds(5))
                .WithDefaultConnectionCloseNegotiation();

                testServer.On <Channel.Open, Channel.OpenOk>((connectionId, frame) => new Channel.OpenOk());
                testServer.On <Channel.Close, Channel.CloseOk>((connectionId, frame) => new Channel.CloseOk());
                testServer.On <Exchange.Declare, Exchange.DeclareOk>((connectionId, frame) =>
                {
                    _exchangesDeclared.Add(frame);
                    return(new Exchange.DeclareOk());
                });
                testServer.On <Queue.Declare, Queue.DeclareOk>((connectionId, frame) =>
                {
                    _queuesDeclared.Add(frame);
                    return(new Queue.DeclareOk());
                });
                testServer.On <Queue.Bind, Queue.BindOk>((connectionId, frame) =>
                {
                    _queuesBound.Add(frame);
                    return(new Queue.BindOk());
                });

                testServer.On <Channel.Close>((id, frame) =>
                {
                    closedChannels.Add(frame.Channel);
                    TryStop();
                });
                testServer.On <Basic.Publish>((connectionId, frame) =>
                {
                    _basicPublishes.Add(frame);
                });
                testServer.On <Basic.Consume>((connectionId, frame) =>
                {
                    var consumerTag = Guid.NewGuid().ToString();
                    _basicConsumes.Add(frame);

                    // We need to respond with ConsumeOk before we can start delivering messages to the client
                    testServer.Send(connectionId, new MethodFrame <Basic.ConsumeOk>(frame.Channel,
                                                                                    new Basic.ConsumeOk
                    {
                        ConsumerTag = ConsumerTag.From(consumerTag)
                    }));

                    Task.Run(() =>
                    {
                        var testMessage = new TestMessage("This is sent from server.");
                        var payload     = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(testMessage));

                        testServer.Send <Basic.Deliver, Basic.ContentHeader>(connectionId,
                                                                             new MethodFrame <Basic.Deliver>(frame.Channel,
                                                                                                             new Basic.Deliver
                        {
                            ConsumerTag   = ConsumerTag.From(consumerTag),
                            ContentHeader = new Basic.ContentHeader
                            {
                                BodySize = payload.Length
                            }
                        }
                                                                                                             .AddContentBodyFragments(new ContentBody
                        {
                            Payload = payload
                        })));
                    });
                });
                testServer.On <Basic.Cancel, Basic.CancelOk>((connectionId, frame) =>
                                                             new Basic.CancelOk {
                    ConsumerTag = frame.Message.ConsumerTag
                });
                testServer.On <Basic.Ack>((connectionId, frame) =>
                {
                    _acks.Add(frame);
                    TryStop();
                });

                ServiceController.OnStopped += code =>
                {
                    testServer.Dispose();
                };

                container.RegisterSingleton(() => testServer.ConnectionFactory.ToRabbitMqConnectionFactory());
            }