Cache BindingDOMCodecServices in tests 85/93785/2
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 11 Nov 2020 17:35:43 +0000 (18:35 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 11 Nov 2020 17:45:09 +0000 (18:45 +0100)
When we are testing we can end up with a multitude of tests
reusing the same BindingRuntimeContext (due to
AbstractSchemaAwareTest caching). Since BindingCodecContext contains
non-trivial amount of state which is derived from it, including
loaded classes, let's attempt to reuse it as much as possible.

Change-Id: Ib41a7f4060c62bd53c2346bae51f055768dce0db
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-dom-adapter/src/test/java/org/opendaylight/mdsal/binding/dom/adapter/test/util/MockSchemaService.java

index 4e69e938050e82f8c89cff1c555e1ad9795747bf..eda1dc56fee1d7a19e696cde1cff3d13984a3b66 100644 (file)
@@ -9,11 +9,15 @@ package org.opendaylight.mdsal.binding.dom.adapter.test.util;
 
 import static com.google.common.base.Verify.verifyNotNull;
 
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
 import com.google.common.collect.ClassToInstanceMap;
 import com.google.common.collect.ImmutableClassToInstanceMap;
 import org.opendaylight.mdsal.binding.dom.adapter.AdapterContext;
 import org.opendaylight.mdsal.binding.dom.adapter.CurrentAdapterSerializer;
 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingCodecContext;
+import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecServices;
 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.mdsal.dom.api.DOMSchemaServiceExtension;
@@ -24,6 +28,17 @@ import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextProvider;
 
 public final class MockSchemaService implements DOMSchemaService, EffectiveModelContextProvider, AdapterContext {
+    // Codec has some amount of non-trivial state, such as generated classes. Its operation should not be affected by
+    // anything except BindingRuntimeContext, hence we should be able to reuse it.
+    private static final LoadingCache<BindingRuntimeContext, BindingDOMCodecServices> CODEC_CACHE =
+        CacheBuilder.newBuilder().weakKeys().weakValues().build(
+            new CacheLoader<BindingRuntimeContext, BindingDOMCodecServices>() {
+                @Override
+                public BindingDOMCodecServices load(final BindingRuntimeContext key) {
+                    return new BindingCodecContext(key);
+                }
+            });
+
     private EffectiveModelContext schemaContext;
     private CurrentAdapterSerializer serializer;
 
@@ -51,7 +66,7 @@ public final class MockSchemaService implements DOMSchemaService, EffectiveModel
     }
 
     public synchronized void changeSchema(final BindingRuntimeContext newContext) {
-        serializer = new CurrentAdapterSerializer(new BindingCodecContext(newContext));
+        serializer = new CurrentAdapterSerializer(CODEC_CACHE.getUnchecked(newContext));
         schemaContext = newContext.getEffectiveModelContext();
         listeners.streamListeners().forEach(listener -> listener.onModelContextUpdated(schemaContext));
     }