Skip to content

Commit

Permalink
Add cached resolver unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaScheller committed Oct 30, 2023
1 parent e52d835 commit d9b2fc8
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 104 deletions.
38 changes: 31 additions & 7 deletions src/CachedResolver/PythonExpose.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import inspect
import logging
import os
from functools import wraps

from pxr import Ar
Expand All @@ -24,14 +25,24 @@ def wrapper(*args, **kwargs):
return wrapper


class ResolverContext:
class UnitTestHelper:
context_initialize_call_counter = 0
resolve_and_cache_call_counter = 0
current_directory_path = ""

@classmethod
def reset(cls):
cls.context_initialize_call_counter = 0
cls.resolve_and_cache_call_counter = 0
cls.current_directory_path = ""

class ResolverContext:
@staticmethod
@log_function_args
def Initialize(context):
"""Initialize the context. This get's called on default and post mapping file path
"""Initialize the context. This get's called on default and post mapping file path
context creation.
Here you can inject data by batch calling context.AddCachingPair(assetPath, resolvePath),
this will then populate the internal C++ resolve cache and all resolves calls
to those assetPaths will not invoke Python and instead use the cache.
Expand All @@ -40,6 +51,9 @@ def Initialize(context):
context (CachedResolverContext): The active context.
"""
LOG.debug("::: ResolverContext.Initialize")
"""The code below is only needed to verify that UnitTests work."""
UnitTestHelper.context_initialize_call_counter += 1
context.AddCachingPair("shot.usd", "/some/path/to/a/file.usd")
return

@staticmethod
Expand All @@ -51,15 +65,25 @@ def ResolveAndCache(assetPath, context):
assetPath (str): An unresolved asset path.
context (CachedResolverContext): The active context.
Returns:
str: The resolved path string. If it points to a non-existent file,
it will be resolved to an empty ArResolvedPath internally, but will
str: The resolved path string. If it points to a non-existent file,
it will be resolved to an empty ArResolvedPath internally, but will
still count as a cache hit and be stored inside the cachedPairs dict.
"""
resolved_asset_path = assetPath
resolved_asset_path = "/some/path/to/a/file.usd"
context.AddCachingPair(assetPath, resolved_asset_path)
LOG.debug("::: ResolverContext.ResolveAndCache | {}".format(context.GetCachingPairs()))
LOG.debug(
"::: ResolverContext.ResolveAndCache | {} | {}".format(assetPath, context.GetCachingPairs())
)
"""
To clear the context cache call:
context.ClearCachingPairs()
"""
"""The code below is only needed to verify that UnitTests work."""
UnitTestHelper.resolve_and_cache_call_counter += 1
if assetPath == "unittest.usd":
current_dir_path = UnitTestHelper.current_directory_path
asset_a_file_path = os.path.join(current_dir_path, "assetA.usd")
asset_b_file_path = os.path.join(current_dir_path, "assetB.usd")
context.AddCachingPair("assetA.usd", asset_a_file_path)
context.AddCachingPair("assetB.usd", asset_b_file_path)
return Ar.ResolvedPath(resolved_asset_path)
3 changes: 2 additions & 1 deletion src/CachedResolver/resolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ CachedResolver::_Resolve(
if (ctx) {
// Search for mapping pairs
auto &mappingPairs = ctx->GetMappingPairs();

auto map_find = mappingPairs.find(assetPath);
if(map_find != mappingPairs.end()){
ArResolvedPath resolvedPath = _ResolveAnchored(this->emptyString, map_find->second);
Expand Down Expand Up @@ -172,6 +171,8 @@ CachedResolver::_Resolve(
if (resolvedPath) {
return resolvedPath;
}
// Only try the first valid context.
break;
}
}
}
Expand Down
Loading

0 comments on commit d9b2fc8

Please sign in to comment.