Skip to content

Commit 55fc05a

Browse files
authored
Merge pull request #1393 from rabbitmq/rabbitmq-dotnet-client-1369
Remove use of Moq
2 parents 6c68581 + a04540d commit 55fc05a

File tree

5 files changed

+237
-75
lines changed

5 files changed

+237
-75
lines changed

projects/Unit/Fixtures.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,8 @@ public class TimingFixture
519519
public static readonly TimeSpan TimingInterval = TimeSpan.FromMilliseconds(300);
520520
public static readonly TimeSpan TimingInterval_2X = TimeSpan.FromMilliseconds(600);
521521
public static readonly TimeSpan TimingInterval_4X = TimeSpan.FromMilliseconds(1200);
522+
public static readonly TimeSpan TimingInterval_8X = TimeSpan.FromMilliseconds(2400);
523+
public static readonly TimeSpan TimingInterval_16X = TimeSpan.FromMilliseconds(4800);
522524
public static readonly TimeSpan SafetyMargin = TimeSpan.FromMilliseconds(150);
523525
public static readonly TimeSpan TestTimeout = TimeSpan.FromSeconds(5);
524526
}

projects/Unit/TestBlockingCell.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public void TestBackgroundUpdateSucceedsWithInfiniteTimeoutTimeSpan()
140140
public void TestBackgroundUpdateFails()
141141
{
142142
var k = new BlockingCell<int>();
143-
SetAfter(TimingInterval_4X, k, 123);
143+
SetAfter(TimingInterval_16X, k, 123);
144144

145145
ResetTimer();
146146
Assert.Throws<TimeoutException>(() => k.WaitForValue(TimingInterval));

projects/Unit/TestOAuth2ClientCredentialsProvider.cs

Lines changed: 119 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,93 +30,169 @@
3030
//---------------------------------------------------------------------------
3131

3232
using System;
33+
using System.Diagnostics;
3334
using System.Threading;
34-
using Moq;
3535
using RabbitMQ.Client.OAuth2;
3636
using Xunit;
37+
using Xunit.Abstractions;
3738

3839
namespace RabbitMQ.Client.Unit
3940
{
41+
public class MockIOAuth2Client : IOAuth2Client
42+
{
43+
private readonly ITestOutputHelper _testOutputHelper;
44+
private IToken _refreshToken;
45+
private IToken _requestToken;
46+
47+
public MockIOAuth2Client(ITestOutputHelper testOutputHelper)
48+
{
49+
_testOutputHelper = testOutputHelper;
50+
}
51+
52+
public IToken RefreshTokenValue
53+
{
54+
get { return _refreshToken; }
55+
set
56+
{
57+
if (value == null)
58+
{
59+
throw new ArgumentNullException(nameof(value));
60+
}
61+
62+
_refreshToken = value;
63+
}
64+
}
65+
66+
public IToken RequestTokenValue
67+
{
68+
get { return _requestToken; }
69+
set
70+
{
71+
if (value == null)
72+
{
73+
throw new ArgumentNullException(nameof(value));
74+
}
75+
76+
_requestToken = value;
77+
}
78+
}
79+
80+
public IToken RefreshToken(IToken initialToken)
81+
{
82+
Debug.Assert(Object.ReferenceEquals(_requestToken, initialToken));
83+
return _refreshToken;
84+
}
85+
86+
public IToken RequestToken()
87+
{
88+
return _requestToken;
89+
}
90+
}
91+
4092
public class TestOAuth2CredentialsProvider
4193
{
42-
protected OAuth2ClientCredentialsProvider _provider;
43-
protected Mock<IOAuth2Client> _oAuth2Client;
94+
private readonly ITestOutputHelper _testOutputHelper;
4495

45-
public TestOAuth2CredentialsProvider()
96+
public TestOAuth2CredentialsProvider(ITestOutputHelper testOutputHelper)
4697
{
47-
_oAuth2Client = new Mock<IOAuth2Client>();
48-
_provider = new OAuth2ClientCredentialsProvider("aName", _oAuth2Client.Object);
98+
_testOutputHelper = testOutputHelper;
4999
}
50100

51101
[Fact]
52-
public void shouldHaveAName()
102+
public void ShouldHaveAName()
53103
{
54-
Assert.Equal("aName", _provider.Name);
104+
const string name = "aName";
105+
IOAuth2Client oAuth2Client = new MockIOAuth2Client(_testOutputHelper);
106+
var provider = new OAuth2ClientCredentialsProvider(name, oAuth2Client);
107+
108+
Assert.Equal(name, provider.Name);
55109
}
56110

57111
[Fact]
58112
public void ShouldRequestTokenWhenAskToRefresh()
59113
{
60-
_oAuth2Client.Setup(p => p.RequestToken()).Returns(newToken("the_access_token", TimeSpan.FromSeconds(60)));
61-
_provider.Refresh();
62-
Assert.Equal("the_access_token", _provider.Password);
114+
const string newTokenValue = "the_access_token";
115+
IToken newToken = NewToken(newTokenValue, TimeSpan.FromSeconds(60));
116+
var oAuth2Client = new MockIOAuth2Client(_testOutputHelper);
117+
oAuth2Client.RequestTokenValue = newToken;
118+
var provider = new OAuth2ClientCredentialsProvider(nameof(ShouldRequestTokenWhenAskToRefresh), oAuth2Client);
119+
120+
provider.Refresh();
121+
122+
Assert.Equal(newTokenValue, provider.Password);
63123
}
64124

65125
[Fact]
66126
public void ShouldRequestTokenWhenGettingPasswordOrValidUntilForFirstTimeAccess()
67127
{
68-
IToken firstToken = newToken("the_access_token", "the_refresh_token", TimeSpan.FromSeconds(1));
69-
_oAuth2Client.Setup(p => p.RequestToken()).Returns(firstToken);
70-
Assert.Equal(firstToken.AccessToken, _provider.Password);
71-
Assert.Equal(firstToken.ExpiresIn, _provider.ValidUntil.Value);
128+
const string accessToken = "the_access_token";
129+
const string refreshToken = "the_refresh_token";
130+
IToken firstToken = NewToken(accessToken, refreshToken, TimeSpan.FromSeconds(1));
131+
var oAuth2Client = new MockIOAuth2Client(_testOutputHelper);
132+
oAuth2Client.RequestTokenValue = firstToken;
133+
var provider = new OAuth2ClientCredentialsProvider(nameof(ShouldRequestTokenWhenGettingPasswordOrValidUntilForFirstTimeAccess), oAuth2Client);
134+
135+
Assert.Equal(firstToken.AccessToken, provider.Password);
136+
Assert.Equal(firstToken.ExpiresIn, provider.ValidUntil.Value);
72137
}
73138

74139
[Fact]
75140
public void ShouldRefreshTokenUsingRefreshTokenWhenAvailable()
76141
{
77-
IToken firstToken = newToken("the_access_token", "the_refresh_token", TimeSpan.FromSeconds(1));
78-
IToken refreshedToken = newToken("the_access_token2", "the_refresh_token_2", TimeSpan.FromSeconds(60));
79-
_oAuth2Client.Setup(p => p.RequestToken()).Returns(firstToken);
80-
_provider.Refresh();
81-
Assert.Equal(firstToken.AccessToken, _provider.Password);
82-
Assert.Equal(firstToken.ExpiresIn, _provider.ValidUntil.Value);
83-
84-
_oAuth2Client.Reset();
85-
Thread.Sleep(1000);
86-
_oAuth2Client.Setup(p => p.RefreshToken(firstToken)).Returns(refreshedToken);
87-
_provider.Refresh();
88-
89-
Assert.Equal(refreshedToken.AccessToken, _provider.Password);
90-
Assert.Equal(refreshedToken.ExpiresIn, _provider.ValidUntil.Value);
142+
const string accessToken = "the_access_token";
143+
const string refreshToken = "the_refresh_token";
144+
const string accessToken2 = "the_access_token_2";
145+
const string refreshToken2 = "the_refresh_token_2";
146+
147+
IToken firstToken = NewToken(accessToken, refreshToken, TimeSpan.FromSeconds(1));
148+
IToken refreshedToken = NewToken(accessToken2, refreshToken2, TimeSpan.FromSeconds(60));
149+
var oAuth2Client = new MockIOAuth2Client(_testOutputHelper);
150+
oAuth2Client.RequestTokenValue = firstToken;
151+
var provider = new OAuth2ClientCredentialsProvider(nameof(ShouldRequestTokenWhenGettingPasswordOrValidUntilForFirstTimeAccess), oAuth2Client);
152+
153+
provider.Refresh();
154+
155+
Assert.Equal(firstToken.AccessToken, provider.Password);
156+
Assert.Equal(firstToken.ExpiresIn, provider.ValidUntil.Value);
157+
158+
oAuth2Client.RefreshTokenValue = refreshedToken;
159+
provider.Refresh();
160+
161+
Assert.Equal(refreshedToken.AccessToken, provider.Password);
162+
Assert.Equal(refreshedToken.ExpiresIn, provider.ValidUntil.Value);
91163
}
92164

93165
[Fact]
94166
public void ShouldRequestTokenWhenRefreshTokenNotAvailable()
95167
{
96-
IToken firstToken = newToken("the_access_token", null, TimeSpan.FromSeconds(1));
97-
IToken refreshedToken = newToken("the_access_token2", null, TimeSpan.FromSeconds(1));
98-
_oAuth2Client.SetupSequence(p => p.RequestToken())
99-
.Returns(firstToken)
100-
.Returns(refreshedToken);
101-
_provider.Refresh();
168+
const string accessToken = "the_access_token";
169+
const string accessToken2 = "the_access_token_2";
170+
IToken firstToken = NewToken(accessToken, null, TimeSpan.FromSeconds(1));
171+
IToken secondToken = NewToken(accessToken2, null, TimeSpan.FromSeconds(60));
172+
173+
var oAuth2Client = new MockIOAuth2Client(_testOutputHelper);
174+
oAuth2Client.RequestTokenValue = firstToken;
175+
var provider = new OAuth2ClientCredentialsProvider(nameof(ShouldRequestTokenWhenRefreshTokenNotAvailable), oAuth2Client);
176+
177+
provider.Refresh();
102178

103-
Assert.Equal(firstToken.AccessToken, _provider.Password);
104-
Assert.Equal(firstToken.ExpiresIn, _provider.ValidUntil.Value);
179+
Assert.Equal(firstToken.AccessToken, provider.Password);
180+
Assert.Equal(firstToken.ExpiresIn, provider.ValidUntil.Value);
105181

106-
_provider.Refresh();
182+
oAuth2Client.RequestTokenValue = secondToken;
183+
provider.Refresh();
107184

108-
Assert.Equal(refreshedToken.AccessToken, _provider.Password);
109-
Assert.Equal(refreshedToken.ExpiresIn, _provider.ValidUntil.Value);
110-
Mock.Verify(_oAuth2Client);
185+
Assert.Equal(secondToken.AccessToken, provider.Password);
186+
Assert.Equal(secondToken.ExpiresIn, provider.ValidUntil.Value);
111187
}
112188

113-
private static Token newToken(string access_token, TimeSpan expiresIn)
189+
private static Token NewToken(string access_token, TimeSpan expiresIn)
114190
{
115191
var token = new JsonToken(access_token, string.Empty, expiresIn);
116192
return new Token(token);
117193
}
118194

119-
private static Token newToken(string access_token, string refresh_token, TimeSpan expiresIn)
195+
private static Token NewToken(string access_token, string refresh_token, TimeSpan expiresIn)
120196
{
121197
JsonToken token = new JsonToken(access_token, refresh_token, expiresIn);
122198
return new Token(token);

0 commit comments

Comments
 (0)