示例#1
0
        public void TheSecondTakeIsBlockedUntilTheKeyCooledDown()
        {
            var timeSource = new ControllableTime();

            using (var q = new ConcurrentBufferedQueue <int, string>(timeSource))
            {
                q.Add(1, "Hallo 1");
                q.Add(1, "Hallo new");

                var  r           = q.Take();
                bool clockWasSet = false;

                r.Key.Should().Be(1);
                r.Value.Should().Be("Hallo new");
                q.Count.Should().Be(0);

                q.Add(1, "Hallo after cooldown");

                Task.Factory.StartNew(() =>
                {
                    Thread.Sleep(50);
                    clockWasSet = true;
                    timeSource.Add(100);
                });

                r = q.Take();

                clockWasSet.Should().BeTrue();
                r.Key.Should().Be(1);
                r.Value.Should().Be("Hallo after cooldown");
            }
        }
示例#2
0
        public void CompleteAddingPropertiesWorkAsExpected()
        {
            using var q = new ConcurrentBufferedQueue <int, string>();

            q.Add(1, "Hallo 1");
            q.Add(2, "Hallo 2");

            q.CompleteAdding();
            q.IsAddingCompleted.Should().BeTrue();
            q.IsCompleted.Should().BeFalse();

            _ = q.Take();
            _ = q.Take();

            q.IsCompleted.Should().BeTrue();
        }
示例#3
0
        public void TakingAnElementRetrievesTheFirstOne()
        {
            using (var q = new ConcurrentBufferedQueue <int, string>(NullTime.Source))
            {
                q.Add(3, "Hallo 3");
                q.Add(7, "Hallo 7");
                q.Add(1, "Hallo 1");

                var result = q.Take();
                result.Key.Should().Be(3);
                result.Value.Should().Be("Hallo 3");

                result = q.Take();
                result.Key.Should().Be(7);
                result.Value.Should().Be("Hallo 7");

                result = q.Take();
                result.Key.Should().Be(1);
                result.Value.Should().Be("Hallo 1");
            }
        }
示例#4
0
        public void CallTakeAfterCompletingThrowsAnException()
        {
            using (var q = new ConcurrentBufferedQueue <int, string>(NullTime.Source))
            {
                var takeAction = new Action(() =>
                {
                    var result = q.Take();
                });

                q.CompleteAdding();
                takeAction.Should().Throw <InvalidOperationException>();
            }
        }
示例#5
0
        public void CallTakeAfterCompletingThrowsAnException()
        {
            using var q = new ConcurrentBufferedQueue <int, string>();

            bool?success    = null;
            var  takeAction = new Action(() => (success, _, _) = q.Take());

            q.CompleteAdding();
            takeAction();

            success.HasValue.Should().BeTrue();
            success.Value.Should().BeFalse();
        }
示例#6
0
        public void CompletingTheBufferWhileTakeIsWaitingThrowsAnException()
        {
            using var q = new ConcurrentBufferedQueue <int, string>();

            Task.Factory.StartNew(() =>
            {
                Thread.Sleep(50);  // Make sure Take is called before the next line is executed
                q.CompleteAdding();
            });

            var(success, _, _) = q.Take();
            success.Should().BeFalse();
        }
示例#7
0
        public void OverrideQueueElementWithSameKey()
        {
            using (var q = new ConcurrentBufferedQueue <int, string>(NullTime.Source))
            {
                q.Add(1, "Hallo 1");
                q.Add(2, "Hallo 2");
                q.Add(1, "Hallo NEW");

                var r = q.Take();

                r.Key.Should().Be(1);
                r.Value.Should().Be("Hallo NEW");
            }
        }
示例#8
0
        public void OverrideQueueElementWithSameKey()
        {
            using var q = new ConcurrentBufferedQueue <int, string>();

            q.Add(1, "Hallo 1");
            q.Add(2, "Hallo 2");
            q.Add(1, "Hallo NEW");

            var(success, key, value) = q.Take();

            success.Should().BeTrue();
            key.Should().Be(1);
            value.Should().Be("Hallo NEW");
        }
示例#9
0
        public void TakingAnElementBlocksUnitAnElementIsAvailable()
        {
            using (var q = new ConcurrentBufferedQueue <int, string>(NullTime.Source))
            {
                Task.Factory.StartNew(() =>
                {
                    Thread.Sleep(50);  //Make sure Take is called before the next line is executed
                    q.Add(3, "Hallo");
                });

                var result = q.Take();
                result.Key.Should().Be(3);
                result.Value.Should().Be("Hallo");
            }
        }
示例#10
0
        public void TakingAnElementBlocksUnitAnElementIsAvailable()
        {
            using var q = new ConcurrentBufferedQueue <int, string>();

            Task.Factory.StartNew(() =>
            {
                Thread.Sleep(50);  // Make sure Take is called before the next line is executed
                q.Add(3, "Hallo");
            });

            var(success, key, value) = q.Take();

            success.Should().BeTrue();
            key.Should().Be(3);
            value.Should().Be("Hallo");
        }
示例#11
0
        public void CompletingTheBufferWhileTakeIsWaitingThrowsAnException()
        {
            using (var q = new ConcurrentBufferedQueue <int, string>(NullTime.Source))
            {
                Task.Factory.StartNew(() =>
                {
                    Thread.Sleep(50);  //Make sure Take is called before the next line is executed
                    q.CompleteAdding();
                });

                var takeAction = new Action(() =>
                {
                    var result = q.Take();
                });

                takeAction.Should().Throw <InvalidOperationException>();
            }
        }