Esempio n. 1
0
        private void RetrieveNewDataForCache(IDataLoadEventListener listener, GracefulCancellationToken cancellationToken)
        {
            listener.OnNotify(this, new NotifyEventArgs(ProgressEventType.Information, "Retrieving new data"));

            var combinedToken = cancellationToken.CreateLinkedSource().Token;

            // Start a task for each cache download permission window and wait until completion
            var tasks =
                _downloaders.Select(
                    downloader =>
                    Task.Run(() => DownloadUntilFinished(downloader, listener, cancellationToken), combinedToken))
                .ToArray();

            try
            {
                Task.WaitAll(tasks);
            }
            catch (AggregateException e)
            {
                var operationCanceledException = e.GetExceptionIfExists <OperationCanceledException>();
                if (operationCanceledException != null)
                {
                    listener.OnNotify(this, new NotifyEventArgs(ProgressEventType.Warning, "Operation cancelled", e));
                }
                else
                {
                    listener.OnNotify(this, new NotifyEventArgs(ProgressEventType.Error, "Exception in downloader task whilst caching data", e));
                    throw;
                }
            }
        }
Esempio n. 2
0
        public void CacheHostOutwithPermissionWindow()
        {
            var rootDir = new DirectoryInfo(TestContext.CurrentContext.TestDirectory);
            var testDir = rootDir.CreateSubdirectory("C");

            if (testDir.Exists)
            {
                Directory.Delete(testDir.FullName, true);
            }

            var loadDirectory = LoadDirectory.CreateDirectoryStructure(testDir, "Test");


            var cp           = WhenIHaveA <CacheProgress>();
            var loadMetadata = cp.LoadProgress.LoadMetadata;

            loadMetadata.LocationOfFlatFiles = loadDirectory.RootPath.FullName;

            // This feels a bit nasty, but quick and much better than having the test wait for an arbitrary time period.
            var listener = new ExpectedNotificationListener("Download not permitted at this time, sleeping for 60 seconds");

            cp.CacheFillProgress   = DateTime.Now.AddDays(-1);
            cp.PermissionWindow_ID = 1;


            var permissionWindow = new PermissionWindow(Repository);

            permissionWindow.RequiresSynchronousAccess = true;
            permissionWindow.ID   = 1;
            permissionWindow.Name = "Test Permission Window";


            //Create a time period that we are outwith (1 hour ago to 30 minutes ago).
            TimeSpan start = DateTime.Now.TimeOfDay.Subtract(new TimeSpan(0, 1, 0, 0));
            TimeSpan stop  = DateTime.Now.TimeOfDay.Subtract(new TimeSpan(0, 0, 30, 0));

            permissionWindow.SetPermissionWindowPeriods(new List <PermissionWindowPeriod>(new []
            {
                new PermissionWindowPeriod(
                    (int)DateTime.Now.DayOfWeek,
                    start,
                    stop)
            }));
            permissionWindow.SaveToDatabase();

            cp.PermissionWindow_ID = permissionWindow.ID;
            cp.SaveToDatabase();

            var dataFlowPipelineEngine = Mock.Of <IDataFlowPipelineEngine>();

            // set up a factory stub to return our engine mock
            var cacheHost = new CachingHost(Repository)
            {
                CacheProgressList = new List <ICacheProgress> {
                    cp
                }
            };

            var stopTokenSource   = new CancellationTokenSource();
            var abortTokenSource  = new CancellationTokenSource();
            var cancellationToken = new GracefulCancellationToken(stopTokenSource.Token, abortTokenSource.Token);

            var task = Task.Run(() => cacheHost.Start(listener, cancellationToken), cancellationToken.CreateLinkedSource().Token);

            // Don't want to cancel before the DownloadUntilFinished loop starts and we receive the first "Download not permitted at this time, sleeping for 60 seconds" message
            listener.ReceivedMessage += abortTokenSource.Cancel;

            try
            {
                task.Wait();
            }
            catch (AggregateException e)
            {
                Assert.AreEqual(1, e.InnerExceptions.Count);
                Assert.IsInstanceOf(typeof(TaskCanceledException), e.InnerExceptions[0], e.InnerExceptions[0].Message);
            }
            finally
            {
                testDir.Delete(true);
            }
        }