예제 #1
0
        public void Can_dequeue_from_all_files()
        {
            using (var queue = new PersistentQueue(path, 10))
            {
                for (byte i = 0; i < 12; i++)
                {
                    using (var session = queue.OpenSession())
                    {
                        session.Enqueue(new[] { i });
                        session.Flush();
                    }
                }
                Assert.AreEqual(1, queue.Internals.CurrentFileNumber);
            }

            using (var queue = new PersistentQueue(path, 10))
            {
                for (byte i = 0; i < 12; i++)
                {
                    using (var session = queue.OpenSession())
                    {
                        Assert.AreEqual(i, session.Dequeue()[0]);
                        session.Flush();
                    }
                }
            }
        }
예제 #2
0
        public void Enqueue_and_dequeue_large_items_with_restart_queue()
        {
            var random = new Random();
            var itemsSizes = new List<int>();
            using (var queue = new PersistentQueue(path))
            {
                using (var session = queue.OpenSession())
                {
                    for (int i = 0; i < smallCount; i++)
                    {
                        var data = new byte[random.Next(1024*512, 1024*1024)];
                        itemsSizes.Add(data.Length);
                        session.Enqueue(data);
                    }
                    session.Flush();
                }
            }

            using (var queue = new PersistentQueue(path))
            {
                using (var session = queue.OpenSession())
                {
                    for (int i = 0; i < smallCount; i++)
                    {
                        Assert.AreEqual(itemsSizes[i], session.Dequeue().Length);
                    }
                    session.Flush();
                }
            }
        }
예제 #3
0
        public void Enqueue_and_dequeue_million_items_restart_queue()
        {
            using (var queue = new PersistentQueue(path))
            {
                using (var session = queue.OpenSession())
                {
                    for (int i = 0; i < largeCount; i++)
                    {
                        session.Enqueue(Guid.NewGuid().ToByteArray());
                    }
                    session.Flush();
                }
            }

            using (var queue = new PersistentQueue(path))
            {
                using (var session = queue.OpenSession())
                {
                    for (int i = 0; i < largeCount; i++)
                    {
                        Ignore(new Guid(session.Dequeue()));
                    }
                    session.Flush();
                }
            }
        }
예제 #4
0
        public void Count_of_items_will_remain_fixed_after_dequeqing_without_flushing()
        {
            using (var queue = new PersistentQueue(path))
            {
                queue.Internals.ParanoidFlushing = false;
                using (var session = queue.OpenSession())
                {
                    for (int j = 0; j < 10; j++)
                    {
                        session.Enqueue(Guid.NewGuid().ToByteArray());
                    }
                    session.Flush();
                }

                using (var session = queue.OpenSession())
                {
                    for (int j = 0; j < 10; j++)
                    {
                        session.Dequeue();
                    }
                    Assert.IsNull(session.Dequeue());

                    //	session.Flush(); explicitly removed
                }
            }
            using (var queue = new PersistentQueue(path))
            {
                Assert.AreEqual(10, queue.EstimatedCountOfItemsInQueue);
            }
        }
 public void Can_get_count_from_queue()
 {
     using (var queue = new PersistentQueue(path))
     {
         Assert.AreEqual(0, queue.EstimatedCountOfItemsInQueue);
     }
 }
