Exemplo n.º 1
0
        public void ShouldCreateOutputAssetWithAccountSelectionStrategy()
        {
            CapacityBasedAccountSelectionStrategy strategy = CapacityBasedAccountSelectionStrategy.FromAccounts(context, true);

            string inputAssetFilePath = Path.Combine(Directory.GetCurrentDirectory(), smallWmv);
            string inputAssetFileName = Path.GetFileName(inputAssetFilePath);

            this.inputAsset = context.Assets.Create("InputAsset", strategy, AssetCreationOptions.StorageEncrypted);
            IAssetFile file = this.inputAsset.AssetFiles.Create(inputAssetFileName);

            file.Upload(inputAssetFilePath);

            IJob  job  = context.Jobs.Create("Job to test using an account selection strategy for an output asset");
            ITask task = job.Tasks.AddNew(
                "Task to test using an account selection strategy for an output asset",
                context.MediaProcessors.GetLatestMediaProcessorByName(MediaProcessorNames.MediaEncoderStandard),
                MediaEncoderStandardTaskPresetStrings.H264SingleBitrate4x3SD,
                TaskOptions.None);

            task.InputAssets.Add(this.inputAsset);
            task.OutputAssets.AddNew("OutputAsset", strategy, AssetCreationOptions.None);

            job.Submit();
            job.GetExecutionProgressTask(CancellationToken.None).Wait();

            Assert.IsNotNull(job);
            Assert.AreEqual(1, job.Tasks.Count, "Unexpected number of tasks in job");
            Assert.AreEqual(1, job.OutputMediaAssets.Count, "Unexpected number of output assets in the job");

            this.outputAsset = job.OutputMediaAssets[0];

            Assert.IsNotNull(outputAsset.StorageAccountName, "Storage account name in output assset is null");
        }
