Permalink
Browse files

Fixing issue #74. Clean up the _serverQueue on blacklist; use only _s…

…erverQueue.Count to determine HasNext.
  • Loading branch information...
1 parent 3a2ce28 commit 149430c5827ee1b3af9f8ba399321a446a04c84f @dlbromen dlbromen committed Oct 11, 2012
@@ -28,25 +28,20 @@ private bool IsBlackListed(Server server)
public bool HasNext
{
- get { lock (_lock) { return (_serverQueue.Count - _blackListed.Count) > 0; } }
+ get { lock (_lock) { return _serverQueue.Count > 0; } }
}
public Server Next()
{
- Server server;
+ Server server = null;
lock (_lock)
{
- do
- {
- server = _serverQueue.Dequeue();
-
- if (IsBlackListed(server))
- server = null;
- else
- _serverQueue.Enqueue(server);
- }
- while (_serverQueue.Count > 0 && server == null);
+ if (_serverQueue.Count > 0)
+ {
+ server = _serverQueue.Dequeue();
+ _serverQueue.Enqueue(server);
+ }
}
return server;
@@ -72,7 +67,17 @@ public void BlackList(Server server)
Debug.WriteLine(server + " has been blacklisted", "connection");
lock (_lock)
{
- _blackListed.Add(server);
+ if (_blackListed.Add(server))
+ {
+ _serverQueue.Clear();
+ foreach (Server srv in _servers)
+ {
+ if(!IsBlackListed(srv))
+ {
+ _serverQueue.Enqueue(srv);
+ }
+ }
+ }
}
}
@@ -0,0 +1,82 @@
+using System;
+using System.Collections.Generic;
+using Xunit;
+
+namespace FluentCassandra.Connections.Tests
+{
+ public class RoundRobinServerManagerTests
+ {
+ [Fact]
+ public void CanBlackListAndCleanQueueTest()
+ {
+ RoundRobinServerManager target = new RoundRobinServerManager(new ConnectionBuilder("Server=unit-test-1,unit-test-2,unit-test-3"));
+
+ Server srv = new Server("unit-test-4");
+ target.Add(srv);
+
+ bool gotServer4 = false;
+
+ for (int i = 0; i < 4; i++)
+ {
+ Server server = target.Next();
+ if (server.ToString().Equals(srv.ToString(), StringComparison.OrdinalIgnoreCase))
+ {
+ gotServer4 = true;
+ break;
+ }
+ }
+
+ Assert.True(gotServer4);
+
+ target.BlackList(srv);
+
+ gotServer4 = false;
+ for (int i = 0; i < 4; i++)
+ {
+ Server server = target.Next();
+ if (server.Equals(srv))
+ {
+ gotServer4 = true;
+ break;
+ }
+ }
+
+ Assert.False(gotServer4);
+ }
+
+ [Fact]
+ public void HasNextWithMoreThanHalfBlacklistedTest()
+ {
+ RoundRobinServerManager target = new RoundRobinServerManager(new ConnectionBuilder("Server=unit-test-1"));
+
+ Server srv1 = null;
+ Server srv2 = new Server("unit-test-2");
+ Server srv3 = new Server("unit-test-3");
+ Server srv4 = new Server("unit-test-4");
+ target.Add(srv2);
+ target.Add(srv3);
+ target.Add(srv4);
+ List<Server> servers = new List<Server> { new Server("unit-test-1"), srv2, srv3, srv4 };
+
+ for (int i = 0; i < 4; i++)
+ {
+ Server srv = target.Next();
+ Assert.True(servers[i].ToString().Equals(srv.ToString(), StringComparison.OrdinalIgnoreCase));
+ if(i == 0)
+ {
+ srv1 = srv;
+ }
+ }
+
+ target.BlackList(srv2);
+ target.BlackList(srv3);
+ Assert.True(target.HasNext);
+
+ target.BlackList(srv1);
+ Assert.True(target.HasNext);
+
+ target.BlackList(srv4);
+ Assert.False(target.HasNext);
+ }
+ }
+}
@@ -62,6 +62,7 @@
<Compile Include="Connections\ConnectionBuilderTests.cs" />
<Compile Include="Connections\ConnectionProviderTests.cs" />
<Compile Include="Connections\NormalConnectionProviderTests.cs" />
+ <Compile Include="Connections\RoundRobinServerManagerTests.cs" />
<Compile Include="CqlHelperTest.cs" />
<Compile Include="Helper.cs" />
<Compile Include="Linq\LinqToCqlObjectsCassandraTests.cs" />

0 comments on commit 149430c

Please sign in to comment.