Esempio n. 1
0
        private void SetHeaders(
            IReadOnlyDictionary <string, string[]> headers)
        {
            foreach (var(key, values) in headers)
            {
                if (_headers.TryAdd(key, values))
                {
                    continue;
                }

                Send(_protocolError);
                break;
            }
        }
        public void TestTryAdd()
        {
            var latch = new CountdownEvent(4);

            var collectionChangedActions = new List <NotifyCollectionChangedAction>();
            var propertiesChanged        = new List <string>();

            void CollectionChangedHandler(object sender, NotifyCollectionChangedEventArgs args)
            {
                collectionChangedActions.Add(args.Action);
                latch?.Signal();
            }

            void PropertyChangedHandler(object sender, PropertyChangedEventArgs args)
            {
                propertiesChanged.Add(args.PropertyName);
                latch?.Signal();
            }

            var observableDictionary =
                new ObservableConcurrentDictionary <string, string>(CollectionChangedHandler, PropertyChangedHandler);

            var success = observableDictionary.TryAdd("1", "value");

            latch.Wait();

            Assert.True(success);

            Assert.Single(observableDictionary);

            var keys = new string[1];

            observableDictionary.Keys.CopyTo(keys, 0);
            Assert.Equal("1", keys[0]);

            var values = new string[1];

            observableDictionary.Values.CopyTo(values, 0);
            Assert.Equal("value", values[0]);

            Assert.Single(collectionChangedActions);
            Assert.Equal(NotifyCollectionChangedAction.Add, collectionChangedActions[0]);

            Assert.Equal(3, propertiesChanged.Count);
            Assert.Contains("Count", propertiesChanged);
            Assert.Contains("Keys", propertiesChanged);
            Assert.Contains("Values", propertiesChanged);

            latch.Dispose();
        }