diff --git a/test/DotNetCore.CAP.SqlServer.Test/DatabaseTestHost.cs b/test/DotNetCore.CAP.SqlServer.Test/DatabaseTestHost.cs index 774ff40..b32fd74 100644 --- a/test/DotNetCore.CAP.SqlServer.Test/DatabaseTestHost.cs +++ b/test/DotNetCore.CAP.SqlServer.Test/DatabaseTestHost.cs @@ -1,5 +1,4 @@ using System.Data; -using System.Data.SqlClient; using System.Threading; using Dapper; using Microsoft.EntityFrameworkCore; @@ -9,11 +8,18 @@ namespace DotNetCore.CAP.SqlServer.Test public abstract class DatabaseTestHost : TestHost { private static bool _sqlObjectInstalled; + public static object _lock = new object(); protected override void PostBuildServices() { base.PostBuildServices(); - InitializeDatabase(); + lock (_lock) + { + if (!_sqlObjectInstalled) + { + InitializeDatabase(); + } + } } public override void Dispose() @@ -24,16 +30,13 @@ namespace DotNetCore.CAP.SqlServer.Test private void InitializeDatabase() { - if (!_sqlObjectInstalled) + using (CreateScope()) { - using (CreateScope()) - { - var storage = GetService(); - var token = new CancellationTokenSource().Token; - CreateDatabase(); - storage.InitializeAsync(token).Wait(); - _sqlObjectInstalled = true; - } + var storage = GetService(); + var token = new CancellationTokenSource().Token; + CreateDatabase(); + storage.InitializeAsync(token).Wait(); + _sqlObjectInstalled = true; } } @@ -44,7 +47,7 @@ namespace DotNetCore.CAP.SqlServer.Test using (var connection = ConnectionUtil.CreateConnection(masterConn)) { connection.Execute($@" -IF NOT EXISTS (SELECT * FROM sysdatabases WHERE name = N'{databaseName}') +IF NOT EXISTS (SELECT * FROM sysdatabases WHERE name = N'{databaseName}') CREATE DATABASE [{databaseName}];"); } } diff --git a/test/DotNetCore.CAP.SqlServer.Test/EFMessageStoreTest.cs b/test/DotNetCore.CAP.SqlServer.Test/EFMessageStoreTest.cs deleted file mode 100644 index e59f393..0000000 --- a/test/DotNetCore.CAP.SqlServer.Test/EFMessageStoreTest.cs +++ /dev/null @@ -1,168 +0,0 @@ -namespace DotNetCore.CAP.EntityFrameworkCore.Test -{ - //public class EFMessageStoreTest : DatabaseTestHost - //{ - // [Fact] - // public void CanCreateSentMessageUsingEF() - // { - // using (var db = CreateContext()) - // { - // var guid = Guid.NewGuid().ToString(); - // var message = new CapPublishedMessage - // { - // Id = guid, - // Content = "this is message body", - // StatusName = StatusName.Enqueued - // }; - // db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; - - // db.SaveChanges(); - - // Assert.True(db.CapSentMessages.Any(u => u.Id == guid)); - // Assert.NotNull(db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued)); - // } - // } - - // [Fact] - // public void CanUpdateSentMessageUsingEF() - // { - // using (var db = CreateContext()) - // { - // var guid = Guid.NewGuid().ToString(); - // var message = new CapPublishedMessage - // { - // Id = guid, - // Content = "this is message body", - // StatusName = StatusName.Enqueued - // }; - // db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; - - // db.SaveChanges(); - - // var selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); - // Assert.NotNull(selectedMessage); - - // selectedMessage.StatusName = StatusName.Succeeded; - // selectedMessage.Content = "Test"; - // db.SaveChanges(); - - // selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Succeeded); - // Assert.NotNull(selectedMessage); - // Assert.True(selectedMessage.Content == "Test"); - // } - // } - - // [Fact] - // public void CanRemoveSentMessageUsingEF() - // { - // using (var db = CreateContext()) - // { - // var guid = Guid.NewGuid().ToString(); - // var message = new CapPublishedMessage - // { - // Id = guid, - // Content = "this is message body", - // StatusName = StatusName.Enqueued - // }; - // db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; - - // db.SaveChanges(); - - // var selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); - // Assert.NotNull(selectedMessage); - - // db.CapSentMessages.Remove(selectedMessage); - // db.SaveChanges(); - // selectedMessage = db.CapSentMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); - // Assert.Null(selectedMessage); - // } - // } - - // [Fact] - // public void CanCreateReceivedMessageUsingEF() - // { - // using (var db = CreateContext()) - // { - // var guid = Guid.NewGuid().ToString(); - // var message = new CapReceivedMessage - // { - // Id = guid, - // Content = "this is message body", - // StatusName = StatusName.Enqueued - // }; - // db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; - - // db.SaveChanges(); - - // Assert.True(db.CapReceivedMessages.Any(u => u.Id == guid)); - // Assert.NotNull(db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued)); - // } - // } - - // [Fact] - // public void CanUpdateReceivedMessageUsingEF() - // { - // using (var db = CreateContext()) - // { - // var guid = Guid.NewGuid().ToString(); - // var message = new CapReceivedMessage - // { - // Id = guid, - // Content = "this is message body", - // StatusName = StatusName.Enqueued - // }; - // db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; - - // db.SaveChanges(); - - // var selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); - // Assert.NotNull(selectedMessage); - - // selectedMessage.StatusName = StatusName.Succeeded; - // selectedMessage.Content = "Test"; - // db.SaveChanges(); - - // selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Succeeded); - // Assert.NotNull(selectedMessage); - // Assert.True(selectedMessage.Content == "Test"); - // } - // } - - // [Fact] - // public void CanRemoveReceivedMessageUsingEF() - // { - // using (var db = CreateContext()) - // { - // var guid = Guid.NewGuid().ToString(); - // var message = new CapReceivedMessage - // { - // Id = guid, - // Content = "this is message body", - // StatusName = StatusName.Enqueued - // }; - // db.Attach(message).State = Microsoft.EntityFrameworkCore.EntityState.Added; - - // db.SaveChanges(); - - // var selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); - // Assert.NotNull(selectedMessage); - - // db.CapReceivedMessages.Remove(selectedMessage); - // db.SaveChanges(); - // selectedMessage = db.CapReceivedMessages.FirstOrDefault(u => u.StatusName == StatusName.Enqueued); - // Assert.Null(selectedMessage); - // } - // } - - // public TestDbContext CreateContext(bool delete = false) - // { - // var db = Provider.GetRequiredService(); - // if (delete) - // { - // db.Database.EnsureDeleted(); - // } - // db.Database.EnsureCreated(); - // return db; - // } - //} -} \ No newline at end of file