<commit_before>#region License

/*
* Copyright  2002-2011 the original author or authors.
* 
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* 
*      http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#endregion

#region Imports

using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Threading;
using NUnit.Framework;
using Rhino.Mocks;
using Spring.Pool.Support;
using Spring.Threading;

#endregion

namespace Spring.Pool
{
	#region Inner Class : Helper

	public class Helper
	{
		private Latch latch;
		private IObjectPool objectPool;
		private ISync sync;

		public bool gone = false;
		public object gotFromPool;

		public Helper(ISync sync, Latch latch)
		{
			Init(latch, sync, null);
		}

		public Helper(Latch latch, ISync sync, IObjectPool objectPool)
		{
			Init(latch, sync, objectPool);
		}

		public void Init(Latch latch, ISync sync, IObjectPool objectPool)
		{
			this.sync = sync;
			this.latch = latch;
			this.objectPool = objectPool;
		}

		public void Go()
		{
			latch.Release();
			sync.Acquire();
			gone = true;
			sync.Release();
		}

		public void UsePool()
		{
			gotFromPool = objectPool.BorrowObject();
			latch.Release();
		}
	}

	#endregion

	/// <summary>
	/// Unit tests for the SimplePool class.
	/// </summary>
	[TestFixture]
	public sealed class SimplePoolTests
	{
		#region Inner Class : MyFactory (IPoolableObjectFactory implementation)

	    private sealed class MyFactory : IPoolableObjectFactory
		{
			public object MakeObject()
			{
				return new object();
			}

            public void DestroyObject(object o)
			{
			}

            public bool ValidateObject(object o)
			{
				return true;
			}

			public void ActivateObject(object o)
			{
			}

            public void PassivateObject(object o)
			{
			}
		}

		#endregion

		private MockRepository mocks;
		private IPoolableObjectFactory factory;
		private SimplePool pool;

		[SetUp]
		public void SetUp()
		{
			mocks = new MockRepository();
            factory = (IPoolableObjectFactory) mocks.DynamicMock(typeof(IPoolableObjectFactory));
		    Expect.Call(factory.MakeObject()).Return(new object()).Repeat.Any();

            mocks.ReplayAll();
            pool = new SimplePool(factory, 1);

            mocks.BackToRecordAll();
		}


		[Test]
		[ExpectedException(typeof (ArgumentNullException))]
		public void InstantiateWithNullPoolableObjectFactory()
		{
			new SimplePool(null, 10);
		}

		[Test]
		[ExpectedException(typeof (ArgumentException))]
		public void InstantiateSpecifyingZeroPooledItems()
		{
			new SimplePool(factory, 0);
		}

		[Test]
		[ExpectedException(typeof (ArgumentException))]
		public void InstantiateSpecifyingNegativePooledItems()
		{
			new SimplePool(factory, -10000);
		}

		[Test]
		public void ActivateOnObjectOnBorrow()
		{
		    Expect.Call(factory.ValidateObject(null)).IgnoreArguments().Return(true).Repeat.Any();
		    factory.ActivateObject(null);
		    LastCall.IgnoreArguments();
            mocks.ReplayAll();

			Assert.AreEqual(0, pool.NumActive, "active wrong");
			Assert.AreEqual(1, pool.NumIdle, "idle wrong");
			pool.BorrowObject();
			Assert.AreEqual(1, pool.NumActive, "active wrong");
			Assert.AreEqual(0, pool.NumIdle, "idle wrong");
            mocks.VerifyAll();
		}

        // TODO fix test!!!
		[Test]
        [Ignore("Cannot figure out why this is failing?")]
		public void PassivateBusyObjectsBeforeClose()
		{
            Expect.Call(factory.ValidateObject(null)).IgnoreArguments().Return(true).Repeat.Any();
            object o = pool.BorrowObject();
			factory.PassivateObject(o);
            mocks.ReplayAll();

            pool.Close();
            mocks.VerifyAll();
        }

		[Test, ExpectedException(typeof (PoolException))]
		public void NoMoreUsableAfterClose()
		{
            object o = pool.BorrowObject();
            factory.PassivateObject(o);
            mocks.ReplayAll();

            pool.Close();
            pool.BorrowObject();
            mocks.VerifyAll();
        }

		[Test, ExpectedException(typeof (PoolException))]
		public void ThrowsExceptionWhenOutOfItemsBecauseFailedValidation()
		{
		    object o = new object();
		    Expect.Call(factory.MakeObject()).Return(o);
			Expect.Call(factory.ValidateObject(o)).Return(false);
		    mocks.ReplayAll();

			pool = new SimplePool(factory, 1);
			pool.BorrowObject();
            mocks.VerifyAll();
        }

		[Test]
		public void PassivateObjectOnReturn()
		{
            Expect.Call(factory.ValidateObject(null)).IgnoreArguments().Return(true).Repeat.Any();
            factory.PassivateObject(null);
		    LastCall.IgnoreArguments();
            mocks.ReplayAll();

			pool.ReturnObject(pool.BorrowObject());
            mocks.VerifyAll();
        }

		[Test]
		public void DestroyObjectOnClose()
		{
            Expect.Call(factory.ValidateObject(null)).IgnoreArguments().Return(true).Repeat.Any();<commit_msg>updating tests to reflect change of behavior introduced by fix to SPRNET-1582<commit_after>#region License

/*
* Copyright  2002-2011 the original author or authors.
* 
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* 
*      http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#endregion

#region Imports

using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Threading;
using NUnit.Framework;
using Rhino.Mocks;
using Spring.Pool.Support;
using Spring.Threading;

#endregion

namespace Spring.Pool
{
	#region Inner Class : Helper

	public class Helper
	{
		private Latch latch;
		private IObjectPool objectPool;
		private ISync sync;

		public bool gone = false;
		public object gotFromPool;

		public Helper(ISync sync, Latch latch)
		{
			Init(latch, sync, null);
		}

		public Helper(Latch latch, ISync sync, IObjectPool objectPool)
		{
			Init(latch, sync, objectPool);
		}

		public void Init(Latch latch, ISync sync, IObjectPool objectPool)
		{
			this.sync = sync;
			this.latch = latch;
			this.objectPool = objectPool;
		}

		public void Go()
		{
			latch.Release();
			sync.Acquire();
			gone = true;
			sync.Release();
		}

		public void UsePool()
		{
			gotFromPool = objectPool.BorrowObject();
			latch.Release();
		}
	}

	#endregion

	/// <summary>
	/// Unit tests for the SimplePool class.
	/// </summary>
	[TestFixture]
	public sealed class SimplePoolTests
	{
		#region Inner Class : MyFactory (IPoolableObjectFactory implementation)

	    private sealed class MyFactory : IPoolableObjectFactory
		{
			public object MakeObject()
			{
				return new object();
			}

            public void DestroyObject(object o)
			{
			}

            public bool ValidateObject(object o)
			{
				return true;
			}

			public void ActivateObject(object o)
			{
			}

            public void PassivateObject(object o)
			{
			}
		}

		#endregion

		private MockRepository mocks;
		private IPoolableObjectFactory factory;
		private SimplePool pool;

		[SetUp]
		public void SetUp()
		{
			mocks = new MockRepository();
            factory = (IPoolableObjectFactory) mocks.DynamicMock(typeof(IPoolableObjectFactory));
		    Expect.Call(factory.MakeObject()).Return(new object()).Repeat.Any();

            mocks.ReplayAll();
            pool = new SimplePool(factory, 1);

            mocks.BackToRecordAll();
		}


		[Test]
		[ExpectedException(typeof (ArgumentNullException))]
		public void InstantiateWithNullPoolableObjectFactory()
		{
			new SimplePool(null, 10);
		}

		[Test]
		[ExpectedException(typeof (ArgumentException))]
		public void InstantiateSpecifyingZeroPooledItems()
		{
			new SimplePool(factory, 0);
		}

		[Test]
		[ExpectedException(typeof (ArgumentException))]
		public void InstantiateSpecifyingNegativePooledItems()
		{
			new SimplePool(factory, -10000);
		}

		[Test]
		public void ActivateOnObjectOnBorrow()
		{
		    Expect.Call(factory.ValidateObject(null)).IgnoreArguments().Return(true).Repeat.Any();
		    factory.ActivateObject(null);
		    LastCall.IgnoreArguments();
            mocks.ReplayAll();

			Assert.AreEqual(0, pool.NumActive, "active wrong");
			Assert.AreEqual(1, pool.NumIdle, "idle wrong");
			pool.BorrowObject();
			Assert.AreEqual(1, pool.NumActive, "active wrong");
			Assert.AreEqual(0, pool.NumIdle, "idle wrong");
            mocks.VerifyAll();
		}

        // TODO fix test!!!
		[Test]
        [Ignore("Cannot figure out why this is failing?")]
		public void PassivateBusyObjectsBeforeClose()
		{
            Expect.Call(factory.ValidateObject(null)).IgnoreArguments().Return(true).Repeat.Any();
            object o = pool.BorrowObject();
			factory.PassivateObject(o);
            mocks.ReplayAll();

            pool.Close();
            mocks.VerifyAll();
        }

		[Test, ExpectedException(typeof (PoolException))]
        [Ignore("No longer expected behavior per SPRNET-1582 being resolved.")]
		public void NoMoreUsableAfterClose()
		{
            object o = pool.BorrowObject();
            factory.PassivateObject(o);
            mocks.ReplayAll();

            pool.Close();
            pool.BorrowObject();
            mocks.VerifyAll();
        }

		[Test, ExpectedException(typeof (PoolException))]
        [Ignore("No longer expected behavior per SPRNET-1582 being resolved.")]
        public void ThrowsExceptionWhenOutOfItemsBecauseFailedValidation()
		{
		    object o = new object();
		    Expect.Call(factory.MakeObject()).Return(o);
			Expect.Call(factory.ValidateObject(o)).Return(false);
		    mocks.ReplayAll();

			pool = new SimplePool(factory, 1);
			pool.BorrowObject();
            mocks.VerifyAll();
        }

		[Test]
		public void PassivateObjectOnReturn()
		{
            Expect.Call(factory.ValidateObject(null)).IgnoreArguments().Return(true).Repeat.Any();
            factory.PassivateObject(null);
		    LastCall.IgnoreArguments();
            mocks.ReplayAll();

			pool.ReturnObject(pool.BorrowObject());
            mocks.VerifyAll();
        }

		[Test]
		public void DestroyObjectOnClose()
		{
            Expect.Call(factory.ValidateObject(null)).IgnoreArguments().Return(true).Repeat.Any();
{'fraction_non_alphanumeric': 0.10922907892327413, 'fraction_numerical': 0.0058597326496978575, 'mean_word_length': 3.120331950207469, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5394337', 'n_tokens_mistral': 3627, 'n_tokens_neox': 3206, 'n_words': 782}