예제 #6
0
        public void After_reading_all_items_from_file_that_is_not_the_active_file_should_delete_file()
        {
            using (var queue = new PersistentQueue(path, 10))
            {
                for (byte i = 0; i < 12; i++)
                {
                    using (var session = queue.OpenSession())
                    {
                        session.Enqueue(new[] { i });
                        session.Flush();
                    }
                }
                Assert.AreEqual(1, queue.Internals.CurrentFileNumber);
            }

            using (var queue = new PersistentQueue(path, 10))
            {
                for (byte i = 0; i < 12; i++)
                {
                    using (var session = queue.OpenSession())
                    {
                        Assert.AreEqual(i, session.Dequeue()[0]);
                        session.Flush();
                    }
                }
            }

            Assert.IsFalse(
                File.Exists(Path.Combine(path, "data.0"))
                );
        }
 public void Can_dequeue_data_from_queue()
 {
     using (var queue = new PersistentQueue(path))
     using (var session = queue.OpenSession())
     {
         session.Enqueue(new byte[] { 1, 2, 3, 4 });
         session.Flush();
         CollectionAssert.AreEqual(new byte[] { 1, 2, 3, 4 }, session.Dequeue());
     }
 }
 public void Can_enter_items_and_get_count_of_items()
 {
     using (var queue = new PersistentQueue(path))
     {
         for (byte i = 0; i < 5; i++)
         {
             using (var session = queue.OpenSession())
             {
                 session.Enqueue(new[] { i });
                 session.Flush();
             }
         }
         Assert.AreEqual(5, queue.EstimatedCountOfItemsInQueue);
     }
 }
        public void After_dequeue_from_queue_item_no_longer_on_queue()
        {
            using (var queue = new PersistentQueue(path))
            using (var session = queue.OpenSession())
            {
                session.Enqueue(new byte[] { 1, 2, 3, 4 });
                session.Flush();
            }

            using (var queue = new PersistentQueue(path))
            using (var session = queue.OpenSession())
            {
                CollectionAssert.AreEqual(new byte[] { 1, 2, 3, 4 }, session.Dequeue());
                Assert.IsNull(session.Dequeue());
                session.Flush();
            }
        }
        public void can_enqueue_and_dequeue_on_separate_threads()
        {
            int t1s, t2s;
            t1s = t2s = 0;
            const int target = 100;
            var rnd = new Random();

            IPersistentQueue _subject = new PersistentQueue("queue");
            var t1 = new Thread(() =>
            {
                for (int i = 0; i < target; i++)
                {
                    using (var session = _subject.OpenSession())
                    {
                        Console.Write("(");
                        session.Enqueue(new byte[] { 1, 2, 3, 4 });
                        Interlocked.Increment(ref t1s);
                        Thread.Sleep(rnd.Next(0, 100));
                        session.Flush();
                        Console.Write(")");
                    }
                }
            });
            var t2 = new Thread(()=> {
                for (int i = 0; i < target; i++)
                {
                    using (var session = _subject.OpenSession())
                    {
                        Console.Write("<");
                        session.Dequeue();
                        Interlocked.Increment(ref t2s);
                        Thread.Sleep(rnd.Next(0, 100));
                        session.Flush();
                        Console.Write(">");
                    }
                }
            });

            t1.Start();
            t2.Start();

            t1.Join();
            t2.Join();
            Assert.That(t1s, Is.EqualTo(target));
            Assert.That(t2s, Is.EqualTo(target));
        }
 public void If_data_stream_is_truncated_will_raise_error()
 {
     using (var queue = new PersistentQueue(path))
     using (var session = queue.OpenSession())
     {
         session.Enqueue(new byte[] { 1, 2, 3, 4 });
         session.Flush();
     }
     using (var fs = new FileStream(Path.Combine(path, "data.0"), FileMode.Open))
     {
         fs.SetLength(2);//corrupt the file
     }
     using (var queue = new PersistentQueue(path))
     using (var session = queue.OpenSession())
     {
         session.Dequeue();
     }
 }
예제 #12
0
 public void Enqueue_million_items_with_thousand_flushes()
 {
     using (var queue = new PersistentQueue(path))
     {
         for (int i = 0; i < 1000; i++)
         {
             using (var session = queue.OpenSession())
             {
                 for (int j = 0; j < 1000; j++)
                 {
                     session.Enqueue(Guid.NewGuid().ToByteArray());
                 }
                 session.Flush();
             }
         }
     }
 }
