protected void ShouldIsolate(ISemaphoreFactory semaphore)
        {
            var result  = 0;
            var threads = new List <Thread>();

            for (var i = 0; i < 4; i++)
            {
                threads.Add(new Thread(new ThreadStart(delegate
                {
                    using (semaphore.Acquire("CalamariTest", "Another process is performing arithmetic, please wait"))
                    {
                        result = 1;
                        Thread.Sleep(200);
                        result = result + 1;
                        Thread.Sleep(200);
                        result = result + 1;
                    }
                })));
            }

            foreach (var thread in threads)
            {
                thread.Start();
            }

            foreach (var thread in threads)
            {
                thread.Join();
            }

            Assert.That(result, Is.EqualTo(3));
        }
        protected void SecondSemaphoreWaitsUntilFirstSemaphoreIsReleased(ISemaphoreFactory semaphore)
        {
            AutoResetEvent autoEvent = new AutoResetEvent(false);
            var            threadTwoShouldGetSemaphore = true;

            var threadOne = new Thread(() =>
            {
                using (semaphore.Acquire("Octopus.Calamari.TestSemaphore", "Another process has the semaphore..."))
                {
                    threadTwoShouldGetSemaphore = false;
                    autoEvent.Set();
                    Thread.Sleep(200);
                    threadTwoShouldGetSemaphore = true;
                }
            });

            var threadTwo = new Thread(() =>
            {
                autoEvent.WaitOne();
                using (semaphore.Acquire("Octopus.Calamari.TestSemaphore", "Another process has the semaphore..."))
                {
                    Assert.That(threadTwoShouldGetSemaphore, Is.True);
                }
            });

            threadOne.Start();
            threadTwo.Start();
            threadOne.Join();
            threadTwo.Join();
        }
        /// <summary>
        ///     Creates a new instance with the given options.
        /// </summary>
        /// <param name="options">The options for this repository.</param>
        /// <param name="semaphoreFactory">The semaphore factory.</param>
        public ConcurrentJsonRepository(JsonStoreOptions options, ISemaphoreFactory semaphoreFactory) : base(options)
        {
            var keyProperty = RepositoryKeyValidator.GetKeyProperty <T, TKey>();

            GetKeyValue = keyProperty.Compile();

            _semaphore = semaphoreFactory.GetSemaphore <T>();
        }
        /// <summary>
        ///     Creates a new instance with the given options and key.
        /// </summary>
        /// <param name="options">The options for this repository.</param>
        /// <param name="keyProperty">A <see cref="Func{TResult}" /> to get the object's key.</param>
        /// <param name="semaphoreFactory">The semaphore factory.</param>
        public ConcurrentJsonRepository(
            JsonStoreOptions options,
            Expression <Func <T, TKey> > keyProperty,
            ISemaphoreFactory semaphoreFactory
            ) : base(options)
        {
            GetKeyValue = keyProperty.Compile();

            _semaphore = semaphoreFactory.GetSemaphore <T>();
        }
 public OnlyOnceClientStoreMigrator(IClientStoreMigrator decorated, IClientStoreBaseliner baseliner, ISemaphoreFactory semaphoreFactory)
 {
     if (baseliner == null)
     {
         throw new ArgumentNullException(nameof(baseliner));
     }
     _decorated        = decorated ?? throw new ArgumentNullException(nameof(decorated));
     _semaphoreFactory = semaphoreFactory ?? throw new ArgumentNullException(nameof(semaphoreFactory));
     _semaphore        = _semaphoreFactory.CreateLock();
     _lazyBaseline     = new Lazy <Task <int?> >(baseliner.GetBaseline, LazyThreadSafetyMode.PublicationOnly);
     _runResult        = null;
 }
