// This test verifies that we don't induct duplicate messages. It sets up a
// state with 2 messages already inducted (i.e. two entries in the ingress
// history) and we try to induct 3 messages. The first is a duplicate of an
// already inducted message, the second is new and the third is a duplicate of
// the second one. We expect only the second message to be inducted.
This test verifies that we don't induct duplicate messages. It sets up a
state with 2 messages already inducted  and we try to induct 3 messages. The first is a duplicate of an
already inducted message, the second is new and the third is a duplicate of
the second one. We expect only the second message to be inducted.
This test verifies that we don ' t induct duplicate messages . It sets up a state with 2 messages already inducted and we try to induct 3 messages . The first is a duplicate of an already inducted message the second is new and the third is a duplicate of the second one . We expect only the second message to be inducted .
fn dont_induct_duplicate_messages() {
    with_test_replica_logger(|log| {
        let mut ingress_history_writer = MockIngressHistory::new();
        let canister_id1 = canister_test_id(0);
        let canister_id2 = canister_test_id(1);
        let metrics_registry = MetricsRegistry::new();
        let msg1: SignedIngressContent = SignedIngressBuilder::new()
            .canister_id(canister_id1)
            .sender(user_test_id(0))
            .nonce(2)
            .build()
            .into();
        let msg2: SignedIngressContent = SignedIngressBuilder::new()
            .canister_id(canister_id2)
            .sender(user_test_id(0))
            .nonce(3)
            .build()
            .into();
        let msg3 = msg2.clone();
        ingress_history_writer
            .expect_set_status()
            .with(always(), eq(msg3.id()), always())
            .times(1)
            .returning(|state, message_id3, _| {
                state.set_ingress_status(
                    message_id3,
                    IngressStatus::Received {
                        receiver: canister_test_id(0).get(),
                        user_id: user_test_id(0),
                        time: mock_time(),
                    },
                );
            });
        let ingress_history_writer = Arc::new(ingress_history_writer);
        let valid_set_rule = ValidSetRuleImpl::new(
            ingress_history_writer,
            Arc::new(CyclesAccountManagerBuilder::new().build()),
            &metrics_registry,
            subnet_test_id(1),
            log,
        );
        let mut state = ReplicatedState::new_rooted_at(
            subnet_test_id(1),
            SubnetType::Application,
            "NOT_USED".into(),
        );
        state.set_ingress_status(
            message_test_id(1),
            IngressStatus::Received {
                receiver: canister_id1.get(),
                user_id: user_test_id(0),
                time: mock_time(),
            },
        );
        state.set_ingress_status(
            msg1.id(),
            IngressStatus::Received {
                receiver: canister_id1.get(),
                user_id: user_test_id(0),
                time: mock_time(),
            },
        );
        insert_canister(&mut state, canister_id1);
        insert_canister(&mut state, canister_id2);
        valid_set_rule.induct_messages(&mut state, vec![msg1, msg2, msg3]);
        assert_eq!(ingress_queue_size(&state, canister_id1), 0);
        assert_eq!(ingress_queue_size(&state, canister_id2), 1);
        assert_inducted_ingress_messages_eq(
            metric_vec(&[
                (&[(LABEL_STATUS, LABEL_VALUE_SUCCESS)], 1),
                (&[(LABEL_STATUS, LABEL_VALUE_DUPLICATE)], 2),
            ]),
            &metrics_registry,
        );
        assert_eq!(
            1,
            fetch_inducted_payload_size_stats(&metrics_registry).count
        );
    });
}
fn dont_induct_duplicate_messages ( ) { with_test_replica_logger ( | log | { let mut ingress_history_writer = MockIngressHistory :: new ( ) ; let canister_id1 = canister_test_id ( 0 ) ; let canister_id2 = canister_test_id ( 1 ) ; let metrics_registry = MetricsRegistry :: new ( ) ; let msg1 : SignedIngressContent = SignedIngressBuilder :: new ( ) . canister_id ( canister_id1 ) . sender ( user_test_id ( 0 ) ) . nonce ( 2 ) . build ( ) . into ( ) ; let msg2 : SignedIngressContent = SignedIngressBuilder :: new ( ) . canister_id ( canister_id2 ) . sender ( user_test_id ( 0 ) ) . nonce ( 3 ) . build ( ) . into ( ) ; let msg3 = msg2 . clone ( ) ; ingress_history_writer . expect_set_status ( ) . with ( always ( ) , eq ( msg3 . id ( ) ) , always ( ) ) . times ( 1 ) . returning ( | state , message_id3 , _ | { state . set_ingress_status ( message_id3 , IngressStatus :: Received { receiver : canister_test_id ( 0 ) . get ( ) , user_id : user_test_id ( 0 ) , time : mock_time ( ) , } , ) ; } ) ; let ingress_history_writer = Arc :: new ( ingress_history_writer ) ; let valid_set_rule = ValidSetRuleImpl :: new ( ingress_history_writer , Arc :: new ( CyclesAccountManagerBuilder :: new ( ) . build ( ) ) , & metrics_registry , subnet_test_id ( 1 ) , log , ) ; let mut state = ReplicatedState :: new_rooted_at ( subnet_test_id ( 1 ) , SubnetType :: Application , " " . into ( ) , ) ; state . set_ingress_status ( message_test_id ( 1 ) , IngressStatus :: Received { receiver : canister_id1 . get ( ) , user_id : user_test_id ( 0 ) , time : mock_time ( ) , } , ) ; state . set_ingress_status ( msg1 . id ( ) , IngressStatus :: Received { receiver : canister_id1 . get ( ) , user_id : user_test_id ( 0 ) , time : mock_time ( ) , } , ) ; insert_canister ( & mut state , canister_id1 ) ; insert_canister ( & mut state , canister_id2 ) ; valid_set_rule . induct_messages ( & mut state , vec ! [ msg1 msg2 msg3 ] ) ; assert_eq ! ( ingress_queue_size ( state canister_id1 ) 0 ) ; assert_eq ! ( ingress_queue_size ( state canister_id2 ) 1 ) ; assert_inducted_ingress_messages_eq ( metric_vec ( & [ ( & [ ( LABEL_STATUS , LABEL_VALUE_SUCCESS ) ] , 1 ) , ( & [ ( LABEL_STATUS , LABEL_VALUE_DUPLICATE ) ] , 2 ) , ] ) , & metrics_registry , ) ; assert_eq ! ( 1 fetch_inducted_payload_size_stats ( metrics_registry ) count ) ; } ) ; }
This test verifies that we don't induct duplicate messages.
This test verifies that we don ' t induct duplicate messages .