Exemple #1
0
        public void When_Dictionary_TryAdd_Then_FactoryIsInvokedOnlyOnce()
        {
            var dictionary = ImmutableDictionary <object, object> .Empty;

            var    invocation = 0;
            object _;

            Transactional.TryAdd(
                ref dictionary,
                new object(),
                o =>
            {
                // Cause concurrency issue
                if (invocation++ == 0)
                {
                    dictionary = ImmutableDictionary <object, object> .Empty.Add(new object(), new object());
                }

                return(new object());
            },
                out _);

            Assert.AreEqual(2, dictionary.Count);
            Assert.AreEqual(1, invocation);
        }