Skip to content

Commit 07b935f

Browse files
More tests
1 parent 7737c58 commit 07b935f

File tree

5 files changed

+145
-45
lines changed

5 files changed

+145
-45
lines changed

projects/client/RabbitMQ.Client/src/client/impl/AutorecoveringConnection.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,8 +782,7 @@ protected void RecoverConnectionDelegate()
782782
#else
783783
Thread.Sleep(m_factory.NetworkRecoveryInterval);
784784
#endif
785-
// TODO: provide a way to handle these exceptions
786-
System.Diagnostics.Debug.WriteLine(e);
785+
// TODO: provide a way to handle these exceptions
787786
}
788787
}
789788
}

projects/client/Unit/RabbitMQ.Client.Unit.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
<Compile Include="src\unit\TestConnectionRecovery.cs" />
9696
<Compile Include="src\unit\TestConnectionShutdown.cs" />
9797
<Compile Include="src\unit\TestConnectionWithBackgroundThreads.cs" />
98+
<Compile Include="src\unit\TestInitialConnection.cs" />
9899
<Compile Include="src\unit\TestConsumerCancelNotify.cs" />
99100
<Compile Include="src\unit\TestConsumerCount.cs" />
100101
<Compile Include="src\unit\TestConsumerExceptions.cs" />

projects/client/Unit/src/unit/Fixtures.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public class IntegrationFixture
6363
protected IModel Model;
6464

6565
protected Encoding encoding = new UTF8Encoding();
66+
public static TimeSpan RECOVERY_INTERVAL = TimeSpan.FromSeconds(2);
6667

6768
[SetUp]
6869
public virtual void Init()
@@ -92,6 +93,54 @@ protected virtual void ReleaseResources()
9293
// no-op
9394
}
9495

96+
//
97+
// Connections
98+
//
99+
100+
protected AutorecoveringConnection CreateAutorecoveringConnection()
101+
{
102+
return CreateAutorecoveringConnection(RECOVERY_INTERVAL);
103+
}
104+
105+
protected AutorecoveringConnection CreateAutorecoveringConnection(IList<string> hostnames)
106+
{
107+
return CreateAutorecoveringConnection(RECOVERY_INTERVAL, hostnames);
108+
}
109+
110+
protected AutorecoveringConnection CreateAutorecoveringConnection(TimeSpan interval)
111+
{
112+
var cf = new ConnectionFactory();
113+
cf.AutomaticRecoveryEnabled = true;
114+
cf.NetworkRecoveryInterval = interval;
115+
return (AutorecoveringConnection)cf.CreateConnection();
116+
}
117+
118+
protected AutorecoveringConnection CreateAutorecoveringConnection(TimeSpan interval, IList<string> hostnames)
119+
{
120+
var cf = new ConnectionFactory();
121+
cf.AutomaticRecoveryEnabled = true;
122+
// tests that use this helper will likely list unreachable hosts,
123+
// make sure we time out quickly on those
124+
cf.RequestedConnectionTimeout = 1000;
125+
cf.NetworkRecoveryInterval = interval;
126+
return (AutorecoveringConnection)cf.CreateConnection(hostnames);
127+
}
128+
129+
protected AutorecoveringConnection CreateAutorecoveringConnectionWithTopologyRecoveryDisabled()
130+
{
131+
var cf = new ConnectionFactory();
132+
cf.AutomaticRecoveryEnabled = true;
133+
cf.TopologyRecoveryEnabled = false;
134+
cf.NetworkRecoveryInterval = RECOVERY_INTERVAL;
135+
return (AutorecoveringConnection)cf.CreateConnection();
136+
}
137+
138+
protected IConnection CreateNonRecoveringConnection()
139+
{
140+
var cf = new ConnectionFactory();
141+
return cf.CreateConnection();
142+
}
143+
95144
//
96145
// Channels
97146
//

projects/client/Unit/src/unit/TestConnectionRecovery.cs

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ namespace RabbitMQ.Client.Unit
5454
[TestFixture]
5555
public class TestConnectionRecovery : IntegrationFixture
5656
{
57-
public static TimeSpan RECOVERY_INTERVAL = TimeSpan.FromSeconds(2);
58-
5957
[SetUp]
6058
public override void Init()
6159
{
@@ -103,6 +101,17 @@ public void TestBasicConnectionRecoveryWithHostnameList()
103101
Assert.IsTrue(c.IsOpen);
104102
CloseAndWaitForRecovery(c);
105103
Assert.IsTrue(c.IsOpen);
104+
c.Close();
105+
}
106+
107+
[Test]
108+
public void TestBasicConnectionRecoveryWithHostnameListAndUnreachableHosts()
109+
{
110+
var c = CreateAutorecoveringConnection(new List<string>() { "191.72.44.22", "127.0.0.1", "localhost" });
111+
Assert.IsTrue(c.IsOpen);
112+
CloseAndWaitForRecovery(c);
113+
Assert.IsTrue(c.IsOpen);
114+
c.Close();
106115
}
107116

108117
[Test]
@@ -855,47 +864,6 @@ protected void CloseAndWaitForShutdown(AutorecoveringConnection conn)
855864
Wait(sl);
856865
}
857866

