private async Task Start()
        {
            if (this.HasId)
            {
                using (var cts = new CancellationTokenSource())
                {
                    var attachConsumerTask = TestcontainersClient.Instance.AttachAsync(this.Id, this.Configuration.OutputConsumer, cts.Token);

                    var startTask = TestcontainersClient.Instance.StartAsync(this.Id, cts.Token);

                    var waitTask = WaitStrategy.WaitUntil(() => this.Configuration.WaitStrategy.Until(this.Id), cancellationToken: cts.Token);

                    var handleDockerExceptionTask = startTask.ContinueWith(task =>
                    {
                        task.Exception?.Handle(exception =>
                        {
                            cts.Cancel();
                            return(false);
                        });
                    });

                    await Task.WhenAll(attachConsumerTask, startTask, waitTask, handleDockerExceptionTask);
                }
            }
        }
示例#2
0
        public void Switch(IWebDriver driver)
        {
            switch (currentStrategy)
            {
            case WaitStrategy.Implicit:
                currentStrategy = WaitStrategy.Explicit;
                if (!strategyPool.ContainsKey(currentStrategy))
                {
                    strategyPool.Add(currentStrategy, GetStrategy(currentStrategy));
                }
                break;

            case WaitStrategy.Explicit:
                currentStrategy = WaitStrategy.Implicit;
                if (!strategyPool.ContainsKey(currentStrategy))
                {
                    strategyPool.Add(currentStrategy, GetStrategy(currentStrategy));
                }
                break;

            default:
                throw new NotImplementedException();
            }

            strategyPool[currentStrategy].SetWaitStrategy(driver);
        }
 public async Task WhileAfter1ms()
 {
     await Assert.ThrowsAsync <TimeoutException>(async() =>
     {
         await WaitStrategy.WaitWhile(() => this.While(string.Empty), timeout: 1);
     });
 }
示例#4
0
        public static PolicyWrap CircuitBreakerWithRetry <T>(WaitStrategy waitStrategy,
                                                             Action <Exception, TimeSpan> onRetry, Action <Exception, TimeSpan> onBreak, Action onReset, int retryCount = RetryCount, int durationOfBreakInSeconds = DurationOfBreakInSeconds) where T : Exception
        {
            var waitStrategyImplementation = GetWaitStrategyImplementation(waitStrategy);

            return(CircuitBreakerWithRetry <T>(waitStrategyImplementation, onRetry, retryCount,
                                               TimeSpan.FromSeconds(durationOfBreakInSeconds), onBreak, onReset));
        }
示例#5
0
 public override void Publish(long lo, long hi)
 {
     for (long l = lo; l <= hi; l++)
     {
         SetAvailable(l);
     }
     WaitStrategy.SignalAllWhenBlocking();
 }
示例#6
0
        public WaitContext(IWebDriver driver, WaitStrategy strategy)
        {
            currentStrategy = strategy;
            strategyPool    = new Dictionary <WaitStrategy, IWaitStrategy>(2)
            {
                { currentStrategy, GetStrategy(currentStrategy) }
            };

            strategyPool[currentStrategy].SetWaitStrategy(driver);
        }
示例#7
0
 internal Fixture(int iterations, LockManager lockManager, WaitStrategy waitStrategy, LockType lockTypeAX, LockType lockTypeAY, LockType lockTypeBX, LockType lockTypeBY)
 {
     this.IterationsConflict = iterations;
     this.LockManager        = lockManager;
     this.WaitStrategy       = waitStrategy;
     this.LockTypeAX         = lockTypeAX;
     this.LockTypeAY         = lockTypeAY;
     this.LockTypeBX         = lockTypeBX;
     this.LockTypeBY         = lockTypeBY;
 }
        public async Task UntilSomeRepeats(int maxCallCount, int expectedCallsCount)
        {
            // Given
            var callCounter = 0;

            // When
            var wait = Wait.ForUnixContainer().UntilOperationIsSucceeded(() => ++ callCounter >= expectedCallsCount, maxCallCount);
            await WaitStrategy.WaitUntil(() => wait.Build().Skip(1).First().Until(null, string.Empty));

            // Then
            Assert.Equal(expectedCallsCount, callCounter);
        }
示例#9
0
        public async Task UntilSomeRepeats(int maxCallCount, int expectedCallsCount)
        {
            // Given
            var callCounter = 0;

            // When
            var wait = new WaitUntilOperationSucceeded(() => ++ callCounter >= expectedCallsCount, maxCallCount);
            await WaitStrategy.WaitUntil(() => wait.Until(string.Empty));

            // Then
            Assert.Equal(expectedCallsCount, callCounter);
        }
