public virtual void TestCounter() { long countResetTimePeriodMs = 200L; ByteArrayManager.Counter c = new ByteArrayManager.Counter(countResetTimePeriodMs); int n = DFSUtil.GetRandom().Next(512) + 512; IList <Future <int> > futures = new AList <Future <int> >(n); ExecutorService pool = Executors.NewFixedThreadPool(32); try { // increment for (int i = 0; i < n; i++) { futures.AddItem(pool.Submit(new _Callable_82(c))); } // sort and wait for the futures futures.Sort(Cmp); } finally { pool.Shutdown(); } // check futures NUnit.Framework.Assert.AreEqual(n, futures.Count); for (int i_1 = 0; i_1 < n; i_1++) { NUnit.Framework.Assert.AreEqual(i_1 + 1, futures[i_1].Get()); } NUnit.Framework.Assert.AreEqual(n, c.GetCount()); // test auto-reset Sharpen.Thread.Sleep(countResetTimePeriodMs + 100); NUnit.Framework.Assert.AreEqual(1, c.Increment()); }
/// <returns> /// the counter for the given key; /// and create a new counter if it does not exist. /// </returns> internal virtual ByteArrayManager.Counter Get(int key, bool createIfNotExist) { lock (this) { ByteArrayManager.Counter count = map[key]; if (count == null && createIfNotExist) { count = new ByteArrayManager.Counter(countResetTimePeriodMs); map[key] = count; } return(count); } }
public _Callable_82(ByteArrayManager.Counter c) { this.c = c; }