Ejemplo n.º 1
0
        public void TestClean()
        {
            int x      = 0;
            var buffer = new ByteBufferAsyncProcessor("TestAsyncProcessor", 1, delegate(byte[] data, int offset, int len, ref long seqN) { x += data[offset]; });

            buffer.Put(new byte[] { 1 });
            buffer.Put(new byte[] { 2 });
            buffer.Put(new byte[] { 3 });

            Assert.False(buffer.AllDataProcessed); //not started

            buffer.Clear();
            Assert.True(buffer.AllDataProcessed);

            buffer.Start();
            buffer.Put(new byte[] { 1, 2, 3 });

            SpinWait.SpinUntil(() => buffer.AllDataProcessed);
            Assert.AreEqual(6, x);
        }
Ejemplo n.º 2
0
        public unsafe void StressTestWithAck()
        {
//      LogLog.SeverityFilter = LoggingLevel.VERBOSE;
//      LogLog.RecordsChanged += record => { Console.WriteLine(record.Format(true)); };

            long prev = 0;
            ByteBufferAsyncProcessor buffer = null;

            buffer = new ByteBufferAsyncProcessor("TestAsyncProcessor", 8,
                                                  delegate(byte[] data, int offset, int len, ref long seqN)
            {
                long l = 0;
                Log.Root.Catch(() =>
                {
                    fixed(byte *b = data)
                    {
                        l = UnsafeReader.CreateReader(b, 8).ReadLong();
                        Assert.True(l > prev);
                        prev = l;
                        if (l % 1 == 0)
                        {
                            Ack(l);
                        }
                    }
                });
                seqN = l;
            });
            buffer.ShrinkIntervalMs = 10;
            buffer.Start();

            void Ack(long seqn)
            {
                buffer?.Acknowledge(seqn);
            }

            var start = Environment.TickCount;

            bool Until() => Environment.TickCount - start < 1000;

            long next  = 0;
            var  tasks = new List <Task>();

            for (int i = 0; i < 4; i++)
            {
                tasks.Add(Task.Run(() =>
                {
                    var rnd = new Random();

                    while (Until())
                    {
                        lock (tasks)
                        {
                            using (var cookie = UnsafeWriter.NewThreadLocalWriter())
                            {
                                cookie.Writer.Write(++next);
                                buffer.Put(cookie);
                            }
                        }
                        if (rnd.Next(1000) < 1)
                        {
                            Thread.Sleep(1);
                        }
                        if (rnd.Next(1000) < 5)
                        {
                            buffer.Clear();
                        }
                    }
                }));
            }

            Task.WaitAll(tasks.ToArray());
//      Console.WriteLine(next);
//      Console.WriteLine(buffer.ChunkCount);
        }