You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

49 lines
1.5 KiB

  1. using System.Collections.Concurrent;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using DotNetCore.CAP.Models;
  5. using FluentAssertions;
  6. using MongoDB.Bson;
  7. using MongoDB.Driver;
  8. using Xunit;
  9. namespace DotNetCore.CAP.MongoDB.Test
  10. {
  11. public class MongoDBUtilTest
  12. {
  13. private readonly IMongoDatabase _database;
  14. string _recieved = "ReceivedTest";
  15. public MongoDBUtilTest()
  16. {
  17. var client = new MongoClient(ConnectionUtil.ConnectionString);
  18. _database = client.GetDatabase("CAP_Test");
  19. //Initialize MongoDB
  20. if (_database.ListCollectionNames().ToList().All(x => x != "Counter"))
  21. {
  22. var collection = _database.GetCollection<BsonDocument>("Counter");
  23. collection.InsertOne(new BsonDocument { { "_id", _recieved }, { "sequence_value", 0 } });
  24. }
  25. }
  26. [Fact]
  27. public async void GetNextSequenceValueAsync_Test()
  28. {
  29. var id = await new MongoDBUtil().GetNextSequenceValueAsync(_database, _recieved);
  30. id.Should().BeGreaterThan(0);
  31. }
  32. [Fact]
  33. public void GetNextSequenceValue_Concurrency_Test()
  34. {
  35. var dic = new ConcurrentDictionary<int, int>();
  36. Parallel.For(0, 30, (x) =>
  37. {
  38. var id = new MongoDBUtil().GetNextSequenceValue(_database, _recieved);
  39. id.Should().BeGreaterThan(0);
  40. dic.TryAdd(id, x).Should().BeTrue(); //The id shouldn't be same.
  41. });
  42. }
  43. }
  44. }