Skip to content

Commit e90510a

Browse files
Merge pull request #241 from rabbitmq/rabbitmq-dotnet-client-238
Fix occasional NullReferenceException when trying to resolve endpoints
2 parents 9041556 + 5054ba2 commit e90510a

File tree

3 files changed

+98
-1
lines changed

3 files changed

+98
-1
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@
55
`ConnectionFactory#CreateConnection` could deadlock in some circumstances.
66

77
GH issue: [rabbitmq-dotnet-client#239](https://github.com/rabbitmq/rabbitmq-dotnet-client/issues/239).
8+
9+
Occasional NullReferenceException when unable to resolve any endpoints/
10+
11+
GH issue: [rabbitmq-dotnet-client#238](https://github.com/rabbitmq/rabbitmq-dotnet-client/issues/238)

projects/client/RabbitMQ.Client/src/client/api/IEndpointResolverExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public static T SelectOne<T>(this IEndpointResolver resolver, Func<AmqpTcpEndpoi
6565
exception = e;
6666
}
6767
}
68-
if(t.Equals(default(T)))
68+
69+
if(Object.Equals(t, default(T)) && exception != null)
6970
{
7071
throw exception;
7172
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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-2016 Pivotal Software, 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 Pivotal Software, Inc.
38+
// Copyright (c) 2007-2016 Pivotal Software, Inc. All rights reserved.
39+
//---------------------------------------------------------------------------
40+
41+
using NUnit.Framework;
42+
using System;
43+
using System.Collections.Generic;
44+
using System.Threading;
45+
46+
namespace RabbitMQ.Client.Unit
47+
{
48+
public class TestEndpointResolver : IEndpointResolver
49+
{
50+
private IEnumerable<AmqpTcpEndpoint> endpoints;
51+
public TestEndpointResolver (IEnumerable<AmqpTcpEndpoint> endpoints)
52+
{
53+
this.endpoints = endpoints;
54+
}
55+
56+
public IEnumerable<AmqpTcpEndpoint> All()
57+
{
58+
return endpoints;
59+
}
60+
}
61+
62+
class TestEndpointException : Exception
63+
{
64+
public TestEndpointException(string message) : base(message)
65+
{
66+
}
67+
}
68+
69+
public class TestIEndpointResolverExtensions
70+
{
71+
[Test]
72+
public void SelectOneShouldReturnDefaultWhenThereAreNoEndpoints()
73+
{
74+
var ep = new TestEndpointResolver(new List<AmqpTcpEndpoint>());
75+
Assert.IsNull(ep.SelectOne<AmqpTcpEndpoint>((x) => null));
76+
}
77+
78+
[Test]
79+
public void SelectOneShouldRaiseThrownExceptionWhenThereAreOnlyInaccessibleEndpoints()
80+
{
81+
var ep = new TestEndpointResolver(new List<AmqpTcpEndpoint> { new AmqpTcpEndpoint()});
82+
Assert.Throws<TestEndpointException>(() => ep.SelectOne<AmqpTcpEndpoint>((x) => { throw new TestEndpointException("bananas"); }));
83+
}
84+
85+
[Test]
86+
public void SelectOneShouldReturnFoundEndpoint()
87+
{
88+
var ep = new TestEndpointResolver(new List<AmqpTcpEndpoint> { new AmqpTcpEndpoint()});
89+
Assert.IsNotNull(ep.SelectOne<AmqpTcpEndpoint>((e) => e));
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)