public void ItCompletesTheTaskWhenSignalIsCalledSampleTimes() { var asyncACE = new AsyncCountdownEvent(SampleTimes); var task = asyncACE.WaitAsync(); for (int i = 0; i < SampleTimes; i++) { asyncACE.Signal(); } task.Wait(TimeSpan.FromSeconds(1)); Assert.IsTrue(task.IsCompleted); }
public async Task Do() { var messages = new ConcurrentQueue<TransportMessage>(); messages.Enqueue(new TransportMessage()); messages.Enqueue(new TransportMessage()); messages.Enqueue(new TransportMessage()); var countdown = new AsyncCountdownEvent(3); var chainFactory = new ChainFactory(); chainFactory.Register(() => new LogElement(countdown)); chainFactory.Register(() => new DelayBefore()); chainFactory.Register(() => new DelayBefore()); chainFactory.Register(() => new DelayBefore()); chainFactory.Register(() => new DelayBefore()); chainFactory.Register(() => new DelayBefore()); chainFactory.Register(() => new DelayBefore()); chainFactory.Register(() => new DelayAfter()); chainFactory.Register(() => new DelayAfter()); chainFactory.Register(() => new DelayAfter()); chainFactory.Register(() => new DelayAfter()); chainFactory.Register(() => new DelayAfter()); chainFactory.Register(() => new DelayInUsing()); chainFactory.Register(() => new DelayBefore()); chainFactory.Register(() => new DelayAfter()); chainFactory.Register(() => new DelayBefore()); chainFactory.Register(() => new DelayAfter()); chainFactory.Register(() => new PassThrough()); chainFactory.Register(() => new PassThrough()); chainFactory.Register(() => new PassThrough()); chainFactory.Register(() => new DelayInUsing()); chainFactory.Register(() => new DelayBefore()); chainFactory.Register(() => new DelayAfter()); chainFactory.Register(() => new PassThrough()); chainFactory.Register(() => new PassThrough()); chainFactory.Register(() => new PassThrough()); chainFactory.Register(() => new ThrowException()); var pushMessages = new PushMessages(messages, maxConcurrency: 1); await pushMessages.StartAsync(tm => Connector(chainFactory, tm)); await Task.Delay(2000); await pushMessages.StopAsync(); }
public async Task Do() { var messages = new ConcurrentQueue<TransportMessage>(); messages.Enqueue(new TransportMessage()); messages.Enqueue(new TransportMessage()); messages.Enqueue(new TransportMessage()); var countdown = new AsyncCountdownEvent(3); var chainFactory = new ChainFactory(); chainFactory.Register(() => new DelayElement()); chainFactory.Register(() => new LogElement()); chainFactory.Register(() => new DecrementElement(countdown)); var pushMessages = new PushMessages(messages, maxConcurrency: 1); await pushMessages.StartAsync(tm => Connector(chainFactory, tm)); await countdown.WaitAsync(); await pushMessages.StopAsync(); }
public async Task Any_given_lease_is_never_handed_out_to_more_than_one_handler_at_a_time() { var random = new Random(); var currentlyGranted = new HashSet<string>(); var everGranted = new HashSet<string>(); var leasedConcurrently = ""; var distributor = CreateDistributor(waitInterval: TimeSpan.FromSeconds(5)).Trace(); var countDown = new AsyncCountdownEvent(10); distributor.OnReceive(async lease => { lock (currentlyGranted) { if (currentlyGranted.Contains(lease.LeasableResource.Name)) { leasedConcurrently = lease.LeasableResource.Name; } currentlyGranted.Add(lease.LeasableResource.Name); everGranted.Add(lease.LeasableResource.Name); } await Task.Delay((int) (1000*random.NextDouble())); lock (currentlyGranted) { currentlyGranted.Remove(lease.LeasableResource.Name); } countDown.Signal(); }); Enumerable.Range(1, 10).ToList().ForEach(_ => { distributor.Distribute(1); }); await countDown.WaitAsync().Timeout(); leasedConcurrently.Should().BeEmpty(); everGranted.Count.Should().Be(10); }
public async Task SignalAndWaitFromOne() { var evt = new AsyncCountdownEvent(1); await evt.SignalAndWaitAsync(); }
public RuntimeInfoCollector(AsyncCountdownEvent countdown) { this.countdown = countdown; }
public void Signal_Underflow_ThrowsException() { var ce = new AsyncCountdownEvent(long.MinValue); AsyncAssert.Throws <OverflowException>(() => ce.Signal()); }
public void SignalThrowsOnError() { var evt = new AsyncCountdownEvent(0); Assert.Throws <InvalidOperationException>(() => evt.Signal()); }
public static Task WaitAsync(this AsyncCountdownEvent countdownEvent, TimeSpan timeout) { return(Task.WhenAny(countdownEvent.WaitAsync(), SystemClock.SleepAsync(timeout))); }
public void ItReturnsTaskWhenWaitAsyncIsCalled() { var asyncACE = new AsyncCountdownEvent(SampleTimes); Assert.IsInstanceOfType(asyncACE.WaitAsync(), typeof(Task)); }
private Task StartAnalysis(IDependencyChainNode <PythonAnalyzerEntry> node, AsyncCountdownEvent ace, Stopwatch stopWatch, CancellationToken cancellationToken) => Task.Run(() => Analyze(node, ace, stopWatch, cancellationToken), cancellationToken);
public async void PodWatchMods() { AsyncCountdownEvent requestHandled = new AsyncCountdownEvent(6); AsyncManualResetEvent serverShutdown = new AsyncManualResetEvent(); var podWatchData = await File.ReadAllTextAsync("podwatch.txt"); var addedAgent = BuildWatchEventStreamLine(podWatchData, WatchEventType.Added); var addedHub = BuildWatchEventStreamLine(podWatchData, WatchEventType.Added, 1); var addedSensor = BuildWatchEventStreamLine(podWatchData, WatchEventType.Added, 3); var v1PodList = JsonConvert.DeserializeObject <V1PodList>(podWatchData); V1Pod modAgentPod = v1PodList.Items[0]; modAgentPod.Status.ContainerStatuses[0].State.Running = null; modAgentPod.Status.ContainerStatuses[0].State.Terminated = new V1ContainerStateTerminated(139, finishedAt: DateTime.Parse("2019-06-12T16:13:07Z"), startedAt: DateTime.Parse("2019-06-12T16:11:22Z")); var modAgent = BuildPodStreamLine(modAgentPod, WatchEventType.Modified); V1Pod modHubPod = v1PodList.Items[1]; modHubPod.Status.ContainerStatuses[0].State.Running = null; modHubPod.Status.ContainerStatuses[1].State.Waiting = new V1ContainerStateWaiting("waiting", "reason"); var modHub = BuildPodStreamLine(modHubPod, WatchEventType.Modified); V1Pod tempSensorPod = v1PodList.Items[3]; // temp sensor has a "LastState" tempSensorPod.Status.ContainerStatuses[1].State.Running = null; var modTempSensor = BuildPodStreamLine(tempSensorPod, WatchEventType.Modified); using (var server = new MockKubeApiServer( async httpContext => { await MockKubeApiServer.WriteStreamLine(httpContext, addedAgent); await MockKubeApiServer.WriteStreamLine(httpContext, addedHub); await MockKubeApiServer.WriteStreamLine(httpContext, addedSensor); await MockKubeApiServer.WriteStreamLine(httpContext, modTempSensor); await MockKubeApiServer.WriteStreamLine(httpContext, modHub); await MockKubeApiServer.WriteStreamLine(httpContext, modAgent); return(false); })) { var client = new Kubernetes( new KubernetesClientConfiguration { Host = server.Uri.ToString() }); var k8sRuntimeInfo = new KubernetesRuntimeInfoProvider(PodwatchNamespace, client); k8sRuntimeInfo.PropertyChanged += (sender, args) => { Assert.Equal("Modules", args.PropertyName); requestHandled.Signal(); }; k8sRuntimeInfo.Start(); await Task.WhenAny(requestHandled.WaitAsync(), Task.Delay(TestTimeout)); var runtimeModules = await k8sRuntimeInfo.GetModules(CancellationToken.None); var moduleRuntimeInfos = runtimeModules as ModuleRuntimeInfo[] ?? runtimeModules.ToArray(); Assert.True(moduleRuntimeInfos.Count() == 3); foreach (var i in moduleRuntimeInfos) { if (!string.Equals("edgeAgent", i.Name)) { Assert.Equal(ModuleStatus.Unknown, i.ModuleStatus); } else { Assert.Equal(ModuleStatus.Failed, i.ModuleStatus); } if (string.Equals("edgeHub", i.Name)) { Assert.Equal(Option.None <DateTime>(), i.ExitTime); Assert.Equal(Option.None <DateTime>(), i.StartTime); } else { Assert.Equal(new DateTime(2019, 6, 12), i.StartTime.GetOrElse(DateTime.MinValue).Date); Assert.Equal(new DateTime(2019, 6, 12), i.ExitTime.GetOrElse(DateTime.MinValue).Date); } if (i is ModuleRuntimeInfo <DockerReportedConfig> d) { Assert.NotEqual("unknown:unknown", d.Config.Image); } } } }
public async Task DirectWatchEventsWithTimeout() { AsyncCountdownEvent eventsReceived = new AsyncCountdownEvent(4); AsyncManualResetEvent serverShutdown = new AsyncManualResetEvent(); using (var server = new MockKubeApiServer(testOutput, async httpContext => { await Task.Delay(TimeSpan.FromSeconds(120)); // The default timeout is 100 seconds await WriteStreamLine(httpContext, MockAddedEventStreamLine); await WriteStreamLine(httpContext, MockDeletedStreamLine); await WriteStreamLine(httpContext, MockModifiedStreamLine); await WriteStreamLine(httpContext, MockErrorStreamLine); // make server alive, cannot set to int.max as of it would block response await serverShutdown.WaitAsync(); return(false); })) { var client = new Kubernetes(new KubernetesClientConfiguration { Host = server.Uri.ToString() }); var events = new HashSet <WatchEventType>(); var errors = 0; var watcher = await client.WatchNamespacedPodAsync( name : "myPod", @namespace : "default", onEvent : (type, item) => { testOutput.WriteLine($"Watcher received '{type}' event."); events.Add(type); eventsReceived.Signal(); }, onError : error => { testOutput.WriteLine($"Watcher received '{error.GetType().FullName}' error."); errors += 1; eventsReceived.Signal(); } ); // wait server yields all events await Task.WhenAny(eventsReceived.WaitAsync(), Task.Delay(TestTimeout)); Assert.True( eventsReceived.CurrentCount == 0, "Timed out waiting for all events / errors to be received." ); Assert.Contains(WatchEventType.Added, events); Assert.Contains(WatchEventType.Deleted, events); Assert.Contains(WatchEventType.Modified, events); Assert.Contains(WatchEventType.Error, events); Assert.Equal(0, errors); Assert.True(watcher.Watching); serverShutdown.Set(); } }
public async Task SuriveBadLine() { AsyncCountdownEvent eventsReceived = new AsyncCountdownEvent(5); AsyncManualResetEvent serverShutdown = new AsyncManualResetEvent(); AsyncManualResetEvent connectionClosed = new AsyncManualResetEvent(); using (var server = new MockKubeApiServer( testOutput, async httpContext => { httpContext.Response.StatusCode = (int)HttpStatusCode.OK; httpContext.Response.ContentLength = null; await WriteStreamLine(httpContext, MockKubeApiServer.MockPodResponse); await WriteStreamLine(httpContext, MockBadStreamLine); await WriteStreamLine(httpContext, MockAddedEventStreamLine); await WriteStreamLine(httpContext, MockBadStreamLine); await WriteStreamLine(httpContext, MockModifiedStreamLine); // make server alive, cannot set to int.max as of it would block response await serverShutdown.WaitAsync(); return(false); })) { var client = new Kubernetes(new KubernetesClientConfiguration { Host = server.Uri.ToString() }); var listTask = await client.ListNamespacedPodWithHttpMessagesAsync("default", watch : true); var events = new HashSet <WatchEventType>(); var errors = 0; var watcher = listTask.Watch <V1Pod>( (type, item) => { testOutput.WriteLine($"Watcher received '{type}' event."); events.Add(type); eventsReceived.Signal(); }, error => { testOutput.WriteLine($"Watcher received '{error.GetType().FullName}' error."); errors += 1; eventsReceived.Signal(); }, onClosed: connectionClosed.Set ); // wait server yields all events await Task.WhenAny(eventsReceived.WaitAsync(), Task.Delay(TestTimeout)); Assert.True( eventsReceived.CurrentCount == 0, "Timed out waiting for all events / errors to be received." ); Assert.Contains(WatchEventType.Added, events); Assert.Contains(WatchEventType.Modified, events); Assert.Equal(3, errors); Assert.True(watcher.Watching); // Let the server know it can initiate a shut down. serverShutdown.Set(); await Task.WhenAny(connectionClosed.WaitAsync(), Task.Delay(TestTimeout)); Assert.True(connectionClosed.IsSet); } }
public async Task WatchAllEvents() { AsyncCountdownEvent eventsReceived = new AsyncCountdownEvent(4 /* first line of response is eaten by WatcherDelegatingHandler */); AsyncManualResetEvent serverShutdown = new AsyncManualResetEvent(); var waitForClosed = new AsyncManualResetEvent(false); using (var server = new MockKubeApiServer(testOutput, async httpContext => { await WriteStreamLine(httpContext, MockAddedEventStreamLine); await WriteStreamLine(httpContext, MockDeletedStreamLine); await WriteStreamLine(httpContext, MockModifiedStreamLine); await WriteStreamLine(httpContext, MockErrorStreamLine); // make server alive, cannot set to int.max as of it would block response await serverShutdown.WaitAsync(); return(false); })) { var client = new Kubernetes(new KubernetesClientConfiguration { Host = server.Uri.ToString() }); var listTask = await client.ListNamespacedPodWithHttpMessagesAsync("default", watch : true); var events = new HashSet <WatchEventType>(); var errors = 0; var watcher = listTask.Watch <V1Pod>( (type, item) => { testOutput.WriteLine($"Watcher received '{type}' event."); events.Add(type); eventsReceived.Signal(); }, error => { testOutput.WriteLine($"Watcher received '{error.GetType().FullName}' error."); errors += 1; eventsReceived.Signal(); }, onClosed: waitForClosed.Set ); // wait server yields all events await Task.WhenAny(eventsReceived.WaitAsync(), Task.Delay(TestTimeout)); Assert.True( eventsReceived.CurrentCount == 0, "Timed out waiting for all events / errors to be received." ); Assert.Contains(WatchEventType.Added, events); Assert.Contains(WatchEventType.Deleted, events); Assert.Contains(WatchEventType.Modified, events); Assert.Contains(WatchEventType.Error, events); Assert.Equal(0, errors); Assert.True(watcher.Watching); serverShutdown.Set(); await Task.WhenAny(waitForClosed.WaitAsync(), Task.Delay(TestTimeout)); Assert.True(waitForClosed.IsSet); Assert.False(watcher.Watching); } }
public async Task DisposeWatch() { var connectionClosed = new AsyncManualResetEvent(); var eventsReceived = new AsyncCountdownEvent(1); bool serverRunning = true; using (var server = new MockKubeApiServer(testOutput, async httpContext => { await WriteStreamLine(httpContext, MockKubeApiServer.MockPodResponse); while (serverRunning) { await WriteStreamLine(httpContext, MockAddedEventStreamLine); } return(true); })) { var client = new Kubernetes(new KubernetesClientConfiguration { Host = server.Uri.ToString() }); var listTask = await client.ListNamespacedPodWithHttpMessagesAsync("default", watch : true); var events = new HashSet <WatchEventType>(); var watcher = listTask.Watch <V1Pod>( (type, item) => { events.Add(type); eventsReceived.Signal(); }, onClosed: connectionClosed.Set ); // wait at least an event await Task.WhenAny(eventsReceived.WaitAsync(), Task.Delay(TestTimeout)); Assert.True( eventsReceived.CurrentCount == 0, "Timed out waiting for events." ); Assert.NotEmpty(events); Assert.True(watcher.Watching); watcher.Dispose(); events.Clear(); // Let the server disconnect serverRunning = false; var timeout = Task.Delay(TestTimeout); while (!timeout.IsCompleted && watcher.Watching) { await Task.Yield(); } Assert.Empty(events); Assert.False(watcher.Watching); Assert.True(connectionClosed.IsSet); } }
public LogStep(AsyncCountdownEvent countdown) { this.countdown = countdown; }
private Task StartAnalysis(IDependencyChainNode <PythonAnalyzerEntry> node, AsyncCountdownEvent ace, Stopwatch stopWatch) => Task.Run(() => Analyze(node, ace, stopWatch));
protected async Task CanHaveMultipleQueueInstancesWithLockingImplAsync(CacheLockProvider distributedLock) { var queue = GetQueue(retries: 0, retryDelay: TimeSpan.Zero); if (queue == null) { return; } using (queue) { await queue.DeleteQueueAsync(); await AssertEmptyQueueAsync(queue); const int workItemCount = 16; const int workerCount = 4; var countdown = new AsyncCountdownEvent(workItemCount); var info = new WorkInfo(); var workers = new List <IQueue <SimpleWorkItem> > { queue }; Log.MinimumLevel = LogLevel.Trace; try { for (int i = 0; i < workerCount; i++) { var q = GetQueue(retries: 0, retryDelay: TimeSpan.Zero); _logger.Trace("Queue Id: {0}, I: {1}", q.QueueId, i); await q.StartWorkingAsync(async w => { _logger.Info("Acquiring distributed lock in work item"); var l = await distributedLock.AcquireAsync("test"); Assert.NotNull(l); _logger.Info("Acquired distributed lock"); SystemClock.Sleep(TimeSpan.FromMilliseconds(50)); await l.ReleaseAsync(); _logger.Info("Released distributed lock"); await w.CompleteAsync(); info.IncrementCompletedCount(); countdown.Signal(); }); workers.Add(q); } await Run.InParallelAsync(workItemCount, async i => { var id = await queue.EnqueueAsync(new SimpleWorkItem { Data = "Hello", Id = i }); _logger.Trace("Enqueued Index: {0} Id: {1}", i, id); }); await countdown.WaitAsync(TimeSpan.FromSeconds(5).ToCancellationToken()); await SystemClock.SleepAsync(50); _logger.Trace("Completed: {0} Abandoned: {1} Error: {2}", info.CompletedCount, info.AbandonCount, info.ErrorCount); _logger.Info("Work Info Stats: Completed: {completed} Abandoned: {abandoned} Error: {errors}", info.CompletedCount, info.AbandonCount, info.ErrorCount); Assert.Equal(workItemCount, info.CompletedCount + info.AbandonCount + info.ErrorCount); // In memory queue doesn't share state. if (queue.GetType() == typeof(InMemoryQueue <SimpleWorkItem>)) { var stats = await queue.GetQueueStatsAsync(); Assert.Equal(info.CompletedCount, stats.Completed); } else { var workerStats = new List <QueueStats>(); for (int i = 0; i < workers.Count; i++) { var stats = await workers[i].GetQueueStatsAsync(); _logger.Info("Worker#{i} Working: {working} Completed: {completed} Abandoned: {abandoned} Error: {errors} Deadletter: {deadletter}", i, stats.Working, stats.Completed, stats.Abandoned, stats.Errors, stats.Deadletter); workerStats.Add(stats); } Assert.Equal(info.CompletedCount, workerStats.Sum(s => s.Completed)); } } finally { foreach (var q in workers) { await q.DeleteQueueAsync(); q.Dispose(); } } } }
public async Task CanReindexVersionedIndexWithUpdatedDocsAsync() { var version1Index = new VersionedEmployeeIndex(_configuration, 1); await version1Index.DeleteAsync(); var version2Index = new VersionedEmployeeIndex(_configuration, 2); await version2Index.DeleteAsync(); using (new DisposableAction(() => version1Index.DeleteAsync().GetAwaiter().GetResult())) { await version1Index.ConfigureAsync(); Assert.True(_client.IndexExists(version1Index.VersionedName).Exists); var repository = new EmployeeRepository(version1Index.Employee); var employee = await repository.AddAsync(EmployeeGenerator.Default, o => o.ImmediateConsistency()); Assert.NotNull(employee?.Id); using (new DisposableAction(() => version2Index.DeleteAsync().GetAwaiter().GetResult())) { await version2Index.ConfigureAsync(); Assert.True(_client.IndexExists(version2Index.VersionedName).Exists); Assert.Equal(1, await version2Index.GetCurrentVersionAsync()); // alias should still point to the old version until reindex var aliasResponse = await _client.GetAliasAsync(descriptor => descriptor.Name(version2Index.Name)); Assert.True(aliasResponse.IsValid); Assert.Equal(1, aliasResponse.Indices.Count); Assert.Equal(version1Index.VersionedName, aliasResponse.Indices.First().Key); var countdown = new AsyncCountdownEvent(1); var reindexTask = version2Index.ReindexAsync((progress, message) => { _logger.Info($"Reindex Progress {progress}%: {message}"); if (progress == 91) { countdown.Signal(); SystemClock.Sleep(1000); } return(Task.CompletedTask); }); // Wait until the first reindex pass is done. await countdown.WaitAsync(); Assert.Equal(1, await version1Index.GetCurrentVersionAsync()); await repository.AddAsync(EmployeeGenerator.Generate(createdUtc: SystemClock.UtcNow)); employee.Name = "Updated"; await repository.SaveAsync(employee); // Resume after everythings been indexed. await reindexTask; aliasResponse = await _client.GetAliasAsync(descriptor => descriptor.Name(version2Index.Name)); Assert.True(aliasResponse.IsValid); Assert.Equal(1, aliasResponse.Indices.Count); Assert.Equal(version2Index.VersionedName, aliasResponse.Indices.First().Key); Assert.Equal(2, await version1Index.GetCurrentVersionAsync()); Assert.Equal(2, await version2Index.GetCurrentVersionAsync()); await _client.RefreshAsync(Indices.All); var countResponse = await _client.CountAsync <Employee>(d => d.Index(version2Index.VersionedName)); _logger.Trace(() => countResponse.GetRequest()); Assert.True(countResponse.IsValid); Assert.Equal(2, countResponse.Count); var result = await repository.GetByIdAsync(employee.Id); Assert.Equal(ToJson(employee), ToJson(result)); Assert.False((await _client.IndexExistsAsync(version1Index.VersionedName)).Exists); } } }
public async Task CanReindexVersionedIndexWithDeletedDocsAsync() { var version1Index = new VersionedEmployeeIndex(_configuration, 1); await version1Index.DeleteAsync(); var version2Index = new VersionedEmployeeIndex(_configuration, 2); await version2Index.DeleteAsync(); using (new DisposableAction(() => version1Index.DeleteAsync().GetAwaiter().GetResult())) { await version1Index.ConfigureAsync(); Assert.True((await _client.IndexExistsAsync(version1Index.VersionedName)).Exists); var repository = new EmployeeRepository(version1Index.Employee); var employee = await repository.AddAsync(EmployeeGenerator.Default, o => o.ImmediateConsistency()); Assert.NotNull(employee?.Id); using (new DisposableAction(() => version2Index.DeleteAsync().GetAwaiter().GetResult())) { await version2Index.ConfigureAsync(); Assert.True((await _client.IndexExistsAsync(version2Index.VersionedName)).Exists); Assert.Equal(1, await version2Index.GetCurrentVersionAsync()); // alias should still point to the old version until reindex var aliasResponse = await _client.GetAliasAsync(descriptor => descriptor.Name(version2Index.Name)); _logger.LogTraceRequest(aliasResponse); Assert.True(aliasResponse.IsValid); Assert.Equal(1, aliasResponse.Indices.Count); Assert.Equal(version1Index.VersionedName, aliasResponse.Indices.First().Key); var countdown = new AsyncCountdownEvent(1); var reindexTask = version2Index.ReindexAsync(async(progress, message) => { _logger.LogInformation($"Reindex Progress {progress}%: {message}"); if (progress == 91) { countdown.Signal(); await SystemClock.SleepAsync(1000); } }); // Wait until the first reindex pass is done. await countdown.WaitAsync(); Assert.Equal(1, await version1Index.GetCurrentVersionAsync()); await repository.RemoveAllAsync(o => o.ImmediateConsistency()); // Resume after everythings been indexed. await reindexTask; aliasResponse = await _client.GetAliasAsync(descriptor => descriptor.Name(version2Index.Name)); _logger.LogTraceRequest(aliasResponse); Assert.True(aliasResponse.IsValid, aliasResponse.GetErrorMessage()); Assert.Equal(1, aliasResponse.Indices.Count); Assert.Equal(version2Index.VersionedName, aliasResponse.Indices.First().Key); Assert.Equal(2, await version1Index.GetCurrentVersionAsync()); Assert.Equal(2, await version2Index.GetCurrentVersionAsync()); var countResponse = await _client.CountAsync <Employee>(d => d.Index(version1Index.VersionedName)); _logger.LogTraceRequest(countResponse); Assert.True(countResponse.ApiCall.HttpStatusCode == 404, countResponse.GetErrorMessage()); Assert.Equal(0, countResponse.Count); countResponse = await _client.CountAsync <Employee>(d => d.Index(version2Index.VersionedName)); _logger.LogTraceRequest(countResponse); Assert.True(countResponse.IsValid, countResponse.GetErrorMessage()); Assert.Equal(1, countResponse.Count); Assert.Equal(employee, await repository.GetByIdAsync(employee.Id)); Assert.False((await _client.IndexExistsAsync(version1Index.VersionedName)).Exists); } } }
public async void PodWatchDelete() { AsyncCountdownEvent requestHandled = new AsyncCountdownEvent(5); AsyncManualResetEvent serverShutdown = new AsyncManualResetEvent(); var podWatchData = await File.ReadAllTextAsync("podwatch.txt"); var addedAgent = BuildWatchEventStreamLine(podWatchData, WatchEventType.Added); var addedHub = BuildWatchEventStreamLine(podWatchData, WatchEventType.Added, 1); var addedSensor = BuildWatchEventStreamLine(podWatchData, WatchEventType.Added, 3); var v1PodList = JsonConvert.DeserializeObject <V1PodList>(podWatchData); V1Pod modHubPod = v1PodList.Items[1]; var modHub = BuildPodStreamLine(modHubPod, WatchEventType.Deleted); V1Pod tempSensorPod = v1PodList.Items[3]; var modTempSensor = BuildPodStreamLine(tempSensorPod, WatchEventType.Deleted); using (var server = new MockKubeApiServer( async httpContext => { await MockKubeApiServer.WriteStreamLine(httpContext, addedAgent); await MockKubeApiServer.WriteStreamLine(httpContext, addedHub); await MockKubeApiServer.WriteStreamLine(httpContext, addedSensor); await MockKubeApiServer.WriteStreamLine(httpContext, modTempSensor); await MockKubeApiServer.WriteStreamLine(httpContext, modHub); return(false); })) { var client = new Kubernetes( new KubernetesClientConfiguration { Host = server.Uri.ToString() }); var k8sRuntimeInfo = new KubernetesRuntimeInfoProvider(PodwatchNamespace, client); k8sRuntimeInfo.PropertyChanged += (sender, args) => { Assert.Equal("Modules", args.PropertyName); requestHandled.Signal(); }; k8sRuntimeInfo.Start(); await Task.WhenAny(requestHandled.WaitAsync(), Task.Delay(TestTimeout)); var runtimeModules = await k8sRuntimeInfo.GetModules(CancellationToken.None); var moduleRuntimeInfos = runtimeModules as ModuleRuntimeInfo[] ?? runtimeModules.ToArray(); Assert.Single(moduleRuntimeInfos); foreach (var i in moduleRuntimeInfos) { Assert.Equal("edgeAgent", i.Name); Assert.Equal(ModuleStatus.Running, i.ModuleStatus); Assert.Equal(new DateTime(2019, 6, 12), i.StartTime.GetOrElse(DateTime.MinValue).Date); Assert.Equal("docker", i.Type); if (i is ModuleRuntimeInfo <DockerReportedConfig> d) { Assert.NotEqual("unknown:unknown", d.Config.Image); } } } }
public async Task CanIncrementSuspendedOrganizationUsageAsync() { var messageBus = GetService <IMessageBus>(); var countdown = new AsyncCountdownEvent(2); await messageBus.SubscribeAsync <PlanOverage>(po => { _logger.LogInformation("Plan Overage for {organization} (Hourly: {IsHourly})", po.OrganizationId, po.IsHourly); countdown.Signal(); }); var o = await _organizationRepository.AddAsync(new Organization { Name = "Test", MaxEventsPerMonth = 750, PlanId = BillingManager.SmallPlan.Id }, opt => opt.Cache()); var project = await _projectRepository.AddAsync(new Project { Name = "Test", OrganizationId = o.Id, NextSummaryEndOfDayTicks = SystemClock.UtcNow.Ticks }, opt => opt.Cache()); Assert.False(await _usageService.IncrementUsageAsync(o, project, false, 5)); await countdown.WaitAsync(TimeSpan.FromMilliseconds(150)); Assert.Equal(2, countdown.CurrentCount); Assert.Equal(5, await _cache.GetAsync <long>(GetHourlyTotalCacheKey(o.Id), 0)); Assert.Equal(5, await _cache.GetAsync <long>(GetHourlyTotalCacheKey(o.Id, project.Id), 0)); Assert.Equal(5, await _cache.GetAsync <long>(GetMonthlyTotalCacheKey(o.Id), 0)); Assert.Equal(5, await _cache.GetAsync <long>(GetMonthlyTotalCacheKey(o.Id, project.Id), 0)); Assert.Equal(0, await _cache.GetAsync <long>(GetHourlyBlockedCacheKey(o.Id), 0)); Assert.Equal(0, await _cache.GetAsync <long>(GetHourlyBlockedCacheKey(o.Id, project.Id), 0)); Assert.Equal(0, await _cache.GetAsync <long>(GetMonthlyBlockedCacheKey(o.Id), 0)); Assert.Equal(0, await _cache.GetAsync <long>(GetMonthlyBlockedCacheKey(o.Id, project.Id), 0)); o.IsSuspended = true; o.SuspendedByUserId = TestConstants.UserId; o.SuspensionDate = SystemClock.UtcNow; o.SuspensionCode = SuspensionCode.Billing; o = await _organizationRepository.SaveAsync(o, opt => opt.Cache()); Assert.True(await _usageService.IncrementUsageAsync(o, project, false, 4995)); await countdown.WaitAsync(TimeSpan.FromMilliseconds(150)); Assert.Equal(1, countdown.CurrentCount); Assert.Equal(5000, await _cache.GetAsync <long>(GetHourlyTotalCacheKey(o.Id), 0)); Assert.Equal(5000, await _cache.GetAsync <long>(GetHourlyTotalCacheKey(o.Id, project.Id), 0)); Assert.Equal(5000, await _cache.GetAsync <long>(GetMonthlyTotalCacheKey(o.Id), 0)); Assert.Equal(5000, await _cache.GetAsync <long>(GetMonthlyTotalCacheKey(o.Id, project.Id), 0)); Assert.Equal(4995, await _cache.GetAsync <long>(GetHourlyBlockedCacheKey(o.Id), 0)); Assert.Equal(4995, await _cache.GetAsync <long>(GetHourlyBlockedCacheKey(o.Id, project.Id), 0)); Assert.Equal(4995, await _cache.GetAsync <long>(GetMonthlyBlockedCacheKey(o.Id), 0)); Assert.Equal(4995, await _cache.GetAsync <long>(GetMonthlyBlockedCacheKey(o.Id, project.Id), 0)); o.RemoveSuspension(); o = await _organizationRepository.SaveAsync(o, opt => opt.Cache()); Assert.False(await _usageService.IncrementUsageAsync(o, project, false, 1)); await countdown.WaitAsync(TimeSpan.FromMilliseconds(150)); Assert.Equal(1, countdown.CurrentCount); Assert.Equal(5001, await _cache.GetAsync <long>(GetHourlyTotalCacheKey(o.Id), 0)); Assert.Equal(5001, await _cache.GetAsync <long>(GetHourlyTotalCacheKey(o.Id, project.Id), 0)); Assert.Equal(5001, await _cache.GetAsync <long>(GetMonthlyTotalCacheKey(o.Id), 0)); Assert.Equal(5001, await _cache.GetAsync <long>(GetMonthlyTotalCacheKey(o.Id, project.Id), 0)); Assert.Equal(4995, await _cache.GetAsync <long>(GetHourlyBlockedCacheKey(o.Id), 0)); Assert.Equal(4995, await _cache.GetAsync <long>(GetHourlyBlockedCacheKey(o.Id, project.Id), 0)); Assert.Equal(4995, await _cache.GetAsync <long>(GetMonthlyBlockedCacheKey(o.Id), 0)); Assert.Equal(4995, await _cache.GetAsync <long>(GetMonthlyBlockedCacheKey(o.Id, project.Id), 0)); }
public LogStep(AsyncCountdownEvent countdown) { this.countdown = countdown; }
public async Task CanIncrementUsageAsync() { var messageBus = GetService <IMessageBus>(); var countdown = new AsyncCountdownEvent(2); await messageBus.SubscribeAsync <PlanOverage>(po => { _logger.LogInformation("Plan Overage for {organization} (Hourly: {IsHourly})", po.OrganizationId, po.IsHourly); countdown.Signal(); }); var o = await _organizationRepository.AddAsync(new Organization { Name = "Test", MaxEventsPerMonth = 750, PlanId = BillingManager.SmallPlan.Id }); var project = await _projectRepository.AddAsync(new Project { Name = "Test", OrganizationId = o.Id, NextSummaryEndOfDayTicks = SystemClock.UtcNow.Ticks }, opt => opt.Cache()); await _configuration.Client.RefreshAsync(Indices.All); Assert.InRange(o.GetHourlyEventLimit(), 1, 750); int totalToIncrement = o.GetHourlyEventLimit() - 1; Assert.False(await _usageService.IncrementUsageAsync(o, project, false, totalToIncrement)); await _configuration.Client.RefreshAsync(Indices.All); o = await _organizationRepository.GetByIdAsync(o.Id); await countdown.WaitAsync(TimeSpan.FromMilliseconds(150)); Assert.Equal(2, countdown.CurrentCount); Assert.Equal(totalToIncrement, await _cache.GetAsync <long>(GetHourlyTotalCacheKey(o.Id), 0)); Assert.Equal(totalToIncrement, await _cache.GetAsync <long>(GetHourlyTotalCacheKey(o.Id, project.Id), 0)); Assert.Equal(totalToIncrement, await _cache.GetAsync <long>(GetMonthlyTotalCacheKey(o.Id), 0)); Assert.Equal(totalToIncrement, await _cache.GetAsync <long>(GetMonthlyTotalCacheKey(o.Id, project.Id), 0)); Assert.Equal(0, await _cache.GetAsync <long>(GetHourlyBlockedCacheKey(o.Id), 0)); Assert.Equal(0, await _cache.GetAsync <long>(GetHourlyBlockedCacheKey(o.Id, project.Id), 0)); Assert.Equal(0, await _cache.GetAsync <long>(GetMonthlyBlockedCacheKey(o.Id), 0)); Assert.Equal(0, await _cache.GetAsync <long>(GetMonthlyBlockedCacheKey(o.Id, project.Id), 0)); Assert.True(await _usageService.IncrementUsageAsync(o, project, false, 2)); await _configuration.Client.RefreshAsync(Indices.All); o = await _organizationRepository.GetByIdAsync(o.Id); await countdown.WaitAsync(TimeSpan.FromMilliseconds(150)); Assert.Equal(1, countdown.CurrentCount); Assert.Equal(totalToIncrement + 2, await _cache.GetAsync <long>(GetHourlyTotalCacheKey(o.Id), 0)); Assert.Equal(totalToIncrement + 2, await _cache.GetAsync <long>(GetHourlyTotalCacheKey(o.Id, project.Id), 0)); Assert.Equal(totalToIncrement + 2, await _cache.GetAsync <long>(GetMonthlyTotalCacheKey(o.Id), 0)); Assert.Equal(totalToIncrement + 2, await _cache.GetAsync <long>(GetMonthlyTotalCacheKey(o.Id, project.Id), 0)); Assert.Equal(1, await _cache.GetAsync <long>(GetHourlyBlockedCacheKey(o.Id), 0)); Assert.Equal(1, await _cache.GetAsync <long>(GetHourlyBlockedCacheKey(o.Id, project.Id), 0)); Assert.Equal(1, await _cache.GetAsync <long>(GetMonthlyBlockedCacheKey(o.Id), 0)); Assert.Equal(1, await _cache.GetAsync <long>(GetMonthlyBlockedCacheKey(o.Id, project.Id), 0)); o = await _organizationRepository.AddAsync(new Organization { Name = "Test", MaxEventsPerMonth = 750, PlanId = BillingManager.SmallPlan.Id }); project = await _projectRepository.AddAsync(new Project { Name = "Test", OrganizationId = o.Id, NextSummaryEndOfDayTicks = SystemClock.UtcNow.Ticks }, opt => opt.Cache()); await _configuration.Client.RefreshAsync(Indices.All); await _cache.RemoveAllAsync(); totalToIncrement = o.GetHourlyEventLimit() + 20; Assert.True(await _usageService.IncrementUsageAsync(o, project, false, totalToIncrement)); await countdown.WaitAsync(TimeSpan.FromMilliseconds(150)); Assert.Equal(0, countdown.CurrentCount); Assert.Equal(totalToIncrement, await _cache.GetAsync <long>(GetHourlyTotalCacheKey(o.Id), 0)); Assert.Equal(totalToIncrement, await _cache.GetAsync <long>(GetHourlyTotalCacheKey(o.Id, project.Id), 0)); Assert.Equal(totalToIncrement, await _cache.GetAsync <long>(GetMonthlyTotalCacheKey(o.Id), 0)); Assert.Equal(totalToIncrement, await _cache.GetAsync <long>(GetMonthlyTotalCacheKey(o.Id, project.Id), 0)); Assert.Equal(20, await _cache.GetAsync <long>(GetHourlyBlockedCacheKey(o.Id), 0)); Assert.Equal(20, await _cache.GetAsync <long>(GetHourlyBlockedCacheKey(o.Id, project.Id), 0)); Assert.Equal(20, await _cache.GetAsync <long>(GetMonthlyBlockedCacheKey(o.Id), 0)); Assert.Equal(20, await _cache.GetAsync <long>(GetMonthlyBlockedCacheKey(o.Id, project.Id), 0)); }
public void AddCount_Overflow_ThrowsException() { var ce = new AsyncCountdownEvent(long.MaxValue); AsyncAssert.Throws <OverflowException>(() => ce.AddCount()); }
/// <summary> /// Resolves all Css properties to their specified values by cascading /// </summary> public void Cascade() { /* XXX: LOTS of room for speed improvement here. We should avoid using LINQ statements in performance critical areas like this. */ var benchmark_id = Benchmark.Start("style-cascade"); // Get a list of only the properties with an Assigned value AsyncCountdownEvent ctdn = null; /* * HashSet<AtomicName<ECssPropertyID>> targetFields = new HashSet<AtomicName<ECssPropertyID>>(); * List<HashSet<AtomicName<ECssPropertyID>>> allFields = CssRules.Values.Select(x => { return x.SetProperties; }).ToList(); */ var targetFields = new FlagCollection <ECssPropertyID>((int)ECssPropertyID.MAX_VALUE); List <FlagCollection <ECssPropertyID> > allFields = CssRules.Values.Select(x => { return(x.SetProperties); }).ToList(); // Remove duplicates //foreach (HashSet<AtomicName<ECssPropertyID>> fields in allFields) foreach (FlagCollection <ECssPropertyID> fields in allFields) { targetFields.And(fields); } if (targetFields.ActiveFlags > 0) { // Cascade all those set values ctdn = new AsyncCountdownEvent(targetFields.ActiveFlags); // Loop over all target properties foreach (int flagIndex in targetFields) //Parallel.ForEach(targetFields, async (AtomicString propName) => { try { AtomicName <ECssPropertyID> propName = new AtomicName <ECssPropertyID>(flagIndex); // Extract this property from every CssPropertySet that has a value for it var propertyList = CssRules.Values.Select(x => { return(x.Get(propName)); }).ToList(); // Order these properties according to CSS 3.0 specifications propertyList.Sort(CssPropertyComparator.Instance); /* * // Because cascading overwrites an existing value with the one from the next propertyset we need to reverse this list. * propertyList.Reverse();*/ // Cascade this list and get what CSS calls the 'Specified' value ICssProperty Value = Cascaded.Get(propName); foreach (ICssProperty o in propertyList) { //bool b = await Value.CascadeAsync(o); bool b = Value.Cascade(o); if (b) { break; // stop cascading the instant we find a set value } } string SourceState = Value.Source.ToString(); //await Cascaded.Set(propName, Value); Cascaded.Set(propName, Value); } finally { ctdn.Signal(); } //}); } ctdn.WaitAsync().Wait(); } // Recalculate ALL properties var PropList = Cascaded.GetAll().ToList(); /*ctdn = new AsyncCountdownEvent(PropList.Count); * Parallel.For(0, PropList.Count, (int i) => * { * ICssProperty prop = PropList[i]; * // We always want to compute these now to get their values resolved. otherwise any with just assigned values will not interpret and output computed values. * prop.Update(ComputeNow: true); * ctdn.Signal(); * }); * ctdn.WaitAsync().Wait();*/ for (int i = 0; i < PropList.Count; i++) { ICssProperty prop = PropList[i]; // We always want to compute these now to get their values resolved. otherwise any with just assigned values will not interpret and output computed values. prop.Update(ComputeNow: true); } // Any values that changed due to this cascade should have thrown a property change event to let the style system know what it needs to update // Remove cascade flag ClearFlag(EPropertySystemDirtFlags.NeedsToCascade); Benchmark.Stop(benchmark_id); }
public void Id_IsNotZero() { var ce = new AsyncCountdownEvent(0); Assert.NotEqual(0, ce.Id); }
public DecrementElement(AsyncCountdownEvent countdown) { this.countdown = countdown; }
public async Task InitialCountZero() { var evt = new AsyncCountdownEvent(0); await evt.WaitAsync(); }
public LogElement(AsyncCountdownEvent countdown) { this.countdown = countdown; }
public virtual async Task CanHaveMultipleQueueInstances() { var queue = GetQueue(retries: 0, retryDelay: TimeSpan.Zero); if (queue == null) { return; } using (queue) { await queue.DeleteQueueAsync(); await AssertEmptyQueueAsync(queue); const int workItemCount = 50; const int workerCount = 3; var countdown = new AsyncCountdownEvent(workItemCount); var info = new WorkInfo(); var workers = new List <IQueue <SimpleWorkItem> > { queue }; try { for (int i = 0; i < workerCount; i++) { var q = GetQueue(retries: 0, retryDelay: TimeSpan.Zero); _logger.Trace("Queue Id: {0}, I: {1}", q.QueueId, i); await q.StartWorkingAsync(async w => await DoWorkAsync(w, countdown, info)); workers.Add(q); } await Run.InParallel(workItemCount, async i => { var id = await queue.EnqueueAsync(new SimpleWorkItem { Data = "Hello", Id = i }); _logger.Trace("Enqueued Index: {0} Id: {1}", i, id); }); await countdown.WaitAsync(); await SystemClock.SleepAsync(50); _logger.Trace("Completed: {0} Abandoned: {1} Error: {2}", info.CompletedCount, info.AbandonCount, info.ErrorCount); _logger.Info("Work Info Stats: Completed: {completed} Abandoned: {abandoned} Error: {errors}", info.CompletedCount, info.AbandonCount, info.ErrorCount); Assert.Equal(workItemCount, info.CompletedCount + info.AbandonCount + info.ErrorCount); // In memory queue doesn't share state. if (queue.GetType() == typeof(InMemoryQueue <SimpleWorkItem>)) { var stats = await queue.GetQueueStatsAsync(); Assert.Equal(0, stats.Working); Assert.Equal(0, stats.Timeouts); Assert.Equal(workItemCount, stats.Enqueued); Assert.Equal(workItemCount, stats.Dequeued); Assert.Equal(info.CompletedCount, stats.Completed); Assert.Equal(info.ErrorCount, stats.Errors); Assert.Equal(info.AbandonCount, stats.Abandoned - info.ErrorCount); Assert.Equal(info.AbandonCount + stats.Errors, stats.Deadletter); } else { var workerStats = new List <QueueStats>(); for (int i = 0; i < workers.Count; i++) { var stats = await workers[i].GetQueueStatsAsync(); _logger.Info("Worker#{i} Working: {working} Completed: {completed} Abandoned: {abandoned} Error: {errors} Deadletter: {deadletter}", i, stats.Working, stats.Completed, stats.Abandoned, stats.Errors, stats.Deadletter); workerStats.Add(stats); } Assert.Equal(info.CompletedCount, workerStats.Sum(s => s.Completed)); Assert.Equal(info.ErrorCount, workerStats.Sum(s => s.Errors)); Assert.Equal(info.AbandonCount, workerStats.Sum(s => s.Abandoned) - info.ErrorCount); Assert.Equal(info.AbandonCount + workerStats.Sum(s => s.Errors), (workerStats.LastOrDefault()?.Deadletter ?? 0)); } } finally { foreach (var q in workers) { await q.DeleteQueueAsync(); q.Dispose(); } } } }
public async Task An_interval_can_be_specified_before_which_a_released_lease_will_be_granted_again() { var tally = new ConcurrentDictionary<string, int>(); var distributor = CreateDistributor(waitInterval: TimeSpan.FromMilliseconds(5000)).Trace(); var countdown = new AsyncCountdownEvent(10); distributor.OnReceive(async lease => { tally.AddOrUpdate(lease.LeasableResource.Name, addValueFactory: s => 1, updateValueFactory: (s, v) => v + 1); countdown.Signal(); }); await distributor.Start(); await countdown.WaitAsync().Timeout(); await distributor.Stop(); tally.Count.Should().Be(10); tally.Should().ContainKeys("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"); tally.Should().ContainValues(1, 1, 1, 1, 1, 1, 1, 1, 1, 1); }
public async Task DataflowBulkInsertBlockRetryTasksAsync(int seqNumber = 1, int frequency = 1000, int taskCount = 2, int count = 1, int orderCount = 100, int maxRetryCount = 2) { if (!_isInitialize) { throw new InvalidOperationException("not initialize"); } long totalOrderCount = taskCount * count * orderCount * maxRetryCount; var logger = _loggerFactory.CreateLogger($"DataflowBulkInser-{seqNumber}-OrderCount:{totalOrderCount}"); int currentRetryCount = 0; try { TimeSpan period = TimeSpan.FromMilliseconds(frequency); var transportTimeWatcher = Stopwatch.StartNew(); TimeSpan totalTransportTime = TimeSpan.Zero; var executionTimeWatcher = Stopwatch.StartNew(); logger.LogInformation($"----begin dataflow bulk insert {totalOrderCount} orders,now:{DateTime.Now.TimeOfDay}----"); while (true) { var signals = new AsyncCountdownEvent(taskCount); if (_cancellationTokenSource.Token.IsCancellationRequested) { _cancellationTokenSource.Token.ThrowIfCancellationRequested(); // _logger?.LogCritical($"message was cancelled"); break; } transportTimeWatcher.Restart(); var insertTasks = from tc in Enumerable.Range(0, taskCount) select Task.Run(() => { return DataflowBulkInsertBlockRetryTasksCoreAsync(signals, tc, count, orderCount); }, _cancellationTokenSource.Token); await Task.WhenAll(insertTasks); await signals.WaitAsync(); totalTransportTime += transportTimeWatcher.Elapsed; transportTimeWatcher.Reset(); await Task.Delay(period); currentRetryCount++; if (currentRetryCount >= maxRetryCount) { //_cancellationTokenSource.CancelAfter(TimeSpan.FromMilliseconds(200)); break; } } logger .LogInformation($"----dataflow bulk insert {totalOrderCount} orders,cost time:\"{executionTimeWatcher.Elapsed}\",transport time:{ totalTransportTime },count/time(sec):{Math.Ceiling(totalOrderCount / totalTransportTime.TotalSeconds)},now:\"{DateTime.Now.TimeOfDay}\"----"); } catch (Exception ex) { logger.LogError($"Error while dataflow bulk insert: {ex.Message}"); } }
public async Task ContinueWatchingThroughInactivity() { try { var eventsReceived = new AsyncCountdownEvent(1); var client = new Kubernetes(KubernetesClientConfiguration.BuildConfigFromConfigFile()); var listTask = await client.ListNamespacedPodWithHttpMessagesAsync("testns", watch : true).ConfigureAwait(false); var events = new HashSet <V1Pod>(); var errors = 0; var connclosed = 0; var eventcount = 0; var watcher = listTask.Watch <V1Pod, V1PodList>( (type, item) => { testOutput.WriteLine($"Watcher received '{type}' event."); events.Add(item); eventsReceived.Signal(); }, error => { testOutput.WriteLine($"Watcher received '{error.GetType().FullName}' error."); errors += 1; eventsReceived.Signal(); }, () => { connclosed += 1; }); var pod1 = GetTestPod("test1"); client.CreateNamespacedPod(pod1, "testns"); // wait server yields all events await Task.WhenAny(eventsReceived.WaitAsync(), Task.Delay(3000)).ConfigureAwait(false); // wait for some time to pass before generating another event await Task.Delay(TimeSpan.FromMinutes(2)).ConfigureAwait(false); eventcount = events.Count; var pod2 = GetTestPod("test2"); client.CreateNamespacedPod(pod2, "testns"); // Allow 30 seconds for the event to come through await Task.Delay(TimeSpan.FromSeconds(30)).ConfigureAwait(false); // validate that one new event has been recieved after a pause Assert.True( events.Count == (eventcount + 1), $"No Events Were Received after Pause Event Count Is {eventcount}"); Assert.Equal(1, errors); Assert.True(watcher.Watching); // cleanup after client.DeleteNamespacedPod("test1", "default"); client.DeleteNamespacedPod("test2", "default"); } catch (Exception ex) { Debug.WriteLine(ex.Message); } }
public virtual async Task CanReceiveMessagesConcurrentlyAsync() { const int iterations = 100; var messageBus = GetMessageBus(); if (messageBus == null) { return; } var messageBuses = new List <IMessageBus>(10); try { var countdown = new AsyncCountdownEvent(iterations * 10); await Run.InParallelAsync(10, async i => { var bus = GetMessageBus(); await bus.SubscribeAsync <SimpleMessageA>(msg => { Assert.Equal("Hello", msg.Data); countdown.Signal(); }); messageBuses.Add(bus); }); var subscribe = Run.InParallelAsync(iterations, i => { #pragma warning disable AsyncFixer02 // Long running or blocking operations under an async method SystemClock.Sleep(RandomData.GetInt(0, 10)); #pragma warning restore AsyncFixer02 // Long running or blocking operations under an async method return(messageBuses.Random().SubscribeAsync <NeverPublishedMessage>(msg => Task.CompletedTask)); }); var publish = Run.InParallelAsync(iterations + 3, i => { switch (i) { case 1: return(messageBus.PublishAsync(new DerivedSimpleMessageA { Data = "Hello" })); case 2: return(messageBus.PublishAsync(new Derived2SimpleMessageA { Data = "Hello" })); case 3: return(messageBus.PublishAsync(new Derived3SimpleMessageA { Data = "Hello" })); case 4: return(messageBus.PublishAsync(new Derived4SimpleMessageA { Data = "Hello" })); case 5: return(messageBus.PublishAsync(new Derived5SimpleMessageA { Data = "Hello" })); case 6: return(messageBus.PublishAsync(new Derived6SimpleMessageA { Data = "Hello" })); case 7: return(messageBus.PublishAsync(new Derived7SimpleMessageA { Data = "Hello" })); case 8: return(messageBus.PublishAsync(new Derived8SimpleMessageA { Data = "Hello" })); case 9: return(messageBus.PublishAsync(new Derived9SimpleMessageA { Data = "Hello" })); case 10: return(messageBus.PublishAsync(new Derived10SimpleMessageA { Data = "Hello" })); case iterations + 1: return(messageBus.PublishAsync(new { Data = "Hello" })); case iterations + 2: return(messageBus.PublishAsync(new SimpleMessageC { Data = "Hello" })); case iterations + 3: return(messageBus.PublishAsync(new SimpleMessageB { Data = "Hello" })); default: return(messageBus.PublishAsync(new SimpleMessageA { Data = "Hello" })); } }); await Task.WhenAll(subscribe, publish); await countdown.WaitAsync(TimeSpan.FromSeconds(2)); Assert.Equal(0, countdown.CurrentCount); } finally { foreach (var mb in messageBuses) { await CleanupMessageBusAsync(mb); } await CleanupMessageBusAsync(messageBus); } }
public async Task CanPersistAndNotLoseMessages() { Log.MinimumLevel = LogLevel.Trace; var messageBus1 = new RabbitMQMessageBus(o => o .ConnectionString("amqp://localhost:5673") .LoggerFactory(Log) .SubscriptionQueueName($"{_topic}-offline") .IsSubscriptionQueueExclusive(false) .SubscriptionQueueAutoDelete(false) .AcknowledgementStrategy(AcknowledgementStrategy.Automatic)); var countdownEvent = new AsyncCountdownEvent(1); var cts = new CancellationTokenSource(); await messageBus1.SubscribeAsync <SimpleMessageA>(msg => { _logger.LogInformation("[Subscriber1] Got message: {Message}", msg.Data); countdownEvent.Signal(); }, cts.Token); await messageBus1.PublishAsync(new SimpleMessageA { Data = "Audit message 1" }); await countdownEvent.WaitAsync(TimeSpan.FromSeconds(5)); Assert.Equal(0, countdownEvent.CurrentCount); cts.Cancel(); await messageBus1.PublishAsync(new SimpleMessageA { Data = "Audit message 2" }); cts = new CancellationTokenSource(); countdownEvent.AddCount(1); await messageBus1.SubscribeAsync <SimpleMessageA>(msg => { _logger.LogInformation("[Subscriber2] Got message: {Message}", msg.Data); countdownEvent.Signal(); }, cts.Token); await countdownEvent.WaitAsync(TimeSpan.FromSeconds(5)); Assert.Equal(0, countdownEvent.CurrentCount); cts.Cancel(); await messageBus1.PublishAsync(new SimpleMessageA { Data = "Audit offline message 1" }); await messageBus1.PublishAsync(new SimpleMessageA { Data = "Audit offline message 2" }); await messageBus1.PublishAsync(new SimpleMessageA { Data = "Audit offline message 3" }); messageBus1.Dispose(); var messageBus2 = new RabbitMQMessageBus(o => o .ConnectionString("amqp://localhost:5673") .LoggerFactory(Log) .SubscriptionQueueName($"{_topic}-offline") .IsSubscriptionQueueExclusive(false) .SubscriptionQueueAutoDelete(false) .AcknowledgementStrategy(AcknowledgementStrategy.Automatic)); cts = new CancellationTokenSource(); countdownEvent.AddCount(4); await messageBus2.SubscribeAsync <SimpleMessageA>(msg => { _logger.LogInformation("[Subscriber3] Got message: {Message}", msg.Data); countdownEvent.Signal(); }, cts.Token); await messageBus2.PublishAsync(new SimpleMessageA { Data = "Another audit message 4" }); await countdownEvent.WaitAsync(TimeSpan.FromSeconds(5)); Assert.Equal(0, countdownEvent.CurrentCount); messageBus2.Dispose(); }
public async Task When_receiver_throws_then_work_distribution_continues() { var received = 0; var distributor = CreateDistributor(waitInterval: TimeSpan.FromMilliseconds(100)).Trace(); var countdown = new AsyncCountdownEvent(20); distributor.OnReceive(async lease => { Interlocked.Increment(ref received); if (received < 10) { throw new Exception("dangit!"); } countdown.Signal(); }); await distributor.Start(); await countdown.WaitAsync().Timeout(); await distributor.Stop(); received.Should().BeGreaterOrEqualTo(20); }
/// <summary> /// Represents an replication operation to all destination servers of an item specified by ETag /// </summary> /// <param name="etag">ETag of an replicated item</param> /// <param name="timeout">Optional timeout</param> /// <param name="database">The database from which to check, if null, the default database for the document store connection string</param> /// <param name="replicas">The min number of replicas that must have the value before we can return (or the number of destinations, if higher)</param> /// <returns>Task which will have the number of nodes that the caught up to the specified etag</returns> public async Task <int> WaitAsync(Etag etag = null, TimeSpan?timeout = null, string database = null, int replicas = 2) { etag = etag ?? documentStore.LastEtagHolder.GetLastWrittenEtag(); if (etag == Etag.Empty) { return(replicas); // if the etag is empty, nothing to do } var asyncDatabaseCommands = documentStore.AsyncDatabaseCommands; if (database != null) { asyncDatabaseCommands = asyncDatabaseCommands.ForDatabase(database); } asyncDatabaseCommands.ForceReadFromMaster(); var doc = await asyncDatabaseCommands.GetAsync("Raven/Replication/Destinations"); if (doc == null) { return(-1); } var replicationDocument = doc.DataAsJson.JsonDeserialization <ReplicationDocument>(); if (replicationDocument == null) { return(-1); } var destinationsToCheck = replicationDocument.Destinations .Where( x => x.Disabled == false && x.IgnoredClient == false) .Select(x => x.ClientVisibleUrl ?? x.Url) .ToList(); if (destinationsToCheck.Count == 0) { return(0); } int toCheck = Math.Min(replicas, destinationsToCheck.Count); var countDown = new AsyncCountdownEvent(toCheck); var errors = new BlockingCollection <Exception>(); foreach (var url in destinationsToCheck) { WaitForReplicationFromServerAsync(url, countDown, etag, errors); } if (await countDown.WaitAsync().WaitWithTimeout(timeout) == false) { throw new TimeoutException( string.Format("Confirmed that the specified etag {0} was replicated to {1} of {2} servers, during {3}", etag, (toCheck - countDown.Count), toCheck, timeout)); } if (errors.Count > 0 && countDown.Count > 0) { throw new AggregateException(errors); } return(countDown.Count); }
public async Task <List <Task> > RunAsync(int numPushers, int numPullers, int numMessagesPerPusher, AsyncCountdownEvent counter, CancellationToken token) { var inUrl = UrlIpc(); var outUrl = UrlIpc(); const int numBrokers = 1; var brokersReady = new AsyncBarrier(numBrokers + 1); var clientsReady = new AsyncBarrier(numPushers + numPullers + numBrokers); var numForwarded = 0; var tasks = new List <Task>(); for (var i = 0; i < numBrokers; ++i) { var task = Task.Run(async() => { using (var pullSocket = implementation.CreateInSocket(inUrl)) using (var pushSocket = implementation.CreateOutSocket(outUrl)) { await brokersReady.SignalAndWait(); // Broker is ready await clientsReady.SignalAndWait(); // Wait for clients while (!token.IsCancellationRequested) { var msg = await pullSocket.Receive(token); await pushSocket.Send(msg.Unwrap()); ++numForwarded; } } }); tasks.Add(task); } await brokersReady.SignalAndWait(); for (var i = 0; i < numPushers; ++i) { var task = Task.Run(async() => { using (var pushSocket = implementation.Factory.PusherCreate(inUrl, false).Unwrap().CreateAsyncContext(implementation.Factory).Unwrap()) { await clientsReady.SignalAndWait(); // This client ready, wait for rest for (var m = 0; m < numMessagesPerPusher; ++m) { await pushSocket.Send(implementation.CreateMessage()); await WaitShort(); } } }); tasks.Add(task); } for (var i = 0; i < numPullers; ++i) { var task = Task.Run(async() => { using (var pullSocket = implementation.CreateClient(outUrl)) { await clientsReady.SignalAndWait(); // This client ready, wait for rest while (!token.IsCancellationRequested) { var _ = await pullSocket.Receive(token); counter.Signal(); } } }); tasks.Add(task); } return(tasks); }
public void SetUp() { countdown = new AsyncCountdownEvent(3); }