public async Task should_load_incrementally_from_an_existing_storage() { var storage = new MemoryStorage(); await Task.WhenAll(Enumerable.Range(0, 8).Select(x => storage.Put(new TestStorageObject(x)))); var collection = new StorageCollection<TestStorageObject>(storage, "0", "6"); Assert.True(collection.HasMoreItems); Assert.Equal(0, collection.Count); await collection.LoadMoreItemsAsync(2); Assert.Equal(2, collection.Count); Assert.Equal(collection.Select(e => e.Id), Enumerable.Range(0, 2).Select(i => i.ToString())); await collection.LoadMoreItemsAsync(2); Assert.Equal(4, collection.Count); Assert.Equal(collection.Select(e => e.Id), Enumerable.Range(0, 4).Select(i => i.ToString())); Assert.False(collection.IsLoading); Assert.True(collection.HasMoreItems); await collection.LoadMoreItemsAsync(10); Assert.Equal(6, collection.Count); Assert.False(collection.HasMoreItems); Assert.Equal(collection.Select(e => e.Id), Enumerable.Range(0, 6).Select(i => i.ToString())); }
/// <summary> /// Accepts the changes. /// </summary> public virtual void AcceptChanges() { Logger.Debug(String.Format("Accepting changes with a following state {0}.", this.ObjectState.ToString())); using (TransactionScope scope = new TransactionScope()) { switch (this.ObjectState) { case MetaObjectState.Added: OnSaved(DataService.ExecuteNonExec(CreateInsertCommand())); this._State = MetaObjectState.Unchanged; break; case MetaObjectState.Modified: OnSaved(DataService.ExecuteNonExec(CreateUpdateCommand())); this._State = MetaObjectState.Unchanged; break; case MetaObjectState.Deleted: DataService.Run(CreateDeleteCommand()); // need to remove item from the collection if (StorageCollection != null) { StorageCollection.RemovedDeletedItem(this); } break; case MetaObjectState.Unchanged: break; } scope.Complete(); } }
/// <summary> /// Accepts the changes. /// </summary> /// <param name="context">The context.</param> /// <param name="saveSystem">if set to <c>true</c> [save system].</param> internal void AcceptChanges(MetaDataContext context, bool saveSystem) { using (TransactionScope scope = new TransactionScope()) { if (saveSystem) { switch (this.ObjectState) { case MetaObjectState.Added: ProcessInsertUpdateResults(DataService.ExecuteNonExec(CreateInsertCommand(context))); break; case MetaObjectState.Modified: ProcessInsertUpdateResults(DataService.ExecuteNonExec(CreateUpdateCommand(context))); break; case MetaObjectState.Deleted: DataService.Run(CreateDeleteCommand(context)); // need to remove item from the collection if (StorageCollection != null) { StorageCollection.RemovedDeletedItem(this); } break; case MetaObjectState.Unchanged: break; } } base.AcceptChanges(context); scope.Complete(); } }
internal MeasurementResult(IPerfConfiguration configuration, string name) { CalculatorResult calcResult = Calculate(StorageCollection.GetOrCreate(name), configuration.ForTime); Name = name; Count = calcResult.Count; AverageCallTime = calcResult.AverageTime; }
public async Task should_load_all_items() { var storage = new MemoryStorage(); await Task.WhenAll(Enumerable.Range(0, 8).Select(x => storage.Put(new TestStorageObject(x)))); var collection = new StorageCollection<TestStorageObject>(storage, "0", "6"); Assert.True(collection.HasMoreItems); Assert.Equal(0, collection.Count); await collection.LoadAllAsync(2); Assert.Equal(6, collection.Count); Assert.False(collection.HasMoreItems); Assert.Equal(collection.Select(e => e.Id), Enumerable.Range(0, 6).Select(i => i.ToString())); }
/// <summary> /// Deletes this instance. /// </summary> public override void Delete() { // If item has been just added, simply remove it from the collection if (this.ObjectState == MetaObjectState.Added) { if (StorageCollection != null) { StorageCollection.Remove(this); } return; } base.Delete(); // need to remove item from the collection if (StorageCollection != null) { StorageCollection.DeleteItem(this); } }
public async Task should_sync_to_storage_change() { var changes = new List<NotifyCollectionChangedEventArgs>(); var storage = new Storage(x => new ObservableStorage<TestStorageObject>(new MemoryStorage<TestStorageObject>())); var collection = new StorageCollection<TestStorageObject>(storage, "0", "6"); collection.CollectionChanged += (sender, e) => { lock (changes) { changes.Add(e); } }; await collection.LoadMoreItemsAsync(10); await Task.Delay(20); Assert.Equal(0, changes.Count); await Task.WhenAll(Enumerable.Range(5, 3).Select(x => storage.Add(new TestStorageObject(x)))); await Task.Delay(20); Assert.Equal(1, changes.Count); Assert.Equal(1, collection.Count); changes.Clear(); await Task.WhenAll(Enumerable.Range(0, 8).Select(x => storage.Put(new TestStorageObject(x)))); await Task.Delay(20); Assert.Equal(6, changes.Count); Assert.Equal(6, collection.Count); changes.Clear(); await Task.WhenAll(Enumerable.Range(4, 4).Select(x => storage.Put(new TestStorageObject(x)))); await Task.Delay(20); Assert.Equal(2, changes.Count); Assert.Equal(6, collection.Count); changes.Clear(); await Task.WhenAll(Enumerable.Range(2, 6).Select(x => storage.Delete<TestStorageObject>(x.ToString()))); await Task.Delay(20); Assert.Equal(4, changes.Count); Assert.Equal(2, collection.Count); changes.Clear(); }
public async Task should_be_unsubscribed_after_gc() { var changes = new List<NotifyCollectionChangedEventArgs>(); var storage = new Storage(x => new ObservableStorage<TestStorageObject>(new MemoryStorage<TestStorageObject>())); var collection = new StorageCollection<TestStorageObject>(storage, "0", "6").WithAllItems(); var handler = new NotifyCollectionChangedEventHandler((sender, e) => { changes.Add(e); }); collection.CollectionChanged += handler; await storage.Add(new TestStorageObject(0)); await Task.Delay(20); Assert.Equal(1, changes.Count); GC.Collect(); GC.WaitForPendingFinalizers(); await storage.Add(new TestStorageObject(1)); await Task.Delay(20); Assert.Equal(2, changes.Count); collection.CollectionChanged -= handler; collection = null; GC.Collect(); GC.WaitForPendingFinalizers(); await storage.Add(new TestStorageObject(3)); await Task.Delay(20); Assert.Equal(2, changes.Count); }
public async Task should_sync_to_concurrent_storage_changes() { var count = 100; var storage = new Storage(x => new ObservableStorage<TestStorageObject>(new MemoryStorage<TestStorageObject>())); await Task.WhenAll(Enumerable.Range(0, count).AsParallel().Select(i => storage.Put(new TestStorageObject(i)))); var collection = new StorageCollection<TestStorageObject>(storage, null, null).WithAllItems(); await Task.WhenAll(Enumerable.Range(count, count).AsParallel().Select(i => storage.Put(new TestStorageObject(i)))); await collection.ChangedTo(m => m.Select(x => x.Id).Distinct().Count() == count * 2); }
public static void RegisterStorage(StorageCollection storage) { storage.Add(new AzureTableStorage("workerrole1", CloudConfigurationManager.GetSetting("StorageConnectionString"))); }
private void InitalizeStorage() { var assemblies = AppDomain.CurrentDomain.GetAssemblies(); var types = assemblies.SelectMany(a => a.GetTypes()); var taskType = types.SingleOrDefault(t => t.Name == "StorageConfig"); if (taskType == null) { throw new Exception(string.Format("Type for 'StorageConfig' not found")); } var registerMethod = taskType.GetMethod("RegisterStorage"); if (registerMethod == null) { throw new Exception(string.Format("Method 'RegisterQueues' not found on Type 'StorageConfig'")); } var storageCollection = new StorageCollection(); registerMethod.Invoke(null, new object[] { storageCollection }); Storage = storageCollection.Single(); }