Esempio n. 1
0
        private List <IScoredCandidateDelta> AddCandidatesToCacheAndVote(int firstVotesCount,
                                                                         int secondVotesCount,
                                                                         MemoryCache realCache)
        {
            var firstCandidate = DeltaHelper.GetCandidateDelta(_hashProvider, _previousDeltaHash,
                                                               producerId: _producerIds.First());

            var secondCandidate = DeltaHelper.GetCandidateDelta(_hashProvider, _previousDeltaHash,
                                                                producerId: _producerIds.Skip(1).First());

            var candidateStream = Enumerable.Repeat(firstCandidate, firstVotesCount)
                                  .Concat(Enumerable.Repeat(secondCandidate, secondVotesCount))
                                  .Shuffle().ToObservable();

            using (candidateStream.Subscribe(_voter))
            {
                var firstKey  = DeltaVoter.GetCandidateCacheKey(firstCandidate);
                var secondKey = DeltaVoter.GetCandidateCacheKey(secondCandidate);

                realCache.TryGetValue(firstKey, out IScoredCandidateDelta firstRetrieved).Should().BeTrue();
                realCache.TryGetValue(secondKey, out IScoredCandidateDelta secondRetrieved).Should().BeTrue();

                return(new List <IScoredCandidateDelta>
                {
                    firstRetrieved, secondRetrieved
                });
            }
        }
Esempio n. 2
0
        public void When_candidates_not_in_cache_should_create_or_update_a_previous_hash_entry()
        {
            using (var realCache = new MemoryCache(new MemoryCacheOptions()))
            {
                _voter = new DeltaVoter(realCache, _producersProvider, _peerSettings, _logger);

                var candidate1 = DeltaHelper.GetCandidateDelta(
                    _hashProvider,
                    _previousDeltaHash,
                    producerId: _producerIds.First());
                var candidate1CacheKey = DeltaVoter.GetCandidateCacheKey(candidate1);

                var candidate2 = DeltaHelper.GetCandidateDelta(
                    _hashProvider,
                    _previousDeltaHash,
                    producerId: _producerIds.Last());
                var candidate2CacheKey = DeltaVoter.GetCandidateCacheKey(candidate2);

                var previousDeltaCacheKey = DeltaVoter.GetCandidateListCacheKey(candidate1);

                _voter.OnNext(candidate1);

                realCache.TryGetValue(candidate1CacheKey,
                                      out ScoredCandidateDelta retrievedCandidate1).Should().BeTrue();
                retrievedCandidate1.Candidate.ProducerId.Should().Be(_producerIds.First());

                realCache.TryGetValue(previousDeltaCacheKey,
                                      out ConcurrentBag <string> retrievedCandidateList).Should().BeTrue();
                retrievedCandidateList.Should().BeEquivalentTo(candidate1CacheKey);

                _voter.OnNext(candidate2);

                realCache.TryGetValue(candidate2CacheKey,
                                      out ScoredCandidateDelta retrievedCandidate2).Should().BeTrue();
                retrievedCandidate2.Candidate.ProducerId.Should().Be(_producerIds.Last());

                realCache.TryGetValue(previousDeltaCacheKey,
                                      out ConcurrentBag <string> retrievedUpdatedCandidateList).Should().BeTrue();
                retrievedUpdatedCandidateList.Should().BeEquivalentTo(candidate1CacheKey, candidate2CacheKey);
            }
        }