public async Task GetOrAddAsyncTest() { DictionaryChange change = null; TransactedConcurrentDictionary <int, string> d = new TransactedConcurrentDictionary <int, string>( new Uri("test://mocks", UriKind.Absolute), (c) => { change = c; return(true); } ); using (var tx = _stateManager.CreateTransaction()) { await d.GetOrAddAsync(tx, 1, (k) => "One"); await d.GetOrAddAsync(tx, 1, (k) => "Two"); Assert.IsNull(change); await tx.CommitAsync(); Assert.AreEqual("One", change.Added); Assert.AreEqual("One", (await GetValue(d, 1)).Value); } }
public async Task <T> GetOrAddAsync <T>(ITransaction tx, Uri name, TimeSpan timeout) where T : IReliableState { IReliableState constructed = null; var result = await _store.GetOrAddAsync(tx, name, collectionName => { var typeArguments = typeof(T).GetGenericArguments(); var typeDefinition = typeof(T).GetGenericTypeDefinition(); if (typeof(IReliableDictionary <,>).IsAssignableFrom(typeDefinition) || typeof(IReliableDictionary2 <,>).IsAssignableFrom(typeDefinition)) { constructed = ConstructMockCollection(collectionName, typeof(MockReliableDictionary <,>), typeArguments); } else if (typeof(IReliableConcurrentQueue <>).IsAssignableFrom(typeDefinition)) { constructed = ConstructMockCollection(collectionName, typeof(MockReliableConcurrentQueue <>), typeArguments); } else { constructed = ConstructMockCollection(collectionName, typeof(MockReliableQueue <>), typeArguments); } return(constructed); }); return((T)result); }