예제 #1
0
        public void Should_be_thread_safe()
        {
            var N = 10000;
            var M = N - 100;

            cache = new SizeLimitedCache <int, string>(k =>
            {
                sourceCalls.Enqueue(k);
                return(k.ToString());
            }, M);
            var numbers = new List <int>(N);

            for (int i = 0; i < N; i++)
            {
                numbers.Add(i);
            }

            Parallel.ForEach(
                numbers,
                new ParallelOptions
            {
                MaxDegreeOfParallelism = 16
            },
                number =>
            {
                cache.GetData(number)
                .Should().Be(number.ToString());
            });

            cache.Count
            .Should().Be(M);
            sourceCalls.Count
            .Should().Be(N);
        }
예제 #2
0
        public void Should_cache_data_and_use_them_later()
        {
            cache.GetData(1);
            cache.GetData(1);
            cache.GetData(2);
            cache.GetData(2);
            cache.GetData(3);

            Assert.Equal(new int[] { 1, 2, 3 }, sourceCalls);
        }