public void FavoritesAndGroupsReloadTest()
        {
            IFavorite favToRemove = this.AddFavorite();
            IGroup groupToRemove = this.AddNewGroup("GroupToRemove");
            // not connected each other
            Tuple<IFavorite, IGroup> toRemove = new Tuple<IFavorite, IGroup>(favToRemove, groupToRemove);
            Tuple<IFavorite, IGroup> toUpdate = this.AddFavoriteWithGroup("GroupToUpdate");

            var testFileWatch = new TestFileWatch();
            var secondaryPersistence = this.InitializeSecondaryPersistence(testFileWatch);

            //do the next steps ot be able serialize otherwise paraller work of persistences refresh and dont relay on OS file changes
            //1. kick off the file changed event in background thread to let the secondary persistence reload in parallel
            ThreadPool.QueueUserWorkItem(new WaitCallback((state) => testFileWatch.FireFileChanged()));

            //2. do the changes in primary persistence, this is not noticed in secondary persistence yet
            Tuple<IFavorite, IGroup> added = this.UpdatePrimaryPersistenceFavorites(toUpdate, toRemove);

            testFileWatch.ObservationWatch.Set(); // signal secondary persistence, that changes are ready to reload

            //3. wait till secondary persistence is done with reload
            testFileWatch.ReleaseWatch.WaitOne(6000);

            //4. assert the results in secondary persistence
            int secondaryFavoritesCount = secondaryPersistence.Favorites.Count();
            Assert.AreEqual(2, secondaryFavoritesCount); // one updated and one added
            Assert.AreEqual(this.addedFavoriteId, added.Item1.Id, "Didnt receive favorite add event");
            Assert.AreEqual(this.updatedFavoriteId, toUpdate.Item1.Id, "Didnt receive favorite update event");
            Assert.AreEqual(this.deletedFavoriteId, toRemove.Item1.Id, "Didnt receive favorite removed event");
            int secondaryGroupsCount = secondaryPersistence.Groups.Count();
            Assert.AreEqual(2, secondaryGroupsCount); // one updated and one added
            Assert.AreEqual(this.addedGroupId, ((Group)added.Item2).Id, "Didnt receive group added event");
            Assert.AreEqual(this.deletedGroupId, ((Group)toRemove.Item2).Id, "Didnt receive group removed event");
            Assert.AreEqual(this.updatedGroupId, ((Group)toUpdate.Item2).Id, "Didnt receive group updated event");
        }
Beispiel #2
0
        private FilePersistence InitializeSecondaryPersistence(TestFileWatch testFileWatch)
        {
            FilePersistence secondaryPersistence = CreateFilePersistence(testFileWatch);

            secondaryPersistence.Dispatcher.FavoritesChanged += this.DispatcherOnFavoritesChanged;
            secondaryPersistence.Dispatcher.GroupsChanged    += this.DispatcherOnGroupsChanged;
            return(secondaryPersistence);
        }
Beispiel #3
0
        public void FavoritesAndGroupsReload_ReportsAllEvents()
        {
            IFavorite favToRemove   = this.AddFavorite();
            IGroup    groupToRemove = this.AddNewGroup("GroupToRemove");
            // not connected each other
            Tuple <IFavorite, IGroup> toRemove = new Tuple <IFavorite, IGroup>(favToRemove, groupToRemove);
            Tuple <IFavorite, IGroup> toUpdate = this.AddFavoriteWithGroup("GroupToUpdate");

            var testFileWatch        = new TestFileWatch();
            var secondaryPersistence = this.InitializeSecondaryPersistence(testFileWatch);

            //do the next steps ot be able serialize otherwise paraller work of persistences refresh and dont relay on OS file changes
            //1. kick off the file changed event in background thread to let the secondary persistence reload in parallel
            ThreadPool.QueueUserWorkItem(new WaitCallback((state) => testFileWatch.FireFileChanged()));

            //2. do the changes in primary persistence, this is not noticed in secondary persistence yet
            Tuple <IFavorite, IGroup> added = this.UpdatePrimaryPersistenceFavorites(toUpdate, toRemove);

            testFileWatch.ObservationWatch.Set(); // signal secondary persistence, that changes are ready to reload

            //3. wait till secondary persistence is done with reload
            testFileWatch.ReleaseWatch.WaitOne(6000);

            //4. assert the results in secondary persistence
            int secondaryFavoritesCount = secondaryPersistence.Favorites.Count();

            Assert.AreEqual(2, secondaryFavoritesCount); // one updated and one added
            Assert.AreEqual(this.addedFavoriteId, added.Item1.Id, "Didnt receive favorite add event");
            Assert.AreEqual(this.updatedFavoriteId, toUpdate.Item1.Id, "Didnt receive favorite update event");
            Assert.AreEqual(this.deletedFavoriteId, toRemove.Item1.Id, "Didnt receive favorite removed event");
            int secondaryGroupsCount = secondaryPersistence.Groups.Count();

            Assert.AreEqual(2, secondaryGroupsCount); // one updated and one added
            Assert.AreEqual(this.addedGroupId, ((Group)added.Item2).Id, "Didnt receive group added event");
            Assert.AreEqual(this.deletedGroupId, ((Group)toRemove.Item2).Id, "Didnt receive group removed event");
            Assert.AreEqual(this.updatedGroupId, ((Group)toUpdate.Item2).Id, "Didnt receive group updated event");
        }
 private FilePersistence InitializeSecondaryPersistence(TestFileWatch testFileWatch)
 {
     var secondaryPersistence = new FilePersistence(new PersistenceSecurity(), testFileWatch);
     // let the persistence load initial state
     secondaryPersistence.Initialize();
     secondaryPersistence.Dispatcher.FavoritesChanged += this.DispatcherOnFavoritesChanged;
     secondaryPersistence.Dispatcher.GroupsChanged += this.DispatcherOnGroupsChanged;
     return secondaryPersistence;
 }