示例#1
0
        public void Parallel_Load_On_Pool()
        {
            var policy = new PoolPolicy <ObjectPoolClassesTests.SomePoolObject>
            {
                FunctionOnCreate = () => new ObjectPoolClassesTests.SomePoolObject {
                    Value = "created"
                },
                ActionOnGet     = o => o.Value = "get",
                ActionOnReturn  = o => o.Value = "returned",
                ActionOnDestroy = o => o.Value = "destroyed",
                MaximumPoolSize = 100,
                InitialPoolSize = 1
            };

            var options = new ParallelOptions {
                MaxDegreeOfParallelism = 10
            };
            var pool = new ObjectPoolConcurrent <ObjectPoolClassesTests.SomePoolObject>(policy)
            {
                IsPoolingEnabled = true
            };

            Assert.That(() =>
                        Parallel.For(0L, 1000, options, (i, loopState) =>
            {
                var someObject = pool.Get();
                pool.Return(someObject);
            }), Throws.Nothing);
            Assert.That(pool.CountActive, Is.EqualTo(0));
            Assert.That(pool.CountInactive, Is.GreaterThan(0));
        }
示例#2
0
        void _LogHex(string tag, eLogLevel logLevel, Span <byte> bytes)
        {
            // ref: https://github.com/dotnet/runtime/blob/master/src/libraries/System.Private.CoreLib/src/System/BitConverter.cs#L364
            if (!IsValidLogLevel(logLevel))
            {
                return;
            }

            mStringBuilderPool.TryTake(out StringBuilder sb);
            sb.Clear();
            sb.Append("Log Hex\n");
            int acc = 0;

            for (int i = 0; i < bytes.Length; ++i)
            {
                sb.Append(bytes[i].ToString("X2"));
                if (++acc == 4)
                {
                    acc = 0;
                    sb.Append(" ");
                }
            }
            string message = sb.ToString();

            mStringBuilderPool.Return(sb);

            __Log(new LogInfo
            {
                DateTime      = DateTime.Now,
                SequnceNumber = Interlocked.Increment(ref mSequenceNumber),
                Tag           = tag,
                ThreadID      = Thread.CurrentThread.ManagedThreadId,
                LogLevel      = logLevel,
                Message       = message,
                Exception     = null
            });
        }