/// <summary>Test that a java interruption can stop the watcher thread</summary> /// <exception cref="System.Exception"/> public virtual void TestInterruption() { DomainSocketWatcher watcher = NewDomainSocketWatcher(10); watcher.watcherThread.Interrupt(); Uninterruptibles.JoinUninterruptibly(watcher.watcherThread); watcher.Close(); }
/// <summary> /// Creates a new DomainSocketWatcher and tracks its thread for termination due /// to an unexpected exception. /// </summary> /// <remarks> /// Creates a new DomainSocketWatcher and tracks its thread for termination due /// to an unexpected exception. At the end of each test, if there was an /// unexpected exception, then that exception is thrown to force a failure of /// the test. /// </remarks> /// <param name="interruptCheckPeriodMs"> /// interrupt check period passed to /// DomainSocketWatcher /// </param> /// <returns>new DomainSocketWatcher</returns> /// <exception cref="System.Exception">if there is any failure</exception> private DomainSocketWatcher NewDomainSocketWatcher(int interruptCheckPeriodMs) { DomainSocketWatcher watcher = new DomainSocketWatcher(interruptCheckPeriodMs, GetType ().Name); watcher.watcherThread.SetUncaughtExceptionHandler(new _UncaughtExceptionHandler_200 (this)); return(watcher); }
public _Runnable_149(AtomicInteger handled, int SocketNum, ReentrantLock Lock, AList <DomainSocket[]> pairs, DomainSocketWatcher watcher) { this.handled = handled; this.SocketNum = SocketNum; this.Lock = Lock; this.pairs = pairs; this.watcher = watcher; }
/// <summary>Test that domain sockets are closed when the watcher is closed.</summary> /// <exception cref="System.Exception"/> public virtual void TestCloseSocketOnWatcherClose() { DomainSocketWatcher watcher = NewDomainSocketWatcher(10000000); DomainSocket[] pair = DomainSocket.Socketpair(); watcher.Add(pair[1], new _Handler_103()); watcher.Close(); Uninterruptibles.JoinUninterruptibly(watcher.watcherThread); NUnit.Framework.Assert.IsFalse(pair[1].IsOpen()); }
/// <summary>Test that we can get notifications out a DomainSocketWatcher.</summary> /// <exception cref="System.Exception"/> public virtual void TestDeliverNotifications() { DomainSocketWatcher watcher = NewDomainSocketWatcher(10000000); DomainSocket[] pair = DomainSocket.Socketpair(); CountDownLatch latch = new CountDownLatch(1); watcher.Add(pair[1], new _Handler_73(latch)); pair[0].Close(); latch.Await(); watcher.Close(); }
/// <exception cref="System.Exception"/> public virtual void TestStress() { int SocketNum = 250; ReentrantLock Lock = new ReentrantLock(); DomainSocketWatcher watcher = NewDomainSocketWatcher(10000000); AList <DomainSocket[]> pairs = new AList <DomainSocket[]>(); AtomicInteger handled = new AtomicInteger(0); Thread adderThread = new Thread(new _Runnable_122(SocketNum, watcher , Lock, pairs, handled)); Thread removerThread = new Thread(new _Runnable_149(handled, SocketNum , Lock, pairs, watcher)); adderThread.Start(); removerThread.Start(); Uninterruptibles.JoinUninterruptibly(adderThread); Uninterruptibles.JoinUninterruptibly(removerThread); watcher.Close(); }
public void Run() { if (DomainSocketWatcher.Log.IsDebugEnabled()) { DomainSocketWatcher.Log.Debug(this + ": starting with interruptCheckPeriodMs = " + this._enclosing.interruptCheckPeriodMs); } SortedDictionary <int, DomainSocketWatcher.Entry> entries = new SortedDictionary <int , DomainSocketWatcher.Entry>(); DomainSocketWatcher.FdSet fdSet = new DomainSocketWatcher.FdSet(); this._enclosing.AddNotificationSocket(entries, fdSet); try { while (true) { this._enclosing.Lock.Lock(); try { foreach (int fd in fdSet.GetAndClearReadableFds()) { this._enclosing.SendCallbackAndRemove("getAndClearReadableFds", entries, fdSet, fd ); } if (!(this._enclosing.toAdd.IsEmpty() && this._enclosing.toRemove.IsEmpty())) { // Handle pending additions (before pending removes). for (IEnumerator <DomainSocketWatcher.Entry> iter = this._enclosing.toAdd.GetEnumerator (); iter.HasNext();) { DomainSocketWatcher.Entry entry = iter.Next(); DomainSocket sock = entry.GetDomainSocket(); DomainSocketWatcher.Entry prevEntry = entries[sock.fd] = entry; Preconditions.CheckState(prevEntry == null, this + ": tried to watch a file descriptor that we " + "were already watching: " + sock); if (DomainSocketWatcher.Log.IsTraceEnabled()) { DomainSocketWatcher.Log.Trace(this + ": adding fd " + sock.fd); } fdSet.Add(sock.fd); iter.Remove(); } // Handle pending removals while (true) { KeyValuePair <int, DomainSocket> entry = this._enclosing.toRemove.FirstEntry(); if (entry == null) { break; } this._enclosing.SendCallbackAndRemove("handlePendingRemovals", entries, fdSet, entry .Value.fd); } this._enclosing.processedCond.SignalAll(); } // Check if the thread should terminate. Doing this check now is // easier than at the beginning of the loop, since we know toAdd and // toRemove are now empty and processedCond has been notified if it // needed to be. if (this._enclosing.closed) { if (DomainSocketWatcher.Log.IsDebugEnabled()) { DomainSocketWatcher.Log.Debug(this.ToString() + " thread terminating."); } return; } // Check if someone sent our thread an InterruptedException while we // were waiting in poll(). if (Thread.Interrupted()) { throw new Exception(); } } finally { this._enclosing.Lock.Unlock(); } DomainSocketWatcher.DoPoll0(this._enclosing.interruptCheckPeriodMs, fdSet); } } catch (Exception) { DomainSocketWatcher.Log.Info(this.ToString() + " terminating on InterruptedException" ); } catch (Exception e) { DomainSocketWatcher.Log.Error(this.ToString() + " terminating on exception", e); } finally { this._enclosing.Lock.Lock(); try { this._enclosing.Kick(); // allow the handler for notificationSockets[0] to read a byte foreach (DomainSocketWatcher.Entry entry in entries.Values) { // We do not remove from entries as we iterate, because that can // cause a ConcurrentModificationException. this._enclosing.SendCallback("close", entries, fdSet, entry.GetDomainSocket().fd); } entries.Clear(); fdSet.Close(); } finally { this._enclosing.Lock.Unlock(); } } }
public _Runnable_451(DomainSocketWatcher _enclosing) { this._enclosing = _enclosing; }
internal NotificationHandler(DomainSocketWatcher _enclosing) { this._enclosing = _enclosing; }
/// <summary>Test that we can create a DomainSocketWatcher and then shut it down.</summary> /// <exception cref="System.Exception"/> public virtual void TestCreateShutdown() { DomainSocketWatcher watcher = NewDomainSocketWatcher(10000000); watcher.Close(); }