forked from joel-cass/Hangfire.MySqlStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7833afc
commit 87a0927
Showing
4 changed files
with
148 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,43 @@ | ||
using System.Reflection; | ||
using System.Transactions; | ||
using Xunit.Sdk; | ||
|
||
namespace Hangfire.MySql.Tests | ||
{ | ||
public class CleanDatabaseAttribute : BeforeAfterTestAttribute | ||
{ | ||
private readonly IsolationLevel _isolationLevel; | ||
|
||
private TransactionScope _transaction; | ||
|
||
public CleanDatabaseAttribute( | ||
IsolationLevel isolationLevel = IsolationLevel.ReadCommitted) | ||
{ | ||
_isolationLevel = isolationLevel; | ||
} | ||
|
||
public override void Before(MethodInfo methodUnderTest) | ||
{ | ||
if (_isolationLevel != IsolationLevel.Unspecified) | ||
{ | ||
_transaction = new TransactionScope( | ||
TransactionScopeOption.RequiresNew, | ||
new TransactionOptions { IsolationLevel = _isolationLevel }); | ||
} | ||
} | ||
|
||
public override void After(MethodInfo methodUnderTest) | ||
{ | ||
try | ||
{ | ||
if (_transaction != null) | ||
{ | ||
_transaction.Dispose(); | ||
} | ||
} | ||
finally | ||
using System.Reflection; | ||
using System.Transactions; | ||
using Xunit.Sdk; | ||
|
||
namespace Hangfire.MySql.Tests | ||
{ | ||
public class CleanDatabaseAttribute : BeforeAfterTestAttribute | ||
{ | ||
private readonly IsolationLevel _isolationLevel; | ||
|
||
private TransactionScope _transaction; | ||
|
||
public CleanDatabaseAttribute( | ||
IsolationLevel isolationLevel = IsolationLevel.ReadCommitted) | ||
{ | ||
_isolationLevel = isolationLevel; | ||
} | ||
|
||
public override void Before(MethodInfo methodUnderTest) | ||
{ | ||
if (_isolationLevel != IsolationLevel.Unspecified) | ||
{ | ||
} | ||
} | ||
} | ||
} | ||
_transaction = new TransactionScope( | ||
TransactionScopeOption.RequiresNew, | ||
new TransactionOptions { IsolationLevel = _isolationLevel }); | ||
} | ||
} | ||
|
||
public override void After(MethodInfo methodUnderTest) | ||
{ | ||
try | ||
{ | ||
if (_transaction != null) | ||
{ | ||
_transaction.Dispose(); | ||
} | ||
} | ||
finally | ||
{ | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,54 @@ | ||
using System; | ||
using System.Threading; | ||
using Dapper; | ||
using MySql.Data.MySqlClient; | ||
|
||
namespace Hangfire.MySql.Tests | ||
{ | ||
public class TestDatabaseFixture : IDisposable | ||
using System; | ||
using System.Threading; | ||
using Dapper; | ||
using MySql.Data.MySqlClient; | ||
|
||
namespace Hangfire.MySql.Tests | ||
{ | ||
public class TestDatabaseFixture : IDisposable | ||
{ | ||
private static readonly object GlobalLock = new object(); | ||
public TestDatabaseFixture() | ||
private static readonly object GlobalLock = new object(); | ||
public TestDatabaseFixture() | ||
{ | ||
Monitor.Enter(GlobalLock); | ||
CreateAndInitializeDatabase(); | ||
} | ||
public void Dispose() | ||
{ | ||
Monitor.Enter(GlobalLock); | ||
CreateAndInitializeDatabase(); | ||
} | ||
public void Dispose() | ||
{ | ||
DropDatabase(); | ||
Monitor.Exit(GlobalLock); | ||
} | ||
|
||
private static void CreateAndInitializeDatabase() | ||
{ | ||
var recreateDatabaseSql = String.Format( | ||
@"CREATE DATABASE IF NOT EXISTS `{0}`", | ||
ConnectionUtils.GetDatabaseName()); | ||
|
||
using (var connection = new MySqlConnection( | ||
ConnectionUtils.GetMasterConnectionString())) | ||
{ | ||
connection.Execute(recreateDatabaseSql); | ||
} | ||
|
||
using (var connection = new MySqlConnection( | ||
ConnectionUtils.GetConnectionString())) | ||
{ | ||
MySqlObjectsInstaller.Install(connection); | ||
} | ||
} | ||
|
||
private static void DropDatabase() | ||
{ | ||
var recreateDatabaseSql = String.Format( | ||
@"DROP DATABASE IF EXISTS `{0}`", | ||
ConnectionUtils.GetDatabaseName()); | ||
|
||
using (var connection = new MySqlConnection( | ||
ConnectionUtils.GetMasterConnectionString())) | ||
{ | ||
connection.Execute(recreateDatabaseSql); | ||
} | ||
} | ||
} | ||
} | ||
Monitor.Exit(GlobalLock); | ||
} | ||
|
||
private static void CreateAndInitializeDatabase() | ||
{ | ||
var recreateDatabaseSql = String.Format( | ||
@"CREATE DATABASE IF NOT EXISTS `{0}`", | ||
ConnectionUtils.GetDatabaseName()); | ||
|
||
using (var connection = new MySqlConnection( | ||
ConnectionUtils.GetMasterConnectionString())) | ||
{ | ||
connection.Execute(recreateDatabaseSql); | ||
} | ||
|
||
using (var connection = new MySqlConnection( | ||
ConnectionUtils.GetConnectionString())) | ||
{ | ||
MySqlObjectsInstaller.Install(connection); | ||
} | ||
} | ||
|
||
private static void DropDatabase() | ||
{ | ||
var recreateDatabaseSql = String.Format( | ||
@"DROP DATABASE IF EXISTS `{0}`", | ||
ConnectionUtils.GetDatabaseName()); | ||
|
||
using (var connection = new MySqlConnection( | ||
ConnectionUtils.GetMasterConnectionString())) | ||
{ | ||
connection.Execute(recreateDatabaseSql); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters