Example #1
0
    async def _extract_meta_data(
        self,
        allow_list: dict,
        cache_manager: CacheManager,
        shared_memory_name: str,
    ) -> dict:
        data = {}
        tasks = []

        shared_status = shared_memory.ShareableList(name=shared_memory_name)
        shared_status[0] = 0

        for metadata_extractor in self.metadata_extractors:
            if allow_list[metadata_extractor.key]:
                if (not cache_manager.bypass
                        and self.is_feature_whitelisted_for_cache(
                            metadata_extractor)
                        and cache_manager.is_host_predefined()
                        and cache_manager.is_enough_cached_data_present(
                            metadata_extractor.key)):
                    extracted_metadata: dict = (
                        cache_manager.get_predefined_metadata(
                            metadata_extractor.key))
                    data.update(extracted_metadata)
                    shared_status[0] += 1
                else:
                    tasks.append(metadata_extractor.start())

        extracted_metadata: tuple[dict] = await asyncio.gather(*tasks)
        shared_status[0] += len(tasks)
        data = {**data, **dict(ChainMap(*extracted_metadata))}
        return data
Example #2
0
def test_get_predefined_metadata(mocker, cache_manager: CacheManager):
    meta_data = []
    feature = ACCESSIBILITY

    empty_cache = cache_manager.convert_cached_data(meta_data, feature)
    assert empty_cache[EXPLANATION] == [Explanation.Cached]
    assert empty_cache[STAR_CASE] == StarCase.ONE
    assert empty_cache[VALUES] == []

    meta_data = (
        f'{{"{VALUES}":[], "{EXPLANATION}": ["{Explanation.AccessibilityServiceReturnedFailure}"], '
        + f'"{STAR_CASE}": "{StarCase.ONE}"}}')

    with mock.patch("cache.cache_manager.read_cached_values_by_feature"):
        cache_manager.convert_cached_data = mocker.MagicMock()
        cache_manager.convert_cached_data.return_value = json.loads(meta_data)
        cache_output = cache_manager.get_predefined_metadata(feature)

    assert isinstance(cache_output, dict)
    assert len(cache_output.keys()) == 1
    assert len(cache_output[feature].keys()) == 4