sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From romb...@apache.org
Subject [sling-org-apache-sling-discovery-base] 05/16: SLING-5200 : add possibility of injecting delays into particular places during simulation, eg in resourceResolver.commit
Date Tue, 07 Nov 2017 09:25:45 GMT
This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.discovery.base-1.0.2
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-discovery-base.git

commit 6e20b4666d1aae86b6500f17c8d294297c4a1e3d
Author: Stefan Egli <stefanegli@apache.org>
AuthorDate: Thu Oct 29 13:09:39 2015 +0000

    SLING-5200 : add possibility of injecting delays into particular places during simulation,
eg in resourceResolver.commit
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/discovery/base@1711246
13f79535-47bb-0310-9956-ffa450edef68
---
 .../discovery/base/its/setup/VirtualInstance.java  | 16 +++-
 .../base/its/setup/VirtualInstanceBuilder.java     |  7 ++
 .../base/its/setup/mock/ArtificialDelay.java       | 92 ++++++++++++++++++++++
 .../setup/mock/DummyResourceResolverFactory.java   | 73 +++++++++++++++++
 .../discovery/base/its/setup/mock/MockFactory.java | 34 +-------
 .../base/its/setup/mock/MockedResource.java        |  2 +-
 .../its/setup/mock/MockedResourceResolver.java     | 19 ++++-
 7 files changed, 206 insertions(+), 37 deletions(-)

diff --git a/src/test/java/org/apache/sling/discovery/base/its/setup/VirtualInstance.java
b/src/test/java/org/apache/sling/discovery/base/its/setup/VirtualInstance.java
index 9e10871..2a12483 100644
--- a/src/test/java/org/apache/sling/discovery/base/its/setup/VirtualInstance.java
+++ b/src/test/java/org/apache/sling/discovery/base/its/setup/VirtualInstance.java
@@ -30,23 +30,22 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
 
-import javax.jcr.Session;
 import javax.servlet.Servlet;
 
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceResolverFactory;
-import org.apache.sling.commons.testing.jcr.RepositoryProvider;
 import org.apache.sling.discovery.InstanceDescription;
 import org.apache.sling.discovery.PropertyProvider;
 import org.apache.sling.discovery.TopologyEventListener;
 import org.apache.sling.discovery.base.commons.BaseDiscoveryService;
 import org.apache.sling.discovery.base.commons.ClusterViewService;
-import org.apache.sling.discovery.base.commons.ViewChecker;
 import org.apache.sling.discovery.base.commons.UndefinedClusterViewException;
+import org.apache.sling.discovery.base.commons.ViewChecker;
 import org.apache.sling.discovery.base.connectors.announcement.AnnouncementRegistry;
 import org.apache.sling.discovery.base.connectors.ping.ConnectorRegistry;
 import org.apache.sling.discovery.base.connectors.ping.TopologyConnectorClientInformation;
 import org.apache.sling.discovery.base.connectors.ping.TopologyConnectorServlet;
+import org.apache.sling.discovery.base.its.setup.mock.ArtificialDelay;
 import org.eclipse.jetty.server.Connector;
 import org.eclipse.jetty.server.Server;
 import org.eclipse.jetty.server.nio.SelectChannelConnector;