Ejemplo n.º 6
0
 public PackageJournal(IJournalRepository journalRepository,
                       ILog log,
                       ICalamariFileSystem fileSystem,
                       IRetentionAlgorithm retentionAlgorithm,
                       ISemaphoreFactory semaphoreFactory)
 {
     this.journalRepository = journalRepository;
     this.log                = log;
     this.fileSystem         = fileSystem;
     this.retentionAlgorithm = retentionAlgorithm;
     this.semaphoreFactory   = semaphoreFactory;
 }
Ejemplo n.º 7
0
 // ReSharper disable once MemberCanBePrivate.Global
 public FileStreamer(IMessagePackSerializer messagePackSerializer, IFile file,
                     ISemaphoreFactory semaphoreFactory, IDirectory directory, ILogger logger)
 {
     using (logger.BeginScope("{Operation}", nameof(FileStreamer)))
     {
         _messagePackSerializer = messagePackSerializer;
         _file             = file;
         _semaphoreFactory = semaphoreFactory;
         _directory        = directory;
         _logger           = logger;
         _logger.LogInformation("Created file streamer");
     }
 }
Ejemplo n.º 8
0
        public ConcurrentRepositoryCrudOperations()
        {
            _path    = Guid.NewGuid().ToString("N");
            _options = new JsonStoreOptions {
                NamingStrategy = new StaticNamingStrategy(_path)
            };
            _semaphoreFactory = new LocalSemaphoreFactory();
            _tokenSource      = new CancellationTokenSource();

            // create a file with an item
            var filePath = Path.Combine(_options.Location, $"{_path}.json");

            JsonFileCreator.CreateMultiItemsRepository(filePath);
        }
Ejemplo n.º 9
0
        public ConcurrentJsonStoreExistingFile()
        {
            // creates a file
            _fileName         = Guid.NewGuid().ToString();
            _semaphoreFactory = new LocalSemaphoreFactory();

            // create a file
            _options = new JsonStoreOptions
            {
                NamingStrategy = new StaticNamingStrategy(_fileName)
            };
            var filePath = Path.Combine(_options.Location, $"{_fileName}.json");

            JsonFileCreator.CreateStore(filePath);
        }
Ejemplo n.º 10
0
 public PerFileSemaphore()
 {
     // create a option to simulate a file
     _options = new JsonStoreOptions();
     _factory = new PerFileSemaphoreFactory(_options);
 }
Ejemplo n.º 11
0
 public DeploymentJournal(ICalamariFileSystem fileSystem, ISemaphoreFactory semaphore, VariableDictionary variables)
 {
     this.fileSystem = fileSystem;
     this.semaphore  = semaphore;
     this.variables  = variables;
 }
 public RePackageCloudServiceConvention(ICalamariFileSystem fileSystem, ISemaphoreFactory semaphoreFactory)
 {
     this.fileSystem       = fileSystem;
     this.semaphoreFactory = semaphoreFactory;
 }
Ejemplo n.º 13
0
 public FileStreamer(IMessagePackSerializer messagePackSerializer, IFile file,
                     ISemaphoreFactory semaphoreFactory, IDirectory directory) : this(messagePackSerializer, file,
                                                                                      semaphoreFactory, directory, new EmptyLogger())
 {
 }
Ejemplo n.º 14
0
 /// <summary>
 ///     Creates a new instance of <see cref="ConcurrentJsonStore{T}" /> with the given options.
 /// </summary>
 /// <param name="options">The options for this store.</param>
 /// <param name="semaphoreFactory">The semaphore factory.</param>
 public ConcurrentJsonStore(JsonStoreOptions options, ISemaphoreFactory semaphoreFactory) : base(options)
 {
     _semaphoreFactory = semaphoreFactory;
 }
Ejemplo n.º 15
0
 public ExtractPackageToApplicationDirectoryConvention(IPackageExtractor extractor, ICalamariFileSystem fileSystem, ISemaphoreFactory semaphore) : base(extractor, fileSystem)
 {
     this.semaphore = semaphore;
 }
Ejemplo n.º 16
0
 public LocalSemaphore()
 {
     _factory = new LocalSemaphoreFactory();
 }