public void Setup() { _mongo = MongoTestHelper.GetCachedMockMongoServer(); _mongo.IsDatabaseNameValid(Arg.Any <string>(), out _message).Returns(true); _mongo.GetDatabase(Arg.Any <string>(), Arg.Any <WriteConcern>()).Returns(x => MongoTestHelper.GetMockMongoDatabase((string)x[0], (WriteConcern)x[1])); TransactionSynchronizationManager.InitSynchronization(); }
public void SessionCallbackWithinSynchronizedTransaction() { SingleConnectionFactory scf = new SingleConnectionFactory(mockConnectionFactory); NmsTemplate template = CreateTemplate(); template.ConnectionFactory = scf; mockConnection.Start(); LastCall.On(mockConnection).Repeat.Times(2); // We're gonna call getTransacted 3 times, i.e. 2 more times. Expect.Call(mockSession.Transacted).Return(UseTransactedSession).Repeat.Twice(); if (UseTransactedTemplate) { mockSession.Commit(); LastCall.On(mockSession).Repeat.Once(); } mockSession.Close(); LastCall.On(mockSession).Repeat.Once(); mockConnection.Stop(); LastCall.On(mockConnection).Repeat.Once(); mockConnection.Close(); LastCall.On(mockConnection).Repeat.Once(); mocks.ReplayAll(); TransactionSynchronizationManager.InitSynchronization(); try { template.Execute(session => { bool b = session.Transacted; return(null); }); template.Execute(session => { bool b = session.Transacted; return(null); }); Assert.AreSame(mockSession, ConnectionFactoryUtils.GetTransactionalSession(scf, null, false)); Assert.AreSame(mockSession, ConnectionFactoryUtils.GetTransactionalSession(scf, scf.CreateConnection(), false)); //In Java this test was doing 'double-duty' and testing TransactionAwareConnectionFactoryProxy, which has //not been implemented in .NET template.Execute(session => { bool b = session.Transacted; return(null); }); IList synchs = TransactionSynchronizationManager.Synchronizations; Assert.AreEqual(1, synchs.Count); ITransactionSynchronization synch = (ITransactionSynchronization)synchs[0]; synch.BeforeCommit(false); synch.BeforeCompletion(); synch.AfterCommit(); synch.AfterCompletion(TransactionSynchronizationStatus.Unknown); } finally { TransactionSynchronizationManager.ClearSynchronization(); //Assert.IsTrue(TransactionSynchronizationManager.ResourceDictionary.Count == 0); //Assert.IsFalse(TransactionSynchronizationManager.SynchronizationActive); scf.Dispose(); } Assert.IsTrue(TransactionSynchronizationManager.ResourceDictionary.Count == 0); mocks.VerifyAll(); }