Skip to content

Commit 9829373

Browse files
committed
connection pool tests
1 parent 0975984 commit 9829373

File tree

6 files changed

+173
-9
lines changed

6 files changed

+173
-9
lines changed

Beanstalk.Client.Test/Beanstalk.Client.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
</ItemGroup>
5959
<ItemGroup>
6060
<Compile Include="BeanstalkProtocolTests.cs" />
61+
<Compile Include="ConnectionPoolTests.cs" />
6162
<Compile Include="ExtensionTests.cs" />
6263
<Compile Include="MockSocket.cs" />
6364
<Compile Include="Properties\AssemblyInfo.cs" />
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* libBeanstalk.NET
3+
* Copyright (C) 2010 Arne F. Claassen
4+
* geekblog [at] claassen [dot] net
5+
* http://github.com/sdether/libBeanstalk.NET
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
using System;
20+
using System.Collections.Generic;
21+
using System.Net;
22+
using Droog.Beanstalk.Client.Net;
23+
using NUnit.Framework;
24+
25+
namespace Droog.Beanstalk.Client.Test {
26+
27+
[TestFixture]
28+
public class ConnectionPoolTests {
29+
30+
[Test]
31+
public void Same_ipaddress_port_gets_same_pool() {
32+
var pool = ConnectionPool.GetPool(new IPAddress(12345), 123);
33+
Assert.AreSame(pool, ConnectionPool.GetPool(new IPAddress(12345), 123));
34+
}
35+
36+
[Test]
37+
public void Different_ipaddress_port_gets_differnt_pool() {
38+
var pool = ConnectionPool.GetPool(new IPAddress(12345), 123);
39+
Assert.AreNotSame(pool, ConnectionPool.GetPool(new IPAddress(234234), 123));
40+
}
41+
42+
[Test]
43+
public void Same_host_port_gets_same_pool() {
44+
var pool = ConnectionPool.GetPool("foo", 123);
45+
Assert.AreSame(pool, ConnectionPool.GetPool("foo", 123));
46+
}
47+
48+
[Test]
49+
public void Different_host_port_gets_differnt_pool() {
50+
var pool = ConnectionPool.GetPool("foo", 123);
51+
Assert.AreNotSame(pool, ConnectionPool.GetPool("bob", 123));
52+
}
53+
54+
[Test]
55+
56+
public void Too_many_busy_connections_throws() {
57+
Func<ISocket> socketFactory = () => new MockSocket();
58+
var pool = new ConnectionPool(socketFactory);
59+
pool.MaxConnections = 5;
60+
var s1 = pool.GetSocket();
61+
var s2 = pool.GetSocket();
62+
var s3 = pool.GetSocket();
63+
var s4 = pool.GetSocket();
64+
var s5 = pool.GetSocket();
65+
try {
66+
pool.GetSocket();
67+
Assert.Fail("didn't throw");
68+
} catch(PoolExhaustedException) {
69+
return;
70+
} catch(Exception e) {
71+
Assert.Fail(string.Format("threw {0} instead of PoolExhaustedException", e));
72+
}
73+
}
74+
75+
[Test]
76+
public void Disposing_pool_socket_returns_it_to_the_pool() {
77+
var sockets = new List<MockSocket>();
78+
Func<ISocket> socketFactory = () => {
79+
var socket = new MockSocket();
80+
sockets.Add(socket);
81+
return socket;
82+
};
83+
var pool = new ConnectionPool(socketFactory);
84+
var s1 = pool.GetSocket();
85+
Assert.AreEqual(1, sockets.Count);
86+
s1.Dispose();
87+
var s2 = pool.GetSocket();
88+
Assert.AreEqual(1, sockets.Count);
89+
}
90+
91+
[Test]
92+
public void Disposing_pool_socket_does_not_dispose_wrapped_socket() {
93+
var sockets = new List<MockSocket>();
94+
Func<ISocket> socketFactory = () => {
95+
var socket = new MockSocket();
96+
sockets.Add(socket);
97+
return socket;
98+
};
99+
var pool = new ConnectionPool(socketFactory);
100+
var s1 = pool.GetSocket();
101+
Assert.AreEqual(1, sockets.Count);
102+
s1.Dispose();
103+
Assert.IsFalse(s1.Connected);
104+
Assert.IsTrue(sockets[0].Connected);
105+
}
106+
107+
[Test]
108+
public void Getting_multiple_pool_sockets_returns_different_instances() {
109+
var sockets = new List<MockSocket>();
110+
Func<ISocket> socketFactory = () => {
111+
var socket = new MockSocket();
112+
sockets.Add(socket);
113+
return socket;
114+
};
115+
var pool = new ConnectionPool(socketFactory);
116+
var s1 = pool.GetSocket();
117+
var s2 = pool.GetSocket();
118+
Assert.AreEqual(2, sockets.Count);
119+
}
120+
121+
[Test]
122+
public void Disconnected_sockets_are_removed_from_pool() {
123+
var sockets = new List<MockSocket>();
124+
Func<ISocket> socketFactory = () => {
125+
var socket = new MockSocket();
126+
sockets.Add(socket);
127+
return socket;
128+
};
129+
var pool = new ConnectionPool(socketFactory);
130+
var s1 = pool.GetSocket();
131+
sockets[0].Dispose();
132+
s1.Dispose();
133+
var s2 = pool.GetSocket();
134+
Assert.AreEqual(2, sockets.Count);
135+
}
136+
}
137+
}

Beanstalk.Client.Test/MockSocket.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public class MockSocket : ISocket {
2929
private Queue<MemoryStream> _receivedData = new Queue<MemoryStream>();
3030
private Queue<string> _sent = new Queue<string>();
3131

32+
public MockSocket() {
33+
Connected = true;
34+
}
35+
3236
public void Dispose() {
3337
CloseCalled++;
3438
Connected = false;

Beanstalk.Client/Beanstalk.Client.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<Compile Include="Net\ConnectionPool.cs" />
5353
<Compile Include="Net\IConnectionPool.cs" />
5454
<Compile Include="Net\ISocket.cs" />
55+
<Compile Include="Net\PoolExhaustedException.cs" />
5556
<Compile Include="Net\PoolSocket.cs" />
5657
<Compile Include="Protocol\IWatchedTubeClient.cs" />
5758
<Compile Include="IWatchedTubeCollection.cs" />

Beanstalk.Client/Net/ConnectionPool.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class ConnectionPool : IConnectionPool {
2929

3030
public static readonly TimeSpan DefaultConnectTimeout = TimeSpan.FromSeconds(10);
3131
public static readonly int DefaultMaxConnections = 100;
32-
private static Dictionary<string, ConnectionPool> _pools = new Dictionary<string, ConnectionPool>();
32+
private static readonly Dictionary<string, ConnectionPool> _pools = new Dictionary<string, ConnectionPool>();
3333

3434
public static ConnectionPool GetPool(IPAddress address, int port) {
3535
lock(_pools) {
@@ -112,12 +112,4 @@ private void Reclaim(ISocket socket) {
112112
}
113113
}
114114
}
115-
116-
public class PoolExhaustedException : Exception {
117-
public readonly int MaxConnections;
118-
119-
public PoolExhaustedException(int maxConnections) {
120-
MaxConnections = maxConnections;
121-
}
122-
}
123115
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* libBeanstalk.NET
3+
* Copyright (C) 2010 Arne F. Claassen
4+
* geekblog [at] claassen [dot] net
5+
* http://github.com/sdether/libBeanstalk.NET
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
using System;
20+
21+
namespace Droog.Beanstalk.Client.Net {
22+
public class PoolExhaustedException : Exception {
23+
public readonly int MaxConnections;
24+
25+
public PoolExhaustedException(int maxConnections) {
26+
MaxConnections = maxConnections;
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)