public static void TestChattelWriter_PutAssetSync_FullWriteCache_BadServer_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(), }; try { writer.PutAssetSync(testAsset); } catch (AggregateException) { // moving right along. } try { writer.PutAssetSync(testAsset); } catch (AggregateException) { // moving right along. } // Write cache currenlty requires one left empty. Assert.Throws <AggregateException>(() => writer.PutAssetSync(testAsset)); }
public static void TestChattelWriter_PutAssetSync_FullWriteCache_HitsRemote() { 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(), }; try { writer.PutAssetSync(testAsset); } catch (AggregateException) { // moving right along. } try { writer.PutAssetSync(testAsset); } catch (AggregateException) { // moving right along. } try { writer.PutAssetSync(testAsset); } catch (WriteCacheFullException) { // moving right along. } // Write cache currently requires one left empty. server.Received(3).StoreAssetSync(testAsset); }
public static void TestChattelWriter_PutAssetSync_MultipleParallel_AllReceived() { var server = Substitute.For <IAssetServer>(); var config = new ChattelConfiguration(server); var localStorage = Substitute.For <IChattelLocalStorage>(); var writer = new ChattelWriter(config, localStorage); var testAsset1 = new StratusAsset { Id = Guid.NewGuid(), }; var testAsset2 = new StratusAsset { Id = Guid.NewGuid(), }; var testAsset3 = new StratusAsset { Id = Guid.NewGuid(), }; Parallel.Invoke( () => writer.PutAssetSync(testAsset1), () => writer.PutAssetSync(testAsset2), () => writer.PutAssetSync(testAsset3) ); server.Received(1).StoreAssetSync(testAsset1); server.Received(1).StoreAssetSync(testAsset2); server.Received(1).StoreAssetSync(testAsset3); }
public static void TestChattelWriter_PutAssetSync_ServerError_AggregateException_ContainsCorrectException() { 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 DriveNotFoundException(); // Just needs an error to cause remote storage failure. }) ; var testAsset = new StratusAsset { Id = Guid.NewGuid(), }; try { writer.PutAssetSync(testAsset); } catch (AggregateException e) { Assert.IsInstanceOf(typeof(DriveNotFoundException), e.InnerException); return; } Assert.Fail(); }
public static void TestChattelWriter_PutAssetSync_WritesBackupRemoteParallel_WithLocalStorage() { var server1 = Substitute.For <IAssetServer>(); var server2 = Substitute.For <IAssetServer>(); var config = new ChattelConfiguration(LOCAL_STORAGE_DIR_INFO.FullName, new List <List <IAssetServer> > { new List <IAssetServer> { server1, }, new List <IAssetServer> { server2, }, }); var localStorage = Substitute.For <IChattelLocalStorage>(); var writer = new ChattelWriter(config, localStorage); var testAsset = new StratusAsset { Id = Guid.NewGuid(), }; server1.WhenForAnyArgs(x => x.StoreAssetSync(testAsset)).Do(x => throw new AssetWriteException(testAsset.Id)); writer.PutAssetSync(testAsset); server2.Received(1).StoreAssetSync(testAsset); }
public static void TestChattelWriter_PutAssetSync_WritesRemoteSeries_CorrectOrder() { var server1 = Substitute.For <IAssetServer>(); var server2 = Substitute.For <IAssetServer>(); var config = new ChattelConfiguration(new List <List <IAssetServer> > { new List <IAssetServer> { server1, }, new List <IAssetServer> { server2, }, }); var writer = new ChattelWriter(config); var testAsset = new StratusAsset { Id = Guid.NewGuid(), }; server1.WhenForAnyArgs(x => x.StoreAssetSync(testAsset)).Do(x => throw new AssetWriteException(testAsset.Id)); writer.PutAssetSync(testAsset); Received.InOrder(() => { server1.StoreAssetSync(testAsset); server2.StoreAssetSync(testAsset); }); }
public static void TestChattelWriter_PutAssetSync_Null_ArgumentNullException() { var config = new ChattelConfiguration(LOCAL_STORAGE_DIR_INFO.FullName); var writer = new ChattelWriter(config); Assert.Throws <ArgumentNullException>(() => writer.PutAssetSync(null)); }
public static void TestChattelWriter_PutAssetSync_EmptyId_ArgumentException() { var config = new ChattelConfiguration(LOCAL_STORAGE_DIR_INFO.FullName); var writer = new ChattelWriter(config); Assert.Throws <ArgumentException>(() => writer.PutAssetSync(new StratusAsset { Id = Guid.Empty, })); }
public static void TestChattelWriter_PutAssetSync_WritesLocalStorage_NoRemote() { 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(), }; writer.PutAssetSync(testAsset); localStorage.Received(1).StoreAsset(testAsset); }
public static void TestChattelWriter_PutAssetSync_WritesRemote_NoLocalStorage() { var server = Substitute.For <IAssetServer>(); var config = new ChattelConfiguration(server); var localStorage = Substitute.For <IChattelLocalStorage>(); var writer = new ChattelWriter(config, localStorage); var testAsset = new StratusAsset { Id = Guid.NewGuid(), }; writer.PutAssetSync(testAsset); server.Received(1).StoreAssetSync(testAsset); }
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 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); }); }
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)); }
/// <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); }