コード例 #1
0
        public async Task CheckHealthAsync_Warning_CheckWarned(long counterValue, long errorThreshold, long warningThreshold)
        {
            var counter = new LocalCounter("test");

            counter.Value = counterValue;
            var counterProvider = new TestCounterProvider(counter);
            var check           = new CounterCheck(counterProvider);

            var settings = new CounterCheckSettings("counter", false, 0, null, errorThreshold, warningThreshold, false);
            var context  = new HealthCheckContext(settings);

            await check.CheckHealthAsync(context, settings);

            Assert.True(context.HasWarned);
        }
コード例 #2
0
ファイル: Guarded.cs プロジェクト: zhangz/Highlander.Net
        private T Enter()
        {
            // check if disposed
            if (_disposed)
            {
                throw new InvalidOperationException(
                          $"Guarded<{typeof(T).Name}>.Locked: Object disposed!");
            }
            // debug temp todo remove - do not allow nested locks (thereby preventing deadlocking)
            LocalCounter threadLockCounter;
            int          threadId = Thread.CurrentThread.ManagedThreadId;

            lock (DebugThreadLockCounter)
            {
                if (!DebugThreadLockCounter.TryGetValue(threadId, out threadLockCounter))
                {
                    threadLockCounter = new LocalCounter();
                    DebugThreadLockCounter[threadId] = threadLockCounter;
                }
            }
            long nested = threadLockCounter.Increment();

            // end debug
            try
            {
                if (nested > 1)
                {
                    throw new InvalidOperationException("Attempted secondary lock acquisition!");
                }

                int spinCount = 0;
                T   lockedObject;
                while ((lockedObject = Interlocked.Exchange(ref _target, null)) == null)
                {
                    // spin - todo - in .Net 4.0 use Thread.SpinWait()
                    bool sleep = false;
                    if (_IsSingleCore)
                    {
                        sleep = true;
                    }
                    else
                    {
                        // black magic from [email protected]
                        spinCount++;
                        // debug
                        if (spinCount % 4000 == 0)
                        {
                            sleep = true;
                        }
                        if (spinCount % 1000000000 == 0) // yes 1 billion, or about 1 second
                        {
                            // if you see this debug message, it is likely you have a deadlock
                            Debug.WriteLine($"Guarded<{typeof(T).Name}>.Enter: Spin={spinCount}");
                        }
                        // end debug
                    }
                    if (sleep)
                    {
                        // debug
                        long sleepCount = Interlocked.Increment(ref _debugSleepCount);
                        if (sleepCount % 100000 == 0)
                        {
                            // if you see this debug message, but not the message above, it is likely there
                            // is another thread locking the target object for very long periods.
                            Debug.WriteLine(String.Format("Guarded<{0}>.Enter: SleepCount={1}", typeof(T).Name, sleepCount));
                        }
                        // end debug
                        Thread.Sleep(0);
                    }
                }
                return(lockedObject);
            }
            finally
            {
                threadLockCounter.Decrement();
            }
        }