public void Add()
        {
            var queue = new StringQueue();

            queue.Enqueue("a");
            Assert.Contains("a", queue);
        }
Exemplo n.º 2
0
        public ViewerCountLogger(WinterBot bot)
        {
            m_queue = new StringQueue(bot, "viewers");

            bot.ViewerCountChanged += bot_ViewerCountChanged;
            bot.StreamOnline += bot_StreamOnline;
            bot.StreamOffline += bot_StreamOffline;
        }
Exemplo n.º 3
0
        public void SetUp()
        {
            _queue = new StringQueue(16);
            Assert.AreEqual(0, _queue.Count);

            _queue.Enqueue("one");
            _queue.Enqueue("two");
            _queue.Enqueue("three");
            Assert.AreEqual(3, _queue.Count);
        }
        private StringQueue CreateTestQueue()
        {
            var queue = new StringQueue();

            for (var i = 0; i < 25; i++)
            {
                queue.Enqueue(new string((char)('a' + i), 1));
            }

            return(queue);
        }
Exemplo n.º 5
0
        public void CreateStringQueue_FromStringList_ShouldCreateAThreadSafeConcurrentQueueWithSameElements(List <string> ls)
        {
            // Compare just the total count and the first item.

            var sq = StringQueue.CreateStringQueue(ls);

            Assert.AreEqual(sq.Count(), ls.Count);

            var item = sq.Dequeue();

            Assert.AreEqual(item, ls.FirstOrDefault());
        }
Exemplo n.º 6
0
        public void Enqueue_NullItem_ShouldReturnTheQueueUnchangedAndNotTriggersEventItemEnqueued(List <string> ls)
        {
            var sq = StringQueue.CreateStringQueue(ls);

            var wait1 = new AutoResetEvent(false);

            sq.ItemEnqueued += (sender, eventArgs) => wait1.Set();

            sq.Enqueue(null);

            Assert.IsFalse(wait1.WaitOne(TimeSpan.FromSeconds(5)));
            Assert.AreEqual(sq.Count(), ls.Count);
        }
Exemplo n.º 7
0
        public void Enqueue_CorrectStringItem_ShouldAddItemToTheQueueAndTriggersEventItemEnqueued(List <string> ls)
        {
            var sq = StringQueue.CreateStringQueue(ls);

            var wait1 = new AutoResetEvent(false);

            sq.ItemEnqueued += (sender, eventArgs) => wait1.Set();

            sq.Enqueue("test");

            Assert.IsTrue(wait1.WaitOne(TimeSpan.FromSeconds(5)));
            Assert.AreEqual(sq.Count(), ls.Count + 1);
        }
 public void StartLogger(string fileName)
 {
     try
     {
         LogQueue              = new StringQueue();
         LogQueue.OnException += LogQueue_OnException;
         LogQueue.OnDequeue   += LogQueue_OnDequeue;
     }
     catch (Exception ex)
     {
         TBYException tex = new TBYException(MethodInfo.GetCurrentMethod().GetFullName(), "Logger açılırken hata oluştu", ex);
         QuantConnect.Logging.Log.Error(tex.ToString());
     }
 }
Exemplo n.º 9
0
        public void Constructor()
        {
            StringQueue queue = new StringQueue(567);

            queue.Enqueue("foo"); queue.Enqueue("bar");
            Assert.AreEqual(2, queue.Count);

            CloneableType[] array = new CloneableType[4] {
                "a", "b", "c", "d"
            };
            queue = new StringQueue(array);
            Assert.AreEqual(4, queue.Count);
            Assert.IsTrue(queue.Equals(array));
        }
Exemplo n.º 10
0
        public void Equals()
        {
            StringQueue queue = new StringQueue();

            queue.Enqueue("one");
            queue.Enqueue("two");
            queue.Enqueue("three");
            Assert.IsTrue(_queue.Equals(queue));

            queue.Enqueue("one");
            Assert.IsFalse(_queue.Equals(queue));

            queue.Dequeue();
            Assert.IsFalse(_queue.Equals(queue));
        }
