Exemple #1
0
 public ThreadSafeDictionaryCache([CanBeNull] IEqualityComparer <TKey> equalityComparer)
 {
     _cache
         = new ThreadSafeLazyRef <ImmutableDictionary <TKey, TValue> >(
               () => ImmutableDictionary <TKey, TValue>
               .Empty
               .WithComparers(equalityComparer));
 }
        public void Has_value_is_false_until_value_accessed()
        {
            var safeLazy = new ThreadSafeLazyRef <string>(() => "s");

            Assert.False(safeLazy.HasValue);
            Assert.Equal("s", safeLazy.Value);
            Assert.True(safeLazy.HasValue);
        }
        public async Task Can_exchange_value()
        {
            var safeLazy = new ThreadSafeLazyRef <string>(() => "");
            var tasks    = new List <Task>();

            for (var i = 0; i < 10; i++)
            {
                tasks.Add(Task.Run(() => safeLazy.ExchangeValue(s => s + "s")));
            }

            await Task.WhenAll(tasks);

            Assert.Equal("ssssssssss", safeLazy.Value);
        }
        public async Task Can_initialize_from_multiple_threads_and_initialization_happens_only_once()
        {
            var counter  = 0;
            var safeLazy = new ThreadSafeLazyRef <string>(() => counter++.ToString());
            var tasks    = new List <Task>();

            for (var i = 0; i < 10; i++)
            {
                tasks.Add(Task.Run(() => safeLazy.Value));
            }

            await Task.WhenAll(tasks);

            Assert.Equal(1, counter);
        }