public void TestDirectoryIsNotNuked() { Assert.AreEqual(0, _directory.ListFiles().Length); DefaultDiskStorage storage = GetStorageSupplier(1).Get(); string resourceId = "file1"; byte[] CONTENT = Encoding.UTF8.GetBytes("content"); // Create a file so we know version directory really exists IInserter inserter = storage.Insert(resourceId, null); WriteToResource(inserter, CONTENT); inserter.Commit(null); // Assign some previous date to the "now" used for file creation long lastModified = _directory.LastWriteTime.Ticks / TimeSpan.TicksPerMillisecond - 1000; _directory.LastWriteTime = new DateTime(lastModified * TimeSpan.TicksPerMillisecond); // Check it was changed Assert.AreEqual(lastModified * TimeSpan.TicksPerMillisecond, _directory.LastWriteTime.Ticks); // Create again, it shouldn't delete the directory GetStorageSupplier(1).Get(); _directory.Refresh(); // _directory exists... Assert.IsTrue(_directory.Exists); // And it's the same as before Assert.AreEqual(lastModified * TimeSpan.TicksPerMillisecond, _directory.LastWriteTime.Ticks); }
public void TestTempFileEviction() { DefaultDiskStorage storage = GetStorageSupplier(1).Get(); string resourceId1 = "resource1"; IInserter inserter = storage.Insert(resourceId1, null); FileInfo tempFile = ((DefaultDiskStorage.InserterImpl)inserter)._temporaryFile; // Make sure that we don't evict a recent temp file PurgeUnexpectedFiles(storage); Assert.IsTrue(tempFile.Exists); // Mark it old, then try eviction again. It should be gone. try { tempFile.LastWriteTime = _clock.Now.Subtract( TimeSpan.FromMilliseconds(DefaultDiskStorage.TEMP_FILE_LIFETIME_MS + 1000)); } catch (Exception) { throw new IOException("Unable to update timestamp of file: " + tempFile); } PurgeUnexpectedFiles(storage); Assert.IsFalse(tempFile.Exists); }
private static FileBinaryResource WriteToStorage( DefaultDiskStorage storage, string resourceId, byte[] value) { IInserter inserter = storage.Insert(resourceId, null); WriteToResource(inserter, value); return((FileBinaryResource)inserter.Commit(null)); }
private static FileInfo Write( DefaultDiskStorage storage, string resourceId, byte[] content) { IInserter inserter = storage.Insert(resourceId, null); FileInfo file = ((DefaultDiskStorage.InserterImpl)inserter)._temporaryFile; FileStream fos = file.Create(); try { fos.Write(content, 0, content.Length); } finally { fos.Dispose(); } return(((FileBinaryResource)inserter.Commit(null)).File); }