Beispiel #1
0
 protected LinkHcoMemoryUserCacheTestBase(MemoryCacheOptions options)
 {
     this.MemoryCacheOptions = new OptionsWrapper <MemoryCacheOptions>(options);
     this.MemoryCache        = new MemoryCache(this.MemoryCacheOptions);
     this.UserCache          = new LinkHcoMemoryUserCache <string, LinkHcoCacheEntry>(
         this.MemoryCache,
         CurrentUserIdentifier,
         SharedUserIdentifier,
         HcoEntryKeyBuilder,
         ControlEntryKeyBuilder,
         ConfigureEntryExpiration,
         ConfigureControlExpiration,
         ConfigureRootExpiration,
         RootControlTokenKey);
 }
Beispiel #2
0
    public async Task Given_MultipleCaches_When_RequestsAreMadeInParallel()
    {
        var threads = new Thread[NumberOfThreads];
        var results = new int[NumberOfThreads];

        for (int i = 0; i < NumberOfThreads; i += 1)
        {
            var randomPayload = this.uriCollection
                                .OrderBy(x => this.random.Next()).ToList();
            int threadNumber = i;
            threads[threadNumber] = new Thread(
                () =>
            {
                int sharedCounter  = 0;
                int successCounter = 0;
                foreach (var uri in randomPayload)
                {
                    sharedCounter = (sharedCounter + 1) % 10;
                    if (this.userCaches[threadNumber].TryGetValue(uri, out var entry))
                    {
                        successCounter += 1;
                    }
                    else
                    {
                        this.userCaches[threadNumber].Set(
                            uri,
                            new LinkHcoCacheEntry(
                                $"{threadNumber}-{NextInt()}",
                                sharedCounter == 0
                                        ? CacheScope.AcrossUserContexts
                                        : CacheScope.ForIndividualUserContext,
                                null));
                    }
                }

                results[threadNumber] = successCounter;
            });
        }

        foreach (var t in threads)
        {
            t.Start();
        }

        foreach (var t in threads)
        {
            t.Join();
        }

        var memCache = this.MemoryCache;

        memCache.Count.Should().BeLessThanOrEqualTo((int)this.MemoryCacheOptions.Value.SizeLimit + this.userCaches.Count + 2);
        memCache.Count.Should().BeGreaterThan(0);
        foreach (var uc in this.userCaches)
        {
            uc.Clear();
        }

        await Task.Delay(TimeSpan.FromMilliseconds(10));

        memCache.Count.Should().Be(1);
        LinkHcoMemoryUserCache.ClearAllLinkHcoCacheEntries(this.MemoryCache, RootControlTokenKey);
        await Task.Delay(TimeSpan.FromMilliseconds(10));

        memCache.Count.Should().Be(0);
    }
Beispiel #3
0
 public When_FullClear()
 {
     LinkHcoMemoryUserCache.ClearAllLinkHcoCacheEntries(this.MemoryCache, RootControlTokenKey);
 }