Exemplo n.º 11
0
        public void Clone()
        {
            StringQueue clone = (StringQueue)_queue.Clone();

            Assert.AreEqual(_queue.Count, clone.Count);

            int count = _queue.Count;

            for (int i = 0; i < count; i++)
            {
                CloneableType x = _queue.Dequeue(), y = clone.Dequeue();
                Assert.AreEqual(x, y);
                Assert.AreSame(x, y);
            }
        }
Exemplo n.º 12
0
        public void Copy()
        {
            StringQueue copy = _queue.Copy();

            Assert.AreEqual(_queue.Count, copy.Count);

            int count = _queue.Count;

            for (int i = 0; i < count; i++)
            {
                CloneableType x = _queue.Dequeue(), y = copy.Dequeue();
                Assert.AreEqual(x, y);
                Assert.AreNotSame(x, y);
            }
        }
Exemplo n.º 13
0
        public bool TrySetFor(MessageHead head)
        {
            ThrowIfEnded();
            BodyType?bodyType = BodyType.TryDetectFor(head);

            if (bodyType == null)
            {
                return(false);
            }

            CompressionType  compression = bodyType.Value.CompressionType;
            TransferEncoding transfer    = bodyType.Value.TransferEncoding;
            int contentLength            = bodyType.Value.ContentLength;

            if (IsSet)
            {
                Reset();
            }
            TransferEncoding = transfer;
            if (transfer == TransferEncoding.Raw)
            {
                ContentLength = contentLength;
                State         = BodyParserState.RawRead;
            }
            else if (transfer == TransferEncoding.Chunked)
            {
                State       = BodyParserState.Chunked_Length;
                StringQueue = new StringQueue();
            }
            if (transfer != TransferEncoding.None)
            {
                CompressionType = compression;
                switch (compression)
                {
                case CompressionType.None: ContentTransform = new RawUnifiedDuplex(); break;

                case CompressionType.Gzip: ContentTransform = new GzipDecompressor(); break;

                case CompressionType.Deflate: ContentTransform = new DeflateDecompressor(); break;

                case CompressionType.Compress: return(false);

                default: return(false);
                }
            }
            IsSet = true;
            return(true);
        }
Exemplo n.º 14
0
        public void Consume_WithVariousConsumers_ShouldConsumeTheQueueCorrectly(List <string> ls)
        {
            var sq = StringQueue.CreateStringQueue(ls);

            // Necesitamos usar Task.Factory.StartNew para probar nuestro método Consume
            // durante un cierto tiempo (5 segundos). Este wrapper por sí mismo no garantiza un nuevo
            // hilo, pero nuestro método se encarga de crearlo, tal como se indica en los requerimientos.
            // La diferencia principal es que Task.Factory usa Thread-pool, que es manejado por el
            // sistema, se supone que de forma más eficiente. También permite especificar un intervalo
            // de tiempo en el que debe correr el proceso, como en este caso.
            // Probar procesos multi-threading siempre es un poco
            // truculento, pero es mucho mejor que no hacer nada.

            var t1 = Task.Factory.StartNew(() => Consumer.CreateConsumerNewThread(sq, "Consumer 1").Consume()).Wait(5000);
            var t2 = Task.Factory.StartNew(() => Consumer.CreateConsumerNewThread(sq, "Consumer 2").Consume()).Wait(5000);
            var t3 = Task.Factory.StartNew(() => Consumer.CreateConsumerNewThread(sq, "Consumer 3").Consume()).Wait(5000);
            var t4 = Task.Factory.StartNew(() => Consumer.CreateConsumerNewThread(sq, "Consumer 4").Consume()).Wait(5000);

            Assert.AreEqual(sq.Count(), 0);
        }
