I'm using C# mongodb driver
TestCase: 1
[Fact]
public void Should_Return_Success_Status_Code_For_All_Concurrent_Requests_DbCommand1()
{
//Arrange
Parallel.ForEach(Enumerable.Range(1, 5), i =>
{
string cmd = "{ \"update\" : \"brands\", \"ordered\" : true, \"updates\" : [{ \"q\" : { \"id\" : \"brandid8cdae601-4ec3-411b-9793-62c69a8a0431\", \"fromDate\" : { \"$lte\" : \"2018-05-31T21:52:00+00:00\" }, \"toDate\" : { \"$gt\" : \"2018-05-31T21:52:00+00:00\" } }, \"u\" : { \"created\" : \"2018-05-31T21:53:00+00:00\", \"lastUpdated\" : \"2018-05-31T21:53:00+00:00\", \"status\" : \"Active\", \"fromDate\" : \"2018-05-31T21:52:00+00:00\", \"toDate\" : \"9999-12-31T23:59:00+00:00\", \"id\" : \"brandid8cdae601-4ec3-411b-9793-62c69a8a0431\", \"name\" : \"BrandName40e26234-43d6-499e-93a8-d34a54275e7f\" }, \"upsert\" : true }] }";
Db.RunCommand(new JsonCommand<BsonDocument>(cmd));
});
//Assert
string cmdDoc = $@"{{find: '{DocumentCollection.Brands}',filter:{{id:'brandid8cdae601-4ec3-411b-9793-62c69a8a0431'}}, }}";
var result = Db.RunCommand(new JsonCommand<BsonDocument>(cmdDoc));
var brandDocuments = result["cursor"]["firstBatch"].AsBsonArray;
brandDocuments.Count.Should().Be(1);
}
TestCase: 2
[Fact]
public void Should_Return_Success_Status_Code_For_All_Concurrent_Requests_DbCommand()
{
string cmd1 = "{ \"update\" : \"brands\", \"ordered\" : true, \"updates\" : [{ \"q\" : { \"id\" : \"brandid8cdae601-4ec3-411b-9793-62c69a8a0431\", \"fromDate\" : { \"$lte\" : \"2018-05-31T21:52:00+00:00\" }, \"toDate\" : { \"$gt\" : \"2018-05-31T21:52:00+00:00\" } }, \"u\" : { \"created\" : \"2018-05-31T21:53:00+00:00\", \"lastUpdated\" : \"2018-05-31T21:53:00+00:00\", \"status\" : \"Active\", \"fromDate\" : \"2018-05-31T21:52:00+00:00\", \"toDate\" : \"9999-12-31T23:59:00+00:00\", \"id\" : \"brandid8cdae601-4ec3-411b-9793-62c69a8a0431\", \"name\" : \"BrandName40e26234-43d6-499e-93a8-d34a54275e7f\" }, \"upsert\" : true }] }";
Db.RunCommand(new JsonCommand<BsonDocument>(cmd1));
//Arrange
Parallel.ForEach(Enumerable.Range(1, 5), i =>
{
string cmd = "{ \"update\" : \"brands\", \"ordered\" : true, \"updates\" : [{ \"q\" : { \"id\" : \"brandid8cdae601-4ec3-411b-9793-62c69a8a0431\", \"fromDate\" : { \"$lte\" : \"2018-05-31T21:52:00+00:00\" }, \"toDate\" : { \"$gt\" : \"2018-05-31T21:52:00+00:00\" } }, \"u\" : { \"created\" : \"2018-05-31T21:53:00+00:00\", \"lastUpdated\" : \"2018-05-31T21:53:00+00:00\", \"status\" : \"Active\", \"fromDate\" : \"2018-05-31T21:52:00+00:00\", \"toDate\" : \"9999-12-31T23:59:00+00:00\", \"id\" : \"brandid8cdae601-4ec3-411b-9793-62c69a8a0431\", \"name\" : \"BrandName40e26234-43d6-499e-93a8-d34a54275e7f\" }, \"upsert\" : true }] }";
Db.RunCommand(new JsonCommand<BsonDocument>(cmd));
});
//Assert
string cmdDoc = $@"{{find: '{DocumentCollection.Brands}',filter:{{id:'brandid8cdae601-4ec3-411b-9793-62c69a8a0431'}}, }}";
var result = Db.RunCommand(new JsonCommand<BsonDocument>(cmdDoc));
var brandDocuments = result["cursor"]["firstBatch"].AsBsonArray;
brandDocuments.Count.Should().Be(1);
}
Testcase 1 creates duplicate documents whereas the Testcase 2 doesn't.
the only difference between test case 1 & 2 is , In Testcase 2 I make sure that the document already exists in the database before concurrent requests
Is this supposed to be the expected behavior ?