public void ReleasingLockedDataFileIsInvalid()
        {
            var sut             = new LockedDataFile("201306158F341A2D6FD7416B87073A0132DD51AE.chk.20150627111406.locked");
            var messageBodyMock = new Mock <MessageBody>(sut);

            Assert.That(
                () => sut.Release(messageBodyMock.Object),
                Throws.InvalidOperationException);
        }
        public void UnlockingLockedDataFileIsInvalid()
        {
            const string filePath        = "201306158F341A2D6FD7416B87073A0132DD51AE.chk.20150627111406.locked";
            var          sut             = new LockedDataFile(filePath);
            var          messageBodyMock = new Mock <MessageBody>(sut);

            Assert.That(
                () => sut.Unlock(messageBodyMock.Object),
                Throws.InvalidOperationException);
        }
        internal override void Lock(MessageBody messageBody)
        {
            // take a new lock ---update timestamp while staying in locked state--- so that this agent instance get
            // exclusive ownership of the data file should there be another remote agent instance working concurrently
            var lockedDataFile = new LockedDataFile(this);
            var result         = DataFileServant.Instance.TryMoveFile(Path, lockedDataFile.Path);

            messageBody.DataFile = result
                                ? (DataFile)lockedDataFile
                                : new AwaitingRetryDataFile(this);
        }
        internal override void Lock(MessageBody messageBody)
        {
            if (_logger.IsDebugEnabled)
            {
                _logger.DebugFormat("Locking {0}.", this);
            }

            // try to add a .timestamp.locked extension to file name to get exclusive ownership should there be another
            // Claim Store Agent working concurrently from another computer
            var lockedDataFile = new LockedDataFile(this);
            var result         = DataFileServant.Instance.TryMoveFile(Path, lockedDataFile.Path);

            messageBody.DataFile = result
                                ? (DataFile)lockedDataFile
                                : new AwaitingRetryDataFile(this);
        }
        public void LockTransitionsToAwaitingRetryDataFileWhenOperationFails()
        {
            var servantMock = new Mock <DataFileServant>();

            DataFileServant.Instance = servantMock.Object;

            const string filePath        = "201306158F341A2D6FD7416B87073A0132DD51AE.chk.20150627111406.locked";
            var          sut             = new LockedDataFile(filePath);
            var          messageBodyMock = new Mock <MessageBody>(sut);

            messageBodyMock.SetupAllProperties();

            servantMock.Setup(s => s.TryMoveFile(filePath, It.IsAny <string>())).Returns(false);

            sut.Lock(messageBodyMock.Object);

            Assert.That(messageBodyMock.Object.DataFile, Is.TypeOf <AwaitingRetryDataFile>());
        }
        public void GatherCopiesDataFileToCentralClaimStoreAndRenamesLocalDataFile()
        {
            var servantMock = new Mock <DataFileServant>();

            DataFileServant.Instance = servantMock.Object;

            const string filePath        = "201306158F341A2D6FD7416B87073A0132DD51AE.chk.20150627111406.locked";
            var          sut             = new LockedDataFile(filePath);
            var          messageBodyMock = new Mock <MessageBody>(sut);

            servantMock.Setup(s => s.TryCreateDirectory(Path.Combine(Path.GetTempPath(), sut.ClaimStoreRelativePath))).Returns(true).Verifiable();
            servantMock.Setup(s => s.TryCopyFile(filePath, It.Is <string>(path => path == Path.Combine(Path.GetTempPath(), sut.ClaimStoreRelativePath)))).Returns(true).Verifiable();
            servantMock.Setup(s => s.TryMoveFile(filePath, It.Is <string>(path => path.Tokenize().State == GatheredDataFile.STATE_TOKEN))).Returns(true).Verifiable();

            sut.Gather(messageBodyMock.Object, Path.GetTempPath());

            servantMock.VerifyAll();
        }
        public void GatherTransitionsToGatheredDataFileWhenOperationSucceeds()
        {
            var servantMock = new Mock <DataFileServant>();

            DataFileServant.Instance = servantMock.Object;

            const string filePath        = "201306158F341A2D6FD7416B87073A0132DD51AE.chk.20150627111406.locked";
            var          sut             = new LockedDataFile(filePath);
            var          messageBodyMock = new Mock <MessageBody>(sut);

            messageBodyMock.SetupAllProperties();

            servantMock.Setup(s => s.TryCreateDirectory(Path.Combine(Path.GetTempPath(), sut.ClaimStoreRelativePath))).Returns(true);
            servantMock.Setup(s => s.TryCopyFile(filePath, It.Is <string>(path => path == Path.Combine(Path.GetTempPath(), sut.ClaimStoreRelativePath)))).Returns(true);
            servantMock.Setup(s => s.TryMoveFile(filePath, It.Is <string>(path => path.Tokenize().State == GatheredDataFile.STATE_TOKEN))).Returns(true);

            sut.Gather(messageBodyMock.Object, Path.GetTempPath());

            Assert.That(messageBodyMock.Object.DataFile, Is.TypeOf <GatheredDataFile>());
        }
        public void LockTransitionsToNewLockedDataFileWhenOperationSucceeds()
        {
            var servantMock = new Mock <DataFileServant>();

            DataFileServant.Instance = servantMock.Object;

            const string filePath        = "201306158F341A2D6FD7416B87073A0132DD51AE.chk.20150627111406.locked";
            var          sut             = new LockedDataFile(filePath);
            var          messageBodyMock = new Mock <MessageBody>(sut);

            messageBodyMock.SetupAllProperties();

            servantMock.Setup(s => s.TryMoveFile(filePath, It.IsAny <string>())).Returns(true);

            sut.Lock(messageBodyMock.Object);

            Assert.That(messageBodyMock.Object.DataFile, Is.TypeOf <LockedDataFile>());
            Assert.That(messageBodyMock.Object.DataFile, Is.Not.SameAs(sut));
            Assert.That(sut.Path.Tokenize().LockTime < messageBodyMock.Object.DataFile.Path.Tokenize().LockTime);
        }