public void When_Decrementing_value_on_cache_below_zero_will_set_to_zero()
        {
            Cache["foo"] = new CachedItem { Buffer = Encoding.ASCII.GetBytes("12") };
            var stream = new MemoryStream();
            var command = new DecrCommand();
            command.SetContext(stream);
            command.Init("foo", "15");

            command.FinishedExecuting += () => { wait.Set(); };
            command.Execute();
            wait.WaitOne();

            Assert.AreEqual("0\r\n", ReadAll(stream));
        }
        public void When_deleting_item_in_cache_will_remove_from_cache()
        {
            Cache["foo"] = new CachedItem();

            var command = new DeleteCommand();
            command.SetContext(new MemoryStream());
            command.Init("foo");

            command.FinishedExecuting += () => wait.Set();
            command.Execute();
            wait.WaitOne();

            var cachedItem = (CachedItem)Cache.Get("foo");
            Assert.IsNull(cachedItem);
        }
        public void When_incrementing_value_on_cache_which_is_in_invalid_format_assumes_it_is_zero()
        {
            Cache["foo"] = new CachedItem {Buffer = new byte[] {1}};
            var stream = new MemoryStream();
            var command = new IncrCommand();
            command.SetContext(stream);

            command.Init("foo", "5");

            command.FinishedExecuting += () => { wait.Set(); };
            command.Execute();
            wait.WaitOne();

            Assert.AreEqual("5\r\n", ReadAll(stream));
        }
        public void When_getting_item_that_has_been_expired_will_return_empty_result()
        {
            Cache["foo"] = new CachedItem
                           	{Buffer = new byte[] {1, 2, 3}, Flags = 2, Key = "foo", ExpiresAt = SystemTime.Now().AddDays(-1)};

            var stream = new MemoryStream();
            var command = new GetsCommand();
            command.SetContext(stream);
            command.Init("foo");

            command.FinishedExecuting += () => { wait.Set(); };
            command.Execute();
            wait.WaitOne();

            stream.Position = 0;
            Assert.AreEqual("END", new StreamReader(stream).ReadLine());
        }
        public void When_Decrementing_value_on_cache_which_is_in_valid_format_use_this_as_base()
        {
            Cache["foo"] = new CachedItem { Buffer = Encoding.ASCII.GetBytes("12") };
            var stream = new MemoryStream();
            var command = new DecrCommand();
            command.SetContext(stream);

            command.Init("foo", "5");

            command.FinishedExecuting += () => { wait.Set(); };
            command.Execute();
            wait.WaitOne();

            Assert.AreEqual("7\r\n", ReadAll(stream));

            wait.Reset();
            command.FinishedExecuting += () => { wait.Set(); };
            command.Execute();
            wait.WaitOne();

            Assert.AreEqual("2\r\n", ReadAll(3, stream));
        }
        public void When_getting_item_that_is_in_cache_will_return_item()
        {
            Cache["foo"] = new CachedItem
                           	{Buffer = new byte[] {1, 2, 3}, Flags = 2, Key = "foo", ExpiresAt = SystemTime.Now().AddDays(1)};

            var stream = new MemoryStream();
            var command = new GetsCommand();
            command.SetContext(stream);
            command.Init("foo");

            command.FinishedExecuting += () => { wait.Set(); };
            command.Execute();
            wait.WaitOne();

            stream.Position = 0;
            string line = new StreamReader(stream).ReadLine();
            Assert.AreEqual("VALUE foo 2 3 630822816000000000", line);
            stream.Position = line.Length + 2; // reset buffering of stream reader
            var buffer = new byte[5];
            stream.Read(buffer, 0, 5);
            CollectionAssert.AreEqual(new byte[] {1, 2, 3, 13, 10}, buffer);
            Assert.AreEqual("END", new StreamReader(stream).ReadLine());
        }
        public void When_getting_several_items_where_some_in_cache_and_some_not_will_return_items_that_are_in_cache()
        {
            Cache["foo0"] = new CachedItem
                                {Buffer = new byte[] {1, 2, 3}, Flags = 2, Key = "foo0", ExpiresAt = SystemTime.Now().AddDays(1)};
            Cache["foo2"] = new CachedItem
                                {Buffer = new byte[] {1, 2, 3}, Flags = 2, Key = "foo2", ExpiresAt = SystemTime.Now().AddDays(1)};

            var stream = new MemoryStream();
            var command = new GetsCommand();
            command.SetContext(stream);
            command.Init("foo0", "foo1", "foo2");

            command.FinishedExecuting += () => { wait.Set(); };
            command.Execute();
            wait.WaitOne();

            stream.Position = 0;
            int currentPos = 0;
            for (int i = 0; i < 3; i++)
            {
                if (i == 1)
                    continue;
                string line = new StreamReader(stream).ReadLine();
                Assert.AreEqual("VALUE foo" + i + " 2 3 630822816000000000", line);
                stream.Position = currentPos + line.Length + 2; // reset buffering of stream reader
                var buffer = new byte[5];
                stream.Read(buffer, 0, 5);
                CollectionAssert.AreEqual(new byte[] {1, 2, 3, 13, 10}, buffer);
                currentPos = (int) stream.Position;
            }
            Assert.AreEqual("END", new StreamReader(stream).ReadLine());
        }
        public void When_deleting_item_in_cache_will_return_deleted()
        {
            Cache["foo"] = new CachedItem();

            var stream = new MemoryStream();
            var command = new DeleteCommand();
            command.SetContext(stream);
            command.Init("foo");

            command.FinishedExecuting += () => wait.Set();
            command.Execute();
            wait.WaitOne();

            Assert.AreEqual("DELETED\r\n", ReadAll(stream));
        }
        public void When_deleting_item_not_in_cache_will_return_nothing_with_no_reply()
        {
            Cache["foo"] = new CachedItem();

            var stream = new MemoryStream();
            var command = new DeleteCommand();
            command.SetContext(stream);
            command.Init("foo2", "0", "noreply");

            command.FinishedExecuting += () => wait.Set();
            command.Execute();
            wait.WaitOne();

            Assert.AreEqual("", ReadAll(stream));
        }
        public void When_deleting_item_in_cache_with_time_will_block_replace_operations()
        {
            Cache["foo2"] = new CachedItem();

            var stream = new MemoryStream();
            var command = new DeleteCommand();
            command.SetContext(stream);
            command.Init("foo2", "500");

            command.FinishedExecuting += () => wait.Set();
            command.Execute();
            wait.WaitOne();

            Assert.AreEqual("DELETED\r\n", ReadAll(stream));

            wait.Reset();

            var buffer = new byte[] { 1, 2, 3, 4 };
            stream = GetStreamWithData(buffer);
            var replaceCommand = new ReplaceCommand();
            replaceCommand.SetContext(stream);
            replaceCommand.Init("foo2", "1", "6000", "4");

            replaceCommand.FinishedExecuting += () => wait.Set();
            replaceCommand.Execute();
            wait.WaitOne();

            Assert.AreEqual("NOT_STORED\r\n", ReadAll(6, stream));
        }