Exemplo n.º 15
0
        public void Dequeue_Item_ShouldUpdateTheQueueAndTriggersEventItemDequeued(List <string> ls)
        {
            var sq = StringQueue.CreateStringQueue(ls);

            var wait1 = new AutoResetEvent(false);

            sq.ItemDequeued += (sender, eventArgs) => wait1.Set();

            var item = sq.Dequeue();

            if (item != null)
            {
                Assert.IsTrue(wait1.WaitOne(TimeSpan.FromSeconds(5)));
            }
            else
            {
                Assert.IsFalse(wait1.WaitOne(TimeSpan.FromSeconds(5)));
            }

            Assert.AreEqual(item, ls.FirstOrDefault());
        }
Exemplo n.º 16
0
    IEnumerator Reading(StringQueue sq)
    {
        while (_On)
        {
            yield return(null);

            string str = "";
            if (!sq.IsEmpty())
            {
                str = sq.getData();

                if (str != "")
                {
                    Debug.Log(str);
                    if (str[0] == '{')
                    {
                        SendDataFormat data = JsonUtility.FromJson <SendDataFormat>(str);
                        if (data.type == "enemyid")
                        {
                            _EnemyId    = data.data;
                            _getEnemyId = true;
                        }
                    }
                    else if (str[0] == 'o' && str[1] == 'm')
                    {
                        string           data = str.Replace("om", "");
                        OmSendDataFormat dd   = JsonUtility.FromJson <OmSendDataFormat>(data);
                        _GameMng._OmGameMng.StoneSet_GetData(dd);
                    }
                    else if (str[0] == 'o' && str[1] == 'c')
                    {
                        string data = str.Replace("oc", "");
                        _GameMng.GameChange_GetData("toom");
                        JsonData item = JsonMapper.ToObject(data);
                        _GameMng._OmGameMng.SetRePosition_OmGame(item);
                    }
                    else if (str[0] == 's' && str[1] == 'h')
                    {
                        _GameMng._AlGameMng.TurnOffEnemySlot(str[2] - '0');
                    }
                    else if (str[0] == 'a' && str[1] == 'l')
                    {
                        string   data = str.Replace("al", "");
                        JsonData item = JsonMapper.ToObject(data);
                        _GameMng._AlGameMng.SetStones_GetData(item);
                    }
                    else if (str[0] == 'a' && str[1] == 's')
                    {
                        string   data = str.Replace("as", "");
                        JsonData item = JsonMapper.ToObject(data);
                        _GameMng._AlGameMng.SetStones_GetData(item);
                    }
                    else if (str[0] == 'c' && str[1] == 'c')
                    {
                        _GameMng.EnemyChangeStoneCountSet(str[2] - '0');
                    }
                    else if (str == "stonestoped")
                    {
                        _GameMng._AlGameMng.TurnChange_GetData();
                    }
                    else if (str == "black" || str == "white")
                    {
                        Matched_GameStart(str);
                    }
                    else if (str == "youlose")
                    {
                        _GameMng._OmGameMng.LoseAfterEffectCheck();
                    }
                    else if (str == "draw")
                    {
                        _GameMng._OmGameMng.LoseAfterEffectCheck();
                    }
                    else if (str == "getdataoknormal")
                    {
                        _GameMng.EnemyReadyPopupSet_Normal(false);
                    }
                    else if (str == "getdataokchange")
                    {
                        _GameMng.EnemyReadyPopupSet_Change(false);
                    }
                    else if (str == "changetoal")
                    {
                        _GameMng.GameChange_GetData("toal");
                    }
                    else if (str == "youwintime")
                    {
                        _SceneMng.OpenWinnerPopup_Time();
                    }
                    else if (str == "youlosetime")
                    {
                        _SceneMng.OpenLoserPopup_Time();
                    }
                    else if (str == "youwindisconnect")
                    {
                        WinThisGame_Disconnect();
                    }
                }
                else
                {
                    SendDataToServer("enemyout");
                    Debug.Log("empty");
                }
            }
        }
    }
