Esempio n. 1
0
        public void LockFileRepositoryClearLockTest()
        {
            // Mock
            var path              = @"x:\temp";
            var lockFileName      = Path.Combine(path, "deployment.lock");
            var fileSystem        = new Mock <IFileSystem>();
            var file              = new Mock <FileBase>();
            var directory         = new Mock <DirectoryBase>();
            var repository        = new Mock <IRepository>();
            var repositoryFactory = new Mock <IRepositoryFactory>();
            var lockFile          = new DeploymentLockFile(lockFileName, Mock.Of <ITraceFactory>());

            // Setup
            fileSystem.SetupGet(f => f.Directory)
            .Returns(directory.Object);
            directory.Setup(d => d.Exists(path))
            .Returns(true);
            fileSystem.SetupGet(f => f.File)
            .Returns(file.Object);
            file.Setup(f => f.Open(lockFileName, FileMode.Create, FileAccess.Write, FileShare.Read))
            .Returns(Mock.Of <Stream>());
            repositoryFactory.Setup(f => f.GetRepository())
            .Returns(repository.Object);
            FileSystemHelpers.Instance = fileSystem.Object;

            // Test
            lockFile.RepositoryFactory = repositoryFactory.Object;
            var locked = lockFile.Lock();

            // Assert
            Assert.True(locked, "lock should be successfule!");
            repository.Verify(r => r.ClearLock(), Times.Once);
        }
Esempio n. 2
0
        public void AsyncLock_BasicTest()
        {
            for (int i = 0; i < 2; i++)
            {
                // Acquire
                Assert.Equal(false, _lockFile.IsHeld);
                Assert.Equal(true, _lockFile.Lock());

                // Test
                Assert.Equal(true, _lockFile.IsHeld);
                Assert.Equal(false, _lockFile.Lock());

                // Release
                _lockFile.Release();
                Assert.Equal(false, _lockFile.IsHeld);
            }
        }
Esempio n. 3
0
        public void AsyncLock_BasicTest()
        {
            for (int i = 0; i < 2; i++)
            {
                // Acquire
                var operationName = "operationName" + i;
                Assert.Equal(false, _lockFile.IsHeld);
                Assert.Equal(true, _lockFile.Lock(operationName));

                // Test
                Assert.Equal(true, _lockFile.IsHeld);
                Assert.Equal(false, _lockFile.Lock("fail"));

                var lockInfo = _lockFile.LockInfo;
                Assert.NotNull(lockInfo);
                Assert.Equal(operationName, lockInfo.OperationName);
                Assert.NotNull(lockInfo.AcquiredDateTime);
                Assert.NotNull(lockInfo.StackTrace);

                // Release
                _lockFile.Release();
                Assert.Equal(false, _lockFile.IsHeld);
            }
        }
Esempio n. 4
0
        public void DeploymentLockSentinelTest()
        {
            // Mock
            System.Environment.SetEnvironmentVariable("WEBSITE_INSTANCE_ID", "Instance1");
            var path              = @"x:\temp1";
            var pathSentinel      = "C:\\local\\ShutdownSentinel\\Sentinel.txt";
            var lockFileName      = Path.Combine(path, "deployment1.lock");
            var fileSystem        = new Mock <IFileSystem>();
            var file              = new Mock <FileBase>();
            var fileInfoBase      = new Mock <IFileInfoFactory>();
            var directory         = new Mock <DirectoryBase>();
            var repository        = new Mock <IRepository>();
            var repositoryFactory = new Mock <IRepositoryFactory>();
            var lockFile          = new DeploymentLockFile(lockFileName, Mock.Of <ITraceFactory>(f => f.GetTracer() == Mock.Of <ITracer>()));

            // Setup
            fileSystem.SetupGet(f => f.Directory)
            .Returns(directory.Object);
            directory.Setup(d => d.Exists(path))
            .Returns(true);
            fileSystem.SetupGet(f => f.File)
            .Returns(file.Object);
            file.Setup(f => f.Open(lockFileName, FileMode.Create, FileAccess.Write, FileShare.Read))
            .Returns(Mock.Of <Stream>());
            file.Setup(f => f.Create(pathSentinel)).Returns(Mock.Of <Stream>());
            fileInfoBase.Setup(f => f.FromFileName(pathSentinel)).Returns(Mock.Of <FileInfoBase>());
            fileSystem.SetupGet(f => f.FileInfo).Returns(fileInfoBase.Object);

            repositoryFactory.Setup(f => f.GetRepository())
            .Returns(repository.Object);
            FileSystemHelpers.Instance = fileSystem.Object;
            lockFile.RepositoryFactory = repositoryFactory.Object;

            // Test
            var locked = lockFile.Lock("operationName");

            file.Setup(f => f.Exists(pathSentinel)).Returns(true);

            Assert.True(locked, "lock should be successful!");
            Assert.True(FileSystemHelpers.FileExists(pathSentinel), "Sentinel file should be present");

            repository.Verify(r => r.ClearLock(), Times.Once);

            // Reset IsAzureEnvironment env variable
            System.Environment.SetEnvironmentVariable("WEBSITE_INSTANCE_ID", null);
        }