Cache reflection operations in AbstractSchemaAwareTest 08/77508/1
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 5 Nov 2018 15:27:45 +0000 (16:27 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 5 Nov 2018 15:27:45 +0000 (16:27 +0100)
The design of AbstractSchemaAwareTest requires reflection-based
loading of SchemaContext for each test case, which is slow.

Instantiate weak caches to speed up tests which are co-located
on the same class loader.

Change-Id: I1a79d7e99f6efcccab37445f3de25a74cb6f02b6
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-dom-adapter/src/test/java/org/opendaylight/mdsal/binding/dom/adapter/test/AbstractSchemaAwareTest.java
binding/mdsal-binding-dom-adapter/src/test/java/org/opendaylight/mdsal/binding/dom/adapter/test/DataTreeChangeListenerTest.java
binding/mdsal-binding-dom-adapter/src/test/java/org/opendaylight/mdsal/binding/dom/adapter/test/ForwardedNotificationAdapterTest.java

index dc72e8bc592e1018e0d5a766f9f5923fe3d8504d..1c558b33b7a3d34674f6c2b9dbc4057881ff3941 100644 (file)
@@ -7,30 +7,47 @@
  */
 package org.opendaylight.mdsal.binding.dom.adapter.test;
 
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import com.google.common.collect.ImmutableSet;
+import java.util.Set;
 import org.junit.Before;
 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 
-public abstract class AbstractSchemaAwareTest  {
+public abstract class AbstractSchemaAwareTest {
+    private static final LoadingCache<ClassLoader, Set<YangModuleInfo>> MODULE_INFO_CACHE = CacheBuilder.newBuilder()
+            .weakKeys().weakValues().build(new CacheLoader<ClassLoader, Set<YangModuleInfo>>() {
+                @Override
+                public Set<YangModuleInfo> load(final ClassLoader key) {
+                    return BindingReflections.loadModuleInfos(key);
+                }
+            });
+    private static final LoadingCache<Set<YangModuleInfo>, SchemaContext> SCHEMA_CONTEXT_CACHE =
+            CacheBuilder.newBuilder().weakValues().build(new CacheLoader<Set<YangModuleInfo>, SchemaContext>() {
+                @Override
+                public SchemaContext load(final Set<YangModuleInfo> key) {
+                    final ModuleInfoBackedContext moduleContext = ModuleInfoBackedContext.create();
+                    moduleContext.addModuleInfos(key);
+                    return moduleContext.tryToCreateSchemaContext().get();
+                }
+            });
 
-    private Iterable<YangModuleInfo> moduleInfos;
-    private SchemaContext schemaContext;
-
-
-    protected Iterable<YangModuleInfo> getModuleInfos() throws Exception {
-        return BindingReflections.loadModuleInfos();
+    @Before
+    public final void setup() throws Exception {
+        setupWithSchema(getSchemaContext());
     }
 
+    protected Set<YangModuleInfo> getModuleInfos() throws Exception {
+        return MODULE_INFO_CACHE.getUnchecked(Thread.currentThread().getContextClassLoader());
+    }
 
-    @Before
-    public final void setup() throws Exception {
-        moduleInfos = getModuleInfos();
-        ModuleInfoBackedContext moduleContext = ModuleInfoBackedContext.create();
-        moduleContext.addModuleInfos(moduleInfos);
-        schemaContext = moduleContext.tryToCreateSchemaContext().get();
-        setupWithSchema(schemaContext);
+    protected SchemaContext getSchemaContext() throws Exception {
+        // ImmutableSet guarantees non-null
+        return SCHEMA_CONTEXT_CACHE.getUnchecked(ImmutableSet.copyOf(getModuleInfos()));
     }
 
     /**
@@ -39,5 +56,4 @@ public abstract class AbstractSchemaAwareTest  {
      * @param context schema context
      */
     protected abstract void setupWithSchema(SchemaContext context);
-
 }
index 35e79fa12edb1cf4e59ee17fafebf07f4fd83d18..029e990ecf48c9c5dcfcefb824135f529f11af64 100644 (file)
@@ -23,6 +23,7 @@ import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
 import com.google.common.util.concurrent.SettableFuture;
 import java.util.Collection;
+import java.util.Set;
 import java.util.concurrent.TimeUnit;
 import org.junit.Test;
 import org.opendaylight.mdsal.binding.api.DataBroker;
@@ -79,7 +80,7 @@ public class DataTreeChangeListenerTest extends AbstractDataBrokerTest {
     }
 
     @Override
-    protected Iterable<YangModuleInfo> getModuleInfos() throws Exception {
+    protected Set<YangModuleInfo> getModuleInfos() throws Exception {
         return ImmutableSet.of(
                 BindingReflections.getModuleInfo(TwoLevelList.class),
                 BindingReflections.getModuleInfo(TreeComplexUsesAugment.class)
index e621e40cb62101e0395b5ddee891085b31680c63..0668b167baad2ff4efed80eae745076a30f5eb3b 100644 (file)
@@ -15,6 +15,7 @@ import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
@@ -38,7 +39,7 @@ public class ForwardedNotificationAdapterTest extends AbstractNotificationBroker
     private static final Logger LOG = LoggerFactory.getLogger(ForwardedNotificationAdapterTest.class);
 
     @Override
-    protected Iterable<YangModuleInfo> getModuleInfos() throws Exception {
+    protected Set<YangModuleInfo> getModuleInfos() throws Exception {
         return ImmutableSet.of(BindingReflections.getModuleInfo(TwoLevelListChanged.class));
 
     }