public async Task CreateBindAndUnbindExchangeAsync()
        {
            var options = new RabbitOptions();

            options.FactoryOptions.Uri = new Uri("amqp://*****:*****@localhost:5672/");

            var top   = new Topologer(options);
            var error = await top.CreateExchangeAsync("TestExchangeTest", "direct", false, false, null).ConfigureAwait(false);

            Assert.False(error);

            error = await top.CreateExchangeAsync("TestExchange2Test", "direct", false, false, null).ConfigureAwait(false);

            Assert.False(error);

            error = await top.BindExchangeToExchangeAsync("TestExchange2Test", "TestExchangeTest", "TestRoutingKeyTest", null).ConfigureAwait(false);

            Assert.False(error);

            error = await top.UnbindExchangeFromExchangeAsync("TestExchange2Test", "TestExchangeTest", "TestRoutingKeyTest", null).ConfigureAwait(false);

            Assert.False(error);

            error = await top.DeleteExchangeAsync("TestExchangeTest").ConfigureAwait(false);

            Assert.False(error);

            error = await top.DeleteExchangeAsync("TestExchange2Test").ConfigureAwait(false);

            Assert.False(error);
        }
Exemple #2
0
        public async Task OverLoopThroughConnectionPoolAsync()
        {
            var options = new RabbitOptions();

            options.FactoryOptions.Uri         = new Uri("amqp://*****:*****@localhost:5672/");
            options.PoolOptions.MaxConnections = 5;
            var       successCount = 0;
            const int loopCount    = 100_000;
            var       connPool     = new ConnectionPool(options);

            var sw = Stopwatch.StartNew();

            for (int i = 0; i < loopCount; i++)
            {
                var connHost = await connPool
                               .GetConnectionAsync()
                               .ConfigureAwait(false);

                if (connHost != null)
                {
                    successCount++;
                }

                await connPool
                .ReturnConnectionAsync(connHost)
                .ConfigureAwait(false);
            }

            sw.Stop();
            _fixture.Output.WriteLine($"OverLoop Iteration Time: {sw.ElapsedMilliseconds} ms");

            Assert.True(successCount == loopCount);
        }
Exemple #3
0
        public async Task PublishToNonExistentQueueAsync()
        {
            var options = new RabbitOptions();

            options.FactoryOptions.Uri = new Uri("amqp://*****:*****@localhost:5672/");

            var pub = new Publisher(
                _fixture.ChannelPool,
                _fixture.SerializationProvider,
                _fixture.EncryptionProvider,
                _fixture.CompressionProvider);

            var letter = MessageExtensions.CreateSimpleRandomLetter("TestQueue", 2000);

            await pub.PublishAsync(letter, false);

            var tokenSource = new CancellationTokenSource(delay: TimeSpan.FromSeconds(1));

            async Task ReadReceiptAsync(CancellationToken cancellationToken)
            {
                var receiptBuffer = pub.GetReceiptBufferReader();
                await receiptBuffer.WaitToReadAsync(cancellationToken);

                var receipt = receiptBuffer.ReadAsync(cancellationToken);
            }

            await Assert
            .ThrowsAnyAsync <OperationCanceledException>(() => ReadReceiptAsync(tokenSource.Token));
        }
        public void CreateTopologerAndInitializeChannelPool()
        {
            var options = new RabbitOptions();

            options.FactoryOptions.Uri = new Uri("amqp://*****:*****@localhost:5672/");

            var top = new Topologer(options);

            Assert.NotNull(top);
        }
Exemple #5
0
        public void CreateChannelPoolWithLocalHost()
        {
            var options = new RabbitOptions();

            options.FactoryOptions.Uri = new Uri("amqp://*****:*****@localhost:5672/");

            var chanPool = new ChannelPool(options);

            Assert.NotNull(chanPool);
        }
Exemple #6
0
        public void InitializeConnectionPoolAsync()
        {
            var options = new RabbitOptions();

            options.FactoryOptions.Uri = new Uri("amqp://*****:*****@localhost:5672/");

            var connPool = new ConnectionPool(options);

            Assert.NotNull(connPool);
        }
        public async Task CreateExchangeAsync()
        {
            var options = new RabbitOptions();

            options.FactoryOptions.Uri = new Uri("amqp://*****:*****@localhost:5672/");

            var top   = new Topologer(options);
            var error = await top.CreateExchangeAsync("TestExchangeTest", "direct", false, false, null).ConfigureAwait(false);

            Assert.False(error);
        }
Exemple #8
0
        public void InitializeChannelPoolAsync()
        {
            var options = new RabbitOptions();

            options.FactoryOptions.Uri = new Uri("amqp://*****:*****@localhost:5672/");

            var chanPool = new ChannelPool(options);

            Assert.NotNull(chanPool);
            Assert.True(chanPool.CurrentChannelId > 0);
        }
