public void DetectsPackageInstalled()
        {
            using (var gel = new GlobalLock())
            {
                var env = new Ex.Env();

                var context = A.App.Application.Context;

                context.PackageManager.MockPackages = new Dictionary <string, A.Content.PM.PackageInfo> {
                    { "package", new A.Content.PM.PackageInfo() }
                };

                var i = new ShouldNotHavePackageInstalled().Init(env, "TEST", "package");

                Assert.False(i.Ok);
            }
        }
        public async Task Retry_based_on_configured_linear_policy()
        {
            ILockProvider           lockProvider             = new LockProvider();
            var                     linearRetryControllerObj = new LinearRetryController(new LinearRetrySettingsProvider());
            var                     globalLockProvider       = new GlobalLock(lockProvider, linearRetryControllerObj);//
            Func <Task <DateTime> > writeLockBlockingAction  = async() =>
            {
                DateTime lockAcquiredTime = new DateTime();
                using (var globalLock = await globalLockProvider.EnterReadLock("test"))
                {
                    lockAcquiredTime = DateTime.Now;
                    Thread.Sleep(500);
                }
                return(lockAcquiredTime);
            };

            Func <Task <DateTime> > writeLockAction = async() =>
            {
                DateTime lockAcquiredTime = new DateTime();
                using (var globalLock = await globalLockProvider.EnterWriteLock("test"))
                {
                    lockAcquiredTime = DateTime.Now;
                }
                return(lockAcquiredTime);
            };

            Task <DateTime> dateTimeTask1 = null, dateTimeTask2 = null;

            Parallel.Invoke(
                () => { dateTimeTask1 = writeLockBlockingAction(); },
                () => {
                Thread.Sleep(20);
                dateTimeTask2 = writeLockAction();
            }
                );

            var dateTime1 = dateTimeTask1.Result;
            var dateTime2 = dateTimeTask2.Result;

            var timeDiff = (dateTime2 - dateTime1).TotalMilliseconds;

            Assert.IsTrue(timeDiff >= 600);
            Assert.IsTrue(timeDiff <= 800); // 200 + 200 + 200 = 600
        }
        public async Task Retry_based_on_configured_policy()
        {
            ILockProvider           lockProvider = new LockProvider();
            var                     exponentialRetryControllerObj = new ExponentialRetryController(new ExponentialRetrySettingsProvider());
            var                     globalLockProvider            = new GlobalLock(lockProvider, exponentialRetryControllerObj);//
            Func <Task <DateTime> > writeLockBlockingAction       = async() =>
            {
                DateTime lockAcquiredTime = new DateTime();
                using (var globalLock = await globalLockProvider.EnterReadLock("test"))
                {
                    lockAcquiredTime = DateTime.Now;
                    Thread.Sleep(500);
                }
                return(lockAcquiredTime);
            };

            Func <Task <DateTime> > writeLockAction = async() =>
            {
                DateTime lockAcquiredTime = new DateTime();
                using (var globalLock = await globalLockProvider.EnterWriteLock("test"))
                {
                    lockAcquiredTime = DateTime.Now;
                }
                return(lockAcquiredTime);
            };

            Task <DateTime> dateTimeTask1 = null, dateTimeTask2 = null;

            Parallel.Invoke(
                () => { dateTimeTask1 = writeLockBlockingAction(); },
                () => {
                Thread.Sleep(20);
                dateTimeTask2 = writeLockAction();
            }
                );

            var dateTime1 = dateTimeTask1.Result;
            var dateTime2 = dateTimeTask2.Result;

            var timeDiff = (dateTime2 - dateTime1).TotalMilliseconds;

            Assert.IsTrue(timeDiff >= 1200);
            Assert.IsTrue(timeDiff <= 1400); // (int)Math.Pow(3, _retryCount) * 10;) for ExponentialRetry. 30 + 90 + 270 + 810 = 1200
        }
        public async Task Should_be_able_to_acquire_read_and_write_locks_in_async_way()
        {
            ILockProvider lockProvider = new LockProvider();

            var globalLockProvider = new GlobalLock(lockProvider);

            var isReadLockAcquired  = false;
            var isWriteLockAcquired = false;

            using (await globalLockProvider.EnterReadLock("test"))
            {
                isReadLockAcquired = true;
            }
            using (await globalLockProvider.EnterWriteLock("test"))
            {
                isWriteLockAcquired = true;
            }
            Assert.IsTrue(isReadLockAcquired);
            Assert.IsTrue(isWriteLockAcquired);
        }
