Esempio n. 1
0
        public void Tests()
        {
            var batchSize   = 2;
            var maxCapacity = 5;
            var q           = new BatchedQueue <int>(batchSize, maxCapacity);

            q.AsString().Should().Be("[()]");

            q.Enqueue(1);
            q.AsString().Should().Be("[(1)]");

            q.Enqueue(2);
            q.AsString().Should().Be("[(1,2)]");

            q.Enqueue(3);
            q.AsString().Should().Be("[(1,2)(3)]");

            q.Enqueue(4);
            q.AsString().Should().Be("[(1,2)(3,4)]");

            q.Enqueue(5);
            q.AsString().Should().Be("[(1,2)(3,4)(5)]");
            q.Count().Should().Be(5);

            q.Enqueue(6);
            q.AsString().Should().Be("[(1,2)(3,4)(5)]");

            var firstBatch = q.DequeueBatch();

            firstBatch.AsString().Should().BeEquivalentTo("(1,2)");
            q.AsString().Should().Be("[(3,4)(5)]");

            q.Enqueue(new[] { 7, 8, 9 });
            q.AsString().Should().Be("[(3,4)(5,7)(8)]");

            q.EnqueueToFront(firstBatch);
            q.AsString().Should().Be("[(1,2)(3,4)(5)]");

            q.EnqueueToFront(L(8, 9, 10));
            q.AsString().Should().Be("[(8,9,10)(1,2)]");

            // dequeue empty list
            firstBatch = q.DequeueBatch();
            firstBatch.AsString().Should().BeEquivalentTo("(8,9,10)");
            q.AsString().Should().Be("[(1,2)]");

            firstBatch = q.DequeueBatch();
            firstBatch.AsString().Should().BeEquivalentTo("(1,2)");
            q.AsString().Should().Be("[()]");

            firstBatch = q.DequeueBatch();
            firstBatch.AsString().Should().BeEquivalentTo("()");
            q.AsString().Should().Be("[()]");
        }
Esempio n. 2
0
        public void Send(IEnumerable <T> items)
        {
            lock (_queueLock)
            {
                _queue.Enqueue(items);

                if (_queue.Count() == 0)
                {
                    return;
                }
            }

            List <T> batch;

            do
            {
                batch = null;

                lock (_queueLock)
                {
                    if (_queue.Count() > 0)
                    {
                        batch = _queue.DequeueBatch();
                    }
                }

                try
                {
                    var json    = JsonSerializer.Serialize(_builder(batch));
                    var request = new HttpRequestMessage(HttpMethod.Post, _uri);
                    request.Content = new StringContent(json, Encoding.UTF8, "application/json");
                    var response = _client.Send(request);

                    if (!response.IsSuccessStatusCode)
                    {
                        throw new Exception($"Http send failed with code {response.StatusCode}: {response.Content}");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    lock (_queueLock)
                    {
                        _queue.EnqueueToFront(batch);
                    }
                    break;
                }
            } while (batch != null);
        }