GetSocket() public method

public GetSocket ( ) : ISocket
return ISocket
 public void When_pool_has_too_many_connections_and_gc_has_run_unreferenced_busy_sockets_are_collected() {
     Func<ISocket> socketFactory = () => new MockSocket();
     var pool = new ConnectionPool(socketFactory) { MaxConnections = 5 };
     var s1 = pool.GetSocket();
     var s2 = pool.GetSocket();
     var s3 = pool.GetSocket();
     var s4 = pool.GetSocket();
     var s5 = pool.GetSocket();
     s5 = null;
     GC.Collect();
     var s6 = pool.GetSocket();
     Assert.IsNotNull(s6);
 }
 public void Too_many_busy_connections_throws() {
     Func<ISocket> socketFactory = () => new MockSocket();
     var pool = new ConnectionPool(socketFactory) { MaxConnections = 5 };
     var s1 = pool.GetSocket();
     var s2 = pool.GetSocket();
     var s3 = pool.GetSocket();
     var s4 = pool.GetSocket();
     var s5 = pool.GetSocket();
     try {
         pool.GetSocket();
         Assert.Fail("didn't throw");
     } catch(PoolExhaustedException) {
         return;
     } catch(Exception e) {
         Assert.Fail(string.Format("threw {0} instead of PoolExhaustedException", e));
     }
 }
 public void Disposing_pool_socket_returns_it_to_the_pool() {
     var sockets = new List<MockSocket>();
     Func<ISocket> socketFactory = () => {
         var socket = new MockSocket();
         sockets.Add(socket);
         return socket;
     };
     var pool = new ConnectionPool(socketFactory);
     var s1 = pool.GetSocket();
     Assert.AreEqual(1, sockets.Count);
     s1.Dispose();
     var s2 = pool.GetSocket();
     Assert.AreEqual(1, sockets.Count);
 }
 public void Disconnected_socket_in_available_pool_is_disposed_on_attempted_reuse() {
     var sockets = new List<MockSocket>();
     Func<ISocket> socketFactory = () => {
         var socket = new MockSocket();
         sockets.Add(socket);
         return socket;
     };
     var pool = new ConnectionPool(socketFactory);
     var s1 = pool.GetSocket();
     s1.Dispose();
     Assert.AreEqual(1, sockets.Count);
     sockets[0].Dispose();
     var s2 = pool.GetSocket();
     Assert.AreEqual(2, sockets.Count);
 }
 public void Idle_available_socket_is_collected_at_cleanup() {
     var sockets = new List<MockSocket>();
     Func<ISocket> socketFactory = () => {
         var socket = new MockSocket();
         sockets.Add(socket);
         return socket;
     };
     var pool = new ConnectionPool(socketFactory) { CleanupInterval = TimeSpan.FromSeconds(1) };
     pool.GetSocket().Dispose();
     Assert.AreEqual(0, sockets[0].DisposeCalled);
     Wait(() => sockets[0].DisposeCalled > 0, TimeSpan.FromSeconds(5), "socket didn't get cleaned up");
 }
 public void Disconnected_sockets_are_removed_from_pool() {
     var sockets = new List<MockSocket>();
     Func<ISocket> socketFactory = () => {
         var socket = new MockSocket();
         sockets.Add(socket);
         return socket;
     };
     var pool = new ConnectionPool(socketFactory);
     var s1 = pool.GetSocket();
     sockets[0].Dispose();
     s1.Dispose();
     var s2 = pool.GetSocket();
     Assert.AreEqual(2, sockets.Count);
 }
 public void Getting_multiple_pool_sockets_returns_different_instances() {
     var sockets = new List<MockSocket>();
     Func<ISocket> socketFactory = () => {
         var socket = new MockSocket();
         sockets.Add(socket);
         return socket;
     };
     var pool = new ConnectionPool(socketFactory);
     var s1 = pool.GetSocket();
     var s2 = pool.GetSocket();
     Assert.AreEqual(2, sockets.Count);
 }
 public void Disposing_pool_socket_does_not_dispose_wrapped_socket() {
     var sockets = new List<MockSocket>();
     Func<ISocket> socketFactory = () => {
         var socket = new MockSocket();
         sockets.Add(socket);
         return socket;
     };
     var pool = new ConnectionPool(socketFactory);
     var s1 = pool.GetSocket();
     Assert.AreEqual(1, sockets.Count);
     s1.Dispose();
     Assert.IsFalse(s1.Connected);
     Assert.IsTrue(sockets[0].Connected);
 }
 public void Disposing_pool_does_not_affect_busy_sockets() {
     var sockets = new List<FakeDisposableSocket>();
     Func<ISocket> socketFactory = () => {
         var socket = new FakeDisposableSocket();
         sockets.Add(socket);
         return socket;
     };
     var pool = new ConnectionPool(socketFactory);
     var s1 = pool.GetSocket();
     Assert.AreEqual(1, sockets.Count, "wrong number of sockets created");
     Assert.IsFalse(sockets[0].Disposed, "socket was disposed");
     pool.Dispose();
     Assert.IsFalse(sockets[0].Disposed, "socket was disposed");
     s1.Dispose();
 }
Exemplo n.º 10
0
 public void Disposing_pool_cleans_up_sockets() {
     var sockets = new List<FakeDisposableSocket>();
     Func<ISocket> socketFactory = () => {
         var socket = new FakeDisposableSocket();
         sockets.Add(socket);
         return socket;
     };
     var pool = new ConnectionPool(socketFactory);
     var s1 = pool.GetSocket();
     var s2 = pool.GetSocket();
     s1.Dispose();
     s2.Dispose();
     Assert.AreEqual(2, sockets.Count, "wrong number of sockets created");
     Assert.IsFalse(sockets[0].Disposed, "first socket was disposed");
     Assert.IsFalse(sockets[1].Disposed, "second socket was disposed");
     pool.Dispose();
     Assert.IsTrue(sockets[0].Disposed, "first socket was not disposed");
     Assert.IsTrue(sockets[1].Disposed, "second socket was not disposed");
 }