コード例 #1
0
        public void When_receiving_empty_favourite_should_log_and_not_hit_the_cache()
        {
            var elector = new DeltaElector(_cache, _deltaProducersProvider, _reputationManager, _logger);

            elector.OnNext(new FavouriteDeltaBroadcast());

            _cache.DidNotReceiveWithAnyArgs().TryGetValue(Arg.Any <object>(), out Arg.Any <object>());
            _cache.DidNotReceiveWithAnyArgs().CreateEntry(Arg.Any <object>());
        }
コード例 #2
0
        public void When_receiving_bad_favourite_should_log_and_not_hit_the_cache(FavouriteDeltaBroadcast badFavourite,
                                                                                  Type exceptionType)
        {
            var elector = new DeltaElector(_cache, _deltaProducersProvider, _hashProvider, _logger);

            elector.OnNext(badFavourite);

            _cache.DidNotReceiveWithAnyArgs().TryGetValue(Arg.Any <object>(), out Arg.Any <object>());
            _cache.DidNotReceiveWithAnyArgs().CreateEntry(Arg.Any <object>());
        }
コード例 #3
0
        public void When_receiving_invalid_candidate_should_log_and_not_hit_the_cache()
        {
            var elector = new DeltaElector(_cache, _deltaProducersProvider, _reputationManager, _logger);

            elector.OnNext(new FavouriteDeltaBroadcast {
                Candidate = new CandidateDeltaBroadcast
                {
                    Hash       = ByteUtil.GenerateRandomByteArray(32).ToByteString(),
                    ProducerId = PeerIdHelper.GetPeerId("unknown_producer")
                },
                VoterId = PeerIdHelper.GetPeerId("candidate field is invalid")
            });

            _cache.DidNotReceiveWithAnyArgs().TryGetValue(Arg.Any <object>(), out Arg.Any <object>());
            _cache.DidNotReceiveWithAnyArgs().CreateEntry(Arg.Any <object>());
        }
コード例 #4
0
        public void When_voter_not_a_producer_should_not_save_vote()
        {
            var favourite = DeltaHelper.GetFavouriteDelta(_hashProvider);

            _deltaProducersProvider
            .GetDeltaProducersFromPreviousDelta(Arg.Any <Cid>())
            .Returns(new List <PeerId> {
                PeerIdHelper.GetPeerId("the only known producer")
            });

            var elector = new DeltaElector(_cache, _deltaProducersProvider, _reputationManager, _logger);

            elector.OnNext(favourite);

            _deltaProducersProvider.Received(1)
            .GetDeltaProducersFromPreviousDelta(Arg.Is <Cid>(h =>
                                                             favourite.Candidate.PreviousDeltaDfsHash.Equals(h.ToArray().ToByteString())));
            _cache.DidNotReceiveWithAnyArgs().TryGetValue(default, out _);
コード例 #5
0
        public void When_receiving_known_favourite_should_not_store_in_cache()
        {
            using (var realCache = new MemoryCache(new MemoryCacheOptions()))
            {
                var favourite        = DeltaHelper.GetFavouriteDelta(_hashProvider);
                var candidateListKey = DeltaElector.GetCandidateListCacheKey(favourite);

                AddVoterAsExpectedProducer(favourite.VoterId);

                var elector = new DeltaElector(realCache, _deltaProducersProvider, _reputationManager, _logger);

                elector.OnNext(favourite);
                elector.OnNext(favourite);

                realCache.TryGetValue(candidateListKey, out IDictionary <FavouriteDeltaBroadcast, bool> retrieved)
                .Should().BeTrue();

                retrieved.Keys.Count.Should().Be(1);
                retrieved.Should().ContainKey(favourite);
            }
        }
コード例 #6
0
        public void When_receiving_new_valid_favourite_should_store_in_cache()
        {
            var favourite        = DeltaHelper.GetFavouriteDelta(_hashProvider);
            var candidateListKey = DeltaElector.GetCandidateListCacheKey(favourite);

            AddVoterAsExpectedProducer(favourite.VoterId);

            var elector = new DeltaElector(_cache, _deltaProducersProvider, _reputationManager, _logger);

            var addedEntry = Substitute.For <ICacheEntry>();

            _cache.CreateEntry(Arg.Is <string>(s => s.Equals(candidateListKey)))
            .Returns(addedEntry);

            elector.OnNext(favourite);

            _cache.Received(1).TryGetValue(Arg.Is <string>(s => s.Equals(candidateListKey)), out Arg.Any <object>());
            _cache.Received(1).CreateEntry(Arg.Is <string>(s => s.Equals(candidateListKey)));

            var addedValue = addedEntry.Value;

            addedValue.Should().BeAssignableTo <IDictionary <FavouriteDeltaBroadcast, bool> >();
            ((IDictionary <FavouriteDeltaBroadcast, bool>)addedValue).Should().ContainKey(favourite);
        }