Exemplo n.º 1
0
        /// <summary>
        ///		Create new writer instance
        /// </summary>
        /// <param name="targetFolder">
        ///		The target of the writer (subtree root).
        /// </param>
        internal RepositoryWriter(IFolder targetFolder)
        {
            _itemAddedEvent = new FastSmartWeakEvent <EventHandler <Events.DataItemAddedEventArgs> >();
            _repoFolder     = targetFolder;
            _directWritersByRelativePath = new Dictionary <string, DirectSingleFolderWriter>();
            _directWritersByFolderKey    = new Dictionary <string, DirectSingleFolderWriter>();
            _trackUnsavedItems           = true;

            //this.DesiredFileSize = Constants.DefaultDataItemsPerFile;

            // must initialise active router
            this.DataRouter = null;

            _transactionManager = new LongSlaveTransactionManager(Folder.Repository, null);
        }
        internal DirectSingleFolderWriter(
            IFolder repoFolder
            , ICoder coder
            , ICoder encryptor)
        {
            this.Coder     = coder;
            this.Encryptor = encryptor;
            _targetFolder  = repoFolder;
            _dirty         = false;

            _desiredItemsPerFile = repoFolder.Properties.DesiredItemsPerFile;
            if (0 >= _desiredItemsPerFile)
            {
                _desiredItemsPerFile = Constants.DefaultDataItemsPerFile;
            }

            _trackUnsavedItems = false;
            SetUpUnsavedItemsTracking();

            _transactionManager = new LongSlaveTransactionManager(_targetFolder.Repository, (ITransactionNotification)this);
        }
        public void TestRepeatedReuseSlave()
        {
            if (!EnvironmentSupportsTransactions)
            {
                Assert.Inconclusive("Cannot test transactions in this environment");
            }
            else
            {
                Assume.That(!FileSystemProvider.IsStorageAmbientTransactionActive);

                Repository.Settings.StorageTransactionSettings = StorageTransactionSettings.RequireTransactions;

                TransactionSubscriber  subscriber = new TransactionSubscriber();
                IFileSystemTransaction transaction;

                LongSlaveTransactionManager tman = new LongSlaveTransactionManager(Repository, subscriber);

                using (TransactionScope masterScope = new TransactionScope())
                {
                    using (var scope = tman.GetTransactionScope())
                    {
                        Assert.IsTrue(scope.HasChangedContext);
                        Assert.IsFalse(scope.IsNullScope);
                        Assert.IsFalse(scope.IsTransactionOwner);
                        Assert.IsFalse(scope.NoTransaction);
                        Assert.AreSame(AmbientTransaction, tman.PendingTransaction);
                        Assert.AreSame(scope.UnderlyingTransaction, tman.PendingTransaction);

                        Assert.IsTrue(tman.CanIOTransactionSpanMultipleRepositoryCalls);

                        Assert.IsTrue(tman.IsTransactionPending());

                        Assert.AreEqual(1, StorageTransactionScope.ScopeNestLevel);
                        transaction = AmbientTransaction;
                    }

                    Assert.AreEqual(0, StorageTransactionScope.ScopeNestLevel);
                    Assert.IsNull(AmbientTransaction);
                    Assert.IsNotNull(tman.PendingTransaction);
                    Assert.IsFalse(tman.CanIOTransactionSpanMultipleRepositoryCalls, "No ambientr transaction here");

                    for (int n = 0; n < 10; ++n)
                    {
                        using (var scope = tman.GetTransactionScope())
                        {
                            Assert.IsTrue(scope.HasChangedContext);
                            Assert.IsFalse(scope.IsNullScope);
                            Assert.IsFalse(scope.IsTransactionOwner);
                            Assert.IsFalse(scope.NoTransaction);

                            Assert.AreSame(transaction, tman.PendingTransaction);
                            Assert.AreSame(AmbientTransaction, tman.PendingTransaction);
                            Assert.AreSame(scope.UnderlyingTransaction, tman.PendingTransaction);

                            Assert.IsTrue(tman.CanIOTransactionSpanMultipleRepositoryCalls);

                            Assert.IsTrue(tman.IsTransactionPending());

                            Assert.AreEqual(1, StorageTransactionScope.ScopeNestLevel);
                            transaction = AmbientTransaction;
                        }
                        Assert.AreEqual(0, StorageTransactionScope.ScopeNestLevel);
                        Assert.IsNull(AmbientTransaction);
                        Assert.IsNotNull(tman.PendingTransaction);
                        Assert.IsFalse(tman.CanIOTransactionSpanMultipleRepositoryCalls, "No ambientr transaction here");
                    }

                    //masterScope.Complete();
                }                 // using (TransactionScope masterScope = new TransactionScope())

                System.Threading.Thread.Sleep(50);

                Console.WriteLine("About to test times notified");

                Assert.AreEqual(1, subscriber.TimesNotified, "Only 1 storage transaction must have been instantiated and therefore 1 notification received");
                Assert.IsFalse(subscriber.Committed, "Master scope has not been completed");
                //Assert.IsFalse(transaction.IsActive);

                Assert.IsNull(tman.PendingTransaction, "Must have received notification and reset pending transaction");
                Assert.IsFalse(tman.CanIOTransactionSpanMultipleRepositoryCalls, "There should be no ambient transaction here at all");

                tman.Dispose();

                Assert.Throws <ObjectDisposedException>(() => tman.GetTransactionScope());
            }
        }