Exemplo n.º 1
0
        public void UploadSegmentMetadata_CalculateTypicalSegmentLength()
        {
            Assert.Throws <ArgumentException>(() => { UploadSegmentMetadata.CalculateSegmentLength(1000, -1); });

            const int maxSegmentCount = 16536;
            long      fileLength      = (long)Math.Pow(2, 30); // see comment below about actually making this larger than Int32.MaxValue
            long      segmentLength;

            for (int segmentCount = 1; segmentCount < maxSegmentCount; segmentCount++)
            {
                segmentLength = UploadSegmentMetadata.CalculateSegmentLength(fileLength, segmentCount);

                //the next two asserts verify that the value calculated will split the input file into a balanced set of segments;
                //all the segments should have the same length, except the last one which may have less than that (but never more).
                //a quick heuristic to verify this is: (SegmentLength-1)*SegmentCount < FileLength <= SegmentLength*SegmentCount
                Assert.True(segmentLength * segmentCount >= fileLength, "SegmentLength * SegmentCount must be at least the length of the input file");
                Assert.True((segmentLength - 1) * segmentCount < fileLength, "(SegmentLength - 1) * SegmentCount must be smaller than the length of the input file");
            }

            // test segmentCount == fileLength;
            segmentLength = UploadSegmentMetadata.CalculateSegmentLength(fileLength, (int)fileLength); //for this to work, FileLength must be less than In32.MaxValue
            Assert.Equal(1, segmentLength);

            // test that if segment count = 0 then the return value is 0.
            Assert.Equal(
                0,
                UploadSegmentMetadata.CalculateSegmentLength(fileLength, 0));
        }
        private UploadMetadata CreateMetadata(int segmentCount)
        {
            var path     = Path.GetTempFileName();
            var metadata = new UploadMetadata()
            {
                MetadataFilePath = path,
                InputFilePath    = _smallFilePath,
                FileLength       = _smallFileContents.Length,
                SegmentCount     = segmentCount,
                SegmentLength    = UploadSegmentMetadata.CalculateSegmentLength(_smallFileContents.Length, segmentCount),
                Segments         = new UploadSegmentMetadata[segmentCount],
                TargetStreamPath = "abc",
                UploadId         = "123",
                IsBinary         = true
            };

            long offset = 0;

            for (int i = 0; i < segmentCount; i++)
            {
                long length = UploadSegmentMetadata.CalculateSegmentLength(i, metadata);
                metadata.Segments[i] = new UploadSegmentMetadata()
                {
                    SegmentNumber = i,
                    Offset        = offset,
                    Status        = SegmentUploadStatus.Pending,
                    Length        = length,
                    Path          = string.Format("{0}.{1}.segment{2}", metadata.TargetStreamPath, metadata.UploadId, i)
                };
                offset += length;
            }

            return(metadata);
        }
