public void SimpleGetOrAdd() { var dict = ImmutableDictionary <string, int> .Empty .Add("A", 11) .Add("B", 22) .Add("C", 33); var r1 = Transactional.GetOrAdd(ref dict, "A", x => x.Length); r1.Should().Be(11); dict.Should().HaveCount(3); var r2 = Transactional.GetOrAdd(ref dict, "D", x => 44); r2.Should().Be(44); dict.Should().HaveCount(4); }
public void When_Dictionary_GetOrAdd_Then_FactoryIsInvokedOnlyOnce() { var dictionary = ImmutableDictionary <object, object> .Empty; var invocation = 0; Transactional.GetOrAdd( ref dictionary, new object(), o => { // Cause concurrency issue if (invocation++ == 0) { dictionary = ImmutableDictionary <object, object> .Empty.Add(new object(), new object()); } return(new object()); }); Assert.AreEqual(2, dictionary.Count); Assert.AreEqual(1, invocation); }