@@ -100,6 +99,8 @@ public class VirtualInstance {
 
     private final VirtualInstanceBuilder builder;
 
+    private final ArtificialDelay delay;
+
     private class ViewCheckerRunner implements Runnable {
 
     	private final int intervalInSeconds;
@@ -143,6 +144,7 @@ public class VirtualInstance {
         this.builder = builder;
     	this.slingId = builder.getSlingId();
         this.debugName = builder.getDebugName();
+        this.delay = builder.getDelay();
         logger.info("<init>: starting slingId="+slingId+", debugName="+debugName);
 
         osgiMock = new OSGiMock();
@@ -180,6 +182,10 @@ public class VirtualInstance {
         osgiMock.activateAll();
     }
     
+    public void setDelay(String operationDescriptor, long delayMillis) {
+        delay.setDelay(operationDescriptor, delayMillis);
+    }
+    
     @Override
     public String toString() {
         return "a [Test]Instance[slingId="+slingId+", debugName="+debugName+"]";
@@ -371,4 +377,8 @@ public class VirtualInstance {
         return builder;
     }
 
+    public String getDebugName() {
+        return debugName;
+    }
+
 }
diff --git a/src/test/java/org/apache/sling/discovery/base/its/setup/VirtualInstanceBuilder.java
b/src/test/java/org/apache/sling/discovery/base/its/setup/VirtualInstanceBuilder.java
index fb287d6..c3c55f7 100644
--- a/src/test/java/org/apache/sling/discovery/base/its/setup/VirtualInstanceBuilder.java
+++ b/src/test/java/org/apache/sling/discovery/base/its/setup/VirtualInstanceBuilder.java
@@ -32,6 +32,7 @@ import org.apache.sling.discovery.base.connectors.announcement.AnnouncementRegis
 import org.apache.sling.discovery.base.connectors.announcement.AnnouncementRegistryImpl;
 import org.apache.sling.discovery.base.connectors.ping.ConnectorRegistry;
 import org.apache.sling.discovery.base.connectors.ping.ConnectorRegistryImpl;
+import org.apache.sling.discovery.base.its.setup.mock.ArtificialDelay;
 import org.apache.sling.discovery.base.its.setup.mock.FailingScheduler;
 import org.apache.sling.discovery.commons.providers.spi.base.DummySlingSettingsService;
 import org.apache.sling.settings.SlingSettingsService;
@@ -74,10 +75,15 @@ public abstract class VirtualInstanceBuilder {
     protected boolean ownRepository;
     private int minEventDelay = 1;
     protected VirtualInstanceBuilder hookedToBuilder;
+    protected final ArtificialDelay delay = new ArtificialDelay();
 
     public VirtualInstanceBuilder() {
     }
     
+    public ArtificialDelay getDelay() {
+        return delay;
+    }
+    
     public VirtualInstanceBuilder newRepository(String path, boolean resetRepo) throws Exception
{
         createNewRepository();
         ownRepository = true;
@@ -170,6 +176,7 @@ public abstract class VirtualInstanceBuilder {
 
     public VirtualInstanceBuilder setDebugName(String debugName) {
         this.debugName = debugName;
+        delay.setDebugName(debugName);
         return this;
     }
 
diff --git a/src/test/java/org/apache/sling/discovery/base/its/setup/mock/ArtificialDelay.java
b/src/test/java/org/apache/sling/discovery/base/its/setup/mock/ArtificialDelay.java
new file mode 100644
index 0000000..e4ab7b9
--- /dev/null
+++ b/src/test/java/org/apache/sling/discovery/base/its/setup/mock/ArtificialDelay.java
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.discovery.base.its.setup.mock;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * General purpose delay object that can be passed around and 
+ * plugged in various places identified by an 'operationDescriptor'
+ * which can be used to inject delays at runtime
+ * @author egli
+ *
+ */
+public class ArtificialDelay {
+
+    private final Logger logger = LoggerFactory.getLogger(this.getClass());
+
+    private final Map<String,Long> operationsMap = new ConcurrentHashMap<String,
Long>();
+
+    private String debugName;
+    
+    private final Object syncObj = new Object();
+    
+    public void setDebugName(String debugName) {
+        this.debugName = debugName;
+    }
+
+    public void setDelay(String operationDescriptor, long delayMillis) {
+        operationsMap.put(operationDescriptor, delayMillis);
+        synchronized(syncObj) {
+            syncObj.notifyAll();
+        }
+    }
+    
+    public void delay(String operationDescriptor) {
+        Long delayMillis = operationsMap.get(operationDescriptor);
+        if (delayMillis == null) {
+            return;
+        }
+        if (delayMillis <= 0) {
+            return;
+        }
+        logger.info("delay: delaying ["+debugName+"] '"+operationDescriptor+"' for "+delayMillis+"ms...");
+        final long start = System.currentTimeMillis();
+        synchronized(syncObj) {
+            while(true) {
+                delayMillis = operationsMap.get(operationDescriptor);
+                if (delayMillis == null) {
+                    logger.info("delay: delaying ["+debugName+"]'"+operationDescriptor+"'
for "+delayMillis+"ms done.");
+                    return;
+                }
+                if (delayMillis <= 0) {
+                    logger.info("delay: delaying ["+debugName+"]'"+operationDescriptor+"'
for "+delayMillis+"ms done.");
+                    return;
+                }
+                final long end = start + delayMillis;
+                long remaining = end - System.currentTimeMillis();
+                if (remaining <= 0) {
+                    break;
+                }
+                try {
+                    logger.info("delay: delaying ["+debugName+"] '"+operationDescriptor+"'
now for "+remaining+"ms...");
+                    syncObj.wait(remaining);
+                } catch (InterruptedException e) {
+                    logger.error("delay: got interrupted: "+e, e);
+                }
+            }
+        }
+        logger.info("delay: delaying ["+debugName+"]'"+operationDescriptor+"' for "+delayMillis+"ms
done.");
+    }
+
+}
diff --git a/src/test/java/org/apache/sling/discovery/base/its/setup/mock/DummyResourceResolverFactory.java
b/src/test/java/org/apache/sling/discovery/base/its/setup/mock/DummyResourceResolverFactory.java
new file mode 100644
index 0000000..ab1f2a1
--- /dev/null
+++ b/src/test/java/org/apache/sling/discovery/base/its/setup/mock/DummyResourceResolverFactory.java
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.discovery.base.its.setup.mock;
+
+import java.util.Map;
+
+import javax.jcr.RepositoryException;
+
+import org.apache.sling.api.resource.LoginException;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.jcr.api.SlingRepository;
+
+/** Factored out of MockFactory: simple implementation
+ * of a ResourceResolverFactory that uses MockedResourceResolver's
+ * mechanism of auto-creating a repository via
+ * RepositoryProvider.instance().getRepository()
+ */
+public class DummyResourceResolverFactory implements ResourceResolverFactory {
+
+    private SlingRepository repository;
+    private ArtificialDelay delay;
+
+    public DummyResourceResolverFactory() {
+        
+    }
+    
+    public void setSlingRepository(SlingRepository repository) {
+        this.repository = repository;
+    }
+    
+    public void setArtificialDelay(ArtificialDelay delay) {
+        this.delay = delay;
+    }
+    
+    @Override
+    public ResourceResolver getResourceResolver(Map<String, Object> authenticationInfo)
throws LoginException {
+        throw new IllegalStateException("not yet implemented");
+    }
+
+    @Override
+    public ResourceResolver getAdministrativeResourceResolver(Map<String, Object> authenticationInfo)
throws LoginException {
+        try {
+            MockedResourceResolver mockedResourceResolver = 
+                    new MockedResourceResolver(repository, delay);
+            repository = (SlingRepository) mockedResourceResolver.getRepository();
+            return mockedResourceResolver;
+        } catch (RepositoryException e) {
+            throw new LoginException(e);
+        }
+    }
+
+    public SlingRepository getSlingRepository() {
+        return repository;
+    }
+
+}
diff --git a/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockFactory.java
b/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockFactory.java
index f4e2404..c7fb766 100644
--- a/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockFactory.java
+++ b/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockFactory.java
@@ -24,11 +24,8 @@ import java.util.Properties;
 import org.apache.sling.api.resource.ResourceResolverFactory;
 import org.apache.sling.jcr.api.SlingRepository;
 import org.apache.sling.settings.SlingSettingsService;
-import org.hamcrest.Description;
 import org.jmock.Expectations;
 import org.jmock.Mockery;
-import org.jmock.api.Action;
-import org.jmock.api.Invocation;
 import org.jmock.integration.junit4.JUnit4Mockery;
 import org.jmock.lib.action.ReturnValueAction;
 import org.jmock.lib.action.VoidAction;
@@ -41,37 +38,14 @@ public class MockFactory {
 
     public static ResourceResolverFactory mockResourceResolverFactory()
             throws Exception {
-    	return mockResourceResolverFactory(null);
+        return mockResourceResolverFactory(null);
     }
 
     public static ResourceResolverFactory mockResourceResolverFactory(final SlingRepository
repositoryOrNull)
             throws Exception {
-        Mockery context = new JUnit4Mockery();
-
-        final ResourceResolverFactory resourceResolverFactory = context
-                .mock(ResourceResolverFactory.class);
-        // final ResourceResolver resourceResolver = new MockResourceResolver();
-        // final ResourceResolver resourceResolver = new
-        // MockedResourceResolver();
-
-        context.checking(new Expectations() {
-            {
-                allowing(resourceResolverFactory)
-                        .getAdministrativeResourceResolver(null);
-                will(new Action() {
-
-                    public Object invoke(Invocation invocation)
-                            throws Throwable {
-                    	return new MockedResourceResolver(repositoryOrNull);
-                    }
-
-                    public void describeTo(Description arg0) {
-                        arg0.appendText("whateva - im going to create a new mockedresourceresolver");
-                    }
-                });
-            }
-        });
-        return resourceResolverFactory;
+        DummyResourceResolverFactory factory = new DummyResourceResolverFactory();
+        factory.setSlingRepository(repositoryOrNull);
+        return factory;
     }
 
     public static SlingSettingsService mockSlingSettingsService(
diff --git a/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockedResource.java
b/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockedResource.java
index 040842b..b71cc23 100644
--- a/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockedResource.java
+++ b/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockedResource.java
@@ -123,7 +123,7 @@ public class MockedResource extends SyntheticResource {
                 ValueMap valueMap = new ValueMapDecorator(map);
                 return (AdapterType) valueMap;
             } catch (Exception e) {
-                e.printStackTrace();
+                logger.error("adaptTo failed with : "+e, e);
                 return null;
             }
         } else if (type.equals(ModifiableValueMap.class)) {
diff --git a/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockedResourceResolver.java
b/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockedResourceResolver.java
index 9d9fd13..aa94ff4 100644
--- a/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockedResourceResolver.java
+++ b/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockedResourceResolver.java
@@ -43,21 +43,31 @@ import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.commons.testing.jcr.RepositoryProvider;
 import org.apache.sling.commons.testing.jcr.RepositoryUtil;
 import org.apache.sling.jcr.api.SlingRepository;
+import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class MockedResourceResolver implements ResourceResolver {
 
-	private final SlingRepository repository;
+    private final Logger logger = LoggerFactory.getLogger(this.getClass());
+
+    private final SlingRepository repository;
 
 	private Session session;
 
     private List<MockedResource> resources = new LinkedList<MockedResource>();
 
+    private final ArtificialDelay delay;
+
     public MockedResourceResolver() throws RepositoryException {
-    	this(null);
+    	this(null, null);
     }
 
     public MockedResourceResolver(SlingRepository repositoryOrNull) throws RepositoryException
{
+        this(repositoryOrNull, null);
+    }
+    
+    public MockedResourceResolver(SlingRepository repositoryOrNull, ArtificialDelay delay)
throws RepositoryException {
+        this.delay = delay;
     	if (repositoryOrNull==null) {
     		this.repository = RepositoryProvider.instance().getRepository();
     		Session adminSession = null;
@@ -86,7 +96,7 @@ public class MockedResourceResolver implements ResourceResolver {
         }
     }
 
-    private Repository getRepository() {
+    public Repository getRepository() {
     	return repository;
     }
 
@@ -301,6 +311,9 @@ public class MockedResourceResolver implements ResourceResolver {
     }
 
     public void commit() throws PersistenceException {
+        if (delay!=null) {
+            delay.delay("pre.commit");
+        }
         try {
             this.session.save();
         } catch (final RepositoryException re) {

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <commits@sling.apache.org>.

Mime
View raw message