예제 #1
0
        public async Task It_invokes_callbacks_when_session_is_completed()
        {
            using (var scope = new TransactionScope())
            {
                var transportTransaction = new TransportTransaction();
                transportTransaction.Set(Transaction.Current);

                var callbackInvoked = false;
                var adapter         = new NHibernateSynchronizedStorageAdapter(SessionFactory);

                using (var storageSession = await adapter.TryAdapt(transportTransaction, new ContextBag()))
                {
                    storageSession.Session(); //Make sure session is initialized
                    storageSession.OnSaveChanges(s =>
                    {
                        callbackInvoked = true;
                        return(Task.FromResult(0));
                    });

                    await storageSession.CompleteAsync();

                    Assert.IsTrue(callbackInvoked);
                }

                scope.Complete();
            }
        }
예제 #2
0
        public async Task It_invokes_callbacks_when_outbox_transaction_is_completed()
        {
            var session     = SessionFactory.OpenSession();
            var transaction = session.BeginTransaction();

            var callbackInvoked = false;

            using (var outboxTransaction = new NHibernateOutboxTransaction(session, transaction))
            {
                var adapter = new NHibernateSynchronizedStorageAdapter(SessionFactory);

                using (var storageSession = await adapter.TryAdapt(outboxTransaction, new ContextBag()))
                {
                    storageSession.Session(); //Make sure session is initialized
                    storageSession.OnSaveChanges(s =>
                    {
                        callbackInvoked = true;
                        return(Task.FromResult(0));
                    });

                    await storageSession.CompleteAsync();
                }

                await outboxTransaction.Commit();
            }

            Assert.IsTrue(callbackInvoked);
        }
예제 #3
0
        public async Task It_does_not_commit_outbox_transaction_if_callback_throws()
        {
            var entityId        = Guid.NewGuid().ToString();
            var exceptionThrown = false;

            using (var session = SessionFactory.OpenSession())
            {
                var transaction = session.BeginTransaction();

                using (var outboxTransaction = new NHibernateOutboxTransaction(session, transaction))
                {
                    var adapter = new NHibernateSynchronizedStorageAdapter(SessionFactory);

                    using (var storageSession = await adapter.TryAdapt(outboxTransaction, new ContextBag()))
                    {
                        storageSession.Session().Save(new TestEntity
                        {
                            Id = entityId
                        });
                        storageSession.OnSaveChanges(s =>
                        {
                            throw new Exception("Simulated");
                        });

                        try
                        {
                            await storageSession.CompleteAsync();

                            await outboxTransaction.Commit();
                        }
                        catch (Exception)
                        {
                            exceptionThrown = true;
                        }
                    }
                }
                Assert.IsTrue(exceptionThrown);
            }

            using (var session = SessionFactory.OpenSession())
            {
                var savedEntity = session.Get <TestEntity>(entityId);
                Assert.IsNull(savedEntity);
            }
        }
예제 #4
0
        public async Task It_does_not_commit_if_callback_throws()
        {
            var entityId = Guid.NewGuid().ToString();

            using (var scope = new TransactionScope())
            {
                var transportTransaction = new TransportTransaction();
                transportTransaction.Set(Transaction.Current);

                var adapter = new NHibernateSynchronizedStorageAdapter(SessionFactory);

                using (var storageSession = await adapter.TryAdapt(transportTransaction, new ContextBag()))
                {
                    storageSession.Session().Save(new TestEntity()
                    {
                        Id = entityId
                    });
                    storageSession.OnSaveChanges(s =>
                    {
                        throw new Exception("Simulated");
                    });

                    try
                    {
                        await storageSession.CompleteAsync();

                        scope.Complete();
                    }
                    catch (Exception)
                    {
                        //NOOP
                    }
                }
            }

            using (var session = SessionFactory.OpenSession())
            {
                var savedEntity = session.Get <TestEntity>(entityId);
                Assert.IsNull(savedEntity);
            }
        }