Exemplo n.º 1
0
        public void TestPipe()
        {
            SafeCounter sc  = new SafeCounter();
            STP         stp = new STP();

            stp.Pipe(
                sc,
                sc1 => { if (sc.Counter == 0)
                         {
                             sc1.Increment();
                         }
                },
                sc1 => { if (sc.Counter == 1)
                         {
                             sc1.Increment();
                         }
                },
                sc1 => { if (sc.Counter == 2)
                         {
                             sc1.Increment();
                         }
                }
                );

            Assert.AreEqual(3, sc.Counter);

            stp.Shutdown();
        }
Exemplo n.º 2
0
        public void WarewolfErrorCounterResource_SafeCounterSwallowsExceptions()
        {
            bool incremented   = false;
            bool decremented   = false;
            bool incrementedBy = false;
            bool setup         = true;
            var  guid          = Guid.NewGuid();
            var  inner         = new Mock <IResourcePerformanceCounter>();

            inner.Setup(a => a.Decrement()).Callback(() => decremented            = true).Throws(new AccessViolationException());
            inner.Setup(a => a.Increment()).Callback(() => incremented            = true).Throws(new AccessViolationException());
            inner.Setup(a => a.IncrementBy(5)).Callback((long a) => incrementedBy = true).Throws(new AccessViolationException());
            inner.Setup(a => a.CategoryInstanceName).Returns("bob");
            inner.Setup(a => a.ResourceId).Returns(guid);
            inner.Setup(a => a.Category).Returns("Neo");
            inner.Setup(a => a.Name).Returns("Morpheus");
            inner.Setup(a => a.IsActive).Returns(true);
            inner.Setup(a => a.Setup()).Callback(() => { setup = true; });
            var safe = new SafeCounter(inner.Object);

            safe.Increment();
            Assert.IsTrue(incremented);
            safe.Decrement();
            Assert.IsTrue(decremented);
            safe.IncrementBy(5);
            Assert.IsTrue(incrementedBy);
            Assert.AreEqual(safe.InnerCounter, inner.Object);
            Assert.AreEqual("Neo", safe.Category);
            Assert.AreEqual(safe.Name, "Morpheus");
            Assert.IsTrue(setup);
        }
Exemplo n.º 3
0
        public void TestJoin()
        {
            STP stp = new STP();

            SafeCounter sc = new SafeCounter();

            stp.Join(
                sc.Increment,
                sc.Increment,
                sc.Increment);

            Assert.AreEqual(3, sc.Counter);

            for (int j = 0; j < 10; j++)
            {
                sc.Reset();

                Action[] actions = new Action[1000];
                for (int i = 0; i < actions.Length; i++)
                {
                    actions[i] = sc.Increment;
                }

                stp.Join(actions);

                Assert.AreEqual(actions.Length, sc.Counter);
            }

            stp.Shutdown();
        }
Exemplo n.º 4
0
        public void TestJoin()
        {
            SmartThreadPool stp = new SmartThreadPool();

            SafeCounter sc = new SafeCounter();

            stp.Join(
                sc.Increment,
                sc.Increment,
                sc.Increment);

            Assert.AreEqual(3, sc.Counter);

            for (int j = 0; j < 10; j++)
            {
                sc.Reset();

                Action[] actions = new Action[1000];
                for (int i = 0; i < actions.Length; i++)
                {
                    actions[i] = sc.Increment;
                }

                stp.Join(actions);

                Assert.AreEqual(actions.Length, sc.Counter);
            }

            stp.Shutdown();
        }
        public void TestPipe()
        {
            SafeCounter sc = new SafeCounter();
            SmartThreadPool stp = new SmartThreadPool();

            stp.Pipe(
                sc,
                sc1 => { if (sc.Counter == 0) { sc1.Increment(); }},
                sc1 => { if (sc.Counter == 1) { sc1.Increment(); }},
                sc1 => { if (sc.Counter == 2) { sc1.Increment(); }}
                );

            Assert.AreEqual(3, sc.Counter);

            stp.Shutdown();
        }
