Skip to content

Commit

Permalink
Merge pull request #1386 from rabbitmq/lukebakken/fix-equal-usage-2
Browse files Browse the repository at this point in the history
Ensure that the order of arguments is `expected`, `actual`, take two!
  • Loading branch information
michaelklishin authored Sep 13, 2023
2 parents b01f75c + d432d3d commit 1e1ff94
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
28 changes: 15 additions & 13 deletions projects/Unit/TestOAuth2ClientCredentialsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
//---------------------------------------------------------------------------

using System;

using System.Threading;
using Moq;
using RabbitMQ.Client.OAuth2;
using Xunit;
Expand Down Expand Up @@ -67,8 +67,8 @@ public void ShouldRequestTokenWhenGettingPasswordOrValidUntilForFirstTimeAccess(
{
IToken firstToken = newToken("the_access_token", "the_refresh_token", TimeSpan.FromSeconds(1));
_oAuth2Client.Setup(p => p.RequestToken()).Returns(firstToken);
Assert.Equal(_provider.Password, firstToken.AccessToken);
Assert.Equal(_provider.ValidUntil.Value, firstToken.ExpiresIn);
Assert.Equal(firstToken.AccessToken, _provider.Password);
Assert.Equal(firstToken.ExpiresIn, _provider.ValidUntil.Value);
}

[Fact]
Expand All @@ -78,16 +78,16 @@ public void ShouldRefreshTokenUsingRefreshTokenWhenAvailable()
IToken refreshedToken = newToken("the_access_token2", "the_refresh_token_2", TimeSpan.FromSeconds(60));
_oAuth2Client.Setup(p => p.RequestToken()).Returns(firstToken);
_provider.Refresh();
Assert.Equal(_provider.Password, firstToken.AccessToken);
Assert.Equal(_provider.ValidUntil.Value, firstToken.ExpiresIn);
_oAuth2Client.Reset();
System.Threading.Thread.Sleep(1000);
Assert.Equal(firstToken.AccessToken, _provider.Password);
Assert.Equal(firstToken.ExpiresIn, _provider.ValidUntil.Value);

_oAuth2Client.Reset();
Thread.Sleep(1000);
_oAuth2Client.Setup(p => p.RefreshToken(firstToken)).Returns(refreshedToken);
_provider.Refresh();
Assert.Equal(_provider.Password, refreshedToken.AccessToken);
Assert.Equal(_provider.ValidUntil.Value, refreshedToken.ExpiresIn);

Assert.Equal(refreshedToken.AccessToken, _provider.Password);
Assert.Equal(refreshedToken.ExpiresIn, _provider.ValidUntil.Value);
}

[Fact]
Expand All @@ -99,12 +99,14 @@ public void ShouldRequestTokenWhenRefreshTokenNotAvailable()
.Returns(firstToken)
.Returns(refreshedToken);
_provider.Refresh();
Assert.Equal(_provider.Password, firstToken.AccessToken);
Assert.Equal(_provider.ValidUntil.Value, firstToken.ExpiresIn);

Assert.Equal(firstToken.AccessToken, _provider.Password);
Assert.Equal(firstToken.ExpiresIn, _provider.ValidUntil.Value);

_provider.Refresh();
Assert.Equal(_provider.Password, refreshedToken.AccessToken);
Assert.Equal(_provider.ValidUntil.Value, refreshedToken.ExpiresIn);

Assert.Equal(refreshedToken.AccessToken, _provider.Password);
Assert.Equal(refreshedToken.ExpiresIn, _provider.ValidUntil.Value);
Mock.Verify(_oAuth2Client);
}

Expand Down
12 changes: 7 additions & 5 deletions projects/Unit/TestTimerBasedCredentialRefresher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
//---------------------------------------------------------------------------

using System;
using System.Threading.Tasks;
using System.Threading;
using Moq;
using Xunit;

Expand Down Expand Up @@ -67,28 +67,30 @@ public void TestDoNotRegisterWhenHasNoExpiry()
}

[Fact]
public async void TestRefreshToken()
public void TestRefreshToken()
{
_credentialsProvider.Setup(p => p.ValidUntil).Returns(TimeSpan.FromSeconds(1));
_credentialsProvider.Setup(p => p.Password).Returns("the-token").Verifiable();
_callback.Setup(p => p.Invoke(true));
_refresher.Register(_credentialsProvider.Object, _callback.Object);
await Task.Delay(1 * 1000);

Thread.Sleep(TimeSpan.FromSeconds(1));

_credentialsProvider.Verify();
_callback.Verify();
}

[Fact]
public async void TestRefreshTokenFailed()
public void TestRefreshTokenFailed()
{
_credentialsProvider.Setup(p => p.ValidUntil).Returns(TimeSpan.FromSeconds(1));
_credentialsProvider.SetupSequence(p => p.Password)
.Returns("the-token")
.Throws(new Exception());
_callback.Setup(p => p.Invoke(false));
_refresher.Register(_credentialsProvider.Object, _callback.Object);
await Task.Delay(1 * 1000);

Thread.Sleep(TimeSpan.FromSeconds(1));

_credentialsProvider.Verify();
_callback.Verify();
Expand Down

0 comments on commit 1e1ff94

Please sign in to comment.