Exemplo n.º 1
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 _);
Exemplo n.º 2
0
        CampaigningProductionSubscription_Should_Trigger_TryGetFavouriteDelta_On_Campaigning_Producing_Phase()
        {
            var favourite = DeltaHelper.GetFavouriteDelta(_hashProvider);

            _deltaVoter.TryGetFavouriteDelta(Arg.Any <Cid>(), out Arg.Any <FavouriteDeltaBroadcast>())
            .Returns(ci =>
            {
                ci[1] = favourite;
                return(true);
            });

            _cycleEventProvider.MovePastNextPhase(PhaseName.Campaigning);

            _deltaVoter.Received(1).TryGetFavouriteDelta(
                Arg.Is <Cid>(b => b == PreviousDeltaBytes),
                out Arg.Any <FavouriteDeltaBroadcast>());
            _deltaHub.Received(1).BroadcastFavouriteCandidateDelta(Arg.Is(favourite));
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
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);
        }