示例#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 IsAddingCompletedIsFalseAfterConstruction()
 {
     using (var q = new ConcurrentBufferedQueue <int, string>(NullTime.Source))
     {
         q.IsAddingCompleted.Should().BeFalse();
     }
 }
示例#3
0
 public void CountIsZeroAfterConstruction()
 {
     using (var q = new ConcurrentBufferedQueue <int, string>(NullTime.Source))
     {
         q.Count.Should().Be(0);
     }
 }
示例#4
0
        public void CompleteAddingAnEmptyQueue()
        {
            using var q = new ConcurrentBufferedQueue <int, string>();

            q.CompleteAdding();
            q.IsAddingCompleted.Should().BeTrue();
            q.IsCompleted.Should().BeTrue();
        }
示例#5
0
 public void AddingAnElementWithSameIdDoesntIncreasesCount()
 {
     using (var q = new ConcurrentBufferedQueue <int, string>(NullTime.Source))
     {
         q.Add(1, "Hallo");
         q.Add(1, "Hallo");
         q.Count.Should().Be(1);
     }
 }
示例#6
0
        public void AddingAnElementIncreasesCount()
        {
            using var q = new ConcurrentBufferedQueue <int, string>();

            q.Add(1, "Hallo");
            q.Count.Should().Be(1);

            q.Add(2, "Hallo");
            q.Count.Should().Be(2);
        }
示例#7
0
        public void AddAfterCompletionThrowsException()
        {
            using var q = new ConcurrentBufferedQueue <int, string>();

            q.CompleteAdding();

            var addAction = new Action(() => q.Add(1, "Hallo 1"));

            addAction.Should().Throw <InvalidOperationException>();
        }
示例#8
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>();
            }
        }
示例#9
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();
        }
示例#10
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();
        }
示例#11
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");
        }
示例#12
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");
            }
        }
示例#13
0
        public void AddAfterCompletionThrowsException()
        {
            using (var q = new ConcurrentBufferedQueue <int, string>(NullTime.Source))
            {
                q.CompleteAdding();

                var addAction = new Action(() =>
                {
                    q.Add(1, "Hallo 1");
                });

                addAction.ShouldThrow <InvalidOperationException>();
            }
        }
示例#14
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");
            }
        }
示例#15
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");
        }
示例#16
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();
        }
示例#17
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>();
            }
        }
示例#18
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");
            }
        }
示例#19
0
 public void IsAddingCompletedIsFalseAfterConstruction()
 {
     using var q = new ConcurrentBufferedQueue <int, string>();
     q.IsAddingCompleted.Should().BeFalse();
 }
示例#20
0
 public void CountIsZeroAfterConstruction()
 {
     using var q = new ConcurrentBufferedQueue <int, string>();
     q.Count.Should().Be(0);
 }