示例#10
0
        public async Task ExecAsync(string id, params string[] command)
        {
            if (command is null)
            {
                return;
            }

            var created = await Docker.Containers.ExecCreateContainerAsync(id, new ContainerExecCreateParameters { Cmd = command });

            await Docker.Containers.StartContainerExecAsync(created.ID);

            await WaitStrategy.WaitWhile(async() => (await Docker.Containers.InspectContainerExecAsync(created.ID)).Running);
        }
        public void Commit(long position, long count)
        {
            long lazyCursorValue = Cursor.VolatileGet();

            var positionToWait = position - count;

            if (positionToWait <= lazyCursorValue)
            {
                WaitStrategy.WaitFor(positionToWait, Cursor);
            }

            Cursor.VolatileSet(position + 1);
            WaitStrategy.Signal();
        }
示例#12
0
        private static IWaitStrategy GetStrategy(WaitStrategy strategy)
        {
            switch (strategy)
            {
            case WaitStrategy.Implicit:
                return(new ImplicitWaitStrategy());

            case WaitStrategy.Explicit:
                return(new ExplicitWaitStrategy());

            default:
                throw new NotImplementedException();
            }
        }
示例#13
0
        public void Commit(long position, long count)
        {
            long lazyCursorValue = Cursor.Get();
            //BUG: single thread writer could not rely on wait strategy because cursor must be moved by itself. Self-deadlock.
            var positionToWait = position - count;

            if (positionToWait <= lazyCursorValue)
            {
                WaitStrategy.WaitFor(positionToWait, Cursor);
            }

            Cursor.VolatileSet(position + 1);
            WaitStrategy.Signal();
        }
示例#14
0
文件: Settings.cs 项目: Qoopi/SWD
        public static void Set(SettingsEnum setting, object value)
        {
            switch (setting)
            {
            case SettingsEnum.DefaultTimeout:
                DefaultTimeout = (int)value;
                break;

            case SettingsEnum.WaitStrategy:
                WaitStrategy = (WaitStrategy)value;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(setting), setting, null);
            }
        }
示例#15
0
        public static bool TryToWait(Func <bool> condition, int waitTimeout, WaitStrategy strategy)
        {
            var isSuccess     = false;
            var alreadyWaited = 0;
            var timeToWait    = strategy == WaitStrategy.Progressive ? 0 : 500;
            var stopwatch     = new Stopwatch();

            while (true)
            {
                stopwatch.Reset();
                stopwatch.Start();
                var result = condition.Invoke();
                if (result)
                {
                    isSuccess = true;
                    break;
                }

                stopwatch.Stop();

                alreadyWaited += stopwatch.Elapsed.Milliseconds;

                if (alreadyWaited >= waitTimeout)
                {
                    break;
                }

                if (strategy == WaitStrategy.Progressive)
                {
                    if (timeToWait == 0)
                    {
                        timeToWait += 100;
                    }
                    else
                    {
                        timeToWait *= 2;
                    }
                }

                Thread.Sleep(timeToWait);

                alreadyWaited += timeToWait;
            }

            return(isSuccess);
        }
        public async Task UntilMessageIsLogged()
        {
            // Given
            const string expectedMessage = nameof(this.UntilMessageIsLogged);

            // When
            // Then
            using (var memoryStream = new MemoryStream())
            {
                using (var streamWriter = new StreamWriter(memoryStream))
                {
                    streamWriter.Write(expectedMessage);
                    streamWriter.Flush();

                    var wait = Wait.ForUnixContainer().UntilMessageIsLogged(memoryStream, expectedMessage);
                    await WaitStrategy.WaitUntil(() => wait.Build().Skip(1).First().Until(null, string.Empty));
                }
            }
        }