Exemple #9
0
        public void CreatePublisher()
        {
            var options = new RabbitOptions();

            options.FactoryOptions.Uri = new Uri("amqp://*****:*****@localhost:5672/");

            var pub = new Publisher(
                _fixture.ChannelPool,
                _fixture.SerializationProvider,
                _fixture.EncryptionProvider,
                _fixture.CompressionProvider);

            Assert.NotNull(pub);
        }
Exemple #10
0
        public async Task PublishManyAsync()
        {
            var options = new RabbitOptions();

            options.FactoryOptions.Uri = new Uri("amqp://*****:*****@localhost:5672/");
            const int letterCount = 10_000;
            const int byteCount   = 500;

            var pub = new Publisher(
                _fixture.ChannelPool,
                _fixture.SerializationProvider,
                _fixture.EncryptionProvider,
                _fixture.CompressionProvider);

            await pub
            .StartAutoPublishAsync()
            .ConfigureAwait(false);

            var queueNames = new List <string>
            {
                "TestQueue0",
                "TestQueue1",
                "TestQueue2",
                "TestQueue3",
                "TestQueue4",
                "TestQueue5",
                "TestQueue6",
                "TestQueue7",
                "TestQueue8",
                "TestQueue9",
            };

            var letters = MessageExtensions.CreateManySimpleRandomLetters(queueNames, letterCount, byteCount);

            var sw = Stopwatch.StartNew();
            await pub
            .PublishManyAsync(letters, false)
            .ConfigureAwait(false);

            sw.Stop();

            const double kiloByteCount = byteCount / 1000.0;

            _fixture.Output.WriteLine($"Published {letterCount} letters, {kiloByteCount} KB each, in {sw.ElapsedMilliseconds} ms.");

            var rate     = letterCount / (sw.ElapsedMilliseconds / 1000.0);
            var dataRate = rate * kiloByteCount;

            _fixture.Output.WriteLine($"Message Rate: {rate.ToString("0.###")} letters / sec, or {(dataRate / 1000.0).ToString("0.###")} MB/s");
        }
Exemple #11
0
        public async Task CreatePublisherAndInitializeChannelPool()
        {
            var options = new RabbitOptions();

            options.FactoryOptions.Uri = new Uri("amqp://*****:*****@localhost:5672/");

            var pub = new Publisher(
                _fixture.ChannelPool,
                _fixture.SerializationProvider,
                _fixture.EncryptionProvider,
                _fixture.CompressionProvider);

            await pub.StartAutoPublishAsync().ConfigureAwait(false);

            Assert.NotNull(pub);
        }
        public RabbitService(Action <RabbitOptions> settings)
        {
            _settings = new RabbitOptions();
            settings.Invoke(_settings);

            new ConnectionFactory
            {
                HostName = _settings.Credentials.HostName,
                UserName = _settings.Credentials.UserName,
                Password = _settings.Credentials.Password
            }.CreateConnection().CreateModel().QueueDeclare(queue: _settings.QueueName, durable: false, exclusive: false, autoDelete: _settings.AutoDelete, arguments: null);

            _rabbitProvider = new RabbitMqProvider(
                new MqCredentials(
                    _settings.Credentials.HostName,
                    _settings.Credentials.UserName,
                    _settings.Credentials.Password,
                    _settings.QueueName),
                _settings.QueueName);
            _rabbitProvider.Bind();
        }
Exemple #13
0
        public async Task PublishAsync()
        {
            var options = new RabbitOptions();

            options.FactoryOptions.Uri = new Uri("amqp://*****:*****@localhost:5672/");

            var pub = new Publisher(
                _fixture.ChannelPool,
                _fixture.SerializationProvider,
                _fixture.EncryptionProvider,
                _fixture.CompressionProvider);

            await pub
            .StartAutoPublishAsync()
            .ConfigureAwait(false);

            var letter = MessageExtensions.CreateSimpleRandomLetter("TestQueue", 2000);
            await pub
            .PublishAsync(letter, false)
            .ConfigureAwait(false);
        }
Exemple #14
0
        public RabbitFixture()
        {
            CompressionProvider   = new GzipProvider();
            HashingProvider       = new Argon2ID_HashingProvider();
            HashKey               = HashingProvider.GetHashKey(Passphrase, Salt, 32);
            EncryptionProvider    = new AesGcmEncryptionProvider(HashKey);
            SerializationProvider = new Utf8JsonProvider();

            Options = JsonFileReader.ReadFileAsync <RabbitOptions>("RabbitMQ\\TestConfig.json").GetAwaiter().GetResult();

            RabbitService = new RabbitService(
                Options,
                SerializationProvider,
                EncryptionProvider,
                CompressionProvider,
                LoggerFactory
                .Create(
                    builder => builder.AddConsole().SetMinimumLevel(LogLevel.Information)));

            ChannelPool = RabbitService.ChannelPool;
            Topologer   = RabbitService.Topologer;
            Publisher   = RabbitService.Publisher;
        }