예제 #1
0
        /// <summary>
        /// the out T can be null and return will be false
        /// if the queue is empty, then this function will be blocked until new message arrive
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public bool TryDequeue(out T msg)
        {
            if (IsQueueEmpty)
            {
                WaitForMsgEvent.Wait(cts.Token);
                WaitForMsgEvent.Reset();
            }

            bool ret = false;

            if (!bWritingQueueA)
            {
                ret = QueueA.TryDequeue(out msg);
            }
            else
            {
                ret = QueueB.TryDequeue(out msg);
            }

            if (!ret && ((!bWritingQueueA && QueueA.Count == 0) || (bWritingQueueA && QueueB.Count == 0)))
            {
                if (bWritingQueueA)
                {
                    bWritingQueueA = false;
                }
                else
                {
                    bWritingQueueA = true;
                }
            }

            return(ret);
        }
예제 #2
0
        public void Enqueue(T obj)
        {
            if (bWritingQueueA)
            {
                QueueA.Enqueue(obj);
            }
            else
            {
                QueueB.Enqueue(obj);
            }

            if (WaitForMsgEvent.SpinCount > 0)
            {
                WaitForMsgEvent.Set();
            }
        }
예제 #3
0
        public void Do(Db db)
        {
            bool dispose = false; if (db == null)

            {
                db = new Db("DATA"); dispose = true;
            }

#if (SERVER)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f)); };
#endif
#if (SOCKETS)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f, db)); };
#endif
            db.Queue <QueueA>().GetAllFromQueue();

            var a = new QueueA()
            {
                SomeString = "test123",
                SomeArray  = new List <int>()
                {
                    1, 2, 3
                }
            };

            db.Queue <QueueA>().PutToQueue(a);
            var res = db.Queue <QueueA>().GetAllFromQueue();

            if (res.Count() != 1 || res.Single().SomeString != "test123" || res.Single().SomeArray.Count() != 3 || res.Single().SomeArray[0] != 1)
            {
                throw new Exception("Assert failure");
            }

#if (SERVER || SOCKETS)
            if (dispose)
            {
                Logic.Dispose();
            }
#else
            if (dispose)
            {
                db.Dispose();
            }
#endif
        }
예제 #4
0
        public void Do(Db db)
        {
            bool dispose = false; if (db == null)

            {
                db = new Db("DATA"); dispose = true;
            }

#if (SERVER)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f)); };
#endif
#if (SOCKETS)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f, db)); };
#endif

            db.Queue <QueueA>().GetAllFromQueue();

            int    total      = 30000;
            int    read       = 0;
            object _read_lock = new object();
            Parallel.ForEach(Enumerable.Range(0, 15).ToList(), f => {
                Task.Run(() =>
                {
                    while (read < total)
                    {
                        try
                        {
                            var res = db.Queue <QueueA>().GetAllFromQueue();
                            if (res.Any() && res.First().SomeArray.Count() != 3)
                            {
                                throw new Exception("not match");
                            }
                            lock (_read_lock)
                            {
                                read += res.Count();
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("A " + ex.Message);
                        }
                    }
                });
            });
            Parallel.ForEach(Enumerable.Range(0, total).ToList(), new ParallelOptions()
            {
                MaxDegreeOfParallelism = 5
            }, f =>
            {
                try
                {
                    var a = new QueueA()
                    {
                        SomeString = "test123",
                        SomeArray  = new List <int>()
                        {
                            1, 2, 3
                        }
                    };

                    db.Queue <QueueA>().PutToQueue(a);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            });


            Thread.Sleep(5000);


            if (read != total)
            {
                throw new Exception("Assert failure");
            }

#if (SERVER || SOCKETS)
            if (dispose)
            {
                Logic.Dispose();
            }
#else
            if (dispose)
            {
                db.Dispose();
            }
#endif
        }
예제 #5
0
        public void Do(Db db)
        {
            bool dispose = false; if (db == null)

            {
                db = new Db("DATA"); dispose = true;
            }

#if (SERVER)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f)); };
#endif
#if (SOCKETS)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f, db)); };
#endif
            int    total   = 500;
            int    bad     = 0;
            int    others  = 0;
            int    good    = 0;
            int    started = 0;
            object _lock   = new object();

            db.Queue <QueueA>().GetAllFromQueue();

            Parallel.ForEach(Enumerable.Range(0, total).ToList(), new ParallelOptions()
            {
                MaxDegreeOfParallelism = 40
            }, f =>
            {
                try
                {
                    lock (_lock)
                    {
                        started++;
                    }
                    var a = new QueueA()
                    {
                        SomeString = "test123",
                        SomeArray  = new List <int>()
                        {
                            1, 2, 3
                        }
                    };

                    db.Queue <QueueA>().PutToQueue(a);
                    lock (_lock)
                    {
                        good++;
                    }
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains("more readers"))
                    {
                        lock (_lock)
                        {
                            bad++;
                        }
                    }
                    else
                    {
                        lock (_lock)
                        {
                            others++;
                        }
                    }
                }
            });


            if (bad != 201 || others != 0 || started != total || good != 299)
            {
                throw new Exception("Assert failure");
            }

#if (SERVER || SOCKETS)
            if (dispose)
            {
                Logic.Dispose();
            }
#else
            if (dispose)
            {
                db.Dispose();
            }
#endif
        }