// ok - as expected
        public virtual void CheckInvalidBlock(ExtendedBlock b)
        {
            SimulatedFSDataset fsdataset = GetSimulatedFSDataset();

            NUnit.Framework.Assert.IsFalse(fsdataset.IsValidBlock(b));
            try
            {
                fsdataset.GetLength(b);
                NUnit.Framework.Assert.IsTrue("Expected an IO exception", false);
            }
            catch (IOException)
            {
            }
            // ok - as expected
            try
            {
                fsdataset.GetBlockInputStream(b);
                NUnit.Framework.Assert.IsTrue("Expected an IO exception", false);
            }
            catch (IOException)
            {
            }
            // ok - as expected
            try
            {
                fsdataset.FinalizeBlock(b);
                NUnit.Framework.Assert.IsTrue("Expected an IO exception", false);
            }
            catch (IOException)
            {
            }
        }
        public virtual void TestInjectionEmpty()
        {
            SimulatedFSDataset fsdataset   = GetSimulatedFSDataset();
            BlockListAsLongs   blockReport = fsdataset.GetBlockReport(bpid);

            NUnit.Framework.Assert.AreEqual(0, blockReport.GetNumberOfBlocks());
            int bytesAdded = AddSomeBlocks(fsdataset);

            blockReport = fsdataset.GetBlockReport(bpid);
            NUnit.Framework.Assert.AreEqual(Numblocks, blockReport.GetNumberOfBlocks());
            foreach (Block b in blockReport)
            {
                NUnit.Framework.Assert.IsNotNull(b);
                NUnit.Framework.Assert.AreEqual(BlockIdToLen(b.GetBlockId()), b.GetNumBytes());
            }
            // Inject blocks into an empty fsdataset
            //  - injecting the blocks we got above.
            SimulatedFSDataset sfsdataset = GetSimulatedFSDataset();

            sfsdataset.InjectBlocks(bpid, blockReport);
            blockReport = sfsdataset.GetBlockReport(bpid);
            NUnit.Framework.Assert.AreEqual(Numblocks, blockReport.GetNumberOfBlocks());
            foreach (Block b_1 in blockReport)
            {
                NUnit.Framework.Assert.IsNotNull(b_1);
                NUnit.Framework.Assert.AreEqual(BlockIdToLen(b_1.GetBlockId()), b_1.GetNumBytes()
                                                );
                NUnit.Framework.Assert.AreEqual(BlockIdToLen(b_1.GetBlockId()), sfsdataset.GetLength
                                                    (new ExtendedBlock(bpid, b_1)));
            }
            NUnit.Framework.Assert.AreEqual(bytesAdded, sfsdataset.GetDfsUsed());
            NUnit.Framework.Assert.AreEqual(sfsdataset.GetCapacity() - bytesAdded, sfsdataset
                                            .GetRemaining());
        }
        public virtual void TestInjectionNonEmpty()
        {
            SimulatedFSDataset fsdataset   = GetSimulatedFSDataset();
            BlockListAsLongs   blockReport = fsdataset.GetBlockReport(bpid);

            NUnit.Framework.Assert.AreEqual(0, blockReport.GetNumberOfBlocks());
            int bytesAdded = AddSomeBlocks(fsdataset);

            blockReport = fsdataset.GetBlockReport(bpid);
            NUnit.Framework.Assert.AreEqual(Numblocks, blockReport.GetNumberOfBlocks());
            foreach (Block b in blockReport)
            {
                NUnit.Framework.Assert.IsNotNull(b);
                NUnit.Framework.Assert.AreEqual(BlockIdToLen(b.GetBlockId()), b.GetNumBytes());
            }
            fsdataset = null;
            // Inject blocks into an non-empty fsdataset
            //  - injecting the blocks we got above.
            SimulatedFSDataset sfsdataset = GetSimulatedFSDataset();

            // Add come blocks whose block ids do not conflict with
            // the ones we are going to inject.
            bytesAdded += AddSomeBlocks(sfsdataset, Numblocks + 1);
            sfsdataset.GetBlockReport(bpid);
            NUnit.Framework.Assert.AreEqual(Numblocks, blockReport.GetNumberOfBlocks());
            sfsdataset.GetBlockReport(bpid);
            NUnit.Framework.Assert.AreEqual(Numblocks, blockReport.GetNumberOfBlocks());
            sfsdataset.InjectBlocks(bpid, blockReport);
            blockReport = sfsdataset.GetBlockReport(bpid);
            NUnit.Framework.Assert.AreEqual(Numblocks * 2, blockReport.GetNumberOfBlocks());
            foreach (Block b_1 in blockReport)
            {
                NUnit.Framework.Assert.IsNotNull(b_1);
                NUnit.Framework.Assert.AreEqual(BlockIdToLen(b_1.GetBlockId()), b_1.GetNumBytes()
                                                );
                NUnit.Framework.Assert.AreEqual(BlockIdToLen(b_1.GetBlockId()), sfsdataset.GetLength
                                                    (new ExtendedBlock(bpid, b_1)));
            }
            NUnit.Framework.Assert.AreEqual(bytesAdded, sfsdataset.GetDfsUsed());
            NUnit.Framework.Assert.AreEqual(sfsdataset.GetCapacity() - bytesAdded, sfsdataset
                                            .GetRemaining());
            // Now test that the dataset cannot be created if it does not have sufficient cap
            conf.SetLong(SimulatedFSDataset.ConfigPropertyCapacity, 10);
            try
            {
                sfsdataset = GetSimulatedFSDataset();
                sfsdataset.AddBlockPool(bpid, conf);
                sfsdataset.InjectBlocks(bpid, blockReport);
                NUnit.Framework.Assert.IsTrue("Expected an IO exception", false);
            }
            catch (IOException)
            {
            }
        }
        public virtual void TestWriteRead()
        {
            SimulatedFSDataset fsdataset = GetSimulatedFSDataset();

            AddSomeBlocks(fsdataset);
            for (int i = 1; i <= Numblocks; ++i)
            {
                ExtendedBlock b = new ExtendedBlock(bpid, i, 0, 0);
                NUnit.Framework.Assert.IsTrue(fsdataset.IsValidBlock(b));
                NUnit.Framework.Assert.AreEqual(BlockIdToLen(i), fsdataset.GetLength(b));
                CheckBlockDataAndSize(fsdataset, b, BlockIdToLen(i));
            }
        }
        /// <exception cref="System.IO.IOException"/>
        internal virtual int AddSomeBlocks(SimulatedFSDataset fsdataset, int startingBlockId
                                           )
        {
            int bytesAdded = 0;

            for (int i = startingBlockId; i < startingBlockId + Numblocks; ++i)
            {
                ExtendedBlock b = new ExtendedBlock(bpid, i, 0, 0);
                // we pass expected len as zero, - fsdataset should use the sizeof actual
                // data written
                ReplicaInPipelineInterface bInfo = fsdataset.CreateRbw(StorageType.Default, b, false
                                                                       ).GetReplica();
                ReplicaOutputStreams @out = bInfo.CreateStreams(true, DataChecksum.NewDataChecksum
                                                                    (DataChecksum.Type.Crc32, 512));
                try
                {
                    OutputStream dataOut = @out.GetDataOut();
                    NUnit.Framework.Assert.AreEqual(0, fsdataset.GetLength(b));
                    for (int j = 1; j <= BlockIdToLen(i); ++j)
                    {
                        dataOut.Write(j);
                        NUnit.Framework.Assert.AreEqual(j, bInfo.GetBytesOnDisk());
                        // correct length even as we write
                        bytesAdded++;
                    }
                }
                finally
                {
                    @out.Close();
                }
                b.SetNumBytes(BlockIdToLen(i));
                fsdataset.FinalizeBlock(b);
                NUnit.Framework.Assert.AreEqual(BlockIdToLen(i), fsdataset.GetLength(b));
            }
            return(bytesAdded);
        }