Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
CAP
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
CAP
Commits
9bc0909c
Commit
9bc0909c
authored
Jul 23, 2018
by
Savorboard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring unit tests have been adapted to apperyor
parent
30853903
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
133 additions
and
116 deletions
+133
-116
ConnectionUtil.cs
test/DotNetCore.CAP.MongoDB.Test/ConnectionUtil.cs
+1
-1
DatabaseTestHost.cs
test/DotNetCore.CAP.MongoDB.Test/DatabaseTestHost.cs
+55
-0
DotNetCore.CAP.MongoDB.Test.csproj
...tCore.CAP.MongoDB.Test/DotNetCore.CAP.MongoDB.Test.csproj
+5
-6
MongoDBMonitoringApiTest.cs
test/DotNetCore.CAP.MongoDB.Test/MongoDBMonitoringApiTest.cs
+11
-22
MongoDBStorageConnectionTest.cs
...tNetCore.CAP.MongoDB.Test/MongoDBStorageConnectionTest.cs
+34
-26
MongoDBStorageTest.cs
test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageTest.cs
+14
-23
MongoDBTransactionTest.cs
test/DotNetCore.CAP.MongoDB.Test/MongoDBTransactionTest.cs
+9
-15
MongoDBUtilTest.cs
test/DotNetCore.CAP.MongoDB.Test/MongoDBUtilTest.cs
+4
-23
No files found.
test/DotNetCore.CAP.MongoDB.Test/ConnectionUtil.cs
View file @
9bc0909c
...
...
@@ -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
test/DotNetCore.CAP.MongoDB.Test/DatabaseTestHost.cs
0 → 100644
View file @
9bc0909c
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
<
IMongoClient
>();
protected
IMongoDatabase
Database
=>
MongoClient
.
GetDatabase
(
MongoDBOptions
.
DatabaseName
);
protected
CapOptions
CapOptions
=>
Provider
.
GetService
<
CapOptions
>();
protected
MongoDBOptions
MongoDBOptions
=>
Provider
.
GetService
<
MongoDBOptions
>();
protected
DatabaseTestHost
()
{
CreateServiceCollection
();
CreateDatabase
();
}
private
void
CreateDatabase
()
{
Provider
.
GetService
<
MongoDBStorage
>().
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
<
IMongoClient
>(
x
=>
new
MongoClient
(
_connectionString
));
services
.
AddSingleton
<
MongoDBStorage
>();
AddService
(
services
);
Provider
=
services
.
BuildServiceProvider
();
}
public
void
Dispose
()
{
MongoClient
.
DropDatabase
(
MongoDBOptions
.
DatabaseName
);
}
}
}
\ No newline at end of file
test/DotNetCore.CAP.MongoDB.Test/DotNetCore.CAP.MongoDB.Test.csproj
View file @
9bc0909c
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
<PackageReference Include="FluentAssertions" Version="5.4.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.
0
" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.
2
" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="Xunit.Priority" Version="1.0.10" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>
<ItemGroup>
...
...
test/DotNetCore.CAP.MongoDB.Test/MongoDBMonitoringApiTest.cs
View file @
9bc0909c
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
<
CapPublishedMessage
>(
_options
.
PublishedCollection
);
collection
.
InsertMany
(
new
CapPublishedMessage
[]
var
collection
=
Database
.
GetCollection
<
CapPublishedMessage
>(
MongoDBOptions
.
PublishedCollection
);
collection
.
InsertMany
(
new
[]
{
new
CapPublishedMessage
{
Id
=
helper
.
GetNextSequenceValue
(
database
,
_o
ptions
.
PublishedCollection
),
Id
=
helper
.
GetNextSequenceValue
(
Database
,
MongoDBO
ptions
.
PublishedCollection
),
Added
=
DateTime
.
Now
.
AddHours
(-
1
),
StatusName
=
"Failed"
,
Content
=
"abc"
},
new
CapPublishedMessage
{
Id
=
helper
.
GetNextSequenceValue
(
database
,
_o
ptions
.
PublishedCollection
),
Id
=
helper
.
GetNextSequenceValue
(
Database
,
MongoDBO
ptions
.
PublishedCollection
),
Added
=
DateTime
.
Now
,
StatusName
=
"Failed"
,
Content
=
"bbc"
...
...
test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageConnectionTest.cs
View file @
9bc0909c
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
<
MongoDBStorage
>().
GetConnection
();
public
MongoDBStorageConnectionTest
()
{
_client
=
new
MongoClient
(
ConnectionUtil
.
ConnectionString
);
_options
=
new
MongoDBOptions
();
_storage
=
new
MongoDBStorage
(
new
CapOptions
(),
_options
,
_client
,
NullLogger
<
MongoDBStorage
>.
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
<
CapReceivedMessage
>(
_options
.
ReceivedCollection
);
StoreReceivedMessageAsync_TestAsync
();
var
collection
=
Database
.
GetCollection
<
CapReceivedMessage
>(
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
<
CapReceivedMessage
>(
MongoDBOptions
.
ReceivedCollection
);
var
updateDef
=
Builders
<
CapReceivedMessage
>
.
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
);
...
...
test/DotNetCore.CAP.MongoDB.Test/MongoDBStorageTest.cs
View file @
9bc0909c
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
<
MongoDBStorage
>.
Instance
);
await
storage
.
InitializeAsync
(
default
(
CancellationToken
));
var
names
=
_client
.
ListDatabaseNames
()?.
ToList
();
names
.
Should
().
Contain
(
options
.
Database
);
var
storage
=
Provider
.
GetService
<
MongoDBStorage
>();
var
names
=
MongoClient
.
ListDatabaseNames
()?.
ToList
();
names
.
Should
().
Contain
(
MongoDBOptions
.
DatabaseName
);
var
collections
=
_client
.
GetDatabase
(
options
.
Database
)
.
ListCollectionNames
()?.
ToList
();
collections
.
Should
().
Contain
(
o
ptions
.
PublishedCollection
);
collections
.
Should
().
Contain
(
o
ptions
.
ReceivedCollection
);
collections
.
Should
().
Contain
(
"Counter"
);
var
collections
=
Database
.
ListCollectionNames
()?.
ToList
();
collections
.
Should
().
Contain
(
MongoDBO
ptions
.
PublishedCollection
);
collections
.
Should
().
Contain
(
MongoDBO
ptions
.
ReceivedCollection
);
collections
.
Should
().
Contain
(
MongoDBOptions
.
CounterCollection
);
var
collection
=
_client
.
GetDatabase
(
options
.
Database
).
GetCollection
<
BsonDocument
>(
"Counter"
);
collection
.
CountDocuments
(
new
BsonDocument
{
{
"_id"
,
o
ptions
.
PublishedCollection
}
}).
Should
().
Be
(
1
);
collection
.
CountDocuments
(
new
BsonDocument
{
{
"_id"
,
o
ptions
.
ReceivedCollection
}
}).
Should
().
Be
(
1
);
var
collection
=
Database
.
GetCollection
<
BsonDocument
>(
MongoDBOptions
.
CounterCollection
);
collection
.
CountDocuments
(
new
BsonDocument
{
{
"_id"
,
MongoDBO
ptions
.
PublishedCollection
}
}).
Should
().
Be
(
1
);
collection
.
CountDocuments
(
new
BsonDocument
{
{
"_id"
,
MongoDBO
ptions
.
ReceivedCollection
}
}).
Should
().
Be
(
1
);
}
}
}
\ No newline at end of file
test/DotNetCore.CAP.MongoDB.Test/MongoDBTest.cs
→
test/DotNetCore.CAP.MongoDB.Test/MongoDBT
ransactionT
est.cs
View file @
9bc0909c
...
...
@@ -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
=
_c
lient
.
ListDatabaseNames
();
var
names
=
MongoC
lient
.
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
=
_c
lient
.
GetDatabase
(
"test"
);
var
db
=
MongoC
lient
.
GetDatabase
(
"test"
);
var
collection1
=
db
.
GetCollection
<
BsonDocument
>(
"test1"
);
var
collection2
=
db
.
GetCollection
<
BsonDocument
>(
"test2"
);
using
(
var
sesstion
=
_c
lient
.
StartSession
())
using
(
var
sesstion
=
MongoC
lient
.
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
=
_c
lient
.
GetDatabase
(
"test"
);
var
db
=
MongoC
lient
.
GetDatabase
(
"test"
);
var
collection
=
db
.
GetCollection
<
BsonDocument
>(
"test3"
);
var
collection4
=
db
.
GetCollection
<
BsonDocument
>(
"test4"
);
using
(
var
session
=
_c
lient
.
StartSession
())
using
(
var
session
=
MongoC
lient
.
StartSession
())
{
session
.
IsInTransaction
.
Should
().
BeFalse
();
session
.
StartTransaction
();
...
...
test/DotNetCore.CAP.MongoDB.Test/MongoDBUtilTest.cs
View file @
9bc0909c
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
<
BsonDocument
>(
"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
<
int
,
int
>();
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.
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment