public void CheckDisposeWorks()
 {
     MemoryStream ms = new MemoryStream(new byte[1]);
     int iAllocCount = OptimizedBinaryData.AllocCount;
     using (OptimizedBinaryData obd = new OptimizedBinaryData(ms))
     {
         Assert.AreEqual(iAllocCount, OptimizedBinaryData.AllocCount);
     }
     Assert.AreEqual(iAllocCount, OptimizedBinaryData.AllocCount);
 }
Esempio n. 2
0
		public FileForTests(string filename)
		{
			if (!System.IO.File.Exists(filename))
				throw new FileNotFoundException("file not found", filename);

			BinaryData fileContent = new OptimizedBinaryData(filename);

			m_files = new List<IContainer>();
			m_contents = fileContent;
			this.m_filename = filename;
		}
 public void CheckNoConflictOnDisposeAndFinalize()
 {
     MemoryStream ms = new MemoryStream(new byte[1]);
     int iAllocCount = OptimizedBinaryData.AllocCount;
     using (OptimizedBinaryData obd = new OptimizedBinaryData(ms))
     {
         Assert.AreEqual(iAllocCount, OptimizedBinaryData.AllocCount);
     }
     System.GC.Collect();
     System.GC.Collect();
     System.GC.WaitForPendingFinalizers();
     Assert.AreEqual(iAllocCount, OptimizedBinaryData.AllocCount);
 }
        public void CheckFinalizerWorks()
        {
            MemoryStream ms = new MemoryStream(new byte[1]);
            int iAllocCount = OptimizedBinaryData.AllocCount;
            OptimizedBinaryData obd = new OptimizedBinaryData(ms);

            Assert.AreEqual(iAllocCount, OptimizedBinaryData.AllocCount);

            // should not be needed - we need weak references
           // BinaryDataMemoryMonitor.Instance.MarkNotInMemory(obd); // make the memory manager forget this object

            obd = null;



            System.GC.Collect();
            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();
            Assert.AreEqual(iAllocCount, OptimizedBinaryData.AllocCount);
        }
        public void TestRegistersInCounters()
        {
            Assert.AreEqual(0, BinaryDataMemoryMonitor.Instance.BytesInMemory);
            byte[] data = new byte[] {1, 2, 3, 4, 5,6 , 7, 8};
            MemoryStream str = new MemoryStream(data);
            using (OptimizedBinaryData obd = new OptimizedBinaryData(str))
            {
                Assert.AreEqual(8, BinaryDataMemoryMonitor.Instance.BytesInMemory);
                Assert.AreEqual(0, BinaryDataMemoryMonitor.Instance.BytesInUse);

                using (new BinaryDataLock(obd))
                {
                    Assert.AreEqual(8, BinaryDataMemoryMonitor.Instance.BytesInMemory);
                    Assert.AreEqual(8, BinaryDataMemoryMonitor.Instance.BytesInUse);
                }
                Assert.AreEqual(8, BinaryDataMemoryMonitor.Instance.BytesInMemory);
                Assert.AreEqual(0, BinaryDataMemoryMonitor.Instance.BytesInUse);
            }
            Assert.AreEqual(0, BinaryDataMemoryMonitor.Instance.BytesInMemory);
            Assert.AreEqual(0, BinaryDataMemoryMonitor.Instance.BytesInUse);
        }
        public void TestPushesDataOutToDisk()
        {
            uint TestThreshold = 60 * 1024;
            BinaryDataMemoryMonitor.Instance.MemoryThreshold = TestThreshold; 

            OptimizedBinaryData obd1 = new OptimizedBinaryData(TESTFILE_DIR + "TestTableTCs.doc");
            Assert.Less(BinaryDataMemoryMonitor.Instance.BytesInMemory, TestThreshold);
            Assert.AreEqual(0, BinaryDataMemoryMonitor.Instance.BytesInUse);
            OptimizedBinaryData obd2 = new OptimizedBinaryData(TESTFILE_DIR + "2TableTCsInOneTable.doc");
            Assert.Less(BinaryDataMemoryMonitor.Instance.BytesInMemory, TestThreshold);
            Assert.AreEqual(0, BinaryDataMemoryMonitor.Instance.BytesInUse);
            OptimizedBinaryData obd3 = new OptimizedBinaryData(TESTFILE_DIR + "TestCleanComments.doc");
            Assert.Less(BinaryDataMemoryMonitor.Instance.BytesInMemory, TestThreshold);
            Assert.AreEqual(0, BinaryDataMemoryMonitor.Instance.BytesInUse);

            using (new BinaryDataLock(obd1))
            {
                Assert.Less(BinaryDataMemoryMonitor.Instance.BytesInMemory, TestThreshold);
                using (new BinaryDataLock(obd2))
                {
                    Assert.Less(BinaryDataMemoryMonitor.Instance.BytesInMemory, TestThreshold);
                    using (new BinaryDataLock(obd3))
                    {
                        Assert.Greater(BinaryDataMemoryMonitor.Instance.BytesInUse, TestThreshold);
                        Assert.Greater(BinaryDataMemoryMonitor.Instance.BytesInMemory, TestThreshold);
                    }
                }
            }


            obd1.Dispose();
            obd2.Dispose();
            obd3.Dispose();

            Assert.AreEqual(0, BinaryDataMemoryMonitor.Instance.BytesInMemory);
            Assert.AreEqual(0, BinaryDataMemoryMonitor.Instance.BytesInUse);
        }
        public void CheckNoSupportForCopyTo()
        {
            MemoryStream ms = new MemoryStream(new byte[1]);
            using (OptimizedBinaryData obd = new OptimizedBinaryData(ms))
            {
                obd.CopyTo(new IntPtr());
            }

        }
        public void CheckGCCleansUpLockBytes()
        {
            int iCount = OptimizedBinaryData.LockBytesObjectCount;
            int iAllocCount = OptimizedBinaryData.AllocCount;
            MemoryStream ms = new MemoryStream(new byte[1]);
            OptimizedBinaryData obd = new OptimizedBinaryData(ms);
            Assert.AreEqual(iCount, OptimizedBinaryData.LockBytesObjectCount);
            using (new BinaryDataLock(obd))
            {


                Object oLockBytes = obd.AsLockBytes();
                Assert.AreEqual(iCount + 1, OptimizedBinaryData.LockBytesObjectCount);

                oLockBytes = null;

            }
            obd = null;
            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();
            Assert.AreEqual(iCount, OptimizedBinaryData.LockBytesObjectCount);
            System.GC.Collect();
            System.GC.WaitForPendingFinalizers();
            Assert.AreEqual(iAllocCount, OptimizedBinaryData.AllocCount);
        }
 public void CheckLifetimeOfLockBytesImplWithQI()
 {
     int iCount = OptimizedBinaryData.LockBytesObjectCount;
     MemoryStream ms = new MemoryStream(new byte[1]);
     using (OptimizedBinaryData obd = new OptimizedBinaryData(ms))
     {
         Assert.AreEqual(iCount, OptimizedBinaryData.LockBytesObjectCount);
         using (new BinaryDataLock(obd))
         {
             Object oLockBytes = obd.AsLockBytes();
             ILockBytes lockBytes = oLockBytes as ILockBytes;
             Assert.AreEqual(iCount + 1, OptimizedBinaryData.LockBytesObjectCount);
             Marshal.ReleaseComObject(lockBytes);
             Assert.AreEqual(iCount, OptimizedBinaryData.LockBytesObjectCount);
         }
     }
 }
        public void TestNoLoadNewItemIntoMemoryIfOverThreshold()
        {
            uint TestThreshold = 20 * 1024;
            BinaryDataMemoryMonitor.Instance.MemoryThreshold = TestThreshold;

            OptimizedBinaryData obd1 = new OptimizedBinaryData(TESTFILE_DIR + "TestTableTCs.doc");
            Assert.Less(BinaryDataMemoryMonitor.Instance.BytesInMemory, TestThreshold);
            Assert.Less(BinaryDataMemoryMonitor.Instance.BytesMemoryHighWater, TestThreshold);

            using (new BinaryDataLock(obd1))
            {
                Assert.Greater(BinaryDataMemoryMonitor.Instance.BytesInMemory, TestThreshold);
                Assert.Greater(BinaryDataMemoryMonitor.Instance.BytesMemoryHighWater, TestThreshold);

                Stream str = obd1.AsStream();
                byte[] dataInMemory = StreamUtils.SafeButUnpleasantReadAllStreamIntoByteArray(str);
                str.Close();

                byte[] originalData = File.ReadAllBytes(TESTFILE_DIR + "TestTableTCs.doc");
                Assert.AreEqual(originalData, dataInMemory);
            }

            obd1.Dispose();
        }