858-
protected AutorecoveringConnection CreateAutorecoveringConnection()
859-
{
860-
return CreateAutorecoveringConnection(RECOVERY_INTERVAL);
861-
}
862-
863-
protected AutorecoveringConnection CreateAutorecoveringConnection(IList<string> hostnames)
864-
{
865-
return CreateAutorecoveringConnection(RECOVERY_INTERVAL, hostnames);
866-
}
867-
868-
protected AutorecoveringConnection CreateAutorecoveringConnection(TimeSpan interval)
869-
{
870-
var cf = new ConnectionFactory();
871-
cf.AutomaticRecoveryEnabled = true;
872-
cf.NetworkRecoveryInterval = interval;
873-
return (AutorecoveringConnection)cf.CreateConnection();
874-
}
875-
876-
protected AutorecoveringConnection CreateAutorecoveringConnection(TimeSpan interval, IList<string> hostnames)
877-
{
878-
var cf = new ConnectionFactory();
879-
cf.AutomaticRecoveryEnabled = true;
880-
cf.NetworkRecoveryInterval = interval;
881-
return (AutorecoveringConnection)cf.CreateConnection(hostnames);
882-
}
883-
884-
protected AutorecoveringConnection CreateAutorecoveringConnectionWithTopologyRecoveryDisabled()
885-
{
886-
var cf = new ConnectionFactory();
887-
cf.AutomaticRecoveryEnabled = true;
888-
cf.TopologyRecoveryEnabled = false;
889-
cf.NetworkRecoveryInterval = RECOVERY_INTERVAL;
890-
return (AutorecoveringConnection)cf.CreateConnection();
891-
}
892-
893-
protected IConnection CreateNonRecoveringConnection()
894-
{
895-
var cf = new ConnectionFactory();
896-
return cf.CreateConnection();
897-
}
898-
899867
protected ManualResetEvent PrepareForRecovery(AutorecoveringConnection conn)
900868
{
901869
var latch = new ManualResetEvent(false);
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// This source code is dual-licensed under the Apache License, version
2+
// 2.0, and the Mozilla Public License, version 1.1.
3+
//
4+
// The APL v2.0:
5+
//
6+
//---------------------------------------------------------------------------
7+
// Copyright (C) 2007-2014 GoPivotal, Inc.
8+
//
9+
// Licensed under the Apache License, Version 2.0 (the "License");
10+
// you may not use this file except in compliance with the License.
11+
// You may obtain a copy of the License at
12+
//
13+
// http://www.apache.org/licenses/LICENSE-2.0
14+
//
15+
// Unless required by applicable law or agreed to in writing, software
16+
// distributed under the License is distributed on an "AS IS" BASIS,
17+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
// See the License for the specific language governing permissions and
19+
// limitations under the License.
20+
//---------------------------------------------------------------------------
21+
//
22+
// The MPL v1.1:
23+
//
24+
//---------------------------------------------------------------------------
25+
// The contents of this file are subject to the Mozilla Public License
26+
// Version 1.1 (the "License"); you may not use this file except in
27+
// compliance with the License. You may obtain a copy of the License
28+
// at http://www.mozilla.org/MPL/
29+
//
30+
// Software distributed under the License is distributed on an "AS IS"
31+
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
32+
// the License for the specific language governing rights and
33+
// limitations under the License.
34+
//
35+
// The Original Code is RabbitMQ.
36+
//
37+
// The Initial Developer of the Original Code is GoPivotal, Inc.
38+
// Copyright (c) 2007-2014 GoPivotal, Inc. All rights reserved.
39+
//---------------------------------------------------------------------------
40+
41+
using NUnit.Framework;
42+
using RabbitMQ.Client.Events;
43+
using RabbitMQ.Client.Exceptions;
44+
using RabbitMQ.Client.Framing.Impl;
45+
using RabbitMQ.Client.Impl;
46+
using System;
47+
using System.Collections.Generic;
48+
using System.Threading;
49+
50+
namespace RabbitMQ.Client.Unit
51+
{
52+
[TestFixture]
53+
public class TestInitialConnection : IntegrationFixture
54+
{
55+
[Test]
56+
public void TestBasicConnectionRecoveryWithHostnameList()
57+
{
58+
var c = CreateAutorecoveringConnection(new List<string>() { "127.0.0.1", "localhost" });
59+
Assert.IsTrue(c.IsOpen);
60+
c.Close();
61+
}
62+
63+
[Test]
64+
public void TestBasicConnectionRecoveryWithHostnameListAndUnreachableHosts()
65+
{
66+
var c = CreateAutorecoveringConnection(new List<string>() { "191.72.44.22", "127.0.0.1", "localhost" });
67+
Assert.IsTrue(c.IsOpen);
68+
c.Close();
69+
}
70+
71+
[Test]
72+
public void TestBasicConnectionRecoveryWithHostnameListWithOnlyUnreachableHosts()
73+
{
74+
Assert.Throws<BrokerUnreachableException>(delegate {
75+
CreateAutorecoveringConnection(new List<string>() {
76+
"191.72.44.22",
77+
"145.23.22.18",
78+
"192.255.255.255"
79+
});
80+
});
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)