示例#17
0
        public async Task UntilMaxRepeats(int maxCallCount, int expectedCallsCount)
        {
            // Given
            var callCounter = 0;

            // When
            await Assert.ThrowsAsync <TimeoutException>(async() =>
            {
                var wait = new WaitUntilOperationSucceeded(() =>
                {
                    callCounter += 1;
                    return(false);
                }, maxCallCount);
                await WaitStrategy.WaitUntil(() => wait.Until(string.Empty));
            });

            // Then
            Assert.Equal(expectedCallsCount, callCounter);
        }
        private async Task <ContainerListResponse> Start(string id, CancellationToken ct = default)
        {
            using (var cts = new CancellationTokenSource())
            {
                await this.client.AttachAsync(id, this.configuration.OutputConsumer, cts.Token)
                .ConfigureAwait(false);

                var startTask = this.client.StartAsync(id, cts.Token);

                var waitTask = Task.Run(async() =>
                {
                    foreach (var waitStrategy in this.configuration.WaitStrategies)
                    {
                        await WaitStrategy.WaitUntil(() => waitStrategy.Until(this.configuration.Endpoint, id), 100, ct: cts.Token)
                        .ConfigureAwait(false);
                    }
                }, cts.Token);

                var tasks = Task.WhenAll(startTask, waitTask);

                try
                {
                    await tasks.ConfigureAwait(false);
                }
                catch (Exception)
                {
                    // Get all thrown exceptions in tasks.
                    if (tasks.Exception != null)
                    {
                        TestcontainersHostService.GetLogger <TestcontainersContainer>().LogError(tasks.Exception, "Can not start container {id}", id);
                        throw tasks.Exception;
                    }
                }
                finally
                {
                    cts.Cancel();
                }
            }

            return(await this.client.GetContainer(id, ct)
                   .ConfigureAwait(false));
        }
        public async Task UntilMaxRepeats(int maxCallCount, int expectedCallsCount)
        {
            // Given
            var callCounter = 0;

            // When
            await Assert.ThrowsAsync <TimeoutException>(async() =>
            {
                var wait = Wait.ForUnixContainer().UntilOperationIsSucceeded(() =>
                {
                    callCounter += 1;
                    return(false);
                }, maxCallCount);

                await WaitStrategy.WaitUntil(() => wait.Build().Skip(1).First().Until(null, string.Empty));
            });

            // Then
            Assert.Equal(expectedCallsCount, callCounter);
        }
        private async Task StartServices(CancellationToken ct)
        {
            if (ct.IsCancellationRequested)
            {
                return;
            }

            try
            {
                await WaitStrategy.WaitUntil(this);

                _logger.LogInformation("Container {} started!", DockerImageName);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Unable to start container services: {}", DockerImageName);

                await PrintContainerLogs(ct);

                throw;
            }
        }
        private async Task <ContainerInspectResponse> Start(string id, CancellationToken ct = default)
        {
            await this.client.AttachAsync(id, this.configuration.OutputConsumer, ct)
            .ConfigureAwait(false);

            await this.client.StartAsync(id, ct)
            .ConfigureAwait(false);

            this.container = await this.client.InspectContainer(id, ct)
                             .ConfigureAwait(false);

            await this.configuration.StartupCallback(this, ct)
            .ConfigureAwait(false);

            // Do not use a too small frequency. Especially with a lot of containers,
            // we send many operations to the Docker endpoint. The endpoint may cancel operations.
            var frequency = (int)TimeSpan.FromSeconds(1).TotalMilliseconds;

            const int timeout = -1;

            foreach (var waitStrategy in this.configuration.WaitStrategies)
            {
                await WaitStrategy.WaitUntil(
                    async() =>
                {
                    this.container = await this.client.InspectContainer(id, ct)
                                     .ConfigureAwait(false);

                    return(await waitStrategy.Until(this, this.Logger)
                           .ConfigureAwait(false));
                },
                    frequency,
                    timeout,
                    ct)
                .ConfigureAwait(false);
            }

            return(this.container);
        }
示例#22
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @SuppressWarnings("unchecked") public ForsetiLockManager(org.neo4j.kernel.configuration.Config config, java.time.Clock clock, org.neo4j.storageengine.api.lock.ResourceType... resourceTypes)
        public ForsetiLockManager(Config config, Clock clock, params ResourceType[] resourceTypes)
        {
            int maxResourceId = FindMaxResourceId(resourceTypes);

            this._lockMaps      = new ConcurrentMap[maxResourceId];
            this._resourceTypes = new ResourceType[maxResourceId];

            /* Wait strategies per resource type */
            WaitStrategy <AcquireLockTimeoutException>[] waitStrategies = new WaitStrategy[maxResourceId];

            foreach (ResourceType type in resourceTypes)
            {
                this._lockMaps[type.TypeId()]      = new ConcurrentDictionary <long, ForsetiLockManager.Lock>(16, 0.6f, 512);
                waitStrategies[type.TypeId()]      = type.WaitStrategy();
                this._resourceTypes[type.TypeId()] = type;
            }
            // TODO Using a FlyweightPool here might still be more than what we actually need.
            // TODO We should investigate if a simple concurrent stack (aka. free-list) would
            // TODO be good enough. In fact, we could add the required fields for such a stack
            // TODO to the ForsetiClient objects themselves, making the stack garbage-free in
            // TODO the (presumably) common case of client re-use.
            _clientPool = new ForsetiClientFlyweightPool(config, clock, _lockMaps, waitStrategies);
        }