Ejemplo n.º 5
0
        public void Invoke(string url)
        {
            string path = HttpContext.Current.Server.MapPath("/") + "/log.txt";

            ThreadPool.QueueUserWorkItem(a => {
                using (var urlLock = new GlobalLock(url))
                {
                    if (!urlLock.AcquireLock())
                    {
                        return;
                    }
                    try
                    {
                        while (true)
                        {
                            using (WebClient client = new WebClient())
                            {
                                var response = client.DownloadString(url);
                                if (response.Contains(MSBuildRetryMessage))
                                {
                                    Thread.Sleep(1000);
                                    continue;
                                }
                                break;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        System.IO.File.AppendAllText(path,
                                                     DateTime.Now.ToLongDateString() + "\r\n" + "Failed for " + url + "\r\n" +
                                                     ex.ToString());
                    }
                }
            });
        }
        public async Task ReadWriteLocks_Should_Wait_For_Completion_Of_WriteLock()
        {
            ILockProvider lockProvider = new LockProvider();

            var globalLockProvider = new GlobalLock(lockProvider);
            Func <Task <DateTime> > writeLockBlockingAction = async() =>
            {
                DateTime lockAcquiredTime = new DateTime();
                using (var globalLock = await globalLockProvider.EnterWriteLock("test"))
                {
                    lockAcquiredTime = DateTime.Now;
                    Thread.Sleep(500);
                }
                return(lockAcquiredTime);
            };

            Func <Task <DateTime> > writeLockAction = async() =>
            {
                DateTime lockAcquiredTime = new DateTime();
                using (var globalLock = await globalLockProvider.EnterWriteLock("test"))
                {
                    lockAcquiredTime = DateTime.Now;
                }
                return(lockAcquiredTime);
            };

            Func <Task <DateTime> > readLockAction = async() =>
            {
                DateTime lockAcquiredTime = new DateTime();
                using (var globalLock = await globalLockProvider.EnterReadLock("test"))
                {
                    lockAcquiredTime = DateTime.Now;
                }
                return(lockAcquiredTime);
            };

            Task <DateTime> dateTimeTask1 = null, dateTimeTask2 = null, dateTimeTask3 = null;

            Parallel.Invoke(
                () => { dateTimeTask1 = writeLockBlockingAction(); },
                () => {
                Thread.Sleep(100);
                dateTimeTask2 = writeLockAction();
            },
                () => {
                Thread.Sleep(100);
                dateTimeTask3 = readLockAction();
            }
                );

            var dateTime1 = dateTimeTask1.Result;
            var dateTime2 = dateTimeTask2.Result;
            var dateTime3 = dateTimeTask3.Result;

            var timeDiff = (dateTime3 - dateTime1).TotalMilliseconds;

            Assert.IsTrue(timeDiff > 500);

            var timeDiff1 = (dateTime2 - dateTime1).TotalMilliseconds;

            Assert.IsTrue(timeDiff1 > 500);
        }
Ejemplo n.º 7
0
        public void GlobalLock_Lock()
        {
            using (GlobalLock lock1 = new GlobalLock("a"),
                   lock2 = new GlobalLock("a"))
            {
                Assert.IsFalse(lock1.IsHeld);
                Assert.IsFalse(lock2.IsHeld);

                lock1.Lock();
                Assert.IsTrue(lock1.IsHeld);
                Assert.IsFalse(lock2.IsHeld);
                lock1.Release();
                Assert.IsFalse(lock1.IsHeld);

                lock1.Lock();
                Assert.IsTrue(lock1.IsHeld);

                try
                {
                    lock2.Lock();
                    Assert.Fail();
                }
                catch (GlobalLockException)
                {
                    // I'm expecting this to fail
                }

                lock1.Lock();
                Assert.IsTrue(lock1.IsHeld);

                lock1.Release();
                Assert.IsTrue(lock1.IsHeld);

                try
                {
                    lock2.Lock();
                    Assert.Fail();
                }
                catch (GlobalLockException)
                {
                    // I'm expecting this to fail
                }

                lock1.Release();
                Assert.IsFalse(lock1.IsHeld);

                lock2.Lock();
                Assert.IsTrue(lock2.IsHeld);

                try
                {
                    lock1.Lock();
                    Assert.Fail();
                }
                catch (GlobalLockException)
                {
                    // I'm expecting this to fail
                }

                lock2.Release();
                Assert.IsFalse(lock2.IsHeld);
            }
        }
Ejemplo n.º 8
0
 public ActionResult Locks()
 {
     return(Json(GlobalLock.GetActiveLocks(), JsonRequestBehavior.AllowGet));
 }
Ejemplo n.º 9
0
 public override void ReleaseWriteLock()
 {
     GlobalLock.Unlock();
     Turnstile.Unlock();
 }
Ejemplo n.º 10
0
 public override void AcquireWriteLock()
 {
     Turnstile.Lock();
     GlobalLock.Lock();
 }
Ejemplo n.º 11
0
        protected override void OnExecute(string rootPath, StringWriter logger)
        {
            var Response = logger;


            using (var deployLock = new GlobalLock(config.SiteId))
            {
                if (!deployLock.AcquireLock())
                {
                    Response.WriteLine("Deployment already in progress");
                    Response.Flush();
                    return;
                }

                using (var buildLock = new GlobalLock(config.BuildFolder))
                {
                    if (!buildLock.AcquireLock())
                    {
                        Response.WriteLine(IISWebRequest.MSBuildRetryMessage);
                        Response.Flush();
                        return;
                    }

                    if (reset)
                    {
                        /*var file = new System.IO.FileInfo(config.BuildFolder + "\\local-repository.json");
                         * if (file.Exists)
                         * {
                         *  file.Delete();
                         * }*/
                        var file = new System.IO.FileInfo(config.BuildResult);
                        if (file.Exists)
                        {
                            file.Delete();
                        }
                        var dir = new System.IO.DirectoryInfo(config.BuildFolder);
                        if (dir.Exists)
                        {
                            dir.Delete(true);
                        }
                    }

                    var executable = rootPath + "\\bin\\IISCI.build.exe";

                    Response.WriteLine("<html>");
                    Response.WriteLine("<body><div id='logger'>");

                    Response.Flush();

                    IISCIProcess p = new IISCIProcess(executable, parameters);
                    p.Run();

                    Response.WriteLine(p.Error);
                    Response.Flush();
                    Response.WriteLine(p.Output);
                    Response.Flush();


                    if (p.Success)
                    {
                        if (config.StartUrls != null)
                        {
                            foreach (var url in config.StartUrls)
                            {
                                IISWebRequest.Instance.Invoke(config.SiteId, url.Url);
                            }
                        }
                    }

                    if (string.IsNullOrWhiteSpace(config.Notify))
                    {
                        return;
                    }

                    try
                    {
                        string        subject    = string.Format("IISCI-Build: {0} for {1}", (p.Success ? "Success" : "Failed"), config.SiteId);
                        string        body       = "<div><h2>" + config.SiteId + "</h2>" + p.Error + p.Output + "</div><hr size='1'/><div style='text-align:right'><a href='https://github.com/neurospeech/iis-ci' target='_blank'>IISCI by NeuroSpeech&reg;</a></div>";
                        List <string> recipients = new List <string>();
                        foreach (var item in config.Notify.Split(',', ';').Where(x => !string.IsNullOrWhiteSpace(x)))
                        {
                            if (!item.Contains('@'))
                            {
                                continue;
                            }
                            recipients.Add(item);
                        }


                        SettingsModel settings = JsonStorage.ReadFileOrDefault <SettingsModel>(settingsPath);

                        SmtpService.Instance.Send(settings, subject, body, recipients);
                    }
                    catch (Exception ex)
                    {
                        Response.WriteLine(ex.ToString());
                    }
                }
            }
        }
Ejemplo n.º 12
0
		public static GlobalLock GetLockObj()
		{
			return _lock ?? (_lock = new GlobalLock());
		}