Beispiel #1
0
        public void GlobalSetup()
        {
            message = new ReadOnlyMemory <byte>(System.Text.Encoding.UTF8.GetBytes("0"));

            connectionFactory = new ConnectionFactory()
            {
                Port        = 5672,
                HostName    = "localhost",
                UserName    = "******",
                Password    = "******",
                VirtualHost = "EnterpriseLog",
                AutomaticRecoveryEnabled = true,
                RequestedHeartbeat       = TimeSpan.FromMinutes(1)
            };

            Func <IConnection> getConnection = () => connectionFactory.CreateConnection();
            Func <IModel>      getModel      = () =>
            {
                using var connection = connectionRingBuffer.Accquire();
                return(connection.Current.CreateModel());
            };

            connectionRingBuffer = new DisposableRingBuffer <IConnection>(3, getConnection, TimeSpan.FromMilliseconds(10));
            modelRingBuffer      = new DisposableRingBuffer <IModel>(10, getModel, TimeSpan.FromMilliseconds(10));
        }
        public void TestInline()
        {
            RingBuffer.RingBuffer <DisposableTestableClass> buffer = null;
            Queue <DisposableTestableClass> queue = new Queue <DisposableTestableClass>();

            for (var i = 0; i < bufferSize; i++)
            {
                queue.Enqueue(new DisposableTestableClass());
            }

            using (DisposableRingBuffer <DisposableTestableClass> disposableBuffer = new DisposableRingBuffer <DisposableTestableClass>(
                       bufferSize,
                       () => queue.Dequeue(),
                       waitTime))
            {
                buffer = disposableBuffer;
                var         factory = new TaskFactory();
                List <Task> tasks   = new List <Task>();
                for (int i = 0; i < runCount; i++)
                {
                    using var bufferedItem = disposableBuffer.Accquire();
                    System.Threading.Thread.Sleep(workTime);
                }
                Assert.Equal(bufferSize, disposableBuffer.Available);
                Assert.Equal(disposableBuffer.Capacity, disposableBuffer.Available);
            }


            Assert.All(queue, it => Assert.True(it.IsDisposed));
            Assert.Empty(queue);
            Assert.Equal(0, buffer.Available);
            Assert.Equal(bufferSize, buffer.Capacity);
        }
        public void TestMultiThreadDisposableTest()
        {
            RingBuffer.RingBuffer <DisposableTestableClass> buffer = null;
            Queue <DisposableTestableClass> queue = new Queue <DisposableTestableClass>();

            for (var i = 0; i < bufferSize; i++)
            {
                queue.Enqueue(new DisposableTestableClass());
            }

            using (DisposableRingBuffer <DisposableTestableClass> disposableBuffer = new DisposableRingBuffer <DisposableTestableClass>(
                       bufferSize,
                       () => queue.Dequeue(),
                       waitTime))
            {
                buffer = disposableBuffer;
                List <Thread> threads = new List <Thread>();

                for (int i = 0; i < runCount; i++)
                {
                    Thread thread = new Thread(() => {
                        using var bufferedItem = disposableBuffer.Accquire();
                        System.Threading.Thread.Sleep(workTime);
                        System.Diagnostics.Debug.WriteLine($"Item {bufferedItem.Current} - processado!");
                    });
                    thread.Start();
                    threads.Add(thread);
                }

                foreach (Thread thread in threads)
                {
                    thread.Join();
                }

                Assert.Equal(bufferSize, disposableBuffer.Available);
                Assert.Equal(disposableBuffer.Capacity, disposableBuffer.Available);
            }


            Assert.All(queue, it => Assert.True(it.IsDisposed));
            Assert.Empty(queue);
            Assert.Equal(0, buffer.Available);
            Assert.Equal(bufferSize, buffer.Capacity);
        }
        private static void Init()
        {
            PublisherRoleProgram.ConnectionFactory = new ConnectionFactory()
            {
                Port        = 5672,
                HostName    = "rabbitmq",
                UserName    = "******",
                Password    = "******",
                VirtualHost = "EnterpriseLog",
                AutomaticRecoveryEnabled = true,
                RequestedHeartbeat       = TimeSpan.FromMinutes(1)
            };

            //Program.Connection = Program.ConnectionFactory.CreateConnection();

            PublisherRoleProgram.connectionRingBuffer = new DisposableRingBuffer <IConnection>(5, PublisherRoleProgram.ConnectionFactory.CreateConnection, TimeSpan.FromMilliseconds(5));

            PublisherRoleProgram.modelRingBuffer = new DisposableRingBuffer <IModel>(20, ModelFactory, TimeSpan.FromMilliseconds(5));
        }
        private static void Init()
        {
            ConsumerRoleProgram.ConnectionFactory = new ConnectionFactory()
            {
                Port        = 5672,
                HostName    = "rabbitmq",
                UserName    = "******",
                Password    = "******",
                VirtualHost = "EnterpriseLog",
                AutomaticRecoveryEnabled = true,
                RequestedHeartbeat       = TimeSpan.FromMinutes(1),
                NetworkRecoveryInterval  = TimeSpan.FromSeconds(10),
                DispatchConsumersAsync   = true,
            };

            //ConsumerRoleProgram.Connection = ConsumerRoleProgram.ConnectionFactory.CreateConnection();

            ConsumerRoleProgram.connectionRingBuffer = new DisposableRingBuffer <IConnection>(10, ConnectionFactoryFunc, TimeSpan.FromMilliseconds(5));

            ConsumerRoleProgram.modelRingBuffer = new DisposableRingBuffer <IModel>(40, ModelFactory, TimeSpan.FromMilliseconds(5));
        }