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; }
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; }
// 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"); } }
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); }
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); }
public PerFileSemaphore() { // create a option to simulate a file _options = new JsonStoreOptions(); _factory = new PerFileSemaphoreFactory(_options); }
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; }
public FileStreamer(IMessagePackSerializer messagePackSerializer, IFile file, ISemaphoreFactory semaphoreFactory, IDirectory directory) : this(messagePackSerializer, file, semaphoreFactory, directory, new EmptyLogger()) { }
/// <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; }
public ExtractPackageToApplicationDirectoryConvention(IPackageExtractor extractor, ICalamariFileSystem fileSystem, ISemaphoreFactory semaphore) : base(extractor, fileSystem) { this.semaphore = semaphore; }
public LocalSemaphore() { _factory = new LocalSemaphoreFactory(); }