public void FileStorage_TestRetentionPeriod()
        {
            var  testDirectory  = new DirectoryInfo(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()));
            long maxSizeInBytes = 100000;
            int  maintenancePeriodInMilliseconds = 3000;
            int  retentionPeriodInMilliseconds   = 2000;
            int  writeTimeOutInMilliseconds      = 1000;

            using var storage = new FileStorage(
                      testDirectory.FullName,
                      maxSizeInBytes,
                      maintenancePeriodInMilliseconds,
                      retentionPeriodInMilliseconds,
                      writeTimeOutInMilliseconds);

            var data  = Encoding.UTF8.GetBytes("Hello, World!");
            var blob1 = (FileBlob)storage.CreateBlob(data);

            // Wait for maintenance job to run
            Thread.Sleep(4000);

            // Blob will be deleted as retention period is 1 sec
            Assert.False(File.Exists(blob1.FullPath));

            testDirectory.Delete(true);
        }
        public void FileStorageTests_TestLeaseExpiration()
        {
            var  testDirectory  = new DirectoryInfo(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()));
            long maxSizeInBytes = 100000;
            int  maintenancePeriodInMilliseconds = 3000;
            int  retentionPeriodInMilliseconds   = 2000;
            int  writeTimeOutInMilliseconds      = 1000;

            using var storage = new FileStorage(
                      testDirectory.FullName,
                      maxSizeInBytes,
                      maintenancePeriodInMilliseconds,
                      retentionPeriodInMilliseconds,
                      writeTimeOutInMilliseconds);

            var data     = Encoding.UTF8.GetBytes("Hello, World!");
            var blob     = (FileBlob)storage.CreateBlob(data);
            var blobPath = blob.FullPath;

            blob.Lease(1000);
            var leasePath = blob.FullPath;

            Assert.True(File.Exists(leasePath));

            // Wait for maintenance job to run
            Thread.Sleep(4000);

            // File name will be change to .blob
            Assert.True(File.Exists(blobPath));
            Assert.False(File.Exists(leasePath));

            testDirectory.Delete(true);
        }
        public void FileStorage_TestWriteTimeoutPeriod()
        {
            var  testDirectory  = new DirectoryInfo(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()));
            long maxSizeInBytes = 100000;
            int  maintenancePeriodInMilliseconds = 3000;
            int  retentionPeriodInMilliseconds   = 2000;
            int  writeTimeOutInMilliseconds      = 1000;

            using var storage = new FileStorage(
                      testDirectory.FullName,
                      maxSizeInBytes,
                      maintenancePeriodInMilliseconds,
                      retentionPeriodInMilliseconds,
                      writeTimeOutInMilliseconds);

            var data  = Encoding.UTF8.GetBytes("Hello, World!");
            var blob2 = (FileBlob)storage.CreateBlob(data);

            // Mock write
            File.Move(blob2.FullPath, blob2.FullPath + ".tmp");

            // validate file moved successfully
            Assert.True(File.Exists(blob2.FullPath + ".tmp"));

            // Wait for maintenance job to run
            Thread.Sleep(4000);

            // tmp file will be deleted as write timeout period is 1 sec
            Assert.False(File.Exists(blob2.FullPath + ".tmp"));
            Assert.False(File.Exists(blob2.FullPath));

            testDirectory.Delete(true);
        }
Ejemplo n.º 4
0
        public void FileBlobTests_DeleteFailsAfterLeaseIsExpired()
        {
            var testDirectory = new DirectoryInfo(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()));

            // set maintenance job interval to 2 secs
            using var storage = new FileStorage(testDirectory.FullName, 10, 2);

            var data = Encoding.UTF8.GetBytes("Hello, World!");

            var blob = storage.CreateBlob(data);

            var leasePeriodMilliseconds = 1;

            // lease for 1 ms
            blob.Lease(leasePeriodMilliseconds);

            // Wait for lease to expire and maintenance job to run
            Thread.Sleep(5000);

            blob.Delete();

            // Assert
            Assert.NotNull(storage.GetBlob());

            testDirectory.Delete(true);
        }
        public void FileStorage_CreateBlobReturnsNullIfStorageIsFull()
        {
            var testDirectory = new DirectoryInfo(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()));

            using var storage = new FileStorage(testDirectory.FullName, 10000);

            PersistentStorageHelper.UpdateDirectorySize(10000);

            var data = Encoding.UTF8.GetBytes("Hello, World!");

            Assert.Null(storage.CreateBlob(data));

            testDirectory.Delete(true);
        }
        public void FileStorage_E2E_Test()
        {
            var testDirectory = new DirectoryInfo(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()));

            using var storage = new FileStorage(testDirectory.FullName);

            var data = Encoding.UTF8.GetBytes("Hello, World!");

            // Create blob.
            IPersistentBlob blob1 = storage.CreateBlob(data);

            // Get blob.
            IPersistentBlob blob2 = storage.GetBlob();

            Assert.Single(storage.GetBlobs());

            // Verify file name from both create blob and get blob are same.
            Assert.Equal(((FileBlob)blob1).FullPath, ((FileBlob)blob2).FullPath);

            // Validate if content in the blob is same as buffer data passed to create blob.
            Assert.Equal(data, blob1.Read());

            testDirectory.Delete(true);
        }