Exemplo n.º 3
0
        public void UploadSegmentMetadata_CalculateParticularSegmentLength()
        {
            //verify bad inputs
            Assert.Throws <ArgumentOutOfRangeException>(
                () => { UploadSegmentMetadata.CalculateSegmentLength(-1, new UploadMetadata()
                {
                    FileLength = 10, SegmentCount = 5, SegmentLength = 2
                }); });

            Assert.Throws <ArgumentOutOfRangeException>(
                () => { UploadSegmentMetadata.CalculateSegmentLength(100, new UploadMetadata()
                {
                    FileLength = 10, SegmentCount = 5, SegmentLength = 2
                }); });

            Assert.Throws <ArgumentException>(
                () => { UploadSegmentMetadata.CalculateSegmentLength(1, new UploadMetadata()
                {
                    FileLength = -10, SegmentCount = 5, SegmentLength = 2
                }); });

            Assert.Throws <ArgumentException>(
                () => { UploadSegmentMetadata.CalculateSegmentLength(1, new UploadMetadata()
                {
                    FileLength = 100, SegmentCount = 2, SegmentLength = 2
                }); });

            Assert.Throws <ArgumentException>(
                () => { UploadSegmentMetadata.CalculateSegmentLength(1, new UploadMetadata()
                {
                    FileLength = 100, SegmentCount = 5, SegmentLength = 26
                }); });

            //test various scenarios with a fixed file length, and varying the segment count from 1 to the FileLength

            int FileLength = 16 * (int)Math.Pow(2, 20);//16MB

            for (int segmentCount = 1; segmentCount <= FileLength; segmentCount += 1024)
            {
                long typicalSegmentLength = UploadSegmentMetadata.CalculateSegmentLength(FileLength, segmentCount);

                var uploadMetadata = new UploadMetadata()
                {
                    FileLength = FileLength, SegmentCount = segmentCount, SegmentLength = typicalSegmentLength
                };
                long firstSegmentLength = UploadSegmentMetadata.CalculateSegmentLength(0, uploadMetadata);
                long lastSegmentLength  = UploadSegmentMetadata.CalculateSegmentLength(segmentCount - 1, uploadMetadata);

                Assert.Equal(typicalSegmentLength, firstSegmentLength);
                if (segmentCount == 1)
                {
                    Assert.Equal(firstSegmentLength, lastSegmentLength);
                }

                long reconstructedFileLength = typicalSegmentLength * (segmentCount - 1) + lastSegmentLength;
                Assert.Equal(FileLength, reconstructedFileLength);
            }
        }
        private void VerifyTargetStreamIsComplete(UploadSegmentMetadata segmentMetadata, UploadMetadata metadata, InMemoryFrontEnd frontEnd)
        {
            Assert.Equal(SegmentUploadStatus.Complete, segmentMetadata.Status);
            Assert.True(frontEnd.StreamExists(segmentMetadata.Path), string.Format("Segment {0} was not uploaded", segmentMetadata.SegmentNumber));
            Assert.Equal(segmentMetadata.Length, frontEnd.GetStreamLength(segmentMetadata.Path));

            var actualContents   = frontEnd.GetStreamContents(segmentMetadata.Path);
            var expectedContents = GetExpectedContents(segmentMetadata, metadata);

            AssertExtensions.AreEqual(expectedContents, actualContents, "Segment {0} has unexpected contents", segmentMetadata.SegmentNumber);
        }
Exemplo n.º 5
0
        private UploadMetadata CreateMetadata(string filePath, long fileLength)
        {
            var metadata = new UploadMetadata()
            {
                InputFilePath    = filePath,
                FileLength       = fileLength,
                TargetStreamPath = StreamPath,
                SegmentCount     = 1,
                SegmentLength    = UploadSegmentMetadata.CalculateSegmentLength(fileLength, 1),
                Segments         = new UploadSegmentMetadata[1],
                IsBinary         = true
            };

            metadata.Segments[0]      = new UploadSegmentMetadata(0, metadata);
            metadata.Segments[0].Path = metadata.TargetStreamPath;
            return(metadata);
        }
Exemplo n.º 6
0
        public void UploadMetadata_CalculateSegmentCount()
        {
            Assert.Throws <ArgumentException>(() => { UploadSegmentMetadata.CalculateSegmentCount(-1); });

            Assert.Equal(0, UploadSegmentMetadata.CalculateSegmentCount(0));

            long maxLength = 100 * (long)Math.Pow(2, 40); //100 TB
            long increment = 10 * (long)Math.Pow(2, 30);  //10GB
            int  lastValue = 0;

            for (long length = (long)Math.Pow(2, 20); length < maxLength; length += increment)
            {
                int value = UploadSegmentMetadata.CalculateSegmentCount(length);
                Assert.True(lastValue <= value, "Function is not monotonically increasing");
                lastValue = value;
            }
        }
 private byte[] GetExpectedContents(UploadSegmentMetadata segment, UploadMetadata metadata)
 {
     byte[] result = new byte[segment.Length];
     Array.Copy(_smallFileContents, segment.SegmentNumber * metadata.SegmentLength, result, 0, segment.Length);
     return(result);
 }