示例#1
0
        public void TestManualScrape()
        {
            CappedLog.CappedLogScope  scope;
            CappedLog.CappedLogConf   conf2, conf3, conf1;
            CappedLog.CappedLogMetric metric11, metric12, metric2, metric3;
            Init(out scope, out conf2, out conf3, out conf1, out metric11, out metric12, out metric2, out metric3);

            int actualCnt1 = 0, actualCnt2 = 0, actualCnt3 = 0;
            Action <KeyValuePair <CappedLog.CappedLogConf, IReadOnlyCollection <CappedLog.CappedLogMessage> > > onScrape = e =>
            {
                var cnt = e.Value.Count;
                if (e.Key == conf1)
                {
                    actualCnt1 += cnt;
                }
                else if (e.Key == conf2)
                {
                    actualCnt2 += cnt;
                }
                else if (e.Key == conf3)
                {
                    actualCnt3 += cnt;
                }
                else
                {
                    throw new NotImplementedException();
                }
            };
            var scrapeInterval = TimeSpan.FromSeconds(1);
            var scrapeProcess  = new ScrapeStub(onScrape);
            var cancel         = new CancellationTokenSource();
            var complete       = new SemaphoreSlim(0, 1);


            var start      = DateTimeOffset.UtcNow;
            var scrapeTask = Scrape(CappedLog.CappedLogScrape.CreateScrape(scope, scrapeProcess), complete, scrapeInterval, cancel.Token);

            int    cnt11 = 0, cnt12 = 0, cnt2 = 0, cnt3 = 0;
            Action insert = () =>
            {
                for (var i = 0; i < 60; ++i)
                {
                    if (Interlocked.Increment(ref cnt11) > 39 || !metric11.TryEnqueue($"message11 {i}"))
                    {
                        Interlocked.Decrement(ref cnt11);
                    }
                    if (Interlocked.Increment(ref cnt12) > 38 || !metric12.TryEnqueue($"message12 {i}"))
                    {
                        Interlocked.Decrement(ref cnt12);
                    }
                    if (Interlocked.Increment(ref cnt2) > 51 || !metric2.TryEnqueue($"message2 {i}"))
                    {
                        Interlocked.Decrement(ref cnt2);
                    }
                    if (Interlocked.Increment(ref cnt3) > 37 || !metric3.TryEnqueue($"message2 {i}"))
                    {
                        Interlocked.Decrement(ref cnt3);
                    }
                    Task.Delay(50).Wait();
                }
            };
            var tasks = new List <Task>();

            for (var i = 0; i < 4; ++i)
            {
                tasks.Add(Task.Factory.StartNew(insert));
            }

            Task.WaitAll(tasks.ToArray());
            Task.Delay(1500).Wait();
            complete.Release();
            var(successCnt, errorCnt) = scrapeTask.Result;

            var elapsed = (DateTimeOffset.UtcNow - start) / scrapeInterval + 1;

            Assert.AreEqual(errorCnt, 0);
            Assert.IsTrue(successCnt > 2);

            Assert.AreEqual(cnt11 + cnt12, actualCnt1);
            Assert.AreEqual(cnt2, actualCnt2);
            Assert.AreEqual(cnt3, actualCnt3);
        }
示例#2
0
        public void TestScrape()
        {
            CappedLog.CappedLogScope  scope;
            CappedLog.CappedLogConf   conf2, conf3, conf1;
            CappedLog.CappedLogMetric metric11, metric12, metric2, metric3;
            Init(out scope, out conf2, out conf3, out conf1, out metric11, out metric12, out metric2, out metric3);

            int errorCnt = 0, successCnt = 0;
            int actualCnt1 = 0, actualCnt2 = 0, actualCnt3 = 0;
            Action <KeyValuePair <CappedLog.CappedLogConf, IReadOnlyCollection <CappedLog.CappedLogMessage> > > onScrape = e =>
            {
                var cnt = e.Value.Count;
                if (e.Key == conf1)
                {
                    actualCnt1 += cnt;
                }
                else if (e.Key == conf2)
                {
                    actualCnt2 += cnt;
                }
                else if (e.Key == conf3)
                {
                    actualCnt3 += cnt;
                }
                else
                {
                    throw new NotImplementedException();
                }
            };
            var scrapeProcess = new ScrapeStub(onScrape);
            var cancel        = new CancellationTokenSource();
            var scrape        = new CappedLog.CappedLogScrape();

            scrape.OnError   += e => { ++errorCnt; };
            scrape.OnSuccess += s => { ++successCnt; };

            scrape
            .SetScrapeInterval(TimeSpan.FromSeconds(1))
            .SetScrape(scope, scrapeProcess);

            var scrapeTask = scrape.Start(cancel.Token);

            int    cnt11 = 0, cnt12 = 0, cnt2 = 0, cnt3 = 0;
            Action insert = () =>
            {
                for (var i = 0; i < 60; ++i)
                {
                    if (Interlocked.Increment(ref cnt11) > 39 || !metric11.TryEnqueue($"message11 {i}"))
                    {
                        Interlocked.Decrement(ref cnt11);
                    }
                    if (Interlocked.Increment(ref cnt12) > 38 || !metric12.TryEnqueue($"message12 {i}"))
                    {
                        Interlocked.Decrement(ref cnt12);
                    }
                    if (Interlocked.Increment(ref cnt2) > 51 || !metric2.TryEnqueue($"message2 {i}"))
                    {
                        Interlocked.Decrement(ref cnt2);
                    }
                    if (Interlocked.Increment(ref cnt3) > 37 || !metric3.TryEnqueue($"message2 {i}"))
                    {
                        Interlocked.Decrement(ref cnt3);
                    }
                    Task.Delay(50).Wait();
                }
            };
            var tasks = new List <Task>();

            for (var i = 0; i < 4; ++i)
            {
                tasks.Add(Task.Factory.StartNew(insert));
            }

            Task.WaitAll(tasks.ToArray());
            Task.Delay(1500).Wait();
            cancel.Cancel();

            Assert.AreEqual(errorCnt, 0);
            Assert.IsTrue(successCnt > 2);

            Assert.AreEqual(cnt11 + cnt12, actualCnt1);
            Assert.AreEqual(cnt2, actualCnt2);
            Assert.AreEqual(cnt3, actualCnt3);
        }