sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From stefane...@apache.org
Subject svn commit: r1711246 - in /sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup: ./ mock/
Date Thu, 29 Oct 2015 13:09:40 GMT
Author: stefanegli
Date: Thu Oct 29 13:09:39 2015
New Revision: 1711246

URL: http://svn.apache.org/viewvc?rev=1711246&view=rev
Log:
SLING-5200 : add possibility of injecting delays into particular places during simulation,
eg in resourceResolver.commit

Added:
    sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/ArtificialDelay.java
  (with props)
    sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/DummyResourceResolverFactory.java
  (with props)
Modified:
    sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/VirtualInstance.java
    sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/VirtualInstanceBuilder.java
    sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockFactory.java
    sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockedResource.java
    sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockedResourceResolver.java

Modified: sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/VirtualInstance.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/VirtualInstance.java?rev=1711246&r1=1711245&r2=1711246&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/VirtualInstance.java
(original)
+++ sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/VirtualInstance.java
Thu Oct 29 13:09:39 2015
@@ -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;
+    }
+
 }

Modified: sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/VirtualInstanceBuilder.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/VirtualInstanceBuilder.java?rev=1711246&r1=1711245&r2=1711246&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/VirtualInstanceBuilder.java
(original)
+++ sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/VirtualInstanceBuilder.java
Thu Oct 29 13:09:39 2015
@@ -32,6 +32,7 @@ import org.apache.sling.discovery.base.c
 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 VirtualInstanceBui
     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 VirtualInstanceBui
 
     public VirtualInstanceBuilder setDebugName(String debugName) {
         this.debugName = debugName;
+        delay.setDebugName(debugName);
         return this;
     }
 

Added: sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/ArtificialDelay.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/ArtificialDelay.java?rev=1711246&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/ArtificialDelay.java
(added)
+++ sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/ArtificialDelay.java
Thu Oct 29 13:09:39 2015
@@ -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.");
+    }
+
+}

Propchange: sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/ArtificialDelay.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/DummyResourceResolverFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/DummyResourceResolverFactory.java?rev=1711246&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/DummyResourceResolverFactory.java
(added)
+++ sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/DummyResourceResolverFactory.java
Thu Oct 29 13:09:39 2015
@@ -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;
+    }
+
+}

Propchange: sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/DummyResourceResolverFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockFactory.java?rev=1711246&r1=1711245&r2=1711246&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockFactory.java
(original)
+++ sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockFactory.java
Thu Oct 29 13:09:39 2015
@@ -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(

Modified: sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockedResource.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockedResource.java?rev=1711246&r1=1711245&r2=1711246&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockedResource.java
(original)
+++ sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockedResource.java
Thu Oct 29 13:09:39 2015
@@ -123,7 +123,7 @@ public class MockedResource extends Synt
                 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)) {

Modified: sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockedResourceResolver.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockedResourceResolver.java?rev=1711246&r1=1711245&r2=1711246&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockedResourceResolver.java
(original)
+++ sling/trunk/bundles/extensions/discovery/base/src/test/java/org/apache/sling/discovery/base/its/setup/mock/MockedResourceResolver.java
Thu Oct 29 13:09:39 2015
@@ -43,21 +43,31 @@ import org.apache.sling.api.resource.Res
 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 impl
         }
     }
 
-    private Repository getRepository() {
+    public Repository getRepository() {
     	return repository;
     }
 
@@ -301,6 +311,9 @@ public class MockedResourceResolver impl
     }
 
     public void commit() throws PersistenceException {
+        if (delay!=null) {
+            delay.delay("pre.commit");
+        }
         try {
             this.session.save();
         } catch (final RepositoryException re) {



Mime
View raw message