示例#1
0
        public void Quota()
        {
            BlockPool  pool = new BlockPool(1024, 3, 3, true);
            MemoryFile file = new MemoryFile(pool);

            using (var s = file.Open(FileAccess.ReadWrite, FileShare.None))
            {
                Assert.AreEqual(0L, pool.BytesAllocated);

                s.Write(new byte[1024]);
                Assert.AreEqual(1024L, pool.BytesAllocated);
                Assert.AreEqual(2048L, pool.BytesAvailable);
                Assert.AreEqual(1024L, s.Length);

                s.Write(new byte[1024]);
                Assert.AreEqual(2048L, pool.BytesAllocated);
                Assert.AreEqual(1024L, pool.BytesAvailable);
                Assert.AreEqual(2048L, s.Length);

                s.Write(new byte[1024]);
                Assert.AreEqual(3072L, s.Length);
                Assert.AreEqual(3072L, pool.BytesAllocated);
                Assert.AreEqual(0L, pool.BytesAvailable);

                try
                {
                    s.Write(new byte[1024]);
                    Assert.Fail();
                } catch (FileSystemExceptionOutOfDiskSpace)
                {
                }
                Assert.AreEqual(3072, s.Length);

                try
                {
                    s.WriteByte(3);
                    Assert.Fail();
                }
                catch (FileSystemExceptionOutOfDiskSpace)
                {
                }
                Assert.AreEqual(3072, s.Length);

                s.SetLength(3071);
                Assert.AreEqual(3071, s.Length);

                s.WriteByte(3);
                Assert.AreEqual(3072, s.Length);

                s.SetLength(0L);
                Assert.AreEqual(0, s.Length);
                Assert.AreEqual(0L, pool.BytesAllocated);
                Assert.AreEqual(3072L, pool.BytesAvailable);

                s.Write(new byte[1024]);
                s.Write(new byte[1024]);
                s.Write(new byte[1024]);
                Assert.AreEqual(3072L, s.Length);
                Assert.AreEqual(3072L, pool.BytesAllocated);
                Assert.AreEqual(0L, pool.BytesAvailable);
            }

            Assert.AreEqual(3072L, pool.BytesAllocated);
            Assert.AreEqual(0L, pool.BytesAvailable);
            Stream ss = file.Open(FileAccess.ReadWrite, FileShare.ReadWrite);

            file.Dispose();
            Assert.AreEqual(3072L, pool.BytesAllocated);
            Assert.AreEqual(0L, pool.BytesAvailable);
            ss.Dispose();
            Assert.AreEqual(0L, pool.BytesAllocated);
            Assert.AreEqual(3072L, pool.BytesAvailable);
        }