本文整理汇总了C#中Pool.Acquire方法的典型用法代码示例。如果您正苦于以下问题:C# Pool.Acquire方法的具体用法?C# Pool.Acquire怎么用?C# Pool.Acquire使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Pool
的用法示例。
在下文中一共展示了Pool.Acquire方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GIVEN_failed_consumer_THEN_it_is_not_returned_to_the_pool
public void GIVEN_failed_consumer_THEN_it_is_not_returned_to_the_pool()
{
var pool = new Pool<XmsPooledConsumer>(1, p => CreateFailingStub(p), store);
Assert.That(store.Count, Is.EqualTo(0));
XmsPooledConsumer notExpected;
XmsPooledConsumer actual;
using (var consumer = pool.Acquire())
{
Assert.That(store.Count, Is.EqualTo(0));
notExpected = consumer;
Assert.Throws<TestException>(() => consumer.ReceiveNoWait());
}
Assert.That(store.Count, Is.EqualTo(0));
using (var consumer = pool.Acquire())
{
actual = consumer;
}
Assert.That(store.Count, Is.EqualTo(1));
Assert.That(actual, Is.Not.SameAs(notExpected));
}
示例2: Test_ReleaseTwiceWithAcquire
public void Test_ReleaseTwiceWithAcquire()
{
var obj = new object();
var pool = new Pool<object>(() => obj, AccessStrategy.LIFO, LoadingStrategy.Eager, 1)
.AttachDebugger(new ReleaseDebugger<object>(state => { throw new InvalidOperationException(); }));
pool.Release(pool.Acquire());
pool.Release(pool.Acquire());
}
示例3: Test_ReleaseEqualObjects
public void Test_ReleaseEqualObjects()
{
var pool = new Pool<string>(() => new string(new[] { 'a', 'b', 'c' }), AccessStrategy.LIFO, LoadingStrategy.Eager, 2)
.AttachDebugger(new ReleaseDebugger<string>(state => { throw new InvalidOperationException(); }));
string s1 = pool.Acquire();
string s2 = pool.Acquire();
Assert.AreEqual(s1, s2);
Assert.AreEqual(s1.GetHashCode(), s2.GetHashCode());
pool.Release(s1);
pool.Release(s2);
Assert.AreEqual(2, pool.Allocated);
Assert.AreEqual(2, pool.Available);
}
示例4: Test_ReleaseGarbage
public void Test_ReleaseGarbage()
{
var pool = new Pool<object>(() => new object(), AccessStrategy.LIFO, LoadingStrategy.Eager, 1)
.AttachDebugger(new ReleaseDebugger<object>(state => { throw new InvalidOperationException(); }));
pool.Acquire();
Assert.Throws<InvalidOperationException>(() => pool.Release(new object()));
}
示例5: Test_Dispose_4
public void Test_Dispose_4()
{
Disposable disposable;
PoolResourceHolder<Disposable> poolResourceHolder;
var pool = new Pool<Disposable>(() => new Disposable(), AccessStrategy.LIFO, LoadingStrategy.Eager, 1);
pool.Dispose();
Assert.Throws<ObjectDisposedException>(() => pool.Acquire());
Assert.Throws<ObjectDisposedException>(() => pool.AcquireHolder());
Assert.Throws<ObjectDisposedException>(() => pool.TryAcquire(TimeSpan.FromDays(1), out disposable));
Assert.Throws<ObjectDisposedException>(() => pool.TryAcquireHolder(TimeSpan.FromDays(1), out poolResourceHolder));
Assert.Throws<ObjectDisposedException>(() => pool.TryAcquireImmediately(out disposable));
Assert.Throws<ObjectDisposedException>(() => pool.TryAcquireHolderImmediately(out poolResourceHolder));
// Асинхронные методы.
try
{
pool.AcquireAsync().Wait();
Assert.Fail("Must throw exception.");
}
catch (AggregateException error)
{
Assert.True(error.InnerExceptions.Single() is ObjectDisposedException);
}
try
{
pool.AcquireHolderAsync().Wait();
Assert.Fail("Must throw exception.");
}
catch (AggregateException error)
{
Assert.True(error.InnerExceptions.Single() is ObjectDisposedException);
}
}
示例6: Test_Lazy_MinimalAllocations
public void Test_Lazy_MinimalAllocations()
{
var pool = new Pool<object>(() => new object(), AccessStrategy.LIFO, LoadingStrategy.Lazy, 3);
for (int i = 0; i < 100; i++)
pool.Release(pool.Acquire());
Assert.AreEqual(1, pool.Allocated);
Assert.AreEqual(1, pool.Available);
}
示例7: ShouldReturnInstanceIntoThePoolAndNextCallerShouldGetIt
public void ShouldReturnInstanceIntoThePoolAndNextCallerShouldGetIt()
{
var pool = new Pool<XmsPooledProducer>(1, p => CreateFailingStub(p), store);
Assert.That(store.Count, Is.EqualTo(0));
IXmsProducer expected;
IXmsProducer actual;
using (var producer = pool.Acquire())
{
expected = producer;
}
using (var producer = pool.Acquire())
{
actual = producer;
}
Assert.That(store.Count, Is.EqualTo(1));
Assert.That(actual, Is.SameAs(expected));
}
示例8: Test_Eager
public void Test_Eager()
{
var pool = new Pool<object>(() => new object(), AccessStrategy.LIFO, LoadingStrategy.Eager, 3);
Assert.AreEqual(3, pool.Allocated);
Assert.AreEqual(3, pool.Available);
pool.Acquire();
Assert.AreEqual(3, pool.Allocated);
Assert.AreEqual(2, pool.Available);
}
示例9: Test_LazyExpanding
public void Test_LazyExpanding()
{
var pool = new Pool<object>(() => new object(), AccessStrategy.LIFO, LoadingStrategy.LazyExpanding, 3);
Assert.AreEqual(0, pool.Allocated);
Assert.AreEqual(0, pool.Available);
object o1 = pool.Acquire();
object o2 = pool.Acquire();
Assert.AreEqual(2, pool.Allocated);
Assert.AreEqual(0, pool.Available);
pool.Release(o1);
pool.Release(o2);
Assert.AreEqual(2, pool.Allocated);
Assert.AreEqual(2, pool.Available);
pool.Acquire();
pool.Acquire();
Assert.AreEqual(3, pool.Allocated);
Assert.AreEqual(1, pool.Available);
}
示例10: ShouldNotReturnFaultyInstanceIntoThePoolAndWeCanGetNewOne
public void ShouldNotReturnFaultyInstanceIntoThePoolAndWeCanGetNewOne()
{
var pool = new Pool<XmsPooledProducer>(1, p => CreateFailingStub(p), store);
Assert.That(store.Count, Is.EqualTo(0));
IXmsProducer notExpected;
IXmsProducer actual;
using (var producer = pool.Acquire())
{
notExpected = producer;
Assert.Throws<TestException>(() => producer.Send(null));
}
using (var producer = pool.Acquire())
{
actual = producer;
}
Assert.That(store.Count, Is.EqualTo(1));
Assert.That(actual, Is.Not.SameAs(notExpected));
}
示例11: Test_Speed
public void Test_Speed()
{
var pool = new Pool<object>(() => new object(), AccessStrategy.LIFO, LoadingStrategy.Eager, 1);
long cycles = 0;
TimeSpan testTime = TimeSpan.FromSeconds(1);
var sw = Stopwatch.StartNew();
while (sw.Elapsed < testTime)
{
pool.Release(pool.Acquire());
cycles++;
}
Console.Out.WriteLine("Done {0} Acquire-Release cycles in 1 second.", cycles);
}
示例12: GIVEN_non_transactional_pool_WHEN_send_THEN_message_is_enqueued
public void GIVEN_non_transactional_pool_WHEN_send_THEN_message_is_enqueued()
{
using (var producer = new XmsProducer(address, false))
producer.SendTestMessage(address);
IBM.XMS.IMessage message;
using (var pool = new Pool<XmsPooledConsumer>(2, p => new XmsPooledConsumer(p, new XmsConsumer(address, false)), store))
using (var producer = pool.Acquire())
{
message = producer.ReceiveNoWait();
}
Assert.That(message,Is.Not.Null);
}
示例13: GIVEN_existing_producer_in_pool_WHEN_aquiring_producer_THEN_the_existing_producer_is_returned
public void GIVEN_existing_producer_in_pool_WHEN_aquiring_producer_THEN_the_existing_producer_is_returned()
{
var pool = new Pool<XmsPooledProducer>(1, p => CreateFailingStub(p), store);
Assert.That(store.Count, Is.EqualTo(0));
XmsPooledProducer expected;
XmsPooledProducer actual;
using (var producer = pool.Acquire())
{
Assert.That(store.Count, Is.EqualTo(0));
expected = producer;
}
Assert.That(store.Count, Is.EqualTo(1));
using (var producer = pool.Acquire())
{
actual = producer;
}
Assert.That(store.Count, Is.EqualTo(1));
Assert.That(actual, Is.SameAs(expected));
}
示例14: Main
static void Main(string[] args)
{
using (Pool<IFoo> pool = new Pool<IFoo>(PoolSize, p => new PooledFoo(p),
LoadingMode.Eager, AccessMode.Fifo))
{
using (ManualResetEvent finishedEvent = new ManualResetEvent(false))
{
int remaining = 10;
for (int i = 0; i < 10; i++)
{
int q = i;
ThreadPool.QueueUserWorkItem(s =>
{
Console.WriteLine("Thread started: {0}", q);
for (int j = 0; j < 50; j++)
{
using (IFoo foo = pool.Acquire())
using (IFoo foo2 = pool.Acquire())
{
foo.Test();
foo2.Test();
}
}
if (Interlocked.Decrement(ref remaining) == 0)
{
finishedEvent.Set();
}
});
}
finishedEvent.WaitOne();
}
Console.WriteLine("Threaded partial load test finished.");
Console.WriteLine();
}
Console.ReadLine();
}
示例15: TestRelease
public void TestRelease()
{
_pool = new Pool<Connection>(10, _factory, LoadingMode.Lazy, AccessMode.FIFO);
var connection = _pool.Acquire();
Assert.IsNotNull(connection);
_pool.Release(connection);
}