public static void TestAssetFilter_CreateTimeRangeFilter_Null_Null_True() { var asset = new StratusAsset { CreateTime = new DateTime(123456789L, DateTimeKind.Utc), }; var filter = new AssetFilter { CreateTimeRangeFilter = new DateRange( null, null ), }; Assert.True(filter.MatchAsset(asset)); }
public static void TestAssetFilter_CreateTimeRangeFilter_WrongKind_Null_False() { var asset = new StratusAsset { CreateTime = new DateTime(123456789L, DateTimeKind.Utc), }; var filter = new AssetFilter { CreateTimeRangeFilter = new DateRange( new DateTime(012345678L, DateTimeKind.Local), null ), }; Assert.False(filter.MatchAsset(asset)); }
public static void TestStorageManager_StoreAsset_DoesntThrowFirstTime() { LOG.Info($"Executing {nameof(TestStorageManager_StoreAsset_DoesntThrowFirstTime)}"); var mgr = new StorageManager( _readerLocalStorage, TimeSpan.FromMinutes(2), _chattelReader, _chattelWriter ); var asset = new StratusAsset { Id = Guid.NewGuid(), }; Assert.DoesNotThrow(() => mgr.StoreAsset(asset, result => { })); }
public static void TestStorageManager_StoreAsset_EmptyId_ArgumentException() { LOG.Info($"Executing {nameof(TestStorageManager_StoreAsset_EmptyId_ArgumentException)}"); var mgr = new StorageManager( _readerLocalStorage, TimeSpan.FromMinutes(2), _chattelReader, _chattelWriter ); var asset = new StratusAsset { Id = Guid.Empty, }; Assert.Throws <ArgumentException>(() => mgr.StoreAsset(asset, result => { })); }
public static void TestAssetLocalStorageLmdbPartitionedLRU_Purge_NonEmptyLocalStorage_LeavesOtherMemoryEntry() { var assetTest1 = new StratusAsset { Id = Guid.NewGuid(), }; var assetTest2 = new StratusAsset { Id = Guid.NewGuid(), }; _localStorage.StoreAsset(assetTest1); _localStorage.StoreAsset(assetTest2); _localStorage.Purge(assetTest1.Id); Assert.True(_localStorageLmdb.Contains(assetTest2.Id)); }
public static void TestAssetLocalStorageLmdbCtor2_Purge_NonEmptyLocalStorage_LeavesOtherDiskEntry() { var assetTest1 = new StratusAsset { Id = Guid.NewGuid(), }; var assetTest2 = new StratusAsset { Id = Guid.NewGuid(), }; _localStorage.StoreAsset(assetTest1); _localStorage.StoreAsset(assetTest2); _localStorage.Purge(assetTest1.Id); Assert.True(_localStorageLmdb.AssetOnDisk(assetTest2.Id)); }
[Timeout(900)] // Must be less than 2x the delay of the server. public static void TestChattelReader_GetAssetAsync2_UncachedAsset_SingleSlowServer_ParallelReads_TakeExpectedTime() { var server = Substitute.For <IAssetServer>(); var config = new ChattelConfiguration(server); var asset = new StratusAsset { Id = Guid.NewGuid(), Name = "Avengers", }; server .RequestAssetSync(asset.Id) .Returns(x => { Thread.Sleep(500); // Slow server call. return(asset); }) ; var reader = new ChattelReader(config); var time1ms = 0L; var time2ms = 0L; Parallel.Invoke( () => { var timer = new Stopwatch(); timer.Restart(); reader.GetAssetAsync(asset.Id, resultAsset => { timer.Stop(); time1ms = timer.ElapsedMilliseconds; }); }, () => { var timer = new Stopwatch(); timer.Restart(); reader.GetAssetAsync(asset.Id, resultAsset => { timer.Stop(); time2ms = timer.ElapsedMilliseconds; }); } ); // Both calls are to take about the same time, as the server is going to take its jolly time getting back to us. Assert.Less(time1ms, 600); Assert.Greater(time1ms, 490); Assert.Less(time2ms, 600); Assert.Greater(time2ms, 490); }
public static void TestChattelReader_GetAssetAsync3_WithUpstream_CacheRuleSkipReadWrite_LocalCacheIsNotWritten() { var server = Substitute.For <IAssetServer>(); var config = new ChattelConfiguration(LOCAL_STORAGE_DIR_INFO.FullName, server); var localStorage = Substitute.For <IChattelLocalStorage>(); var asset = new StratusAsset { Id = Guid.NewGuid(), }; server.RequestAssetSync(asset.Id).Returns(asset); var reader = new ChattelReader(config, localStorage); reader.GetAssetAsync(asset.Id, resultAsset => {}, ChattelReader.CacheRule.SkipRead | ChattelReader.CacheRule.SkipWrite); localStorage.Received(0).StoreAsset(asset); }
public static void TestChattelWriter_PutAssetSync_Duplicates_AssetExistsException() { var config = new ChattelConfiguration(LOCAL_STORAGE_DIR_INFO.FullName); var localStorage = Substitute.For <IChattelLocalStorage>(); var writer = new ChattelWriter(config, localStorage); var testAsset = new StratusAsset { Id = Guid.NewGuid(), }; localStorage.TryGetAsset(testAsset.Id, out var junk).Returns(x => { x[1] = testAsset; return(true); }); Assert.Throws <AssetExistsException>(() => writer.PutAssetSync(testAsset)); }
public static void TestAssetLocalStorageLmdbCtor2_PurgeAll_SingleFilter_Match_NonEmptyLocalStorage_RemovesDiskEntry() { var assetTest = new StratusAsset { Id = Guid.NewGuid(), Local = true, }; _localStorage.StoreAsset(assetTest); _localStorage.PurgeAll(new List <AssetFilter> { new AssetFilter { LocalFilter = true, } }); Assert.False(_localStorageLmdb.AssetOnDisk(assetTest.Id)); }
public void TestWriteToCF() { if (!_runTests) { return; } //delete any leftover files in the writeback cache foreach (var file in Directory.EnumerateFiles("cache/cf_writeback")) { File.Delete(file); } Cache.DiskWriteBackCache wbc = new Cache.DiskWriteBackCache(); AssetBase baseAsset = new AssetBase(); baseAsset.Data = new byte[] { 0xA, 0x9, 0x8, 0x7, 0x6, 0x5, 0xA, 0x9, 0x8, 0x7, 0x6, 0x5 }; baseAsset.Name = "Name"; baseAsset.Description = "Description"; baseAsset.FullID = UUID.Random(); baseAsset.Local = true; baseAsset.Temporary = true; baseAsset.Type = 5; baseAsset.Metadata.CreationDate = DateTime.Now; var stAsset = StratusAsset.FromAssetBase(baseAsset); wbc.StoreAsset(stAsset); wbc.DoWriteCycle(); //the asset should still be in the WB cache Assert.IsNotNull(wbc.GetAsset(baseAsset.FullID.Guid)); //... but we should now be able to get the asset from CF AssetBase cfAsset = _client.RequestAssetSync(baseAsset.FullID); CompareObjects comp = new CompareObjects(); comp.CompareStaticFields = false; comp.CompareStaticProperties = false; Assert.IsTrue(comp.Compare(baseAsset, cfAsset), comp.DifferencesString); CollectionAssert.AreEqual(baseAsset.Data, cfAsset.Data); }
public static void TestAssetLocalStorageLmdbCtor2_PurgeAll_SingleFilter_Nonmatch_NonEmptyLocalStorage_DoesntRemoveMemoryEntry() { var assetTest = new StratusAsset { Id = Guid.NewGuid(), Local = false, }; _localStorage.StoreAsset(assetTest); _localStorage.PurgeAll(new List <AssetFilter> { new AssetFilter { LocalFilter = true, } }); Assert.True(_localStorageLmdb.Contains(assetTest.Id)); }
public static StratusAsset CreateAndCacheAsset(string name, sbyte type, byte[] data, Guid?id = null) { var asset = new StratusAsset { CreateTime = DateTime.UtcNow, Data = data, Description = $"{name} description", Id = id ?? Guid.NewGuid(), Local = true, Name = name, StorageFlags = 0, Temporary = false, Type = type, }; f_stopHttpApiTests.Setup.LocalStorage.StoreAsset(asset); return(asset); }
public static void TestChattelWriter_PutAssetSync_WritesLocalBeforeRemote() { var server = Substitute.For <IAssetServer>(); var config = new ChattelConfiguration(LOCAL_STORAGE_DIR_INFO.FullName, WRITE_CACHE_FILE_INFO.FullName, 4, server); var localStorage = Substitute.For <IChattelLocalStorage>(); var writer = new ChattelWriter(config, localStorage); var testAsset = new StratusAsset { Id = Guid.NewGuid(), }; writer.PutAssetSync(testAsset); Received.InOrder(() => { localStorage.StoreAsset(testAsset); server.StoreAssetSync(testAsset); }); }
/// <summary> /// Handles an incoming request for an asset from the remote server. /// </summary> /// <returns>The asset or null if not found.</returns> /// <param name="assetID">Asset identifier.</param> public StratusAsset RequestAssetSync(Guid assetID) { Asset whipAsset = null; try { whipAsset = _provider.GetAsset(assetID.ToString()); } catch (AssetServerError e) { LOG.Log(Logging.LogLevel.Error, () => $"[{_serverHandle}] Error getting asset from server.", e); return(null); } catch (AuthException e) { LOG.Log(Logging.LogLevel.Error, () => $"[{_serverHandle}] Authentication error getting asset from server.", e); return(null); } return(StratusAsset.FromWHIPAsset(whipAsset)); }
/// <summary> /// Stores the asset in the folder tree. /// </summary> /// <param name="asset">Asset to store.</param> public void StoreAsset(StratusAsset asset) { if (!_config.LocalStorageEnabled || asset == null) // Caching is disabled or stupidity. { return; } var path = UuidToLocalPath(asset.Id); if (!_assetsBeingWritten.TryAdd(path, asset)) { LOG.Log(Logging.LogLevel.Debug, () => $"[ASSET_READER] Attempted to write an asset to local storage, but another thread is already doing so. Skipping write of {path}"); // Can't add it, which means it's already being written to disk by another thread. No need to continue. return; } try { // Since UuidToLocalPath always returns a path underneath the local storage folder, this will only attempt to create folders there. Directory.CreateDirectory(Directory.GetParent(path).FullName); using (var file = File.Open(path, FileMode.CreateNew)) { Serializer.Serialize(file, asset); } // Writing is done, remove it from the work list. _assetsBeingWritten.TryRemove(path, out var temp); LOG.Log(Logging.LogLevel.Debug, () => $"[ASSET_READER] Wrote an asset to local storage: {path}"); } catch (UnauthorizedAccessException e) { _config.DisableLocalStorage(); LOG.Log(Logging.LogLevel.Error, () => "[ASSET_READER] Attempted to write an asset to local storage, but this user is not allowed access. Disabling local storage.", e); } catch (PathTooLongException e) { _config.DisableLocalStorage(); LOG.Log(Logging.LogLevel.Error, () => "[ASSET_READER] Attempted to write an asset to local storage, but the path was too long for the filesystem. Disabling local storage.", e); } catch (DirectoryNotFoundException e) { _config.DisableLocalStorage(); LOG.Log(Logging.LogLevel.Error, () => "[ASSET_READER] Attempted to write an asset to local storage, but local storage folder was not found. Disabling local storage.", e); } catch (IOException e) { // This could be temporary. LOG.Log(Logging.LogLevel.Error, () => "[ASSET_READER] Attempted to write an asset to local storage, but there was an IO error.", e); } }
public void TestRawAssetCaching() { Cache.Cache cache = new Cache.Cache(); OpenMetaverse.UUID id = OpenMetaverse.UUID.Random(); StratusAsset sa = new StratusAsset { Id = id.Guid, Data = new byte[Config.Constants.MAX_STREAM_CACHE_SIZE * 2] }; cache.CacheAssetData(id, sa); Assert.AreEqual(1, cache.ItemCount); Assert.AreEqual(Config.Constants.MAX_STREAM_CACHE_SIZE * 2, cache.Size); Cache.CacheEntry cachedAsset; Assert.IsTrue(cache.TryGetAsset(id, out cachedAsset)); Assert.AreEqual(id.Guid, cachedAsset.FullAsset.Id); }
public void Respond(StratusAsset asset) { asset = asset ?? throw new ArgumentNullException(nameof(asset)); if (asset.Id != AssetId) { throw new AssetIdMismatchException($"Expecting {AssetId}, but got asset with ID {asset.Id}"); } if (_handler == null) { throw new AssetAlreadySetException($"Cannot call {nameof(Respond)} twice!"); } _handler(asset); _handler = null; _errHandler = null; }
public static void TestChattelReader_GetAssetAsync2_LocalCacheIsWritten() { // Simply need to verify that CacheRule.Normal is in effect. var server = Substitute.For <IAssetServer>(); var config = new ChattelConfiguration(LOCAL_STORAGE_DIR_INFO.FullName, server); var localStorage = Substitute.For <IChattelLocalStorage>(); var asset = new StratusAsset { Id = Guid.NewGuid(), }; server.RequestAssetSync(asset.Id).Returns(asset); var reader = new ChattelReader(config, localStorage); reader.GetAssetAsync(asset.Id, resultAsset => {}); localStorage.Received(1).StoreAsset(asset); }
public static void TestStorageManager_PurgeAllLocalAsset_NonEmptyLocalStorage_OnlyRemovesLocal() { LOG.Info($"Executing {nameof(TestStorageManager_PurgeAllLocalAsset_NonEmptyLocalStorage_OnlyRemovesLocal)}"); var mgr = new StorageManager( _readerLocalStorage, TimeSpan.FromMinutes(2), _chattelReader, _chattelWriter ); var asset1 = new StratusAsset { Id = Guid.NewGuid(), Local = true, }; var asset2 = new StratusAsset { Id = Guid.NewGuid(), Local = false, }; var wait = new AutoResetEvent(false); mgr.StoreAsset(asset1, result => wait.Set()); wait.WaitOne(); wait.Reset(); mgr.StoreAsset(asset2, result => wait.Set()); wait.WaitOne(); mgr.PurgeAllLocalAssets(); var foundAsset1 = true; // Put in opposite state to what is expected. var foundAsset2 = false; wait.Reset(); mgr.CheckAsset(asset1.Id, found => { foundAsset1 = found; wait.Set(); }); wait.WaitOne(); wait.Reset(); mgr.CheckAsset(asset2.Id, found => { foundAsset2 = found; wait.Set(); }); wait.WaitOne(); Assert.False(foundAsset1); Assert.True(foundAsset2); }
/// <summary> /// Based on the asset type determines whether or not the asset data has a chance of having references to other asset IDs or not. /// </summary> /// <returns><c>true</c>, if its possible that the data contains asset ID references, <c>false</c> otherwise.</returns> /// <param name="asset">Asset.</param> public static bool MightContainReferences(this StratusAsset asset) { // Assume that if you don't know what it is that it might contain references to other assets. return(!( asset.Type == (sbyte)AssetType.Animation || // Raw data with no IDs. asset.Type == (sbyte)AssetType.CallingCard || // No data. asset.Type == (sbyte)AssetType.ImageJPEG || // Raw data with no IDs. asset.Type == (sbyte)AssetType.ImageTGA || // Raw data with no IDs. asset.Type == (sbyte)AssetType.Landmark || // No IDs ever. asset.Type == (sbyte)AssetType.Mesh || // Raw data with no IDs. asset.Type == (sbyte)AssetType.Sound || // Raw data with no IDs. asset.Type == (sbyte)AssetType.SoundWAV || // Raw data with no IDs. asset.Type == (sbyte)AssetType.Texture || // Raw data with no IDs. asset.Type == (sbyte)AssetType.TextureTGA || // Raw data with no IDs. asset.IsLink() || asset.IsFolder() )); }
public static void TestChattelWriter_PutAssetSync_ServerError_AggregateException() { var server = Substitute.For <IAssetServer>(); var config = new ChattelConfiguration(LOCAL_STORAGE_DIR_INFO.FullName, WRITE_CACHE_FILE_INFO.FullName, 4, server); var localStorage = Substitute.For <IChattelLocalStorage>(); var writer = new ChattelWriter(config, localStorage); server .WhenForAnyArgs(x => x.StoreAssetSync(Arg.Any <StratusAsset>())) .Do(x => { throw new Exception(); // Just needs an error to cause remote storage failure. }) ; var testAsset = new StratusAsset { Id = Guid.NewGuid(), }; Assert.Throws <AggregateException>(() => writer.PutAssetSync(testAsset)); }
private string GetContentType(StratusAsset asset) { if (asset.IsImageAsset()) { if (Encoding.ASCII.GetString(asset.Data, 0, Math.Min(asset.Data.Length, JPEG2000_MAGIC_NUMBERS.Length)).Equals(JPEG2000_MAGIC_NUMBERS, StringComparison.Ordinal)) { return("image/x-j2c"); } if (Encoding.ASCII.GetString(asset.Data, 0, Math.Min(asset.Data.Length, JPEG_MAGIC_NUMBERS.Length)).Equals(JPEG_MAGIC_NUMBERS, StringComparison.Ordinal)) { return("image/jpeg"); } return("image/x-tga"); } // else Mesh return("application/vnd.ll.mesh"); }
/// <summary> /// Handles a request to store an asset to the remote server. /// </summary> /// <param name="asset">Asset.</param> /// <exception cref="T:Chattel.AssetWriteException">Thrown if there was an error storing the asset.</exception> public void StoreAssetSync(StratusAsset asset) { asset = asset ?? throw new ArgumentNullException(nameof(asset)); if (asset.Id == Guid.Empty) { throw new ArgumentException("Assets must not have a zero ID"); } try { _provider.PutAsset(StratusAsset.ToWHIPAsset(asset)); } catch (AssetServerError e) { LOG.Log(Logging.LogLevel.Error, () => $"[{_serverHandle}] Error sending asset to server.", e); throw new AssetWriteException(asset.Id, e); } catch (AuthException e) { LOG.Log(Logging.LogLevel.Error, () => $"[{_serverHandle}] Authentication error sending asset to server.", e); throw new AssetWriteException(asset.Id, e); } }
public static void TestChattelReader_GetAssetAsync3_NoUpstream_CacheRuleSkipReadWrite_LocalCacheIsReadAnyway() { var config = new ChattelConfiguration(LOCAL_STORAGE_DIR_INFO.FullName); var localStorage = Substitute.For <IChattelLocalStorage>(); var assetId = Guid.NewGuid(); localStorage .TryGetAsset(assetId, out var junk) .Returns(x => { x[1] = new StratusAsset(); return(true); }) ; var reader = new ChattelReader(config, localStorage); reader.GetAssetAsync(assetId, resultAsset => {}, ChattelReader.CacheRule.SkipRead | ChattelReader.CacheRule.SkipWrite); localStorage.Received(1).TryGetAsset(assetId, out junk); }
/// <summary> /// Stores the asset. Mainly just a wrapper for the assetWriter PutAssetSync method. /// </summary> /// <param name="asset">Asset.</param> /// <param name="resultCallback">Result callback.</param> public void StoreAsset(StratusAsset asset, StorageResultCallback resultCallback) { asset = asset ?? throw new ArgumentNullException(nameof(asset)); resultCallback = resultCallback ?? throw new ArgumentNullException(nameof(resultCallback)); if (asset.Id == Guid.Empty) { throw new ArgumentException("Asset cannot have zero ID.", nameof(asset)); } PutResult result; try { _assetWriter.PutAssetSync(asset); result = PutResult.DONE; } catch (AssetExistsException) { result = PutResult.DUPLICATE; } catch (Exception e) { LOG.Error("Error storing asset.", e); result = PutResult.FAILURE; } if (result == PutResult.DONE) { // Clear negative cache entry. if (_negativeCache != null) { _negativeCacheLock.EnterWriteLock(); try { _negativeCache.Remove(asset.Id.ToString("N")); } finally { _negativeCacheLock.ExitWriteLock(); } } } resultCallback(result); }
public static void TestStorageManager_PurgeAllLocalAsset_NonEmptyLocalStorage_DoesntThrow() { LOG.Info($"Executing {nameof(TestStorageManager_PurgeAllLocalAsset_NonEmptyLocalStorage_DoesntThrow)}"); var mgr = new StorageManager( _readerLocalStorage, TimeSpan.FromMinutes(2), _chattelReader, _chattelWriter ); var asset = new StratusAsset { Id = Guid.NewGuid(), }; var wait = new AutoResetEvent(false); mgr.StoreAsset(asset, result => wait.Set()); wait.WaitOne(); Assert.DoesNotThrow(mgr.PurgeAllLocalAssets); }
public static void TestAssetLocalStorageLmdb_Ctor2_RestoresIndex() { var asset = new StratusAsset { Id = Guid.NewGuid(), }; using (var localStorage = new AssetLocalStorageLmdb( _chattelConfigRead, DATABASE_MAX_SIZE_BYTES )) { IChattelLocalStorage localStorageViaInterface = localStorage; localStorageViaInterface.StoreAsset(asset); } using (var localStorage = new AssetLocalStorageLmdb( _chattelConfigRead, DATABASE_MAX_SIZE_BYTES )) { Assert.True(localStorage.Contains(asset.Id)); } }
public static void TestWriteCache_Ctor_ExistingFile_MockWriter_MockLocalStorage_CallsServerStore() { var firstId = Guid.NewGuid(); var lastId = Guid.NewGuid(); var records = new Tuple <Guid, bool>[] { new Tuple <Guid, bool>(firstId, false), new Tuple <Guid, bool>(Guid.Empty, true), new Tuple <Guid, bool>(lastId, true), }; CreateWriteCache(WRITE_CACHE_FILE_INFO, records); var localStorage = Substitute.For <IChattelLocalStorage>(); var server = Substitute.For <IAssetServer>(); var writer = new ChattelWriter(new ChattelConfiguration(server), localStorage, false); var firstAsset = new StratusAsset { Id = firstId, }; var lastAsset = new StratusAsset { Id = lastId, }; localStorage.TryGetAsset(firstId, out var asset1).Returns(parms => { parms[1] = firstAsset; return(true); }); localStorage.TryGetAsset(lastId, out var asset2).Returns(parms => { parms[1] = lastAsset; return(true); }); localStorage.StoreAsset(firstAsset); localStorage.StoreAsset(lastAsset); new WriteCache( WRITE_CACHE_FILE_INFO, (uint)records.Length, writer, localStorage ); server.Received().StoreAssetSync(firstAsset); server.DidNotReceive().StoreAssetSync(lastAsset); }
public void StoreAssetSync(StratusAsset asset) { if (asset == null) { throw new ArgumentNullException(nameof(asset)); } if (asset.Id == Guid.Empty) { throw new ArgumentException("Assets must not have a zero ID"); } using (var memStream = new MemoryStream()) { try { ProtoBuf.Serializer.Serialize(memStream, asset); memStream.Position = 0; var mheaders = GenerateStorageHeaders(asset, memStream); WarnIfLongOperation("CreateObject", () => _provider.CreateObject( GenerateContainerName(asset.Id), memStream, GenerateAssetObjectName(asset.Id), "application/octet-stream", headers: mheaders, useInternalUrl: _useInternalURL, region: _defaultRegion ) ); } catch (ResponseException e) { if (e.Response.StatusCode == System.Net.HttpStatusCode.PreconditionFailed) { throw new AssetExistsException(asset.Id, e); } throw new AssetWriteException(asset.Id, e); } } }
public void TestSimpleConversionToBaseAsset() { StratusAsset stAsset = new StratusAsset(); stAsset.Data = new byte[] { 0xA, 0x9, 0x8, 0x7, 0x6, 0x5, 0xA, 0x9, 0x8, 0x7, 0x6, 0x5 }; stAsset.Name = "Name"; stAsset.Description = "Description"; stAsset.Id = UUID.Random().Guid; stAsset.Local = true; stAsset.Temporary = true; stAsset.Type = 5; stAsset.CreateTime = DateTime.Now; var baseAsset = stAsset.ToAssetBase(); Assert.AreEqual(stAsset.Id, baseAsset.FullID.Guid); CollectionAssert.AreEqual(stAsset.Data, baseAsset.Data); Assert.AreEqual(stAsset.Description, baseAsset.Description); Assert.AreEqual(stAsset.Local, baseAsset.Local); Assert.AreEqual(stAsset.Name, baseAsset.Name); Assert.AreEqual(stAsset.Temporary, baseAsset.Temporary); Assert.AreEqual(stAsset.Type, baseAsset.Type); Assert.AreEqual(stAsset.CreateTime, baseAsset.Metadata.CreationDate); }
public void TestRawAssetCaching() { Cache.Cache cache = new Cache.Cache(); OpenMetaverse.UUID id = OpenMetaverse.UUID.Random(); StratusAsset sa = new StratusAsset {Id = id.Guid, Data = new byte[Config.Constants.MAX_STREAM_CACHE_SIZE * 2] }; cache.CacheAssetData(id, sa); Assert.AreEqual(1, cache.ItemCount); Assert.AreEqual(Config.Constants.MAX_STREAM_CACHE_SIZE * 2, cache.Size); Cache.CacheEntry cachedAsset; Assert.IsTrue(cache.TryGetAsset(id, out cachedAsset)); Assert.AreEqual(id.Guid, cachedAsset.FullAsset.Id); }
/// <summary> /// Puts an asset into the writeback cache /// </summary> /// <param name="asset"></param> public void StoreAsset(StratusAsset asset) { CheckCacheDir(); lock (_oplock) { if (_ids.Contains(asset.Id) || _recentlyWritten.ContainsKey(asset.Id)) { //we already have this asset scheduled to write throw new AssetAlreadyExistsException("Asset " + asset.Id.ToString() + " already cached for writeback"); } try { using (FileStream fstream = File.OpenWrite(GetAssetFileName(asset.Id))) { ProtoBuf.Serializer.Serialize(fstream, asset); } } catch (Exception e) { m_log.ErrorFormat("There was an error writing an asset back to disk. The process will be terminated. {0}", e); Environment.Exit(-1); } _ids.Add(asset.Id); } }
public void CacheAssetData(UUID assetId, StratusAsset asset) { _assetCache.Add(assetId, new CacheEntry { FullAsset = asset }, asset.Data.Length); }
public void TestSimpleSerializeDeserialize() { StratusAsset stAsset1 = new StratusAsset(); stAsset1.Data = new byte[] { 0xA, 0x9, 0x8, 0x7, 0x6, 0x5, 0xA, 0x9, 0x8, 0x7, 0x6, 0x5 }; stAsset1.Name = "Name"; stAsset1.Description = "Description"; stAsset1.Id = UUID.Random().Guid; stAsset1.Local = true; stAsset1.Temporary = true; stAsset1.Type = 5; stAsset1.CreateTime = DateTime.Now; CompareObjects comp = new CompareObjects(); comp.CompareStaticFields = false; comp.CompareStaticProperties = false; StratusAsset stAsset2; using (MemoryStream ms = new MemoryStream()) { ProtoBuf.Serializer.Serialize<StratusAsset>(ms, stAsset1); ms.Position = 0; stAsset2 = ProtoBuf.Serializer.Deserialize<StratusAsset>(ms); Assert.IsTrue(comp.Compare(stAsset1, stAsset2), comp.DifferencesString); } }