public void TestOneProducer() { var processed = 0; var buffer = new ByteBufferAsyncProcessor("TestAsyncProcessor", 10, delegate(byte[] data, int offset, int len, ref long seqN) { Assert.Greater(len, 0); for (int i = 0; i < len - 1; i++) { Assert.AreEqual(data[offset + i + 1], (byte)(data[offset + i] + 1)); } processed += len; }); byte b = 0; int l = 0; var toProcess = 0; for (int i = 0; i < 300; i++) { toProcess += l; byte[] p = new byte[l++]; for (int j = 0; j < p.Length; j++) { p[j] = b++; } buffer.Put(p); if (i == 20) { buffer.Start(); //testing delayed start } if (i > 0 && i % 50 == 0) { SpinWait.SpinUntil(() => buffer.AllDataProcessed); //give it to process } } SpinWait.SpinUntil(() => buffer.AllDataProcessed); //give it to process Assert.AreEqual(toProcess, processed); Assert.True(buffer.Stop(1000)); }
//can't take socket from mySoсketProvider: it could be not set yet protected void AddTerminationActions([NotNull] Thread receiverThread) { // ReSharper disable once ImpureMethodCallOnReadonlyValueField myLifetime.OnTermination(() => { Log.Verbose("{0}: start termination of lifetime", Id); var sendBufferStopped = SendBuffer.Stop(5000); Log.Verbose("{0}: send buffer stopped, success: {1}", Id, sendBufferStopped); lock (Lock) { Log.Verbose("{0}: closing socket because of lifetime", Id); CloseSocket(Socket); Monitor.PulseAll(Lock); } Log.Verbose("{0}: waiting for receiver thread", Id); receiverThread.Join(TimeoutMs + 100); Log.Verbose("{0}: termination finished", Id); } ); }