public void everything_should_go_fine() { var miniNode = new MiniNode(PathName); miniNode.Start(); var tcpPort = miniNode.TcpEndPoint.Port; var tcpSecPort = miniNode.TcpSecEndPoint.Port; var httpPort = miniNode.HttpEndPoint.Port; const int cnt = 50; var countdown = new CountdownEvent(cnt); // --- first part of events WriteEvents(cnt, miniNode, countdown); Assert.IsTrue(countdown.Wait(TimeSpan.FromSeconds(10)), "Too long writing first part of events."); countdown.Reset(); // -- set up truncation var truncatePosition = miniNode.Db.Config.WriterCheckpoint.ReadNonFlushed(); miniNode.Db.Config.TruncateCheckpoint.Write(truncatePosition); miniNode.Db.Config.TruncateCheckpoint.Flush(); // --- second part of events WriteEvents(cnt, miniNode, countdown); Assert.IsTrue(countdown.Wait(TimeSpan.FromSeconds(10)), "Too long writing second part of events."); countdown.Reset(); miniNode.Shutdown(keepDb: true, keepPorts: true); // --- first restart and truncation miniNode = new MiniNode(PathName, tcpPort, tcpSecPort, httpPort); miniNode.Start(); Assert.AreEqual(-1, miniNode.Db.Config.TruncateCheckpoint.Read()); Assert.That(miniNode.Db.Config.WriterCheckpoint.Read(), Is.GreaterThanOrEqualTo(truncatePosition)); // -- third part of events WriteEvents(cnt, miniNode, countdown); Assert.IsTrue(countdown.Wait(TimeSpan.FromSeconds(10)), "Too long writing third part of events."); countdown.Reset(); miniNode.Shutdown(keepDb: true, keepPorts: true); // -- second restart miniNode = new MiniNode(PathName, tcpPort, tcpSecPort, httpPort); Assert.AreEqual(-1, miniNode.Db.Config.TruncateCheckpoint.Read()); miniNode.Start(); // -- if we get here -- then everything is ok miniNode.Shutdown(); }
public void Should_release_the_pool() { // Arrange var blockTheThread = new AutoResetEvent(false); var countdownEvent = new CountdownEvent(1); var queue = Substitute.For<IInMemoryPriorityQueue<GenericPriorityMessage<BasicDeliverEventArgs>>>(); queue.When(x => x.Dequeue()).Do(callInfo => { countdownEvent.Signal(); blockTheThread.WaitOne(); }); var consumer = new PriorityBurrowConsumer(Substitute.For<IModel>(), Substitute.For<IMessageHandler>(), Substitute.For<IRabbitWatcher>(), false, 1); consumer.Init(queue, Substitute.For<CompositeSubscription>(), 1, Guid.NewGuid().ToString()); consumer.Ready(); // Action countdownEvent.Wait(); countdownEvent.Reset(); blockTheThread.Set(); consumer.MessageHandlerHandlingComplete(null); countdownEvent.Wait(); // Assert queue.Received(2).Dequeue(); consumer.Dispose(); blockTheThread.Dispose(); }
protected override void ExecuteLevel(IList<Computation> computationsOfLevel) { using (var countEvent = new CountdownEvent(1)) { foreach (var item in computationsOfLevel) { var cc = item.Context as ParallelComputationContext; if (cc != null) { countEvent.AddCount(); cc.RunTransform(() => { item.Transform(); countEvent.Signal(); }); } else { countEvent.Signal(); countEvent.Wait(); item.Transform(); countEvent.Reset(); } OnComputationCompleted(new ComputationEventArgs(item)); } countEvent.Signal(); countEvent.Wait(); } }
public void MultipleBindingTest () { BufferBlock<int> buffer = new BufferBlock<int> (); var evt = new CountdownEvent (10); int count = 0; ActionBlock<int> block = new ActionBlock<int> ((i) => { Interlocked.Decrement (ref count); evt.Signal (); }); IDisposable bridge = buffer.LinkTo (block); for (int i = 0; i < 10; i++) Assert.IsTrue (buffer.Post (i)); evt.Wait (); Assert.AreEqual (-10, count); count = 0; evt.Reset (); bridge.Dispose (); ActionBlock<int> block2 = new ActionBlock<int> ((i) => { Interlocked.Increment (ref count); evt.Signal (); }); buffer.LinkTo (block2); for (int i = 0; i < 10; i++) Assert.IsTrue (buffer.Post (i)); evt.Wait (); Assert.AreEqual (10, count); }
public void with_truncate_position_in_completed_chunk_everything_should_go_fine() { const int chunkSize = 1024*1024; const int cachedSize = chunkSize*3; var miniNode = new MiniNode(PathName, chunkSize: chunkSize, cachedChunkSize: cachedSize, inMemDb: false); miniNode.Start(); var tcpPort = miniNode.TcpEndPoint.Port; var tcpSecPort = miniNode.TcpSecEndPoint.Port; var httpPort = miniNode.ExtHttpEndPoint.Port; const int cnt = 1; var countdown = new CountdownEvent(cnt); // --- first part of events WriteEvents(cnt, miniNode, countdown, MiniNode.ChunkSize / 5 * 3); Assert.IsTrue(countdown.Wait(TimeSpan.FromSeconds(10)), "Took too long writing first part of events."); countdown.Reset(); // -- set up truncation var truncatePosition = miniNode.Db.Config.WriterCheckpoint.ReadNonFlushed(); miniNode.Db.Config.TruncateCheckpoint.Write(truncatePosition); miniNode.Db.Config.TruncateCheckpoint.Flush(); // --- second part of events WriteEvents(cnt, miniNode, countdown, MiniNode.ChunkSize / 2); Assert.IsTrue(countdown.Wait(TimeSpan.FromSeconds(10)), "Took too long writing second part of events."); countdown.Reset(); miniNode.Shutdown(keepDb: true, keepPorts: true); // --- first restart and truncation miniNode = new MiniNode(PathName, tcpPort, tcpSecPort, httpPort, chunkSize: chunkSize, cachedChunkSize: cachedSize, inMemDb: false); miniNode.Start(); Assert.AreEqual(-1, miniNode.Db.Config.TruncateCheckpoint.Read()); Assert.That(miniNode.Db.Config.WriterCheckpoint.Read(), Is.GreaterThanOrEqualTo(truncatePosition)); // -- third part of events WriteEvents(cnt, miniNode, countdown, MiniNode.ChunkSize / 5); Assert.IsTrue(countdown.Wait(TimeSpan.FromSeconds(10)), "Took too long writing third part of events."); countdown.Reset(); // -- if we get here -- then everything is ok miniNode.Shutdown(); }
public void IsSetTestCase() { var evt = new CountdownEvent (5); Assert.IsFalse(evt.IsSet, "#1"); evt.Signal(5); Assert.IsTrue(evt.IsSet, "#2"); evt.Reset(); Assert.IsFalse(evt.IsSet, "#3"); }
public void CurrentCountTestCase() { var evt = new CountdownEvent (5); Assert.AreEqual(5, evt.CurrentCount, "#1"); evt.AddCount(); Assert.AreEqual(6, evt.CurrentCount, "#2"); evt.TryAddCount(2); Assert.AreEqual(8, evt.CurrentCount, "#3"); evt.Signal(4); Assert.AreEqual(4, evt.CurrentCount, "#4"); evt.Reset(); Assert.AreEqual(5, evt.CurrentCount, "#5"); }
public void Dispose () { var ce = new CountdownEvent (1); ce.Dispose (); Assert.AreEqual (1, ce.CurrentCount, "#0a"); Assert.AreEqual (1, ce.InitialCount, "#0b"); Assert.IsFalse (ce.IsSet, "#0c"); try { ce.AddCount (); Assert.Fail ("#1"); } catch (ObjectDisposedException) { } try { ce.Reset (); Assert.Fail ("#2"); } catch (ObjectDisposedException) { } try { ce.Signal (); Assert.Fail ("#3"); } catch (ObjectDisposedException) { } try { ce.TryAddCount (); Assert.Fail ("#4"); } catch (ObjectDisposedException) { } try { ce.Wait (5); Assert.Fail ("#4"); } catch (ObjectDisposedException) { } try { var v = ce.WaitHandle; Assert.Fail ("#5"); } catch (ObjectDisposedException) { } }
private static void TestSendReceiveRequestCore( IPEndPoint endPoint, int count, CountdownEvent latch ) { using ( var udpClient = new UdpClient( AddressFamily.InterNetwork ) ) { udpClient.Connect( endPoint ); for ( int i = 0; i < count; i++ ) { if ( latch != null ) { latch.Reset(); } var ids = Enumerable.Repeat( 0, latch == null ? 1 : latch.InitialCount ).Select( _ => Guid.NewGuid().ToString() ).ToArray(); if ( !Task.WaitAll( ids.Select( id => Task.Factory.StartNew( _ => { using ( var buffer = new MemoryStream() ) { using ( var packer = Packer.Create( buffer, false ) ) { PackRequest( packer, id ); } buffer.Position = 0; if ( latch != null ) { latch.Signal(); if ( !latch.Wait( Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) ) { throw new TimeoutException(); } } // send udpClient.Send( buffer.ToArray(), ( int )buffer.Length ); } }, id ) ).ToArray(), Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) ) { throw new TimeoutException(); } // receive IPEndPoint received = endPoint; var result = Unpacking.UnpackArray( udpClient.Receive( ref endPoint ) ).Value; AssertResponse( result, ids ); } } }
// Validates init, set, reset state transitions. private static void RunCountdownEventTest0_StateTrans(int initCount, int increms, bool takeAllAtOnce) { string methodParameters = string.Format("RunCountdownEventTest0_StateTrans(initCount={0}, increms={1}, takeAllAtOnce={2})", initCount, increms, takeAllAtOnce); CountdownEvent ev = new CountdownEvent(initCount); // Check initial count. if (ev.InitialCount != initCount) { Debug.WriteLine(methodParameters); Assert.True(false, string.Format(" > error: initial count wrong, saw {0} expected {1}", ev.InitialCount, initCount)); } // Increment (optionally). for (int i = 0; i < increms; i++) { ev.AddCount(); if (ev.CurrentCount != initCount + i + 1) { Debug.WriteLine(methodParameters); Assert.True(false, string.Format(" > error: after incrementing, count is wrong, saw {0}, expect {1}", ev.CurrentCount, initCount + i + 1)); } } // Decrement until it hits 0. if (takeAllAtOnce) { ev.Signal(initCount + increms); } else { for (int i = 0; i < initCount + increms; i++) { if (ev.IsSet) { Debug.WriteLine(methodParameters); Assert.True(false, string.Format(" > error: latch is set after {0} signals", i)); } ev.Signal(); } } // Check the status. if (!ev.IsSet) { Debug.WriteLine(methodParameters); Assert.True(false, string.Format(" > error: latch was not set after all signals received")); } if (ev.CurrentCount != 0) { Debug.WriteLine(methodParameters); Assert.True(false, string.Format(" > error: latch count wasn't 0 after all signals received")); } // Now reset the event and check its count. ev.Reset(); if (ev.CurrentCount != ev.InitialCount) { Debug.WriteLine(methodParameters); Assert.True(false, string.Format(" > error: latch count wasn't correctly reset")); } }
public static void RunCountdownEventTest2_Exceptions() { CountdownEvent cde = null; Assert.Throws<ArgumentOutOfRangeException>(() => cde = new CountdownEvent(-1)); // Failure Case: Constructor didn't throw AORE when -1 passed cde = new CountdownEvent(1); Assert.Throws<ArgumentOutOfRangeException>(() => cde.Signal(0)); // Failure Case: Signal didn't throw AORE when 0 passed cde = new CountdownEvent(0); Assert.Throws<InvalidOperationException>(() => cde.Signal()); // Failure Case: Signal didn't throw IOE when the count is zero cde = new CountdownEvent(1); Assert.Throws<InvalidOperationException>(() => cde.Signal(2)); // Failure Case: Signal didn't throw IOE when the signal count > current count Assert.Throws<ArgumentOutOfRangeException>(() => cde.AddCount(0)); // Failure Case: AddCount didn't throw AORE when 0 passed cde = new CountdownEvent(0); Assert.Throws<InvalidOperationException>(() => cde.AddCount(1)); // Failure Case: AddCount didn't throw IOE when the count is zero cde = new CountdownEvent(int.MaxValue - 10); Assert.Throws<InvalidOperationException>(() => cde.AddCount(20)); // Failure Case: AddCount didn't throw IOE when the count > int.Max cde = new CountdownEvent(2); Assert.Throws<ArgumentOutOfRangeException>(() => cde.Reset(-1)); // Failure Case: Reset didn't throw AORE when the count is zero Assert.Throws<ArgumentOutOfRangeException>(() => cde.Wait(-2)); // Failure Case: Wait(int) didn't throw AORE when the totalmilliseconds < -1 Assert.Throws<ArgumentOutOfRangeException>(() => cde.Wait(TimeSpan.FromDays(-1))); // Failure Case: FAILED. Wait(TimeSpan) didn't throw AORE when the totalmilliseconds < -1 Assert.Throws<ArgumentOutOfRangeException>(() => cde.Wait(TimeSpan.MaxValue)); // Failure Case: Wait(TimeSpan, CancellationToken) didn't throw AORE when the totalmilliseconds > int.max Assert.Throws<ArgumentOutOfRangeException>(() => cde.Wait(TimeSpan.FromDays(-1), new CancellationToken())); // Failure Case: Wait(TimeSpan) didn't throw AORE when the totalmilliseconds < -1 Assert.Throws<ArgumentOutOfRangeException>(() => cde.Wait(TimeSpan.MaxValue, new CancellationToken())); // Failure Case: Wait(TimeSpan, CancellationToken) didn't throw AORE when the totalmilliseconds > int.max cde.Dispose(); Assert.Throws<ObjectDisposedException>(() => cde.Wait()); // Failure Case: Wait() didn't throw ODE after Dispose }
public void TestPullReplicationWithUsername() { using(var remoteDb = _sg.CreateDatabase(TempDbName())) { remoteDb.DisableGuestAccess(); var docIdTimestamp = Convert.ToString((ulong)DateTime.UtcNow.TimeSinceEpoch().TotalMilliseconds); var doc1Id = string.Format("doc1-{0}", docIdTimestamp); var doc2Id = string.Format("doc2-{0}", docIdTimestamp); remoteDb.AddDocument(doc1Id, "attachment.png"); remoteDb.AddDocument(doc2Id, "attachment2.png"); var repl = database.CreatePullReplication(remoteDb.RemoteUri); repl.Authenticator = new BasicAuthenticator("jim", "borden"); var wait = new CountdownEvent(1); repl.Changed += (sender, e) => { if(wait.CurrentCount == 0) { return; } WriteDebug("New replication status {0}", e.Source.Status); if((e.Source.Status == ReplicationStatus.Idle || e.Source.Status == ReplicationStatus.Stopped) && e.Source.ChangesCount > 0 && e.Source.CompletedChangesCount == e.Source.ChangesCount) { wait.Signal(); } }; repl.Start(); Assert.IsTrue(wait.Wait(TimeSpan.FromSeconds(60)), "Pull replication timed out"); Assert.IsNotNull(database.GetExistingDocument(doc1Id), "Didn't get doc1 from puller"); Assert.IsNotNull(database.GetExistingDocument(doc2Id), "Didn't get doc2 from puller"); Assert.IsNull(repl.LastError); repl.Stop(); docIdTimestamp = Convert.ToString((ulong)DateTime.UtcNow.TimeSinceEpoch().TotalMilliseconds); doc1Id = string.Format("doc1-{0}", docIdTimestamp); doc2Id = string.Format("doc2-{0}", docIdTimestamp); remoteDb.AddDocument(doc1Id, "attachment.png"); remoteDb.AddDocument(doc2Id, "attachment2.png"); repl = database.CreatePullReplication(remoteDb.RemoteUri); repl.Changed += (sender, e) => { if(wait.CurrentCount == 0) { return; } WriteDebug("New replication status {0}", e.Source.Status); if((e.Source.Status == ReplicationStatus.Idle || e.Source.Status == ReplicationStatus.Stopped) && e.Source.CompletedChangesCount == e.Source.ChangesCount) { wait.Signal(); } }; repl.Authenticator = new BasicAuthenticator("jim", "bogus"); wait.Reset(1); repl.Start(); Assert.IsTrue(wait.Wait(TimeSpan.FromSeconds(60)), "Pull replication timed out"); Assert.IsNull(database.GetExistingDocument(doc1Id), "Got rogue doc1 from puller"); Assert.IsNull(database.GetExistingDocument(doc2Id), "Got rogue doc2 from puller"); repl.Stop(); } }
public void Reset_Invalid () { var ev = new CountdownEvent (1); try { ev.Reset (-1); Assert.Fail ("#1"); } catch (ArgumentOutOfRangeException) { Assert.AreEqual (1, ev.CurrentCount, "#1a"); } }
public void Reset_Zero () { var ev = new CountdownEvent (1); Assert.IsFalse (ev.IsSet, "#1"); ev.Reset (0); Assert.IsTrue (ev.IsSet, "#2"); Assert.IsTrue (ev.Wait (0), "#3"); Assert.AreEqual (0, ev.CurrentCount, "#4"); }
private static void TestSendNotifyCore( IPEndPoint endPoint, CountdownEvent arrivalLatch, IProducerConsumerCollection<string> arrivedIds, int count ) { using ( var clientTransportManager = new UdpClientTransportManager( new RpcClientConfiguration() { PreferIPv4 = true } ) ) using ( var connectTask = clientTransportManager.ConnectAsync( endPoint ) ) { if ( !connectTask.Wait( Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) ) { throw new TimeoutException(); } using ( var clientTransport = connectTask.Result ) { for ( int i = 0; i < count; i++ ) { if ( arrivalLatch != null ) { arrivalLatch.Reset(); } var args = Enumerable.Repeat( 0, arrivalLatch.InitialCount ).Select( _ => Guid.NewGuid().ToString() ).ToArray(); var exceptions = new ConcurrentBag<Exception>(); if ( !Task.Factory.ContinueWhenAll( args.Select( arg => Task.Factory.StartNew( () => { var requestContext = clientTransport.GetClientRequestContext(); requestContext.SetNotification( "Dummy", ( exception, completedSynchronously ) => { if ( exception != null ) { exceptions.Add( exception ); } arrivalLatch.Signal(); } ); requestContext.ArgumentsPacker.PackArrayHeader( 1 ); requestContext.ArgumentsPacker.Pack( arg ); return requestContext; } ) ).ToArray(), previouses => { var contexts = previouses.Select( previous => previous.Result ).ToArray(); foreach ( var context in contexts ) { clientTransport.Send( context ); } } ).ContinueWith( previous => { if ( previous.IsFaulted ) { throw previous.Exception; } // receive if ( !arrivalLatch.Wait( Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) ) { throw new TimeoutException( "Receive" ); } if ( exceptions.Any() ) { throw new AggregateException( exceptions ); } } ).Wait( Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) ) { throw new TimeoutException(); } } } } }
public void Execute() { // // CountdownEventクラスには、以下のメソッドが存在する。 // ・AddCountメソッド // ・Resetメソッド // AddCountメソッドは、CountdownEventの内部カウントをインクリメントする。 // Resetメソッドは、現在の内部カウントをリセットする。 // // どちらのメソッドも、Int32を引数に取るオーバーロードが用意されており // 指定した数を設定することも出来る。 // // 尚、AddCountメソッドを利用する際の注意点として // 既に内部カウントが0の状態でAddCountを実行すると例外が発生する。 // つまり、既にIsSetがTrue(シグナル状態)でAddCountするとエラーとなる。 // // // 内部カウントが0の状態で、AddCountしてみる. // using (var cde = new CountdownEvent(0)) { // 初期の状態を表示. PrintCurrentCountdownEvent(cde); try { // // 既にシグナル状態の場合に、さらにAddCountしようとすると例外が発生する. // cde.AddCount(); } catch (InvalidOperationException invalidEx) { Output.WriteLine("*** {0} ***", invalidEx.Message); } // 現在の状態を表示. PrintCurrentCountdownEvent(cde); } Output.WriteLine(""); using (var cde = new CountdownEvent(1)) { // 初期の状態を表示. PrintCurrentCountdownEvent(cde); // // 10個の別処理を実行する. // それぞれの内部処理にてランダムでSLEEPして、終了タイミングをバラバラに設定. // Output.WriteLine("別処理開始・・・"); for (var i = 0; i < 10; i++) { Task.Factory.StartNew(TaskProc, cde); } do { // 現在の状態を表示. PrintCurrentCountdownEvent(cde, "t"); Thread.Sleep(TimeSpan.FromSeconds(2)); } while (cde.CurrentCount != 1); Output.WriteLine("・・・別処理終了"); // // 待機. // Output.WriteLine("メインスレッドにて最後のカウントをデクリメント"); cde.Signal(); cde.Wait(); // 現在の状態を表示. PrintCurrentCountdownEvent(cde); Output.WriteLine(""); // // 内部カウントをリセット. // Output.WriteLine("内部カウントをリセット"); cde.Reset(); // 現在の状態を表示. PrintCurrentCountdownEvent(cde); // // 待機. // Output.WriteLine("メインスレッドにて最後のカウントをデクリメント"); cde.Signal(); cde.Wait(); // 現在の状態を表示. PrintCurrentCountdownEvent(cde); } }
public void start() { System.Net.ServicePointManager.ServerCertificateValidationCallback += (s, ce, ca, p) => true; // used for mono certificates issues boards = new List<string>(); boards.AddRange(config.readString("trelloBoardsToWatch", "comma,separated,list,of,board_ids").Split(',')); processingFinished = new CountdownEvent(boards.Count); processingStarted = new CountdownEvent(boards.Count); ApiKey = config.readString("trelloApiKey", "null"); Token = config.readString("trelloToken","null"); channel = config.readString("ircChannel", "#trello-notifications"); l_addMemberToCard = Program.localization.readString("addMemberToCard", @"[{0}] [https://trello.com/c/{1} {2} added {3} to ""{4}"" card!]"); l_commentCard = Program.localization.readString("commentCard", @"[{0}] [https://trello.com/c/{1} {2} commented on {3} card!]"); l_createCard = Program.localization.readString("createCard", @"[{0}] [https://trello.com/c/{1} {2} created ""{3}"" card in {4} list!]"); l_createList = Program.localization.readString("createList", @"[{0}] [https://trello.com/b/{1} {2} created ""{3}"" list]"); lantencyWatcher = new Stopwatch(); while(true) { if (!IrcClient.connected) continue; #if !DEBUG Console.Clear(); #endif ConsoleNotifications.writeNotify("Starting fetching data from trello!"); ConsoleNotifications.writeDebug("Current time before:" + DateSinceCheck); foreach (string board in boards) { ThreadPool.QueueUserWorkItem(new WaitCallback(checkForData), board); } processingStarted.Wait(); lantencyWatcher.Start(); ConsoleNotifications.writeNotify("All threads started!"); //getCurrentTime(); processingFinished.Wait(); ConsoleNotifications.writeNotify("All threads completed data fetch!"); lantencyWatcher.Stop(); ConsoleNotifications.writeDebug("Latency: " + lantencyWatcher.ElapsedMilliseconds + "ms"); lantencyWatcher.Reset(); processingFinished.Reset(boards.Count); processingStarted.Reset(boards.Count); Thread.Sleep(config.readInt("trelloFetchPeriod", 10000)); ConsoleNotifications.writeNotify("Current time after:" + DateSinceCheck); } }
public void TestEchoRequestContinuous() { try { const int count = 3; bool[] serverStatus = new bool[ count ]; string message = "Hello, world"; using ( var waitHandle = new CountdownEvent( count ) ) using ( var server = CallbackServer.Create( CreateConfiguration( host => new TcpServerTransportManager( host ), ( id, args ) => { try { Assert.That( args[ 0 ] == message ); Assert.That( args[ 1 ].IsTypeOf<Int64>().GetValueOrDefault() ); lock ( serverStatus ) { serverStatus[ id.Value ] = true; } return args; } finally { waitHandle.Signal(); } } ) ) ) { TestEchoRequestContinuousCore( serverStatus, waitHandle, count, message ); waitHandle.Reset(); Array.Clear( serverStatus, 0, serverStatus.Length ); // Again TestEchoRequestContinuousCore( serverStatus, waitHandle, count, message ); } } catch ( SocketException sockEx ) { Console.Error.WriteLine( "{0}({1}:0x{1:x8})", sockEx.SocketErrorCode, sockEx.ErrorCode ); Console.Error.WriteLine( sockEx ); throw; } }
void Loop(object o) { X = new CountdownEvent(256); long orderId = 0; Log.InfoFormat("IsServerGC:{0} ({1})", System.Runtime.GCSettings.IsServerGC, System.Runtime.GCSettings.LatencyMode); Log.InfoFormat("ProcessorCount: {0}", Environment.ProcessorCount); Log.InfoFormat("64bit: {0}", Environment.Is64BitProcess); Console.WriteLine("Press CTRL+C key to exit"); var start = Stopwatch.StartNew(); var interval = Stopwatch.StartNew(); Log.Warn("Sleeping for the bus to purge the queue. Loop requires the queue to be empty."); Thread.Sleep(5000); while (!shutdown) { X.Reset(); Console.Write("*"); Parallel.For(0, X.InitialCount, i => { var id = Interlocked.Increment(ref orderId); Bus.SendLocal(new SubmitOrder { OrderId = id.ToString(CultureInfo.InvariantCulture) }); }); try { X.Wait(stopLoop.Token); } catch (OperationCanceledException) { break; } var elapsed = interval.ElapsedTicks; interval.Restart(); var currentPerMessageTicks = elapsed / X.InitialCount; var currentThroughput = TicksPerTimeUnit / currentPerMessageTicks; var averagePerMessageTicks = start.ElapsedTicks / orderId; var averageThroughput = TicksPerTimeUnit / averagePerMessageTicks; Console.Title = string.Format("{0:N0}/{5} ~{1:N0}/{5} +{2:N0} @{3} p{4:N0}", currentThroughput, averageThroughput, orderId, start.Elapsed.Humanize(2), processedCount, TimeUnit ); //Thread.Sleep(15000); // Should result in downscale of polling sql server threads } }
public void Reset_FullInitialized () { var ev = new CountdownEvent (0); Assert.IsTrue (ev.IsSet, "#1"); Assert.AreEqual (0, ev.CurrentCount, "#2"); ev.Reset (4); Assert.IsFalse (ev.IsSet, "#3"); Assert.AreEqual (4, ev.CurrentCount, "#4"); Assert.IsFalse (ev.Wait (0), "#5"); }
// Validates init, set, reset state transitions. private static bool RunCountdownEventTest0_StateTrans(int initCount, int increms, bool takeAllAtOnce) { TestHarness.TestLog("* RunCountdownEventTest0_StateTrans(initCount={0}, increms={1}, takeAllAtOnce={2})", initCount, increms, takeAllAtOnce); CountdownEvent ev = new CountdownEvent(initCount); // Check initial count. if (ev.InitialCount != initCount) { TestHarness.TestLog(" > error: initial count wrong, saw {0} expected {1}", ev.InitialCount, initCount); return false; } // Increment (optionally). for (int i = 0; i < increms; i++) { ev.AddCount(); if (ev.CurrentCount != initCount + i + 1) { TestHarness.TestLog(" > error: after incrementing, count is wrong, saw {0}, expect {1}", ev.CurrentCount, initCount + i + 1); return false; } } // Decrement until it hits 0. if (takeAllAtOnce) { ev.Signal(initCount + increms); } else { for (int i = 0; i < initCount + increms; i++) { if (ev.IsSet) { TestHarness.TestLog(" > error: latch is set after {0} signals", i); return false; } ev.Signal(); } } // Check the status. if (!ev.IsSet) { TestHarness.TestLog(" > error: latch was not set after all signals received"); return false; } if (ev.CurrentCount != 0) { TestHarness.TestLog(" > error: latch count wasn't 0 after all signals received"); return false; } // Now reset the event and check its count. ev.Reset(); if (ev.CurrentCount != ev.InitialCount) { TestHarness.TestLog(" > error: latch count wasn't correctly reset"); return false; } return true; }
public void ResetTest () { var evt = new CountdownEvent (5); Assert.AreEqual (5, evt.CurrentCount); evt.Signal (); Assert.AreEqual (4, evt.CurrentCount); evt.Reset (); Assert.AreEqual (5, evt.CurrentCount); Assert.AreEqual (5, evt.InitialCount); evt.Signal (); evt.Signal (); Assert.AreEqual (3, evt.CurrentCount); Assert.AreEqual (5, evt.InitialCount); evt.Reset (10); Assert.AreEqual (10, evt.CurrentCount); Assert.AreEqual (10, evt.InitialCount); }
public void TestBatcherAddAfterCancel() { var evt = new CountdownEvent(1); var scheduler = new SingleTaskThreadpoolScheduler(); var batcher = new Batcher<int>(new TaskFactory(scheduler), 5, 500, (inbox) => { evt.Signal(); }); batcher.QueueObject(0); Assert.IsTrue(evt.Wait(1000), "Batcher didn't initially run"); evt.Reset(2); batcher.QueueObject(0); batcher.Clear(); batcher.QueueObject(0); Assert.False(evt.Wait(TimeSpan.FromSeconds(1.5)), "Batcher ran too many times"); Assert.True(evt.CurrentCount == 1, "Batcher never ran"); }
public void TestLiveQueryStop() { const int numDocs = 10; var doneSignal = new CountdownEvent(1); // Run a live query var view = database.GetView("vu"); view.SetMap((document, emit) => { emit(document["sequence"], null); }, "1"); var changedCalled = false; var query = view.CreateQuery().ToLiveQuery(); query.Changed += (object sender, QueryChangeEventArgs e) => { changedCalled = true; Assert.IsNull(e.Error); if (e.Rows.Count == numDocs) { doneSignal.Signal(); } }; // create the docs that will cause the above change listener to decrement countdown latch CreateDocumentsAsync(database, numDocs); query.Start(); var success = doneSignal.Wait(TimeSpan.FromSeconds(5)); Assert.IsTrue(success); query.Stop(); CreateDocumentsAsync(database, numDocs); changedCalled = false; doneSignal.Reset(); doneSignal.Wait(TimeSpan.FromSeconds(3)); Assert.IsTrue(!changedCalled); }
private void UT_SubscribeToEvents(Context context, IList<KeyEvent> expectedEvents, KeyEventSubscriptionType? eventSubscriptionType = null, string key = null, KeyEvent? eventType = null) { ConcurrentQueue<KeyValuePair<string, KeyEvent>> result = new ConcurrentQueue<KeyValuePair<string, KeyEvent>>(); CountdownEvent handle = new CountdownEvent(expectedEvents.Count); Action<string, KeyEvent> action = (k, e) => { result.Enqueue(new KeyValuePair<string, KeyEvent>(k, e)); handle.Signal(); }; Action unsubscribeAction = () => { }; if (key == null && !eventType.HasValue && eventSubscriptionType.HasValue) { context.KeyEvents.Subscribe(eventSubscriptionType.Value, action); unsubscribeAction = () => context.KeyEvents.Unsubscribe(eventSubscriptionType.Value); } else if (key != null) { context.KeyEvents.Subscribe(key, action); unsubscribeAction = () => context.KeyEvents.Unsubscribe(key); } else if (eventType.HasValue) { context.KeyEvents.Subscribe(eventType.Value, action); unsubscribeAction = () => context.KeyEvents.Unsubscribe(eventType.Value); } var objectKey = key ?? Guid.NewGuid().ToString(); context.Cache.SetObject(objectKey, new { Name = "alex", Created = DateTime.UtcNow }); context.Cache.Remove(objectKey); Assert.IsTrue(handle.Wait(5000)); Assert.AreEqual(expectedEvents.Count, result.Count); foreach (var expectedEvent in expectedEvents) { KeyValuePair<string, KeyEvent> e; Assert.IsTrue(result.TryDequeue(out e)); Assert.AreEqual(expectedEvent, e.Value); Assert.AreEqual(objectKey, e.Key); } //Now test Unsubscribe. No more events should be received in queue and handle will timeout. handle.Reset(1); unsubscribeAction(); context.Cache.SetObject(objectKey, new { Name = "alex", Created = DateTime.UtcNow }, TimeSpan.FromMilliseconds(500)); Assert.IsFalse(handle.Wait(1000)); Assert.IsTrue(result.IsEmpty); context.KeyEvents.Unsubscribe(KeyEventSubscriptionType.All); }
public void TestPullReplicationWithUsername() { PerformWithoutGuestAccess(() => { var docIdTimestamp = Convert.ToString(Runtime.CurrentTimeMillis()); var doc1Id = string.Format("doc1-{0}", docIdTimestamp); var doc2Id = string.Format("doc2-{0}", docIdTimestamp); Log.D(Tag, "Adding " + doc1Id + " directly to sync gateway"); AddDocWithId(doc1Id, "attachment.png"); Log.D(Tag, "Adding " + doc2Id + " directly to sync gateway"); AddDocWithId(doc2Id, "attachment2.png"); var remote = GetReplicationURL(); var repl = database.CreatePullReplication(remote); repl.Authenticator = new BasicAuthenticator("jim", "borden"); repl.Continuous = true; var wait = new CountdownEvent(1); repl.Changed += (sender, e) => { if(wait.CurrentCount == 0) { return; } Log.D("ReplicationTest", "New replication status {0}", e.Source.Status); if((e.Source.Status == ReplicationStatus.Idle || e.Source.Status == ReplicationStatus.Stopped) && e.Source.ChangesCount > 0 && e.Source.CompletedChangesCount == e.Source.ChangesCount) { wait.Signal(); } }; repl.Start(); Assert.IsTrue(wait.Wait(TimeSpan.FromSeconds(60)), "Pull replication timed out"); Assert.IsNotNull(database.GetExistingDocument(doc1Id), "Didn't get doc1 from puller"); Assert.IsNotNull(database.GetExistingDocument(doc2Id), "Didn't get doc2 from puller"); Assert.IsNull(repl.LastError); repl.Stop(); docIdTimestamp = Convert.ToString(Runtime.CurrentTimeMillis()); doc1Id = string.Format("doc1-{0}", docIdTimestamp); doc2Id = string.Format("doc2-{0}", docIdTimestamp); Log.D(Tag, "Adding " + doc1Id + " directly to sync gateway"); AddDocWithId(doc1Id, "attachment.png"); Log.D(Tag, "Adding " + doc2Id + " directly to sync gateway"); AddDocWithId(doc2Id, "attachment2.png"); repl.Authenticator = new BasicAuthenticator("jim", "bogus"); wait.Reset(1); repl.Start(); Assert.IsTrue(wait.Wait(TimeSpan.FromSeconds(60)), "Pull replication timed out"); Assert.IsNull(database.GetExistingDocument(doc1Id), "Got rogue doc1 from puller"); Assert.IsNull(database.GetExistingDocument(doc2Id), "Got rogue doc2 from puller"); repl.Stop(); Thread.Sleep(2000); }); }
protected void ExecuteLevel(ConcurrentQueue<Computation> computationsOfLevel) { //int reqs = 0; //int prov = 0; //foreach (var item in computationsOfLevel) //{ // var pc = item.Context as ParallelComputationContext; // reqs += pc.transformationRequirements; // prov += pc.dependentContexts; //} //Console.WriteLine("{0} computations provided, {1} needed", prov, reqs); using (var countEvent = new CountdownEvent(1)) { while (true) { Computation item; if (computationsOfLevel.TryDequeue(out item)) { var cc = item.Context as ParallelComputationContext; if (cc != null) { countEvent.AddCount(); cc.RunTransform(() => { item.Transform(); countEvent.Signal(); }); } else { countEvent.Signal(); countEvent.Wait(); item.Transform(); countEvent.Reset(); } OnComputationCompleted(new ComputationEventArgs(item)); } else { break; } } countEvent.Signal(); //Thread.Sleep(2000); //if (countEvent.CurrentCount > 0) Debugger.Break(); countEvent.Wait(); } }
private static void TestSendNotifyCore( IPEndPoint endPoint, CountdownEvent arrivalLatch, IProducerConsumerCollection<string> arrivedIds, int count ) { using ( var udpClient = new UdpClient( AddressFamily.InterNetwork ) ) using ( var concurrencyLatch = new CountdownEvent( arrivalLatch.InitialCount ) ) { udpClient.Connect( endPoint ); for ( int i = 0; i < count; i++ ) { if ( concurrencyLatch != null ) { concurrencyLatch.Reset(); } arrivalLatch.Reset(); // Clear ids. string dummy; while ( arrivedIds.TryTake( out dummy ) ) { } var ids = Enumerable.Repeat( 0, concurrencyLatch == null ? 1 : concurrencyLatch.InitialCount ).Select( _ => Guid.NewGuid().ToString() ).ToArray(); if ( !Task.WaitAll( ids.Select( id => Task.Factory.StartNew( _ => { using ( var buffer = new MemoryStream() ) { using ( var packer = Packer.Create( buffer, false ) ) { PackRequest( packer, id ); } buffer.Position = 0; if ( concurrencyLatch != null ) { concurrencyLatch.Signal(); if ( !concurrencyLatch.Wait( Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) ) { throw new TimeoutException(); } } // send udpClient.Send( buffer.ToArray(), ( int )buffer.Length ); } }, id ) ).ToArray(), Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) ) { throw new TimeoutException(); } // wait if ( !arrivalLatch.Wait( Debugger.IsAttached ? Timeout.Infinite : TimeoutMilliseconds ) ) { throw new TimeoutException(); } Assert.That( arrivedIds, Is.EquivalentTo( ids ) ); } } }
public async Task RunsTasksConcurrently(int concurrentTasks) { // Control + 3 experiments var totalTasks = 1 + 3; // Expected number of batches var expectedBatches = Math.Ceiling(1D * totalTasks / concurrentTasks); // Use CountdownEvents to ensure tasks don't finish before all tasks in that batch have started var startedSignal = new CountdownEvent(concurrentTasks); var finishedSignal = new CountdownEvent(concurrentTasks); // Batch counter int batch = 1; // Our test task var task = new Func<Task<KeyValuePair<int, int>>>(() => { return Task.Run(() => { // Signal that we have started var last = startedSignal.Signal(); var myBatch = batch; // Wait till all tasks for this batch have started startedSignal.Wait(); // Signal we have finished finishedSignal.Signal(); // Last task to start needs to reset the events if (last) { // Wait for all tasks in the batch to have finished finishedSignal.Wait(); // Reset the countdown events startedSignal.Reset(); finishedSignal.Reset(); batch++; } // Return threadId return new KeyValuePair<int, int>(myBatch, Thread.CurrentThread.ManagedThreadId); }); }); // Run the experiment string experimentName = nameof(RunsTasksConcurrently) + concurrentTasks; await Scientist.ScienceAsync<KeyValuePair<int, int>>(experimentName, concurrentTasks, experiment => { // Add our control and experiments experiment.Use(task); for (int idx = 2; idx <= totalTasks; idx++) { experiment.Try($"experiment{idx}", task); } }); // Get the test result var result = TestHelper.Results<KeyValuePair<int, int>>(experimentName).First(); // Consolidate the returned values from the tasks var results = result.Observations.Select(x => x.Value); // Assert correct number of batches Assert.Equal(expectedBatches, results.Select(x => x.Key).Distinct().Count()); // Now check each batch for (int batchNo = 1; batchNo <= expectedBatches; batchNo++) { // Get the threadIds used by each task in the batch var batchThreadIds = results.Where(x => x.Key == batchNo).Select(x => x.Value); // Assert expected number of concurrent tasks in batch Assert.Equal(concurrentTasks, batchThreadIds.Count()); // Assert unique threadIds in batch Assert.Equal(batchThreadIds.Count(), batchThreadIds.Distinct().Count()); } }
public void DoubleWaitTest () { ParallelTestHelper.Repeat (delegate { Console.WriteLine ("run"); var evt = new ManualResetEventSlim (); var t = Task.Factory.StartNew (() => evt.Wait (2000)); var cntd = new CountdownEvent (2); bool r1 = false, r2 = false; ThreadPool.QueueUserWorkItem (delegate { cntd.Signal (); r1 = t.Wait (1000); Console.WriteLine ("out 1 {0}", r1); cntd.Signal (); }); ThreadPool.QueueUserWorkItem (delegate { cntd.Signal (); r2 = t.Wait (1000); Console.WriteLine ("out 2 {0}", r2); cntd.Signal (); }); cntd.Wait (2000); cntd.Reset (); evt.Set (); cntd.Wait (2000); Assert.IsTrue (r1); Assert.IsTrue (r2); }, 5); }
static async Task Main(string[] args) { // Initialize a queue and a CountdownEvent ConcurrentQueue <int> queue = new ConcurrentQueue <int>(Enumerable.Range(0, 10000)); System.Threading.CountdownEvent cde = new System.Threading.CountdownEvent(10000); // initial count = 10000 // This is the logic for all queue consumers Action consumer = () => { int local; // decrement CDE count once for each element consumed from queue while (queue.TryDequeue(out local)) { cde.Signal(); } }; // Now empty the queue with a couple of asynchronous tasks Task t1 = Task.Factory.StartNew(consumer); Task t2 = Task.Factory.StartNew(consumer); // And wait for queue to empty by waiting on cde cde.Wait(); // will return when cde count reaches 0 Console.WriteLine("Done emptying queue. InitialCount={0}, CurrentCount={1}, IsSet={2}", cde.InitialCount, cde.CurrentCount, cde.IsSet); // Proper form is to wait for the tasks to complete, even if you that their work // is done already. await Task.WhenAll(t1, t2); // Resetting will cause the CountdownEvent to un-set, and resets InitialCount/CurrentCount // to the specified value cde.Reset(10); // AddCount will affect the CurrentCount, but not the InitialCount cde.AddCount(2); Console.WriteLine("After Reset(10), AddCount(2): InitialCount={0}, CurrentCount={1}, IsSet={2}", cde.InitialCount, cde.CurrentCount, cde.IsSet); // Now try waiting with cancellation CancellationTokenSource cts = new CancellationTokenSource(); cts.Cancel(); // cancels the CancellationTokenSource try { cde.Wait(cts.Token); } catch (OperationCanceledException) { Console.WriteLine("cde.Wait(preCanceledToken) threw OCE, as expected"); } finally { cts.Dispose(); } // It's good to release a CountdownEvent when you're done with it. cde.Dispose(); Console.ReadLine(); }