Exemplo n.º 2
0
        public void ShouldCreateOutputAssetWithAccountSelectionStrategy()
        {
            CapacityBasedAccountSelectionStrategy strategy = CapacityBasedAccountSelectionStrategy.FromAccounts(context, true);

            string inputAssetFilePath = Path.Combine(TestContext.TestDeploymentDir, smallWmv);
            string inputAssetFileName = Path.GetFileName(inputAssetFilePath);

            IAsset     inputAsset = context.Assets.Create("", strategy, AssetCreationOptions.StorageEncrypted);
            IAssetFile file       = inputAsset.AssetFiles.Create(inputAssetFileName);

            file.Upload(inputAssetFilePath);

            IJob  job  = context.Jobs.Create("Job to test using an account selection strategy for an output asset");
            ITask task = job.Tasks.AddNew("Task to test using an account selection strategy for an output asset", GetMediaProcessor(Encoder), Preset, TaskOptions.None);

            task.InputAssets.Add(inputAsset);
            task.OutputAssets.AddNew("OutputAsset", strategy, AssetCreationOptions.None);

            job.Submit();

            // Note that we don't want for the job to finish.  We just need the submit to succeed.
            IJob refreshedJob = context.Jobs.Where(c => c.Id == job.Id).FirstOrDefault();

            Assert.IsNotNull(refreshedJob);
            Assert.AreEqual(1, refreshedJob.Tasks.Count, "Unexpected number of tasks in job");
            Assert.AreEqual(1, refreshedJob.Tasks[0].OutputAssets.Count, "Unexpected number of output assets in the job");
            Assert.IsNotNull(refreshedJob.Tasks[0].OutputAssets[0].StorageAccountName, "Storage account name in output assset is null");
        }
        public void CapacityBasedShouldFromAccountsShouldFilterBasedOnInputArray()
        {
            List <IStorageAccount> storageAccountList = GetStorageAccountList(_fourStorageAccountNameArray, _evenBytesUsedValues);

            // Copy 3 of the 4 names to the filter array
            string[] filterArray = new string[_fourStorageAccountNameArray.Length - 1];
            Array.Copy(_fourStorageAccountNameArray, filterArray, filterArray.Length);

            // save the name of the skipped entry
            string nameSkipped = _fourStorageAccountNameArray[_fourStorageAccountNameArray.Length - 1];

            // Create the CapacityBasedAccountSelectionStrategy
            MediaContextBase context = GetMediaContextBase(storageAccountList);
            CapacityBasedAccountSelectionStrategy strategy = CapacityBasedAccountSelectionStrategy.FromAccounts(context, maximumStorageAccountCapacity: oneGB, storageAccountNames: filterArray);

            // Now ensure that the internal list only has the expected number of entries.
            IList <CapacityBasedAccountSelectionStrategyListEntry> accountListFromStrategy = strategy.GetStorageAccounts();

            Assert.AreEqual(filterArray.Length, accountListFromStrategy.Count);

            foreach (CapacityBasedAccountSelectionStrategyListEntry entry in accountListFromStrategy)
            {
                Assert.AreNotEqual(nameSkipped, entry.StorageAccount.Name);
            }

            // Add the name previously skipped
            strategy.AddStorageAccountByName(nameSkipped, false, oneGB);

            VerifyStrategyEntriesMatchExpectations(storageAccountList, strategy, oneGB, false);

            // Now verify that if I have names in the filter array that don't exist in the account no exception occurs
            strategy = CapacityBasedAccountSelectionStrategy.FromAccounts(context, maximumStorageAccountCapacity: oneGB, storageAccountNames: _fiveStorageAccountNameArray);
            VerifyStrategyEntriesMatchExpectations(storageAccountList, strategy, oneGB, false);
        }
        public void CapacityBasedShouldPickAccountsBasedOnWeightedDistribution()
        {
            // Try even weighting
            List <IStorageAccount> storageAccountList = GetStorageAccountList(_fourStorageAccountNameArray, _evenBytesUsedValues);

            MediaContextBase context = GetMediaContextBase(storageAccountList);
            CapacityBasedAccountSelectionStrategy strategy = CapacityBasedAccountSelectionStrategy.FromAccounts(context);

            strategy.Random = new RandomNumberGeneratorMock(_valuesForRandomNumberGeneratorToReturnEven);

            for (int i = 0; i < storageAccountList.Count; i++)
            {
                string accountNameToUse = strategy.SelectAccountForAsset();
                Assert.AreEqual(storageAccountList[i].Name, accountNameToUse);
            }

            VerifyStrategyEntriesMatchExpectations(storageAccountList, strategy, CapacityBasedAccountSelectionStrategy.oneHundredEightyTB, false);

            // Try skewed weighting
            // Note that the first account and the last account in the list are almost full.  With the "random" numbers we picked
            // we will always pick the two middle accounts.
            storageAccountList = GetStorageAccountList(_fourStorageAccountNameArray, _skewedBytesUsedValues);
            context            = GetMediaContextBase(storageAccountList);
            strategy           = CapacityBasedAccountSelectionStrategy.FromAccounts(context, maximumStorageAccountCapacity: oneGB);

            strategy.Random = new RandomNumberGeneratorMock(_valuesForRandomNumberGeneratorToReturnEven);

            string[] expectedAccountNames = new string[] { _fiveStorageAccountNameArray[1], _fiveStorageAccountNameArray[1], _fiveStorageAccountNameArray[2], _fiveStorageAccountNameArray[2] };
            for (int i = 0; i < storageAccountList.Count; i++)
            {
                string accountNameToUse = strategy.SelectAccountForAsset();
                Assert.AreEqual(expectedAccountNames[i], accountNameToUse);
            }

            VerifyStrategyEntriesMatchExpectations(storageAccountList, strategy, oneGB, false);

            // Try skewed weighting again but change the "random" numbers we generate to be very small and very large so that
            // we pick the first and last account even though they are almost full.
            storageAccountList = GetStorageAccountList(_fourStorageAccountNameArray, _skewedBytesUsedValues);
            context            = GetMediaContextBase(storageAccountList);
            strategy           = CapacityBasedAccountSelectionStrategy.FromAccounts(context, maximumStorageAccountCapacity: oneGB);

            strategy.Random = new RandomNumberGeneratorMock(_valuesForRandomNumberGeneratorToReturnTopAndBottom);

            expectedAccountNames = new string[] { _fiveStorageAccountNameArray[0], _fiveStorageAccountNameArray[0], _fiveStorageAccountNameArray[3], _fiveStorageAccountNameArray[3] };
            for (int i = 0; i < storageAccountList.Count; i++)
            {
                string accountNameToUse = strategy.SelectAccountForAsset();
                Assert.AreEqual(expectedAccountNames[i], accountNameToUse);
            }

            VerifyStrategyEntriesMatchExpectations(storageAccountList, strategy, oneGB, false);
        }
        public void CapacityBasedShouldOmitAccountsWithNoDataByDefault()
        {
            List <IStorageAccount> storageAccountList = GetStorageAccountList(_fourStorageAccountNameArray, _oneZeroBytesUsedValues);

            MediaContextBase context = GetMediaContextBase(storageAccountList);
            CapacityBasedAccountSelectionStrategy strategy = CapacityBasedAccountSelectionStrategy.FromAccounts(context, maximumStorageAccountCapacity: oneGB);

            string accountNameToUse = strategy.SelectAccountForAsset();

            Assert.IsNotNull(accountNameToUse);
            Assert.AreEqual(0, _oneZeroBytesUsedValues[1].Value);
            Assert.AreEqual(_fourStorageAccountNameArray[1], accountNameToUse);

            VerifyStrategyEntriesMatchExpectations(storageAccountList, strategy, oneGB, false);
        }
        public void CapacityBasedShouldThrowIfNoAccounts()
        {
            MediaContextBase context = GetMediaContextBase(null);
            CapacityBasedAccountSelectionStrategy strategy = new CapacityBasedAccountSelectionStrategy(context);

            try
            {
                strategy.SelectAccountForAsset();
            }
            catch (InvalidOperationException e)
            {
                Assert.AreEqual("No storage accounts configured to select from.", e.Message);
                throw;
            }
        }
        public void CapacityBasedShouldIncludeAccountsWithNoDataWhenEnabled()
        {
            List <IStorageAccount> storageAccountList = GetStorageAccountList(_fourStorageAccountNameArray, _oneNullBytesUsedValues);

            MediaContextBase context = GetMediaContextBase(storageAccountList);

            CapacityBasedAccountSelectionStrategy strategy = CapacityBasedAccountSelectionStrategy.FromAccounts(context, includeAccountsWithNoCapacityData: true, maximumStorageAccountCapacity: oneGB);

            strategy.Random = new RandomNumberGeneratorMock(_valuesForRandomNumberGeneratorToReturnEven);

            for (int i = 0; i < storageAccountList.Count; i++)
            {
                string accountNameToUse = strategy.SelectAccountForAsset();
                Assert.AreEqual(storageAccountList[i].Name, accountNameToUse);
            }

            VerifyStrategyEntriesMatchExpectations(storageAccountList, strategy, oneGB, true);
        }
        public void CapacityBasedShouldThrowWhenNoAccountCanBeSelected()
        {
            List <IStorageAccount> storageAccountList = GetStorageAccountList(_fourStorageAccountNameArray, _evenBytesUsedValues);

            MediaContextBase context = GetMediaContextBase(storageAccountList);
            CapacityBasedAccountSelectionStrategy strategy = CapacityBasedAccountSelectionStrategy.FromAccounts(context, maximumStorageAccountCapacity: oneGB);

            VerifyStrategyEntriesMatchExpectations(storageAccountList, strategy, oneGB, false);

            try
            {
                string accountNameToUse = strategy.SelectAccountForAsset();
            }
            catch (InvalidOperationException e)
            {
                Assert.AreEqual("Unable to find any storage accounts with available capacity!", e.Message);
                throw;
            }
        }
        public void CapacityBasedShouldThrowIfAccountNamesCannotBeFound()
        {
            List <IStorageAccount> storageAccountList = GetStorageAccountList(_fourStorageAccountNameArray, _evenBytesUsedValues);

            MediaContextBase context = GetMediaContextBase(storageAccountList);

            CapacityBasedAccountSelectionStrategy strategy = new CapacityBasedAccountSelectionStrategy(context);

            try
            {
                foreach (string accountName in _fiveStorageAccountNameArray)
                {
                    strategy.AddStorageAccountByName(accountName);
                }
            }
            catch (ArgumentException ae)
            {
                Assert.IsTrue(ae.Message.Contains("Unable to find a storage account with name \"account5\""));
                Assert.AreEqual("storageAccountName", ae.ParamName);
                throw;
            }
        }
        private void VerifyStrategyEntriesMatchExpectations(List <IStorageAccount> expectedList, CapacityBasedAccountSelectionStrategy strategy, long maximumStorageAccountCapacity, bool considerFullCapacityIfNoDataAvailable)
        {
            IList <CapacityBasedAccountSelectionStrategyListEntry> accountListFromStrategy = strategy.GetStorageAccounts();

            Assert.AreEqual(expectedList.Count, accountListFromStrategy.Count);

            foreach (CapacityBasedAccountSelectionStrategyListEntry entry in accountListFromStrategy)
            {
                IStorageAccount accountFromExpectedList = expectedList.Where(st => st.Name == entry.StorageAccount.Name).SingleOrDefault();

                long expectedAvailableCapacity = CalculateExpectedAvailableCapacity(accountFromExpectedList.BytesUsed, maximumStorageAccountCapacity, considerFullCapacityIfNoDataAvailable);

                Assert.AreEqual(accountFromExpectedList.Name, entry.StorageAccount.Name);
                Assert.AreEqual(accountFromExpectedList.BytesUsed, entry.StorageAccount.BytesUsed);
                Assert.AreEqual(expectedAvailableCapacity, entry.AvailableCapacity);
            }
        }
        private void VerifyStrategyEntriesMatchExpectations(List<IStorageAccount> expectedList, CapacityBasedAccountSelectionStrategy strategy, long maximumStorageAccountCapacity, bool considerFullCapacityIfNoDataAvailable)
        {
            IList<CapacityBasedAccountSelectionStrategyListEntry> accountListFromStrategy = strategy.GetStorageAccounts();
            Assert.AreEqual(expectedList.Count, accountListFromStrategy.Count);

            foreach (CapacityBasedAccountSelectionStrategyListEntry entry in accountListFromStrategy)
            {
                IStorageAccount accountFromExpectedList = expectedList.Where(st => st.Name == entry.StorageAccount.Name).SingleOrDefault();

                long expectedAvailableCapacity = CalculateExpectedAvailableCapacity(accountFromExpectedList.BytesUsed, maximumStorageAccountCapacity, considerFullCapacityIfNoDataAvailable);

                Assert.AreEqual(accountFromExpectedList.Name, entry.StorageAccount.Name);
                Assert.AreEqual(accountFromExpectedList.BytesUsed, entry.StorageAccount.BytesUsed);
                Assert.AreEqual(expectedAvailableCapacity, entry.AvailableCapacity);
            }
        }
        public void CapacityBasedShouldThrowIfNoAccounts()
        {
            MediaContextBase context = GetMediaContextBase(null);
            CapacityBasedAccountSelectionStrategy strategy = new CapacityBasedAccountSelectionStrategy(context);

            try
            {
                strategy.SelectAccountForAsset();
            }
            catch (InvalidOperationException e)
            {
                Assert.AreEqual("No storage accounts configured to select from.", e.Message);
                throw;
            }
        }
        public void CapacityBasedShouldThrowIfAccountNamesCannotBeFound()
        {
            List<IStorageAccount> storageAccountList = GetStorageAccountList(_fourStorageAccountNameArray, _evenBytesUsedValues);

            MediaContextBase context = GetMediaContextBase(storageAccountList);

            CapacityBasedAccountSelectionStrategy strategy = new CapacityBasedAccountSelectionStrategy(context);

            try
            {
                foreach (string accountName in _fiveStorageAccountNameArray)
                {
                    strategy.AddStorageAccountByName(accountName);
                }
            }
            catch (ArgumentException ae)
            {
                Assert.IsTrue(ae.Message.Contains("Unable to find a storage account with name \"account5\""));
                Assert.AreEqual("storageAccountName", ae.ParamName);
                throw;
            }
        }