@SuppressWarnings("checkstyle:npathcomplexity")
    public PartitionServiceState getPartitionServiceState() {
        PartitionServiceState state = getPartitionTableState();

        if (state != SAFE) {
            return state;
        }

        if (!checkAndTriggerReplicaSync()) {
            return REPLICA_NOT_SYNC;
        }

        return SAFE;
    }
@Test
    public void shouldNotBeSafe_whenReplicasAreNotSync() {
        Config config = new Config();
        ServiceConfig serviceConfig = TestMigrationAwareService.createServiceConfig(1);
        ConfigAccessor.getServicesConfig(config).addServiceConfig(serviceConfig);

        TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory();
        HazelcastInstance hz = factory.newHazelcastInstance(config);
        HazelcastInstance hz2 = factory.newHazelcastInstance(config);

        InternalPartitionServiceImpl partitionService1 = getNode(hz).partitionService;
        InternalPartitionServiceImpl partitionService2 = getNode(hz2).partitionService;
        int maxPermits = drainAllReplicaSyncPermits(partitionService1);
        int maxPermits2 = drainAllReplicaSyncPermits(partitionService2);
        assertEquals(maxPermits, maxPermits2);

        warmUpPartitions(hz, hz2);

        setBackupPacketDropFilter(hz, 100);
        setBackupPacketDropFilter(hz2, 100);

        NodeEngine nodeEngine = getNode(hz).nodeEngine;
        for (int i = 0; i < nodeEngine.getPartitionService().getPartitionCount(); i++) {
            Operation op = new TestPutOperationWithAsyncBackup(i);
            nodeEngine.getOperationService().invokeOnPartition(null, op, i).join();
        }

        final PartitionReplicaStateChecker replicaStateChecker1 = partitionService1.getPartitionReplicaStateChecker();
        final PartitionReplicaStateChecker replicaStateChecker2 = partitionService2.getPartitionReplicaStateChecker();

        assertEquals(PartitionServiceState.REPLICA_NOT_SYNC, replicaStateChecker1.getPartitionServiceState());
        assertEquals(PartitionServiceState.REPLICA_NOT_SYNC, replicaStateChecker2.getPartitionServiceState());

        addReplicaSyncPermits(partitionService1, maxPermits);
        addReplicaSyncPermits(partitionService2, maxPermits);

        assertTrueEventually(() -> {
            assertEquals(PartitionServiceState.SAFE, replicaStateChecker1.getPartitionServiceState());
            assertEquals(PartitionServiceState.SAFE, replicaStateChecker2.getPartitionServiceState());
            // assert no leftovers of PartitionReplicaSyncRequestOffloadable
            assertEquals(0, getAsyncOperationsCount(hz));
            assertEquals(0, getAsyncOperationsCount(hz2));
        });
    }