private static void Listen(BlockingCollection<ISyncEvent> queue, SyncEventManager manager) { Logger.Debug("Starting to listen on SyncEventQueue"); while (!queue.IsCompleted) { ISyncEvent syncEvent = null; // Blocks if number.Count == 0 // IOE means that Take() was called on a completed collection. // Some other thread can call CompleteAdding after we pass the // IsCompleted check but before we call Take. // In this example, we can simply catch the exception since the // loop will break on the next iteration. try { syncEvent = queue.Take(); } catch (InvalidOperationException) { } if (syncEvent != null) { try{ manager.Handle(syncEvent); }catch(Exception e) { Logger.Error("Exception in EventHandler"); Logger.Error(e); } } } Logger.Debug("Stopping to listen on SyncEventQueue"); }
private static void Listen(BlockingCollection <ISyncEvent> queue, SyncEventManager manager) { Logger.Debug("Starting to listen on SyncEventQueue"); while (!queue.IsCompleted) { ISyncEvent syncEvent = null; // Blocks if number.Count == 0 // IOE means that Take() was called on a completed collection. // Some other thread can call CompleteAdding after we pass the // IsCompleted check but before we call Take. // In this example, we can simply catch the exception since the // loop will break on the next iteration. try { syncEvent = queue.Take(); } catch (InvalidOperationException) { } if (syncEvent != null) { try{ manager.Handle(syncEvent); }catch (Exception e) { Logger.Error("Exception in EventHandler"); Logger.Error(e); } } } Logger.Debug("Stopping to listen on SyncEventQueue"); }
public void AddHandlerTest() { var handlerMock = new Mock<SyncEventHandler>(); var eventMock = new Mock<ISyncEvent>(); SyncEventManager manager = new SyncEventManager(); manager.AddEventHandler(handlerMock.Object); manager.Handle(eventMock.Object); handlerMock.Verify(foo => foo.Handle(eventMock.Object), Times.Once()); }
/// <summary> /// Constructor. /// </summary> public SyncEventQueue(SyncEventManager manager) { if (manager == null) { throw new ArgumentException("manager may not be null"); } this.manager = manager; // Start to listen in a separate thread. this.consumer = new Task(() => Listen(this.queue, this.manager)); this.consumer.Start(); }
public void ContinueIfHandlerNotSucceedsTest() { var handlerMock1 = new Mock<SyncEventHandler>(); handlerMock1.Setup(foo => foo.Handle(It.IsAny<ISyncEvent>())).Returns(false); handlerMock1.Setup(foo => foo.Priority).Returns(2); var handlerMock2 = new Mock<SyncEventHandler>(); handlerMock2.Setup(foo => foo.Priority).Returns(1); var eventMock = new Mock<ISyncEvent>(); SyncEventManager manager = new SyncEventManager(); manager.AddEventHandler(handlerMock1.Object); manager.AddEventHandler(handlerMock2.Object); manager.Handle(eventMock.Object); handlerMock1.Verify(foo => foo.Handle(eventMock.Object), Times.Once()); handlerMock2.Verify(foo => foo.Handle(eventMock.Object), Times.Once()); }
/// <summary> /// Constructor. /// </summary> public RepoBase(RepoInfo repoInfo, IActivityListener activityListener) { EventManager = new SyncEventManager(); EventManager.AddEventHandler(new DebugLoggingHandler()); EventManager.AddEventHandler(new GenericSyncEventHandler<RepoConfigChangedEvent>(0, RepoInfoChanged)); Queue = new SyncEventQueue(EventManager); RepoInfo = repoInfo; LocalPath = repoInfo.TargetDirectory; Name = repoInfo.Name; RemoteUrl = repoInfo.Address; this.activityListener = activityListener; if (repoInfo.IsSuspended) Status = SyncStatus.Suspend; // Folder lock. // Disabled for now. Can be an interesting feature, but should be made opt-in, as // most users would be surprised to see this file appear. // folderLock = new FolderLock(LocalPath); Watcher = new Watcher(LocalPath); Watcher.EnableRaisingEvents = true; // Main loop syncing every X seconds. remote_timer.Elapsed += delegate { // Synchronize. SyncInBackground(); }; remote_timer.AutoReset = true; Logger.Info("Repo " + repoInfo.Name + " - Set poll interval to " + repoInfo.PollInterval + "ms"); remote_timer.Interval = repoInfo.PollInterval; //Partial sync interval.. local_timer.Elapsed += delegate { // Run partial sync. SyncInBackground(false); }; local_timer.AutoReset = false; local_timer.Interval = delay_interval; }
public void DeleteWorksCorrectlyTest() { var handlerMock1 = new Mock<SyncEventHandler>(); handlerMock1.Setup(foo => foo.Handle(It.IsAny<ISyncEvent>())).Returns(false); handlerMock1.Setup(foo => foo.Priority).Returns(1); var handlerMock2 = new Mock<SyncEventHandler>(); handlerMock2.Setup(foo => foo.Handle(It.IsAny<ISyncEvent>())).Returns(false); handlerMock2.Setup(foo => foo.Priority).Returns(1); var handlerMock3 = new Mock<SyncEventHandler>(); handlerMock3.Setup(foo => foo.Handle(It.IsAny<ISyncEvent>())).Returns(false); handlerMock3.Setup(foo => foo.Priority).Returns(1); var eventMock = new Mock<ISyncEvent>(); SyncEventManager manager = new SyncEventManager(); manager.AddEventHandler(handlerMock1.Object); manager.AddEventHandler(handlerMock2.Object); manager.AddEventHandler(handlerMock3.Object); manager.RemoveEventHandler(handlerMock2.Object); manager.Handle(eventMock.Object); handlerMock1.Verify(foo => foo.Handle(eventMock.Object), Times.Once()); handlerMock2.Verify(foo => foo.Handle(eventMock.Object), Times.Never()); handlerMock3.Verify(foo => foo.Handle(eventMock.Object), Times.Once()); }
public void FirstInsertedHandlerWithSamePrioWinsTest() { var handlerMock1 = new Mock<SyncEventHandler>(); handlerMock1.Setup(foo => foo.Handle(It.IsAny<ISyncEvent>())).Returns(true); handlerMock1.Setup(foo => foo.Priority).Returns(1); var handlerMock2 = new Mock<SyncEventHandler>(); handlerMock2.Setup(foo => foo.Handle(It.IsAny<ISyncEvent>())).Returns(true); handlerMock2.Setup(foo => foo.Priority).Returns(1); var eventMock = new Mock<ISyncEvent>(); SyncEventManager manager = new SyncEventManager(); manager.AddEventHandler(handlerMock1.Object); manager.AddEventHandler(handlerMock2.Object); manager.Handle(eventMock.Object); handlerMock1.Verify(foo => foo.Handle(eventMock.Object), Times.Once()); handlerMock2.Verify(foo => foo.Handle(eventMock.Object), Times.Never()); }