Exemplo n.º 17
0
        private int ProcessData(byte[] data, bool writeExcess)
        {
            ThrowIfEnded();
            if (!IsSet)
            {
                throw new InvalidOperationException("Not set");
            }
            int i = 0, len; char c; byte[] sliced;

            for (; i < data.Length;)
            {
                switch (State)
                {
                case BodyParserState.RawRead:
                    len    = ContentLength == -1 ? data.Length - i : Math.Min(data.Length - i, ContentLength - CurrentReadBytes);
                    sliced = new byte[len];
                    Buffer.BlockCopy(data, i, sliced, 0, len);
                    i += len;
                    CurrentReadBytes += len;
                    ContentTransform.Write(data);
                    if (ContentTransform.Buffered > 0)
                    {
                        Readable.Write(ContentTransform.Read());
                    }
                    if (CurrentReadBytes == ContentLength)
                    {
                        State = BodyParserState.Dormant;
                        OnEnd?.Invoke();
                        break;
                    }
                    break;

                case BodyParserState.Chunked_Length:
                    c = (char)data[i++];
                    if (c != CR)
                    {
                        StringQueue.Append(c);
                    }
                    else
                    {
                        if (!int.TryParse(StringQueue.Next(), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out int result))
                        {
                            End(); return(-1);
                        }
                        chunkLen = result;
                        State    = BodyParserState.Chunked_LenLf;
                    }
                    break;

                case BodyParserState.Chunked_LenLf:
                    c = (char)data[i++];
                    if (c != LF)
                    {
                        End(); return(-1);
                    }
                    if (chunkLen == 0)
                    {
                        State = BodyParserState.Chunked_Trailer;
                    }
                    else
                    {
                        State = BodyParserState.Chunked_ChunkData;
                    }
                    break;

                case BodyParserState.Chunked_ChunkData:
                    len    = Math.Min(data.Length - i, chunkLen - chunkIndex);
                    sliced = new byte[len];
                    Buffer.BlockCopy(data, i, sliced, 0, len);
                    i += len;
                    CurrentReadBytes += len;
                    chunkIndex       += len;
                    ContentTransform.Write(sliced);
                    Readable.Write(ContentTransform.Read());
                    if (chunkLen == chunkIndex)
                    {
                        State = BodyParserState.Chunked_ChunkCr;
                    }
                    break;

                case BodyParserState.Chunked_ChunkCr:
                    c = (char)data[i++];
                    if (c != CR)
                    {
                        End(); return(-1);
                    }
                    State = BodyParserState.Chunked_ChunkLf;
                    break;

                case BodyParserState.Chunked_ChunkLf:
                    c = (char)data[i++];
                    if (c != LF)
                    {
                        End(); return(-1);
                    }
                    State    = BodyParserState.Chunked_Length;
                    chunkLen = chunkIndex = 0;
                    StringQueue.New();
                    break;

                case BodyParserState.Chunked_Trailer:
                    c = (char)data[i++];
                    if (c != CR)
                    {
                        chunkIndex++;
                    }
                    else
                    {
                        if (chunkIndex == 0)
                        {
                            State = BodyParserState.Chunked_Lf;
                        }
                        else
                        {
                            chunkIndex = -1;      // LF will be added
                        }
                    }
                    break;

                case BodyParserState.Chunked_Lf:
                    c = (char)data[i++];
                    if (c != LF)
                    {
                        End(); return(-1);
                    }
                    State = BodyParserState.Dormant;
                    OnEnd?.Invoke();
                    ContentLength = CurrentReadBytes;
                    break;

                default: throw new InvalidOperationException("ProcessData cannot execute on Dormant state");
                }
            }
            if (writeExcess)
            {
                len    = data.Length - i;
                sliced = new byte[len];
                Buffer.BlockCopy(data, i, sliced, 0, len);
                Writable.Write(sliced);
            }
            return(i);
        }
Exemplo n.º 18
0
 public ReadMng(ClientMng c, StringQueue sq, StreamReader r)
 {
     cmng        = c;
     StringQueue = sq;
     reader      = r;
 }
Exemplo n.º 19
0
 public void TearDown()
 {
     _queue.Clear();
     _queue = null;
 }
