From 2b4cd5576707c3b0fa00ab1f187c196b196b5661 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Mon, 23 Jul 2018 16:24:17 +0800 Subject: [PATCH] Refactoring unit tests have been adapted to apperyor --- .../ConnectionUtil.cs | 2 +- .../DatabaseTestHost.cs | 55 +++++++++++++++++ .../DotNetCore.CAP.MongoDB.Test.csproj | 11 ++-- .../MongoDBMonitoringApiTest.cs | 33 ++++------ .../MongoDBStorageConnectionTest.cs | 60 +++++++++++-------- .../MongoDBStorageTest.cs | 37 +++++------- ...ngoDBTest.cs => MongoDBTransactionTest.cs} | 24 +++----- .../MongoDBUtilTest.cs | 27 ++------- 8 files changed, 133 insertions(+), 116 deletions(-) create mode 100644 test/DotNetCore.CAP.MongoDB.Test/DatabaseTestHost.cs rename test/DotNetCore.CAP.MongoDB.Test/{MongoDBTest.cs => MongoDBTransactionTest.cs} (79%) diff --git a/test/DotNetCore.CAP.MongoDB.Test/ConnectionUtil.cs b/test/DotNetCore.CAP.MongoDB.Test/ConnectionUtil.cs index 9c8aa29..da8de03 100644 --- a/test/DotNetCore.CAP.MongoDB.Test/ConnectionUtil.cs +++ b/test/DotNetCore.CAP.MongoDB.Test/ConnectionUtil.cs @@ -2,6 +2,6 @@ namespace DotNetCore.CAP.MongoDB.Test { public class ConnectionUtil { - public static string ConnectionString = "mongodb://mongo1:27017,mongo2:27018,mongo3:27019/?replicaSet=my-mongo-set"; + public static string ConnectionString = "mongodb://localhost:27017"; } } \ No newline at end of file diff --git a/test/DotNetCore.CAP.MongoDB.Test/DatabaseTestHost.cs b/test/DotNetCore.CAP.MongoDB.Test/DatabaseTestHost.cs new file mode 100644 index 0000000..cd837fb --- /dev/null +++ b/test/DotNetCore.CAP.MongoDB.Test/DatabaseTestHost.cs @@ -0,0 +1,55 @@ +using System; +using System.Threading; +using Microsoft.Extensions.DependencyInjection; +using MongoDB.Driver; + +namespace DotNetCore.CAP.MongoDB.Test +{ + public abstract class DatabaseTestHost : IDisposable + { + private string _connectionString; + + protected IServiceProvider Provider { get; private set; } + protected IMongoClient MongoClient => Provider.GetService(); + protected IMongoDatabase Database => MongoClient.GetDatabase(MongoDBOptions.DatabaseName); + protected CapOptions CapOptions => Provider.GetService(); + protected MongoDBOptions MongoDBOptions => Provider.GetService(); + + protected DatabaseTestHost() + { + CreateServiceCollection(); + CreateDatabase(); + } + + private void CreateDatabase() + { + Provider.GetService().InitializeAsync(CancellationToken.None).GetAwaiter().GetResult(); + } + + protected virtual void AddService(ServiceCollection serviceCollection) + { + + } + + private void CreateServiceCollection() + { + var services = new ServiceCollection(); + services.AddOptions(); + services.AddLogging(); + _connectionString = ConnectionUtil.ConnectionString; + + services.AddSingleton(new MongoDBOptions() { DatabaseConnection = _connectionString }); + services.AddSingleton(new CapOptions()); + services.AddSingleton(x => new MongoClient(_connectionString)); + services.AddSingleton(); + + AddService(services); + Provider = services.BuildServiceProvider(); + } + + public void Dispose() + { + MongoClient.DropDatabase(MongoDBOptions.DatabaseName); + } + } +} \ No newline at end of file diff --git a/test/DotNetCore.CAP.MongoDB.Test/DotNetCore.CAP.MongoDB.Test.csproj b/test/DotNetCore.CAP.MongoDB.Test/DotNetCore.CAP.MongoDB.Test.csproj index 5ec46fc..9fb481e 100644 --- a/test/DotNetCore.CAP.MongoDB.Test/DotNetCore.CAP.MongoDB.Test.csproj +++ b/test/DotNetCore.CAP.MongoDB.Test/DotNetCore.CAP.MongoDB.Test.csproj @@ -1,19 +1,18 @@ - + netcoreapp2.1 - false + + - + - - - + diff --git a/test/DotNetCore.CAP.MongoDB.Test/MongoDBMonitoringApiTest.cs b/test/DotNetCore.CAP.MongoDB.Test/MongoDBMonitoringApiTest.cs index 31c0f26..2592b43 100644 --- a/test/DotNetCore.CAP.MongoDB.Test/MongoDBMonitoringApiTest.cs +++ b/test/DotNetCore.CAP.MongoDB.Test/MongoDBMonitoringApiTest.cs @@ -1,47 +1,36 @@ -using MongoDB.Driver; -using DotNetCore.CAP.MongoDB; -using Xunit; using System; -using DotNetCore.CAP.Models; -using FluentAssertions; +using System.Linq; using DotNetCore.CAP.Dashboard.Monitoring; using DotNetCore.CAP.Infrastructure; -using System.Linq; +using DotNetCore.CAP.Models; +using FluentAssertions; +using Xunit; namespace DotNetCore.CAP.MongoDB.Test { - public class MongoDBMonitoringApiTest + [Collection("MongoDB")] + public class MongoDBMonitoringApiTest : DatabaseTestHost { - private readonly MongoClient _client; - private readonly MongoDBOptions _options; private readonly MongoDBMonitoringApi _api; public MongoDBMonitoringApiTest() { - _client = new MongoClient(ConnectionUtil.ConnectionString); - _options = new MongoDBOptions(); - _api = new MongoDBMonitoringApi(_client, _options); + _api = new MongoDBMonitoringApi(MongoClient, MongoDBOptions); - Init(); - } - - private void Init() - { var helper = new MongoDBUtil(); - var database = _client.GetDatabase(_options.Database); - var collection = database.GetCollection(_options.PublishedCollection); - collection.InsertMany(new CapPublishedMessage[] + var collection = Database.GetCollection(MongoDBOptions.PublishedCollection); + collection.InsertMany(new[] { new CapPublishedMessage { - Id = helper.GetNextSequenceValue(database,_options.PublishedCollection), + Id = helper.GetNextSequenceValue(Database,MongoDBOptions.PublishedCollection), Added = DateTime.Now.AddHours(-1), StatusName = "Failed", Content = "abc" }, new CapPublishedMessage { - Id = helper.GetNextSequenceValue(database,_options.PublishedCollection), + Id = helper.GetNextSequenceValue(Database,MongoDBOptions.PublishedCollection), Added = DateTime.Now, StatusName = "Failed", Content = "bbc" diff --git a/test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageConnectionTest.cs b/test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageConnectionTest.cs index a258a5d..2b326af 100644 --- a/test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageConnectionTest.cs +++ b/test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageConnectionTest.cs @@ -1,63 +1,71 @@ -using System.Threading; +using System; using DotNetCore.CAP.Infrastructure; using DotNetCore.CAP.Models; using FluentAssertions; -using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.DependencyInjection; using MongoDB.Driver; using Xunit; -using Xunit.Priority; namespace DotNetCore.CAP.MongoDB.Test { - [TestCaseOrderer(PriorityOrderer.Name, PriorityOrderer.Assembly)] - public class MongoDBStorageConnectionTest + [Collection("MongoDB")] + public class MongoDBStorageConnectionTest : DatabaseTestHost { - private readonly MongoClient _client; - private readonly MongoDBOptions _options; - private readonly MongoDBStorage _storage; - private readonly IStorageConnection _connection; + private IStorageConnection _connection => + Provider.GetService().GetConnection(); - public MongoDBStorageConnectionTest() - { - _client = new MongoClient(ConnectionUtil.ConnectionString); - _options = new MongoDBOptions(); - _storage = new MongoDBStorage(new CapOptions(), _options, _client, NullLogger.Instance); - _connection = _storage.GetConnection(); - } - - [Fact, Priority(1)] + [Fact] public async void StoreReceivedMessageAsync_TestAsync() { - await _storage.InitializeAsync(default(CancellationToken)); - - var id = await - _connection.StoreReceivedMessageAsync(new CapReceivedMessage(new MessageContext + var id = await _connection.StoreReceivedMessageAsync(new CapReceivedMessage(new MessageContext { Group = "test", Name = "test", Content = "test-content" })); + id.Should().BeGreaterThan(0); } - [Fact, Priority(2)] + [Fact] public void ChangeReceivedState_Test() { - var collection = _client.GetDatabase(_options.Database).GetCollection(_options.ReceivedCollection); + StoreReceivedMessageAsync_TestAsync(); + var collection = Database.GetCollection(MongoDBOptions.ReceivedCollection); var msg = collection.Find(x => true).FirstOrDefault(); _connection.ChangeReceivedState(msg.Id, StatusName.Scheduled).Should().BeTrue(); collection.Find(x => x.Id == msg.Id).FirstOrDefault()?.StatusName.Should().Be(StatusName.Scheduled); } - [Fact, Priority(3)] + [Fact] public async void GetReceivedMessagesOfNeedRetry_TestAsync() { var msgs = await _connection.GetReceivedMessagesOfNeedRetry(); + + msgs.Should().BeEmpty(); + + var msg = new CapReceivedMessage + { + Group = "test", + Name = "test", + Content = "test-content", + StatusName = StatusName.Failed + }; + var id = await _connection.StoreReceivedMessageAsync(msg); + + var collection = Database.GetCollection(MongoDBOptions.ReceivedCollection); + + var updateDef = Builders + .Update.Set(x => x.Added, DateTime.Now.AddMinutes(-5)); + + await collection.UpdateOneAsync(x => x.Id == id, updateDef); + + msgs = await _connection.GetReceivedMessagesOfNeedRetry(); msgs.Should().HaveCountGreaterThan(0); } - [Fact, Priority(4)] + [Fact] public void GetReceivedMessageAsync_Test() { var msg = _connection.GetReceivedMessageAsync(1); diff --git a/test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageTest.cs b/test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageTest.cs index dbbe95e..1a5f4de 100644 --- a/test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageTest.cs +++ b/test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageTest.cs @@ -1,38 +1,29 @@ -using System.Threading; using FluentAssertions; -using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.DependencyInjection; using MongoDB.Bson; using MongoDB.Driver; using Xunit; namespace DotNetCore.CAP.MongoDB.Test { - public class MongoDBStorageTest + [Collection("MongoDB")] + public class MongoDBStorageTest : DatabaseTestHost { - private readonly MongoClient _client; - - public MongoDBStorageTest() - { - _client = new MongoClient(ConnectionUtil.ConnectionString); - } - [Fact] - public async void InitializeAsync_Test() + public void InitializeAsync_Test() { - var options = new MongoDBOptions(); - var storage = new MongoDBStorage(new CapOptions(), options, _client, NullLogger.Instance); - await storage.InitializeAsync(default(CancellationToken)); - var names = _client.ListDatabaseNames()?.ToList(); - names.Should().Contain(options.Database); + var storage = Provider.GetService(); + var names = MongoClient.ListDatabaseNames()?.ToList(); + names.Should().Contain(MongoDBOptions.DatabaseName); - var collections = _client.GetDatabase(options.Database).ListCollectionNames()?.ToList(); - collections.Should().Contain(options.PublishedCollection); - collections.Should().Contain(options.ReceivedCollection); - collections.Should().Contain("Counter"); + var collections = Database.ListCollectionNames()?.ToList(); + collections.Should().Contain(MongoDBOptions.PublishedCollection); + collections.Should().Contain(MongoDBOptions.ReceivedCollection); + collections.Should().Contain(MongoDBOptions.CounterCollection); - var collection = _client.GetDatabase(options.Database).GetCollection("Counter"); - collection.CountDocuments(new BsonDocument { { "_id", options.PublishedCollection } }).Should().Be(1); - collection.CountDocuments(new BsonDocument { { "_id", options.ReceivedCollection } }).Should().Be(1); + var collection = Database.GetCollection(MongoDBOptions.CounterCollection); + collection.CountDocuments(new BsonDocument { { "_id", MongoDBOptions.PublishedCollection } }).Should().Be(1); + collection.CountDocuments(new BsonDocument { { "_id", MongoDBOptions.ReceivedCollection } }).Should().Be(1); } } } \ No newline at end of file diff --git a/test/DotNetCore.CAP.MongoDB.Test/MongoDBTest.cs b/test/DotNetCore.CAP.MongoDB.Test/MongoDBTransactionTest.cs similarity index 79% rename from test/DotNetCore.CAP.MongoDB.Test/MongoDBTest.cs rename to test/DotNetCore.CAP.MongoDB.Test/MongoDBTransactionTest.cs index 096e04c..dd7351a 100644 --- a/test/DotNetCore.CAP.MongoDB.Test/MongoDBTest.cs +++ b/test/DotNetCore.CAP.MongoDB.Test/MongoDBTransactionTest.cs @@ -6,23 +6,17 @@ using Xunit; namespace DotNetCore.CAP.MongoDB.Test { - public class MongoDBTest + [Collection("MongoDB")] + public class MongoDBTransactionTest : DatabaseTestHost { - private readonly MongoClient _client; - - public MongoDBTest() - { - _client = new MongoClient(ConnectionUtil.ConnectionString); - } - [Fact] public void MongoDB_Connection_Test() { - var names = _client.ListDatabaseNames(); + var names = MongoClient.ListDatabaseNames(); names.ToList().Should().NotBeNullOrEmpty(); } - [Fact] + [Fact(Skip = "Because of Appveyor dose not support MongoDB 4.0, so we skip this test for now.")] public void Transaction_Test() { var document = new BsonDocument @@ -36,10 +30,10 @@ namespace DotNetCore.CAP.MongoDB.Test { "y", 102 } }} }; - var db = _client.GetDatabase("test"); + var db = MongoClient.GetDatabase("test"); var collection1 = db.GetCollection("test1"); var collection2 = db.GetCollection("test2"); - using (var sesstion = _client.StartSession()) + using (var sesstion = MongoClient.StartSession()) { sesstion.StartTransaction(); collection1.InsertOne(document); @@ -51,7 +45,7 @@ namespace DotNetCore.CAP.MongoDB.Test collection2.CountDocuments(filter).Should().BeGreaterThan(0); } - [Fact] + [Fact(Skip = "Because of Appveyor dose not support MongoDB 4.0, so we skip this test for now.")] public void Transaction_Rollback_Test() { var document = new BsonDocument @@ -59,12 +53,12 @@ namespace DotNetCore.CAP.MongoDB.Test {"name", "MongoDB"}, {"date", DateTimeOffset.Now.ToString()} }; - var db = _client.GetDatabase("test"); + var db = MongoClient.GetDatabase("test"); var collection = db.GetCollection("test3"); var collection4 = db.GetCollection("test4"); - using (var session = _client.StartSession()) + using (var session = MongoClient.StartSession()) { session.IsInTransaction.Should().BeFalse(); session.StartTransaction(); diff --git a/test/DotNetCore.CAP.MongoDB.Test/MongoDBUtilTest.cs b/test/DotNetCore.CAP.MongoDB.Test/MongoDBUtilTest.cs index 0944a21..45695f9 100644 --- a/test/DotNetCore.CAP.MongoDB.Test/MongoDBUtilTest.cs +++ b/test/DotNetCore.CAP.MongoDB.Test/MongoDBUtilTest.cs @@ -1,36 +1,17 @@ using System.Collections.Concurrent; -using System.Linq; using System.Threading.Tasks; -using DotNetCore.CAP.Models; using FluentAssertions; -using MongoDB.Bson; -using MongoDB.Driver; using Xunit; namespace DotNetCore.CAP.MongoDB.Test { - public class MongoDBUtilTest + [Collection("MongoDB")] + public class MongoDBUtilTest : DatabaseTestHost { - private readonly IMongoDatabase _database; - string _recieved = "ReceivedTest"; - - public MongoDBUtilTest() - { - var client = new MongoClient(ConnectionUtil.ConnectionString); - _database = client.GetDatabase("CAP_Test"); - - //Initialize MongoDB - if (_database.ListCollectionNames().ToList().All(x => x != "Counter")) - { - var collection = _database.GetCollection("Counter"); - collection.InsertOne(new BsonDocument { { "_id", _recieved }, { "sequence_value", 0 } }); - } - } - [Fact] public async void GetNextSequenceValueAsync_Test() { - var id = await new MongoDBUtil().GetNextSequenceValueAsync(_database, _recieved); + var id = await new MongoDBUtil().GetNextSequenceValueAsync(Database, MongoDBOptions.ReceivedCollection); id.Should().BeGreaterThan(0); } @@ -40,7 +21,7 @@ namespace DotNetCore.CAP.MongoDB.Test var dic = new ConcurrentDictionary(); Parallel.For(0, 30, (x) => { - var id = new MongoDBUtil().GetNextSequenceValue(_database, _recieved); + var id = new MongoDBUtil().GetNextSequenceValue(Database, MongoDBOptions.ReceivedCollection); id.Should().BeGreaterThan(0); dic.TryAdd(id, x).Should().BeTrue(); //The id shouldn't be same. });