예제 #13
0
        public void Two_sessions_off_the_same_queue_cannot_get_same_item()
        {
            using (var queue = new PersistentQueue(path))
            using (var session = queue.OpenSession())
            {
                session.Enqueue(new byte[] { 1, 2, 3, 4 });
                session.Flush();
            }

            using (var queue = new PersistentQueue(path))
            using (var session2 = queue.OpenSession())
            using (var session1 = queue.OpenSession())
            {
                CollectionAssert.AreEqual(new byte[] { 1, 2, 3, 4 }, session1.Dequeue());
                Assert.IsNull(session2.Dequeue());
            }
        }
예제 #14
0
 public void Entering_more_than_count_of_items_will_work()
 {
     using (var queue = new PersistentQueue(path, 10))
     {
         for (byte i = 0; i < 11; i++)
         {
             using (var session = queue.OpenSession())
             {
                 session.Enqueue(new[] { i });
                 session.Flush();
             }
         }
         Assert.AreEqual(11, queue.EstimatedCountOfItemsInQueue);
     }
 }
예제 #15
0
 public void Can_resume_writing_to_second_file_when_restart_queue()
 {
     using (var queue = new PersistentQueue(path, 10))
     {
         for (byte i = 0; i < 11; i++)
         {
             using (var session = queue.OpenSession())
             {
                 session.Enqueue(new[] { i });
                 session.Flush();
             }
         }
         Assert.AreEqual(1, queue.Internals.CurrentFileNumber);
     }
     using (var queue = new PersistentQueue(path, 10))
     {
         for (byte i = 0; i < 2; i++)
         {
             using (var session = queue.OpenSession())
             {
                 session.Enqueue(new[] { i });
                 session.Flush();
             }
         }
         Assert.AreEqual(1, queue.Internals.CurrentFileNumber);
     }
 }
예제 #16
0
 public void Can_limit_amount_of_items_in_queue_file()
 {
     using (IPersistentQueue queue = new PersistentQueue(path, 10))
     {
         Assert.AreEqual(10, queue.MaxFileSize);
     }
 }
예제 #17
0
 public void Can_enqueue_data_in_queue()
 {
     using (var queue = new PersistentQueue(path))
     using (var session = queue.OpenSession())
     {
         session.Enqueue(new byte[] { 1, 2, 3, 4 });
         session.Flush();
     }
 }
예제 #18
0
 public void When_creating_more_items_than_allowed_in_first_file_will_create_additional_file()
 {
     using (var queue = new PersistentQueue(path, 10))
     {
         for (byte i = 0; i < 11; i++)
         {
             using (var session = queue.OpenSession())
             {
                 session.Enqueue(new[] { i });
                 session.Flush();
             }
         }
         Assert.AreEqual(1, queue.Internals.CurrentFileNumber);
     }
 }
예제 #19
0
        public void Truncated_transaction_is_ignored()
        {
            var txLogInfo = new FileInfo(Path.Combine(path, "transaction.log"));

            using (var queue = new PersistentQueue(path)
            {
                // avoid auto tx log trimming
                TrimTransactionLogOnDispose = false
            })
            {
                queue.Internals.ParanoidFlushing = false;

                using (var session = queue.OpenSession())
                {
                    for (int j = 0; j < 20; j++)
                    {
                        session.Enqueue(BitConverter.GetBytes(j));
                        session.Flush();
                    }
                }
            }

            using (var txLog = txLogInfo.Open(FileMode.Open))
            {
                txLog.SetLength(txLog.Length - 5);// corrupt last transaction
                txLog.Flush();
            }

            using (var queue = new PersistentQueue(path))
            {
                using (var session = queue.OpenSession())
                {
                    for (int j = 0; j < 19; j++)
                    {
                        Assert.AreEqual(j, BitConverter.ToInt32(session.Dequeue(), 0));
                    }
                    Assert.IsNull(session.Dequeue());// the last transaction was corrupted
                    session.Flush();
                }
            }
        }
