1 #region Copyright notice and license 2 3 // Copyright 2017 gRPC authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 #endregion 18 19 using System; 20 using Grpc.Core; 21 using Grpc.Core.Internal; 22 using Grpc.Core.Utils; 23 using NUnit.Framework; 24 25 namespace Grpc.Core.Internal.Tests 26 { 27 public class DefaultObjectPoolTest 28 { 29 [Test] 30 [TestCase(10, 2)] 31 [TestCase(10, 1)] 32 [TestCase(0, 2)] 33 [TestCase(2, 0)] ObjectIsReused(int sharedCapacity, int threadLocalCapacity)34 public void ObjectIsReused(int sharedCapacity, int threadLocalCapacity) 35 { 36 var pool = new DefaultObjectPool<TestPooledObject>(() => new TestPooledObject(), sharedCapacity, threadLocalCapacity); 37 var origLeased = pool.Lease(); 38 pool.Return(origLeased); 39 Assert.AreSame(origLeased, pool.Lease()); 40 Assert.AreNotSame(origLeased, pool.Lease()); 41 } 42 43 [Test] ZeroCapacities()44 public void ZeroCapacities() 45 { 46 var pool = new DefaultObjectPool<TestPooledObject>(() => new TestPooledObject(), 0, 0); 47 var origLeased = pool.Lease(); 48 pool.Return(origLeased); 49 Assert.AreNotSame(origLeased, pool.Lease()); 50 } 51 52 [Test] DisposeCleansSharedPool()53 public void DisposeCleansSharedPool() 54 { 55 var pool = new DefaultObjectPool<TestPooledObject>(() => new TestPooledObject(), 10, 0); 56 var origLeased = pool.Lease(); 57 pool.Return(origLeased); 58 pool.Dispose(); 59 Assert.AreNotSame(origLeased, pool.Lease()); 60 } 61 62 [Test] LeaseSetsReturnAction()63 public void LeaseSetsReturnAction() 64 { 65 var pool = new DefaultObjectPool<TestPooledObject>(() => new TestPooledObject(), 10, 0); 66 var origLeased = pool.Lease(); 67 origLeased.ReturnAction(origLeased); 68 pool.Dispose(); 69 Assert.AreNotSame(origLeased, pool.Lease()); 70 } 71 72 [Test] Constructor()73 public void Constructor() 74 { 75 Assert.Throws<ArgumentNullException>(() => new DefaultObjectPool<TestPooledObject>(null, 10, 2)); 76 Assert.Throws<ArgumentException>(() => new DefaultObjectPool<TestPooledObject>(() => new TestPooledObject(), -1, 10)); 77 Assert.Throws<ArgumentException>(() => new DefaultObjectPool<TestPooledObject>(() => new TestPooledObject(), 10, -1)); 78 } 79 80 class TestPooledObject : IPooledObject<TestPooledObject> 81 { 82 public Action<TestPooledObject> ReturnAction; 83 SetReturnToPoolAction(Action<TestPooledObject> returnAction)84 public void SetReturnToPoolAction(Action<TestPooledObject> returnAction) 85 { 86 this.ReturnAction = returnAction; 87 } 88 Dispose()89 public void Dispose() 90 { 91 92 } 93 } 94 } 95 } 96