public void TestConstructorWithAlreadyEndedTransaction()
        {
            WeightedTransaction <Transaction> testTransaction = new WeightedTransaction <Transaction>(
                Transaction.EndedDummy
                );

            IObservationSubscriber subscriber = this.mockery.NewMock <IObservationSubscriber>();

            Expect.AtLeast(0).On(subscriber).Method("ProgressUpdated");
            // This should no be called because otherwise, the 'Ended' event would be raised
            // to the transaction group before all transactions have been added into
            // the internal list, leading to an early ending or even multiple endings.
            Expect.Never.On(subscriber).Method("Ended");

            using (
                ObservedWeightedTransaction <Transaction> test =
                    new ObservedWeightedTransaction <Transaction>(
                        testTransaction,
                        new ObservedWeightedTransaction <Transaction> .ReportDelegate(
                            subscriber.ProgressUpdated
                            ),
                        new ObservedWeightedTransaction <Transaction> .ReportDelegate(
                            subscriber.Ended
                            )
                        )
                ) {
                this.mockery.VerifyAllExpectationsHaveBeenMet();
            }
        }
        public void TestConstructorWithEndingTransaction()
        {
            WeightedTransaction <Transaction> testTransaction = new WeightedTransaction <Transaction>(
                new FunkyTransaction()
                );

            IObservationSubscriber subscriber = this.mockery.NewMock <IObservationSubscriber>();

            Expect.AtLeast(0).On(subscriber).Method("ProgressUpdated");
            Expect.Once.On(subscriber).Method("Ended");

            using (
                ObservedWeightedTransaction <Transaction> test =
                    new ObservedWeightedTransaction <Transaction>(
                        testTransaction,
                        new ObservedWeightedTransaction <Transaction> .ReportDelegate(
                            subscriber.ProgressUpdated
                            ),
                        new ObservedWeightedTransaction <Transaction> .ReportDelegate(
                            subscriber.Ended
                            )
                        )
                ) {
                this.mockery.VerifyAllExpectationsHaveBeenMet();
            }
        }
        public void TestConstructorWithEndingTransaction()
        {
            WeightedTransaction <Transaction> testTransaction = new WeightedTransaction <Transaction>(
                new FunkyTransaction()
                );

            Mock <IObservationSubscriber> subscriber = this.mockery.CreateMock <IObservationSubscriber>();

            subscriber.Expects.AtLeast(0).Method(m => m.ProgressUpdated());
            subscriber.Expects.One.Method(m => m.Ended());

            using (
                ObservedWeightedTransaction <Transaction> test =
                    new ObservedWeightedTransaction <Transaction>(
                        testTransaction,
                        new ObservedWeightedTransaction <Transaction> .ReportDelegate(
                            subscriber.MockObject.ProgressUpdated
                            ),
                        new ObservedWeightedTransaction <Transaction> .ReportDelegate(
                            subscriber.MockObject.Ended
                            )
                        )
                ) {
                this.mockery.VerifyAllExpectationsHaveBeenMet();
            }
        }
        public void TestWeightStorage()
        {
            TestTransaction transaction = new TestTransaction();
            WeightedTransaction <Transaction> testWrapper = new WeightedTransaction <Transaction>(
                transaction, 12.0f
                );

            Assert.AreEqual(12.0f, testWrapper.Weight);
        }
        public void TestDefaultWeight()
        {
            TestTransaction transaction = new TestTransaction();
            WeightedTransaction <Transaction> testWrapper = new WeightedTransaction <Transaction>(
                transaction
                );

            Assert.AreEqual(1.0f, testWrapper.Weight);
        }
        public void TestTransactionStorage()
        {
            TestTransaction transaction = new TestTransaction();
            WeightedTransaction <Transaction> testWrapper = new WeightedTransaction <Transaction>(
                transaction
                );

            Assert.AreSame(transaction, testWrapper.Transaction);
        }
        /// <summary>Initializes a new observed transaction</summary>
        /// <param name="weightedTransaction">Weighted transaction being observed</param>
        /// <param name="progressUpdateCallback">
        ///   Callback to invoke when the transaction's progress changes
        /// </param>
        /// <param name="endedCallback">
        ///   Callback to invoke when the transaction has ended
        /// </param>
        internal ObservedWeightedTransaction(
            WeightedTransaction <TransactionType> weightedTransaction,
            ReportDelegate progressUpdateCallback,
            ReportDelegate endedCallback
            )
        {
            this.weightedTransaction = weightedTransaction;

            // See if this transaction has already ended (initial check for performance)
            if (weightedTransaction.Transaction.Ended)
            {
                // Since we don't subscribe to the .Ended event (which would be fired immediately on
                // subscription if the transaction was already finished), we will emulate this
                // behavior here. There is no race condition here: The transition to .Ended occurs
                // only once and will never happen in reverse. This is just a minor optimization to
                // prevent object coupling where none is neccessary and to save some processing time.
                this.progress = 1.0f;
                progressUpdateCallback();

                // Do not call the ended callback here. This constructor is called when the
                // TransactionGroup constructs its list of transactions. If this is called and
                // the first transaction to be added to the group happens to be in the ended
                // state, the transactionGroup will immediately think it has ended!
                //!DONT!endedCallback();

                return;
            }

            this.endedCallback          = endedCallback;
            this.progressUpdateCallback = progressUpdateCallback;

            // This might trigger the event handler to be invoked right here if the transaction
            // ended between our initial optimization attempt and this line. It's unlikely,
            // however, so we'll not waste time with another optimization attempt.
            this.weightedTransaction.Transaction.AsyncEnded += new EventHandler(asyncEnded);

            // See whether this transaction implements the IProgressReporter interface and if
            // so, connect to its progress report event in order to pass these reports on
            // to whomever created ourselfes.
            this.progressReporter = this.weightedTransaction.Transaction as IProgressReporter;
            if (this.progressReporter != null)
            {
                this.asyncProgressChangedEventHandler = new EventHandler <ProgressReportEventArgs>(
                    asyncProgressChanged
                    );
                this.progressReporter.AsyncProgressChanged += this.asyncProgressChangedEventHandler;
            }
        }
Пример #8
0
        public void TestWrapperCollection()
        {
            WeightedTransaction <Transaction> transaction = new WeightedTransaction <Transaction>(
                Transaction.EndedDummy
                );

            ObservedWeightedTransaction <Transaction> observed =
                new ObservedWeightedTransaction <Transaction>(
                    transaction,
                    endedCallback,
                    progressUpdatedCallback
                    );

            WeightedTransactionWrapperCollection <Transaction> wrapper =
                new WeightedTransactionWrapperCollection <Transaction>(
                    new ObservedWeightedTransaction <Transaction>[] { observed }
                    );

            Assert.AreSame(transaction, wrapper[0]);
        }