public async Task Should_not_load_items_if_another_initialization_is_in_progressAsync() { //Arrange var storagefolder = new Mock <IStorageFolder <FileStoredStory> >(); var testHelper = new AsyncReadTester(); storagefolder .Setup(fake => fake.GetStoredItemsAsync()) .Returns(testHelper.ReadAsync()); var folderprovider = new Mock <IStorageFolderProvider>(); folderprovider .Setup(fake => fake.GetStorageFolder <FileStoredStory>(It.IsAny <string>())) .Returns(storagefolder.Object); var datastore = new InMemoryFileBackedStoryDataStore(folderprovider.Object); var firstInit = datastore.InitializeAsync(); //Act var secondInit = datastore.InitializeAsync(); testHelper.StopFakeReading(); await Task.WhenAll(firstInit, secondInit); //Assert Assert.IsFalse(testHelper.CalledWhileInProgress); }
public async Task Should_not_try_to_write_files_if_another_instance_is_reading() { //Arrange var storagefolder = new Mock <IStorageFolder <FileStoredStory> >(); var testHelper = new AsyncReadTester(); storagefolder .Setup(mock => mock.GetStoredItemsAsync()) .Returns(testHelper.ReadAsync()); storagefolder .Setup(mock => mock.CreateFileForItemAsync(It.IsAny <FileStoredStory>(), It.IsAny <string>())) .Callback(() => testHelper.SetOtherFunctionCalled()); var folderprovider = new Mock <IStorageFolderProvider>(); folderprovider .Setup(fake => fake.GetStorageFolder <FileStoredStory>(It.IsAny <string>())) .Returns(storagefolder.Object); var datastore1 = new InMemoryFileBackedStoryDataStore(folderprovider.Object); var datastore2 = new InMemoryFileBackedStoryDataStore(folderprovider.Object); var reading = datastore1.InitializeAsync(); //Act var writing = datastore2.AddOrUpdateAsync(new[] { new Story() }); testHelper.StopFakeReading(); await Task.WhenAll(reading, writing); //Assert Assert.IsFalse(testHelper.CalledWhileInProgress); }
public void Initialize_stores_the_retrieved_items_in_the_DataStore() { //Arrange var story = new FileStoredStory { Id = 42 }; var storagefolder = new Mock <IStorageFolder <FileStoredStory> >(); storagefolder .Setup(fake => fake.GetStoredItemsAsync()) .Returns(MakeAsync(new[] { story })); var folderprovider = new Mock <IStorageFolderProvider>(); folderprovider .Setup(fake => fake.GetStorageFolder <FileStoredStory>(It.IsAny <string>())) .Returns(storagefolder.Object); var datastore = new InMemoryFileBackedStoryDataStore(folderprovider.Object); //Act datastore.InitializeAsync().Wait(); //Assert var result = datastore.GetAllAsync().Result; Assert.AreEqual(story.Id, result.Single().Id); }
public void Get_retrieves_stories_with_requested_ids() { //Arrange var id1 = 42; var id2 = 27; var story1 = new FileStoredStory { Id = id1 }; var story2 = new FileStoredStory { Id = id2 }; var story3 = new FileStoredStory { Id = 123 }; var storagefolder = new Mock <IStorageFolder <FileStoredStory> >(); storagefolder .Setup(fake => fake.GetStoredItemsAsync()) .Returns(MakeAsync(new[] { story1, story2, story3 })); var folderprovider = new Mock <IStorageFolderProvider>(); folderprovider .Setup(fake => fake.GetStorageFolder <FileStoredStory>(It.IsAny <string>())) .Returns(storagefolder.Object); var datastore = new InMemoryFileBackedStoryDataStore(folderprovider.Object); datastore.InitializeAsync().Wait(); //Act var result = datastore.GetAsync(new[] { id1, id2 }).Result; //Assert var containsStory1 = result.Any(b => b.Id == id1); var containsStory2 = result.Any(b => b.Id == id2); Assert.IsTrue(containsStory1); Assert.IsTrue(containsStory2); Assert.AreEqual(2, result.Count()); }
public void Initializing_gets_the_items_from_StorageFolder() { //Arrange var storagefolder = new Mock <IStorageFolder <FileStoredStory> >(); storagefolder .Setup(fake => fake.GetStoredItemsAsync()) .Returns(MakeAsync(new List <FileStoredStory>())); var folderprovider = new Mock <IStorageFolderProvider>(); folderprovider .Setup(fake => fake.GetStorageFolder <FileStoredStory>(It.IsAny <string>())) .Returns(storagefolder.Object); var datastore = new InMemoryFileBackedStoryDataStore(folderprovider.Object); //Act datastore.InitializeAsync().Wait(); //Assert storagefolder.Verify(mock => mock.GetStoredItemsAsync(), Times.Once); }
public void IsInitialized_is_true_after_calling_initialize() { //Arrange var storagefolder = new Mock <IStorageFolder <FileStoredStory> >(); storagefolder .Setup(fake => fake.GetStoredItemsAsync()) .Returns(MakeAsync(new List <FileStoredStory>())); var folderprovider = new Mock <IStorageFolderProvider>(); folderprovider .Setup(fake => fake.GetStorageFolder <FileStoredStory>(It.IsAny <string>())) .Returns(storagefolder.Object); var datastore = new InMemoryFileBackedStoryDataStore(folderprovider.Object); //Act datastore.InitializeAsync().Wait(); //Assert Assert.IsTrue(datastore.IsInitialized); }