// Intentionally left No-op, subclasses may customize it
}
- protected DataBrokerTestCustomizer createDataBrokerTestCustomizer() {
+ protected DataBrokerTestCustomizer createDataBrokerTestCustomizer() {
return new DataBrokerTestCustomizer();
}
public abstract class AbstractSchemaAwareTest {
- private Iterable<YangModuleInfo> moduleInfos;
- private SchemaContext schemaContext;
-
-
protected Iterable<YangModuleInfo> getModuleInfos() throws Exception {
return BindingReflections.loadModuleInfos();
}
+ protected SchemaContext getSchemaContext() throws Exception {
+ Iterable<YangModuleInfo> moduleInfos = getModuleInfos();
+ ModuleInfoBackedContext moduleContext = ModuleInfoBackedContext.create();
+ moduleContext.addModuleInfos(moduleInfos);
+ return moduleContext.tryToCreateSchemaContext().get();
+ }
@Before
public final void setup() throws Exception {
- moduleInfos = getModuleInfos();
- ModuleInfoBackedContext moduleContext = ModuleInfoBackedContext.create();
- moduleContext.addModuleInfos(moduleInfos);
- schemaContext = moduleContext.tryToCreateSchemaContext().get();
- setupWithSchema(schemaContext);
+ setupWithSchema(getSchemaContext());
}
/**
* Setups test with Schema context.
* This method is called before {@link #setupWithSchemaService(SchemaService)}
- *
- * @param context
*/
protected abstract void setupWithSchema(SchemaContext context);
--- /dev/null
+/*
+ * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.md.sal.binding.test;
+
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+/**
+ * AbstractDataBrokerTest which creates the SchemaContext
+ * only once, and keeps it in a static, instead of re-recreating
+ * it for each Test, and is thus faster.
+ *
+ * @author Michael Vorburger
+ */
+public class ConstantSchemaAbstractDataBrokerTest extends AbstractDataBrokerTest {
+
+ @Override
+ protected SchemaContext getSchemaContext() throws Exception {
+ return SchemaContextSingleton.getSchemaContext(() -> super.getSchemaContext());
+ }
+
+}
// into this DataBrokerTestModule, and make AbstractDataBrokerTest
// use it, instead of the way around it currently is (the opposite);
// this is just for historical reasons... and works for now.
- AbstractDataBrokerTest dataBrokerTest = new AbstractDataBrokerTest();
+ ConstantSchemaAbstractDataBrokerTest dataBrokerTest = new ConstantSchemaAbstractDataBrokerTest();
dataBrokerTest.setup();
return dataBrokerTest.getDataBroker();
} catch (Exception e) {
--- /dev/null
+/*
+ * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.md.sal.binding.test;
+
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+/**
+ * {@link SchemaContext} singleton holder (static).
+ *
+ * <p>This is useful in scenarios such as unit tests, but not OSGi environments,
+ * where there is a flat classpath and thus really only one single
+ * SchemaContext.
+ *
+ * @author Michael Vorburger
+ */
+public final class SchemaContextSingleton {
+
+ private static SchemaContext staticSchemaContext;
+
+ public static synchronized SchemaContext getSchemaContext(Supplier<SchemaContext> supplier) throws Exception {
+ if (staticSchemaContext == null) {
+ staticSchemaContext = supplier.get();
+ }
+ return staticSchemaContext;
+ }
+
+ private SchemaContextSingleton() { }
+
+ @FunctionalInterface
+ public interface Supplier<T> {
+ T get() throws Exception;
+ }
+}
import static com.google.common.truth.Truth.assertThat;
+import org.junit.Ignore;
import org.junit.Test;
import org.opendaylight.controller.md.sal.binding.test.DataBrokerTestModule;
public void ensureDataBrokerTestModuleWorksWithoutException() {
assertThat(DataBrokerTestModule.dataBroker()).isNotNull();
}
+
+ @Test
+ @Ignore // This test is flaky on build server VMs (although fine locally)
+ public void slowYangLoadingShouldOnlyHappenOnceAndNotDelayEachDataBroker() {
+ // TODO Write a lil' Timer utility class to make this kind of timing test code more readable
+ long startAtMs = System.currentTimeMillis();
+ DataBrokerTestModule.dataBroker();
+ long firstDataBrokerAtMs = System.currentTimeMillis();
+ long firstDataBrokerDurationMs = firstDataBrokerAtMs - startAtMs;
+ DataBrokerTestModule.dataBroker();
+ long secondDataBrokerDurationMs = System.currentTimeMillis() - firstDataBrokerAtMs;
+ assertThat(Math.abs(secondDataBrokerDurationMs - firstDataBrokerDurationMs))
+ .isLessThan(firstDataBrokerDurationMs / 4);
+ }
}