示例#23
0
        public static Func <int, TimeSpan> GetWaitStrategyImplementation(WaitStrategy waitStrategy)
        {
            switch (waitStrategy)
            {
            case WaitStrategy.MinimalWait:
                return(i => TimeSpan.FromMilliseconds(10));

            case WaitStrategy.ShortWait:
                return(i => TimeSpan.FromSeconds(5));

            case WaitStrategy.LongWait:
                return(i => TimeSpan.FromSeconds(10));

            case WaitStrategy.LinearWait:
                return(i => TimeSpan.FromSeconds(5 * i));

            case WaitStrategy.LinearWaitLong:
                return(i => TimeSpan.FromSeconds(10 * i));

            case WaitStrategy.CappedLinearWait:
                return(i =>
                {
                    var wait = TimeSpan.FromSeconds(5 * i);
                    return wait > TimeSpan.FromMinutes(1) ? TimeSpan.FromMinutes(1) : wait;
                });

            case WaitStrategy.CappedLinearWaitLong:
                return(i =>
                {
                    var wait = TimeSpan.FromSeconds(10 * i);
                    return wait > TimeSpan.FromMinutes(1) ? TimeSpan.FromMinutes(1) : wait;
                });

            default:
                throw new ArgumentOutOfRangeException(nameof(waitStrategy), waitStrategy, null);
            }
        }
        private async Task <ContainerListResponse> Start(string id, CancellationToken ct = default)
        {
            using (var cts = new CancellationTokenSource())
            {
                var attachOutputConsumerTask = this.client.AttachAsync(id, this.configuration.OutputConsumer, cts.Token);

                var startTask = this.client.StartAsync(id, cts.Token);

                var waitTask = Task.Run(async() =>
                {
                    foreach (var waitStrategy in this.configuration.WaitStrategies)
                    {
                        await WaitStrategy.WaitUntil(() => waitStrategy.Until(this.configuration.Endpoint, id), ct: cts.Token);
                    }
                }, cts.Token);

                var tasks = Task.WhenAll(attachOutputConsumerTask, startTask, waitTask);

                try
                {
                    await tasks;
                }
                catch (Exception)
                {
                    if (tasks.Exception != null)
                    {
                        throw tasks.Exception;
                    }
                }
                finally
                {
                    cts.Cancel();
                }
            }

            return(await this.client.GetContainer(id, ct));
        }
示例#25
0
        private Task Start()
        {
            return(this.id.IfSomeAsync(id =>
            {
                var cts = new CancellationTokenSource();

                var attachConsumerTask = TestcontainersClient.Instance.AttachAsync(id, this.Configuration.OutputConsumer, cts.Token);

                var startTask = TestcontainersClient.Instance.StartAsync(id, cts.Token);

                var waitTask = WaitStrategy.WaitUntil(() => this.Configuration.WaitStrategy.Until(id), cancellationToken: cts.Token);

                var handleDockerExceptionTask = startTask.ContinueWith((task) =>
                {
                    task.Exception?.Handle(exception =>
                    {
                        cts.Cancel();
                        return false;
                    });
                });

                return Task.WhenAll(attachConsumerTask, startTask, waitTask, handleDockerExceptionTask);
            }));
        }
        public async Task UntilMessageIsLogged()
        {
            // Given
            const string expectedMessage = "Message has been logged.";

            await using var memoryStream = new MemoryStream();
            await using var streamWriter = new StreamWriter(memoryStream);

            // When
            await streamWriter.WriteAsync(expectedMessage);

            await streamWriter.FlushAsync();

            var wait = Wait.ForUnixContainer()
                       .UntilMessageIsLogged(memoryStream, expectedMessage)
                       .Build()
                       .Skip(1)
                       .First();

            // Then
            var exception = await Record.ExceptionAsync(() => WaitStrategy.WaitUntil(() => wait.Until(null, null)));

            Assert.Null(exception);
        }
 public override void Publish(long sequence)
 {
     Cursor.SetValue(sequence);
     WaitStrategy.SignalAllWhenBlocking();
 }
示例#28
0
 public static AsyncPolicy RetryAsync <TException>(int retryCount = RetryCount, WaitStrategy waitStrategy = WaitStrategy.LinearWait, Action <Exception, TimeSpan, int, Context> onRetry = null) where TException : Exception
 {
     return(Policy.Handle <TException>().WaitAndRetryAsync(retryCount, GetWaitStrategyImplementation(waitStrategy), onRetry ?? ((exception, span, arg3, arg4) => { })));
 }
示例#29
0
 public static void Wait(Func <bool> condition, Func <string> errorMessage, int waitTimeout, WaitStrategy strategy)
 {
     if (!TryToWait(condition, waitTimeout, strategy))
     {
         throw new AssertionException(errorMessage.Invoke());
     }
 }
示例#30
0
        public static void Wait(Func <bool> conditionSuccess, Func <bool> conditionFailure, Func <string> errorMessageTimeout, Func <string> errorMessageFailure, int waitTimeout, WaitStrategy strategy)
        {
            var result = TryToWait(conditionSuccess, conditionFailure, waitTimeout, strategy, out var isFailure);

            if (result)
            {
                return;
            }
            if (isFailure)
            {
                throw new AssertionException(errorMessageFailure.Invoke());
            }

            throw new AssertionException(errorMessageTimeout.Invoke());
        }