public async Task TransactionScope_WithoutMaxRetries_ShouldRetryUntilSuccessful() { var throwingDummy = new ThrowingDummy(4); var repo = _mongoClient.GetRepository <ArrayContainer>(); var doc = new ArrayContainer(new List <Dummy> { new Dummy("olle"), new Dummy("bengt") }, "roy"); await repo.InsertAsync(doc); await repo.WithTransactionAsync(async() => { doc.Dummies.Add(new Dummy("rolle")); await repo.ReplaceOneAsync( x => x.Id == doc.Id, doc ); await repo.UpdateOneAsync( x => x.Id == doc.Id, x => x.Push(x => x.Dummies, new Dummy("fia")) ); }, TransactionType.TransactionScope, 3); var allSaved = await _mongoClient.GetRepository <ArrayContainer>().GetAll().ToListAsync(); allSaved.Should().ContainSingle() .Which.Dummies.Should().HaveCount(4); }
public async Task AutomaticallyEnlistsWithAmbientTransaction() { var throwingDummy = new ThrowingDummy(4); var repo = _mongoClient.GetRepository <ArrayContainer>("tenant_c"); var doc = new ArrayContainer(new List <Dummy> { new Dummy("olle"), new Dummy("bengt") }, "roy"); await repo.InsertAsync(doc); using (var trans = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { doc.Dummies.Add(new Dummy("rolle")); await repo.ReplaceOneAsync( x => x.Id == doc.Id, doc ); await repo.UpdateOneAsync( x => x.Id == doc.Id, x => x.Push(x => x.Dummies, new Dummy("fia")) ); trans.Complete(); } var allSaved = await repo.GetAll().ToListAsync(); allSaved.Should().ContainSingle() .Which.Dummies.Should().HaveCount(4); }
public async Task NativeTransaction_WithoutMaxRetries_ShouldRetryUntilSuccessful() { var throwingDummy = new ThrowingDummy(4); var repo = _mongoClient.GetRepository <MyStandaloneEntity>(); await repo.WithTransactionAsync(async() => { throwingDummy.TryMe(); await repo.InsertAsync(new MyStandaloneEntity("test", new SharedClass("test"))); }, maxRetries : 0); var allSaved = await _mongoClient.GetRepository <MyStandaloneEntity>().GetAll().ToListAsync(); allSaved.Should().HaveCount(1); }
public async Task TransactionScope_WithMaxRetries_ShouldRetryUntilMaxRetriesReached() { var throwingDummy = new ThrowingDummy(5); var repo = _mongoClient.GetRepository <MyStandaloneEntity>(); await Assert.ThrowsAsync <MongoException>(async() => { await repo.WithTransactionAsync(async() => { throwingDummy.TryMe(); await repo.InsertAsync(new MyStandaloneEntity("test", new SharedClass("test"))); }, TransactionType.TransactionScope, maxRetries: 3); }); var allSaved = await _mongoClient.GetRepository <MyStandaloneEntity>().GetAll().ToListAsync(); allSaved.Should().BeEmpty(); }