Exemplo n.º 1
0
        public void TestCreateAndAppendFile_Bigger()
        {
            // this method test FileLog's behaviour when file size is exactly max size per FBT and new data is appended
            // there was a problem before with new FBT not being created in this case
            var file1 = log.CreateFile();

            log.Close();
            // a little hack to avoid writing gigabyte of data
            FileStream stream      = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
            var        newFileSize = FileLogTestsHelper.CreateBigLogFile(file1.Hint, stream, true);

            log = new FileLog(filename, FileMode.Open);
            // Don't use hint from previously open log file
            log.AppendDataToFile(file1, new ByteArray(new byte[] { 233, 234, 235, 236 }));
            log.AppendDataToFile(file1, new ByteArray(new byte[] { 237, 238, 239 }));
            log.Close();
            stream = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite);
            using (BinaryReader binReader = new BinaryReader(stream))
            {
                stream.Position = 0;
                // move to file table pointer
                stream.Position = binReader.ReadInt64() + 4 * sizeof(long);
                // move to pointer to file 1
                stream.Position = binReader.ReadInt64() + sizeof(long);
                // move to file 1 - file size
                stream.Position = binReader.ReadInt64();
                Assert.Equal(newFileSize + 7, binReader.ReadInt64());
                long nextFBT = binReader.ReadInt64();
                Assert.InRange(nextFBT, 1, stream.Length);
                // move to last FBT entry (next pointer was read already)
                stream.Position += (FileLog.FilesPerBlock - 1) * sizeof(long);
                // move to last FBT2 entry
                stream.Position = binReader.ReadInt64() + FileLog.FilesPerBlock * sizeof(long);
                // move to data block
                stream.Position = binReader.ReadInt64();
                for (int i = 0; i < FileLog.BlockSize; ++i)
                {
                    // check that previous data were not altered
                    Assert.Equal(i % 128, binReader.ReadByte());
                }
                // move to second FBT
                stream.Position = nextFBT;
                // no third FBT
                Assert.Equal(0, binReader.ReadInt64());
                long fbt2Ptr = binReader.ReadInt64();
                Assert.InRange(fbt2Ptr, 1, stream.Length);
                // no other entry in the second FBT
                Assert.Equal(0, binReader.ReadInt64());
                stream.Position = fbt2Ptr;
                long dataPtr = binReader.ReadInt64();
                Assert.InRange(dataPtr, 1, stream.Length);
                stream.Position = dataPtr;
                for (int i = 0; i < 7; ++i)
                {
                    var b = binReader.ReadByte();
                    Assert.Equal(233 + i, b);
                }
            }
        }
Exemplo n.º 2
0
        public void TestReadFileDataToBuffer_Big()
        {
            var hint = log.CreateFile();

            logReader.Close();
            log.Close();
            var stream      = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
            var newFileSize = FileLogTestsHelper.CreateBigLogFile(hint.Hint, stream);

            stream.Close();
            log = new FileLog(filename, FileMode.Open);
            log.AppendDataToFile(hint, new ByteArray(new byte[] { 1, 2, 3, 4 }));
            byte[] buffer = new byte[128];
            buffer[0] = 15;
            logReader = log.CreateReader();
            logReader.ReadFileDataToBuffer(hint, buffer, newFileSize, 4, 1);
            Assert.Equal(15, buffer[0]);
            Assert.Equal(1, buffer[1]);
            Assert.Equal(2, buffer[2]);
        }