Exemplo n.º 20
0
        protected override int ProcessData(byte[] data, bool writeExcess)
        {
            ThrowIfEnded();
            bool run = true; int i = 0;

            for (; i < data.Length && run; i++)
            {
                char c = (char)data[i];
                switch (State)
                {
                case RequestParserState.Method:
                    if (c != WHITESPACE)
                    {
                        StringQueue.Append(c);
                    }
                    else
                    {
                        Incoming.Method = StringQueue.Next();
                        StringQueue.New();
                        State = RequestParserState.Query;
                    }
                    break;

                case RequestParserState.Query:
                    if (c != WHITESPACE)
                    {
                        StringQueue.Append(c);
                    }
                    else
                    {
                        if (!Query.TryParse(StringQueue.Next(), out Query result))
                        {
                            End();
                            return(-1);
                        }
                        Incoming.Query = result;
                        StringQueue.New();
                        State = RequestParserState.Version;
                    }
                    break;

                case RequestParserState.Version:
                    if (c != CR)
                    {
                        StringQueue.Append(c);
                    }
                    else
                    {
                        if (!HttpVersion.TryParse(StringQueue.Next(), out HttpVersion result))
                        {
                            End();
                            return(-1);
                        }
                        Incoming.Version = result;
                        StringQueue.New();
                        State = RequestParserState.FirstLf;
                    }
                    break;

                case RequestParserState.FirstLf:
                    if (c != LF)
                    {
                        End(); return(-1);
                    }
                    State = RequestParserState.HeaderName;
                    break;

                case RequestParserState.HeaderName:
                    if (c == CR)
                    {
                        State = RequestParserState.Lf;
                    }
                    else if (c != COLON)
                    {
                        StringQueue.Append(c);
                    }
                    else
                    {
                        StringQueue.New();
                        State = RequestParserState.HeaderValue;
                    }
                    break;

                case RequestParserState.HeaderValue:
                    if (c != CR)
                    {
                        StringQueue.Append(c);
                    }
                    else
                    {
                        Incoming.Headers.Set(StringQueue.Next(), StringQueue.Next().Trim());
                        State = RequestParserState.HeaderLf;
                    }
                    break;

                case RequestParserState.HeaderLf:
                    if (c != LF)
                    {
                        End(); return(-1);
                    }
                    else
                    {
                        StringQueue.New();
                        State = RequestParserState.HeaderName;
                    }
                    break;

                case RequestParserState.Lf:
                    if (c != LF)
                    {
                        End(); return(-1);
                    }
                    run = false;
                    PushIncoming();
                    State = RequestParserState.Method;
                    break;
                }
            }
            if (writeExcess)
            {
                int    len    = data.Length - i;
                byte[] sliced = new byte[len];
                System.Buffer.BlockCopy(data, i, sliced, 0, len);
                Bwrite(sliced);
            }
            return(i);
        }
Exemplo n.º 21
0
        /// <summary>
        /// Parses a line of text and stores it in the vocabulary
        /// </summary>
        /// <param name="line"></param>
        public void ParseLine(string line)
        {
            StringQueue queue = new StringQueue(1, vocab);

            string[] words = line.Split(' ');
            if (words.Length > 3)
            {
                bool canStart = true;
                queue.Enqueue("START");	// lav en startnode
                bool canTerminate = false;
                for (int i=0; i<words.Length; i++) // for each word in message
                {
                    string word = words[i];
                    if (word.Trim().Length > 0)
                    {
                        if (i == words.Length) // last word in message
                            canTerminate = true;
                        queue.Enqueue(word);
                        if (queue.Count == 3) // 3 words are queued - lets make a fragment of them
                        {
                            queue.CreateFragment(canStart, canTerminate);

                            canStart = false;
                            queue.Dequeue();
                        }
                    }
                }
                queue.Enqueue("S**T");
                queue.CreateFragment(canStart, true);
            }
            queue.Clear();
        }
Exemplo n.º 22
0
 public void CreateStringQueue_FromNullStringList_ShouldThrowArgumentNUllException(List <string> ls)
 {
     Assert.Throws <ArgumentNullException>(() => StringQueue.CreateStringQueue(ls));
 }