Beispiel #1
0
 return(new NamedQueueSourceSettings(ConnectionSettings, Queue, Declarations, NoLocal, Exclusive, AckRequired, ConsumerTag,
                                     arguments.ToDictionary(key => key.paramName, val => val.paramValue)));
            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());
            }