예제 #20
0
        public void If_tx_log_grows_too_large_it_will_be_trimmed_while_queue_is_in_operation()
        {
            var txLogInfo = new FileInfo(Path.Combine(path, "transaction.log"));

            using (var queue = new PersistentQueue(path)
            {
                SuggestedMaxTransactionLogSize = 32 // single entry
            })
            {
                queue.Internals.ParanoidFlushing = false;

                using (var session = queue.OpenSession())
                {
                    for (int j = 0; j < 20; j++)
                    {
                        session.Enqueue(Guid.NewGuid().ToByteArray());
                    }
                    session.Flush();
                }
                // there is no way optimize here, so we should get expected size, even though it is bigger than
                // what we suggested as the max
                txLogInfo.Refresh();
                long txSizeWhenOpen = txLogInfo.Length;

                using (var session = queue.OpenSession())
                {
                    for (int j = 0; j < 20; j++)
                    {
                        session.Dequeue();
                    }
                    Assert.IsNull(session.Dequeue());

                    session.Flush();
                }
                txLogInfo.Refresh();
                Assert.Less(txLogInfo.Length, txSizeWhenOpen);
            }
        }
예제 #21
0
        public void Will_remove_truncated_transaction()
        {
            var txLogInfo = new FileInfo(Path.Combine(path, "transaction.log"));

            using (var queue = new PersistentQueue(path))
            {
                using (var session = queue.OpenSession())
                {
                    for (int j = 0; j < 20; j++)
                    {
                        session.Enqueue(BitConverter.GetBytes(j));
                        session.Flush();
                    }
                }
            }

            using (var txLog = txLogInfo.Open(FileMode.Open))
            {
                txLog.SetLength(5);// corrupt all transactions
                txLog.Flush();
            }

            new PersistentQueue(path).Dispose();

            txLogInfo.Refresh();

            Assert.AreEqual(36, txLogInfo.Length);//empty transaction size
        }
예제 #22
0
        public void Transaction_log_size_shrink_after_queue_disposed()
        {
            long txSizeWhenOpen;
            var txLogInfo = new FileInfo(Path.Combine(path, "transaction.log"));
            using (var queue = new PersistentQueue(path))
            {
                queue.Internals.ParanoidFlushing = false;
                using (var session = queue.OpenSession())
                {
                    for (int j = 0; j < 10; j++)
                    {
                        session.Enqueue(Guid.NewGuid().ToByteArray());
                    }
                    session.Flush();
                }

                using (var session = queue.OpenSession())
                {
                    for (int j = 0; j < 10; j++)
                    {
                        session.Dequeue();
                    }
                    session.Flush();
                }
                txSizeWhenOpen = txLogInfo.Length;
            }
            txLogInfo.Refresh();
            Assert.Less(txLogInfo.Length, txSizeWhenOpen);
        }
예제 #23
0
        public void Not_flushing_the_session_will_revert_dequequed_items_two_sessions_same_queue()
        {
            using (var queue = new PersistentQueue(path))
            using (var session = queue.OpenSession())
            {
                session.Enqueue(new byte[] { 1, 2, 3, 4 });
                session.Flush();
            }

            using (var queue = new PersistentQueue(path))
            using (var session2 = queue.OpenSession())
            {
                using (var session1 = queue.OpenSession())
                {
                    CollectionAssert.AreEqual(new byte[] { 1, 2, 3, 4 }, session1.Dequeue());
                    //Explicitly ommitted: session.Flush();
                }
                CollectionAssert.AreEqual(new byte[] { 1, 2, 3, 4 }, session2.Dequeue());
                session2.Flush();
            }
        }
