Kaynağa Gözat

Add concurrent locks prevent multithreaded execution create the database

master
yangxiaodong 7 yıl önce
ebeveyn
işleme
7512c11633
2 değiştirilmiş dosya ile 15 ekleme ve 180 silme
  1. +15
    -12
      test/DotNetCore.CAP.SqlServer.Test/DatabaseTestHost.cs
  2. +0
    -168
      test/DotNetCore.CAP.SqlServer.Test/EFMessageStoreTest.cs

+ 15
- 12
test/DotNetCore.CAP.SqlServer.Test/DatabaseTestHost.cs Dosyayı Görüntüle

@@ -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<SqlServerStorage>();
var token = new CancellationTokenSource().Token;
CreateDatabase();
storage.InitializeAsync(token).Wait();
_sqlObjectInstalled = true;
}
var storage = GetService<SqlServerStorage>();
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}];");
}
}


+ 0
- 168
test/DotNetCore.CAP.SqlServer.Test/EFMessageStoreTest.cs Dosyayı Görüntüle

@@ -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<TestDbContext>();
// if (delete)
// {
// db.Database.EnsureDeleted();
// }
// db.Database.EnsureCreated();
// return db;
// }
//}
}

Yükleniyor…
İptal
Kaydet