Add UserCredentialsConnectionFactoryAdapter

This commit is contained in:
Cezary Piatek
2019-07-26 12:52:28 +02:00
parent ec3e168bee
commit 1bb9edfcd7
3 changed files with 377 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
using System;
using System.Diagnostics;
using System.Threading;
namespace Spring.Messaging.Nms.Connections
{
public class MultithreadingTestHelper
{
[DebuggerStepThrough]
public static TestThreadHandler RunOnSeparateThread(Action action)
{
Exception exception = null;
var thread1 = new Thread(() =>
{
try
{
action();
}
catch (Exception e)
{
exception = e;
}
});
thread1.Start();
return new TestThreadHandler(() =>
{
thread1.Join();
if (exception != null)
{
throw exception;
}
});
}
public class TestThreadHandler
{
private readonly Action _waitAction;
public TestThreadHandler(Action waitAction)
{
_waitAction = waitAction;
}
[DebuggerStepThrough]
public void Wait()
{
_waitAction();
}
}
}
}

View File

@@ -0,0 +1,164 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Apache.NMS;
using FakeItEasy;
using NUnit.Framework;
namespace Spring.Messaging.Nms.Connections
{
[TestFixture]
public class UserCredentialsConnectionFactoryAdapterTests
{
[Test]
public void CreateConnectionWithoutCredentialsWhenTheyAreNotSet()
{
var underlyingConnectionFactory = A.Fake<IConnectionFactory>();
var connectionFactory = new UserCredentialsConnectionFactoryAdapter(underlyingConnectionFactory);
connectionFactory.CreateConnection();
A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappenedOnceExactly();
A.CallTo(() => underlyingConnectionFactory.CreateConnection(A<string>._, A<string>._)).MustHaveHappened(0, Times.Exactly);
}
[Test]
public void CreateConnectionWithoutCredentialsWhenLoginIsNotSet()
{
var underlyingConnectionFactory = A.Fake<IConnectionFactory>();
var connectionFactory = new UserCredentialsConnectionFactoryAdapter(underlyingConnectionFactory);
connectionFactory.Password = "Secret";
connectionFactory.CreateConnection();
A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappenedOnceExactly();
A.CallTo(() => underlyingConnectionFactory.CreateConnection(A<string>._, A<string>._)).MustHaveHappened(0, Times.Exactly);
}
[Test]
public void CreateConnectionWithCredentialsWhenTheyAreSet()
{
var underlyingConnectionFactory = A.Fake<IConnectionFactory>();
var connectionFactory = new UserCredentialsConnectionFactoryAdapter(underlyingConnectionFactory);
connectionFactory.UserName = "SampleUser";
connectionFactory.Password = "Secret";
connectionFactory.CreateConnection();
A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappened(0, Times.Exactly);
A.CallTo(() => underlyingConnectionFactory.CreateConnection("SampleUser", "Secret")).MustHaveHappenedOnceExactly();
}
[Test]
public void SetConnectionCredentialsOnlyForCurrentThread()
{
var underlyingConnectionFactory = A.Fake<IConnectionFactory>();
var connectionFactory = new UserCredentialsConnectionFactoryAdapter(underlyingConnectionFactory);
// Call CreateConnection on thread that also called SetCredentialsForCurrentThread
MultithreadingTestHelper.RunOnSeparateThread(() =>
{
connectionFactory.SetCredentialsForCurrentThread("SampleUser", "Password");
connectionFactory.CreateConnection();
A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappened(0, Times.Exactly);
A.CallTo(() => underlyingConnectionFactory.CreateConnection("SampleUser", "Password")).MustHaveHappenedOnceExactly();
}).Wait();
// Call CreateConnection on thread that didn't callSetCredentialsForCurrentThread
MultithreadingTestHelper.RunOnSeparateThread(() =>
{
connectionFactory.CreateConnection();
A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappenedOnceExactly();
A.CallTo(() => underlyingConnectionFactory.CreateConnection(A<string>._, A<string>._)).MustHaveHappenedOnceExactly();
}).Wait();
// Call CreateConnection on the main thread
connectionFactory.CreateConnection();
A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappened(2, Times.Exactly);
A.CallTo(() => underlyingConnectionFactory.CreateConnection(A<string>._, A<string>._)).MustHaveHappenedOnceExactly();
}
[Test]
public void InheritCredentialsIfTheyAreNotSetForSpecificThread()
{
var underlyingConnectionFactory = A.Fake<IConnectionFactory>();
var connectionFactory = new UserCredentialsConnectionFactoryAdapter(underlyingConnectionFactory);
connectionFactory.UserName = "SampleUser";
connectionFactory.Password = "Secret";
// Call CreateConnection on thread that also called SetCredentialsForCurrentThread
MultithreadingTestHelper.RunOnSeparateThread(() =>
{
connectionFactory.CreateConnection();
A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappened(0, Times.Exactly);
A.CallTo(() => underlyingConnectionFactory.CreateConnection("SampleUser", "Secret")).MustHaveHappenedOnceExactly();
connectionFactory.SetCredentialsForCurrentThread("ThreadSampleUser", "ThreadPassword");
connectionFactory.CreateConnection();
A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappened(0, Times.Exactly);
A.CallTo(() => underlyingConnectionFactory.CreateConnection("ThreadSampleUser", "ThreadPassword")).MustHaveHappenedOnceExactly();
connectionFactory.RemoveCredentialsFromCurrentThread();
connectionFactory.CreateConnection();
A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappened(0, Times.Exactly);
A.CallTo(() => underlyingConnectionFactory.CreateConnection("SampleUser", "Secret")).MustHaveHappened(2, Times.Exactly);
}).Wait();
connectionFactory.CreateConnection();
A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappened(0, Times.Exactly);
A.CallTo(() => underlyingConnectionFactory.CreateConnection("SampleUser", "Secret")).MustHaveHappened(3, Times.Exactly);
}
[Test]
public void SetDifferentCredentialsOnDifferentThreads()
{
var underlyingConnectionFactory = A.Fake<IConnectionFactory>();
var connectionFactory = new UserCredentialsConnectionFactoryAdapter(underlyingConnectionFactory);
connectionFactory.UserName = "SampleUser";
connectionFactory.Password = "Secret";
var barrier = new Barrier(2);
// Call CreateConnection on thread that also called SetCredentialsForCurrentThread
var thread1 = MultithreadingTestHelper.RunOnSeparateThread(() =>
{
connectionFactory.SetCredentialsForCurrentThread("Thread1SampleUser", "Password");
barrier.SignalAndWait();
connectionFactory.CreateConnection();
A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappened(0, Times.Exactly);
A.CallTo(() => underlyingConnectionFactory.CreateConnection("Thread1SampleUser", "Password")).MustHaveHappenedOnceExactly();
});
// Call CreateConnection on thread that didn't callSetCredentialsForCurrentThread
var thread2 = MultithreadingTestHelper.RunOnSeparateThread(() =>
{
connectionFactory.SetCredentialsForCurrentThread("Thread2SampleUser", "Password");
barrier.SignalAndWait();
connectionFactory.CreateConnection();
A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappened(0, Times.Exactly);
A.CallTo(() => underlyingConnectionFactory.CreateConnection("Thread2SampleUser", "Password")).MustHaveHappenedOnceExactly();
});
connectionFactory.CreateConnection();
A.CallTo(() => underlyingConnectionFactory.CreateConnection()).MustHaveHappened(0, Times.Exactly);
A.CallTo(() => underlyingConnectionFactory.CreateConnection("SampleUser", "Secret")).MustHaveHappenedOnceExactly();
thread1.Wait();
thread2.Wait();
}
}
}