예제 #24
0
        public void Truncated_transaction_is_ignored_and_can_continue_to_add_items_to_queue()
        {
            var txLogInfo = new FileInfo(Path.Combine(path, "transaction.log"));

            using (var queue = new PersistentQueue(path)
            {
                // avoid auto tx log trimming
                TrimTransactionLogOnDispose = false
            })
            {
                queue.Internals.ParanoidFlushing = false;
                using (var session = queue.OpenSession())
                {
                    for (int j = 0; j < 20; j++)
                    {
                        session.Enqueue(BitConverter.GetBytes(j));
                        session.Flush();
                    }
                }
            }

            using (var txLog = txLogInfo.Open(FileMode.Open))
            {
                txLog.SetLength(txLog.Length - 5);// corrupt last transaction
                txLog.Flush();
            }

            using (var queue = new PersistentQueue(path)
            {
                // avoid auto tx log trimming
                TrimTransactionLogOnDispose = false
            })
            {
                using (var session = queue.OpenSession())
                {
                    for (int j = 20; j < 40; j++)
                    {
                        session.Enqueue(BitConverter.GetBytes(j));
                    }
                    session.Flush();
                }
            }

            var data = new List<int>();
            using (var queue = new PersistentQueue(path))
            {
                using (var session = queue.OpenSession())
                {
                    var dequeue = session.Dequeue();
                    while (dequeue != null)
                    {
                        data.Add(BitConverter.ToInt32(dequeue, 0));
                        dequeue = session.Dequeue();
                    }
                    session.Flush();
                }
            }
            var expected = 0;
            foreach (var i in data)
            {
                if (expected == 19)
                    continue;
                Assert.AreEqual(expected, data[i]);
                expected++;
            }
        }
예제 #25
0
 public void Enqueue_million_items_with_single_flush()
 {
     using (var queue = new PersistentQueue(path))
     {
         using (var session = queue.OpenSession())
         {
             for (int i = 0; i < largeCount; i++)
             {
                 session.Enqueue(Guid.NewGuid().ToByteArray());
             }
             session.Flush();
         }
     }
 }
예제 #26
0
        public void Dequeue_items_that_were_not_flushed_will_appear_after_queue_restart()
        {
            using (var queue = new PersistentQueue(path))
            {
                using (var session = queue.OpenSession())
                {
                    for (int j = 0; j < 10; j++)
                    {
                        session.Enqueue(Guid.NewGuid().ToByteArray());
                    }
                    session.Flush();
                }

                using (var session = queue.OpenSession())
                {
                    for (int j = 0; j < 10; j++)
                    {
                        session.Dequeue();
                    }
                    Assert.IsNull(session.Dequeue());

                    //	session.Flush(); explicitly removed
                }
            }
            using (var queue = new PersistentQueue(path))
            {
                using (var session = queue.OpenSession())
                {
                    for (int j = 0; j < 10; j++)
                    {
                        session.Dequeue();
                    }
                    Assert.IsNull(session.Dequeue());
                    session.Flush();
                }
            }
        }
예제 #27
0
 public void Dequeing_from_empty_queue_will_return_null()
 {
     using (var queue = new PersistentQueue(path))
     using (var session = queue.OpenSession())
     {
         Assert.IsNull(session.Dequeue());
     }
 }
예제 #28
-1
        public void Can_handle_truncated_start_transaction_seperator()
        {
            var txLogInfo = new FileInfo(Path.Combine(path, "transaction.log"));

            using (var queue = new PersistentQueue(path)
            {
                // avoid auto tx log trimming
                TrimTransactionLogOnDispose = false
            })
            {
                using (var session = queue.OpenSession())
                {
                    for (int j = 0; j < 20; j++)
                    {
                        session.Enqueue(BitConverter.GetBytes(j));
                        session.Flush();
                    }
                }
            }

            using (var txLog = txLogInfo.Open(FileMode.Open))
            {
                txLog.SetLength(5);// corrupt last transaction
                txLog.Flush();
            }

            using (var queue = new PersistentQueue(path))
            {
                using (var session = queue.OpenSession())
                {
                    Assert.IsNull(session.Dequeue());// the last transaction was corrupted
                    session.Flush();
                }
            }
        }