Exemplo n.º 6
0
 /// <exception cref="System.Exception"></exception>
 private void AssertCount(SafeCounter actual, int expected, string
                          name)
 {
     actual.AssertEquals(expected, MaxChecks);
 }
Exemplo n.º 7
0
 public _IClosure4_140(SafeCounter _enclosing, int expected, IntByRef ret)
 {
     this._enclosing = _enclosing;
     this.expected   = expected;
     this.ret        = ret;
 }
Exemplo n.º 8
0
 public _IClosure4_131(SafeCounter _enclosing)
 {
     this._enclosing = _enclosing;
 }
Exemplo n.º 9
0
				public _IClosure4_140(SafeCounter _enclosing, int expected, IntByRef ret)
				{
					this._enclosing = _enclosing;
					this.expected = expected;
					this.ret = ret;
				}
Exemplo n.º 10
0
				public _IClosure4_131(SafeCounter _enclosing)
				{
					this._enclosing = _enclosing;
				}
Exemplo n.º 11
0
 /// <exception cref="System.Exception"></exception>
 private void AssertCount(SafeCounter actual, int expected, string
     name)
 {
     actual.AssertEquals(expected, MaxChecks);
 }
Exemplo n.º 12
0
        /// <summary>
        /// Initialize all the counters in the table
        /// </summary>
        /// <param name="listenPort">listenPort</param>
        /// <param name="typeIdList">type id list</param>
        /// <param name="maxTypeId">max type id</param>
        /// <param name="isInit">true for init and false for restart</param>
        public void InitializeCounters(int listenPort, List <short> typeIdList, short maxTypeId, bool isInit)
        {
            // compose category string
            perfCounterCategoryNameString =
                PerformanceCounterConstant.CategoryNameBase + "(Port " + listenPort + ")";

            if (isInit)
            {
                CreateCounterCategory(perfCounterCategoryNameString);
            }

            int tempNumberOfTypeIds = typeIdList.Count;

            // initialize all the counters
            if (PerformanceCounterCategory.Exists(perfCounterCategoryNameString))
            {
                // create type id and index mapping array
                short[] tempTypeIdIndexMappingArray = new short[maxTypeId + 1];

                for (short i = 0; i < tempNumberOfTypeIds; i++)
                {
                    tempTypeIdIndexMappingArray[typeIdList[i]] = i;
                }

                // clean-up and initialize the counter table,  the first dimension is all the counter names
                // the second dimension is all the type ids plus a "total" instance
                SafeCounter[,] tempCounterTable = new SafeCounter[PerformanceCounterConstant.CounterInfo.GetLength(0), tempNumberOfTypeIds + 1];

                // fill the counterTable
                for (int j = 0; j < PerformanceCounterConstant.CounterInfo.GetLength(0); j++)
                {
                    for (int k = 0; k < tempNumberOfTypeIds + 1; k++)
                    {
                        string instanceName;

                        // *** total instance is the last entry in the row ***
                        if (k == tempNumberOfTypeIds)
                        {
                            // need to check if the we have total counter in this case
                            // if the counter is of rate type, then we have a total counter
                            // if not, total counter will not make sense.
                            if (!NeedTotalCounter(PerformanceCounterConstant.CounterInfo[j, 1]))
                            {
                                continue;
                            }

                            instanceName = "total";
                        }
                        else
                        {
                            instanceName = "type id " + typeIdList[(short)k];
                        }

                        tempCounterTable[j, k] = new SafeCounter(
                            perfCounterCategoryNameString,
                            PerformanceCounterConstant.CounterInfo[j, 0],
                            instanceName,
                            false);

                        tempCounterTable[j, k].Reset();
                    }
                }

                Interlocked.Exchange(ref numberOfTypeIds, tempNumberOfTypeIds);
                Interlocked.Exchange(ref typeIdIndexMappingArray, tempTypeIdIndexMappingArray);

                counterTable = tempCounterTable;
            }
            else
            {
                if (LoggingUtil.Log.IsWarnEnabled)
                {
                    LoggingUtil.Log.Warn(string.Format("The performance counter category {0} does not exist", perfCounterCategoryNameString));
                }
            }
        }