Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2013 JBoss Inc
   *
   * Licensed 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.kie.workbench.common.services.datamodel.backend.server.builder;
 
 import java.net.URL;
 import java.util.List;
 import  javax.enterprise.context.spi.CreationalContext;
 import  javax.enterprise.inject.spi.Bean;
 import  javax.enterprise.inject.spi.BeanManager;
 
 import  org.guvnor.common.services.project.builder.events.InvalidateDMOProjectCacheEvent;
 import  org.guvnor.common.services.project.builder.model.BuildResults;
 import  org.guvnor.common.services.project.builder.service.BuildService;
 import  org.guvnor.structure.server.config.ConfigGroup;
 import  org.guvnor.structure.server.config.ConfigType;
 import  org.guvnor.structure.server.config.ConfigurationFactory;
 import  org.guvnor.structure.server.config.ConfigurationService;
 import  org.jboss.weld.environment.se.StartMain;
 import  org.junit.Before;
 import  org.junit.Test;
 import  org.kie.workbench.common.services.backend.builder.LRUBuilderCache;
 import  org.kie.workbench.common.services.shared.project.KieProject;
 import  org.kie.workbench.common.services.shared.project.KieProjectService;
 import  org.uberfire.backend.server.util.Paths;
 import  org.uberfire.backend.vfs.Path;
 import  org.uberfire.java.nio.fs.file.SimpleFileSystemProvider;
 import  org.uberfire.rpc.SessionInfo;
 
 import static org.junit.Assert.*;
 import static org.mockito.Mockito.*;
 
 public class BuilderConcurrencyTest {
 
     private static final String GLOBAL_SETTINGS = "settings";
 
     private final SimpleFileSystemProvider fs = new SimpleFileSystemProvider();
     private BeanManager beanManager;
 
     private Paths paths;
     private ConfigurationService configurationService;
     private ConfigurationFactory configurationFactory;
     private BuildService buildService;
     private KieProjectService projectService;
     private LRUBuilderCache builderCache;
 
     @Before
     public void setUp() throws Exception {
         //Bootstrap WELD container
         StartMain startMain = new StartMain( new String[ 0 ] );
          = startMain.go().getBeanManager();
 
         //Instantiate Paths used in tests for Path conversion
         final Bean pathsBean = (Bean) .getBeans( Paths.class ).iterator().next();
         final CreationalContext cc1 = .createCreationalContext( pathsBean );
          = (Paths) .getReference( pathsBean,
                                                   Paths.class,
                                                   cc1 );
 
         //Instantiate ConfigurationService
         final Bean configurationServiceBean = (Bean) .getBeans( ConfigurationService.class ).iterator().next();
         final CreationalContext cc2 = .createCreationalContext( configurationServiceBean );
          = (ConfigurationService) .getReference( configurationServiceBean,
                                                                                 ConfigurationService.class,
                                                                                 cc2 );
 
         //Instantiate ConfigurationFactory
         final Bean configurationFactoryBean = (Bean) .getBeans( ConfigurationFactory.class ).iterator().next();
         final CreationalContext cc3 = .createCreationalContext( configurationFactoryBean );
          = (ConfigurationFactory) .getReference( configurationFactoryBean,
                                                                                 ConfigurationFactory.class,
                                                                                 cc3 );
 
         //Instantiate BuildService
         final Bean buildServiceBean = (Bean) .getBeans( BuildService.class ).iterator().next();
         final CreationalContext cc4 = .createCreationalContext( buildServiceBean );
          = (BuildService) .getReference( buildServiceBean,
                                                                 BuildService.class,
                                                                 cc4 );
 
        //Instantiate ProjectService
        final Bean projectServiceBean = (Bean) .getBeans( KieProjectService.class ).iterator().next();
        final CreationalContext cc5 = .createCreationalContext( projectServiceBean );
         = (KieProjectService) .getReference( projectServiceBean,
                                                                       KieProjectService.class,
                                                                       cc5 );
        //Instantiate LRUBuilderCache
        final Bean LRUBuilderCacheBean = (Bean) .getBeans( LRUBuilderCache.class ).iterator().next();
        final CreationalContext cc6 = .createCreationalContext( LRUBuilderCacheBean );
         = (LRUBuilderCache) .getReference( LRUBuilderCacheBean,
                                                                   LRUBuilderCache.class,
                                                                   cc6 );
        //Instantiate LRUProjectDataModelOracleCache
        final Bean LRUProjectDataModelOracleCacheBean = (Bean) .getBeans( LRUProjectDataModelOracleCache.class ).iterator().next();
        final CreationalContext cc7 = .createCreationalContext( LRUProjectDataModelOracleCacheBean );
         = (LRUProjectDataModelOracleCache.getReference( LRUProjectDataModelOracleCacheBean,
                                                                                     LRUProjectDataModelOracleCache.class,
                                                                                     cc7 );
        //Define mandatory properties
        List<ConfigGroup> globalConfigGroups = .getConfiguration( ConfigType.GLOBAL );
        boolean globalSettingsDefined = false;
        for ( ConfigGroup globalConfigGroup : globalConfigGroups ) {
            if ( .equalsglobalConfigGroup.getName() ) ) {
                globalSettingsDefined = true;
                break;
            }
        }
        if ( !globalSettingsDefined ) {
            .addConfiguration( getGlobalConfiguration() );
        }
    }
    private ConfigGroup getGlobalConfiguration() {
        //Global Configurations used by many of Drools Workbench editors
        final ConfigGroup group = .newConfigGroup( ConfigType.GLOBAL,
                                                                       ,
                                                                       "" );
        group.addConfigItem( .newConfigItem( "build.enable-incremental",
                                                                 "true" ) );
        return group;
    }
    @Test
    //https://bugzilla.redhat.com/show_bug.cgi?id=1145105
    public void testBuilderConcurrency() throws URISyntaxException {
        final URL pomUrl = this.getClass().getResource"/BuilderConcurrencyRepo/pom.xml" );
        final org.uberfire.java.nio.file.Path nioPomPath = .getPath( pomUrl.toURI() );
        final Path pomPath = .convert( nioPomPath );
        final URL resourceUrl = this.getClass().getResource"/BuilderConcurrencyRepo/src/main/resources/update.drl" );
        final org.uberfire.java.nio.file.Path nioResourcePath = .getPath( resourceUrl.toURI() );
        final Path resourcePath = .convert( nioResourcePath );
        final SessionInfo sessionInfo = mock( SessionInfo.class );
        //Force full build before attempting incremental changes
        final KieProject project = .resolveProject( resourcePath );
        final BuildResults buildResults = .build( project );
        assertNotNull( buildResults );
        assertEquals( 0,
                      buildResults.getErrorMessages().size() );
        assertEquals( 1,
                      buildResults.getInformationMessages().size() );
        //Perform incremental build
        final int THREADS = 200;
        final Result result = new Result();
        ExecutorService es = Executors.newCachedThreadPool();
        for ( int i = 0; i < THREADSi++ ) {
            switch ( i % 3 ) {
                case 0:
                    es.executenew Runnable() {
                        @Override
                        public void run() {
                            try {
                                ..println"Thread " + Thread.currentThread().getName() + " has started: BuildService.build( project )" );
                                .build( project );
                                ..println"Thread " + Thread.currentThread().getName() + " has completed." );
                            } catch ( Throwable e ) {
                                result.setFailedtrue );
                                result.setMessagee.getMessage() );
                                ..printlne.getMessage() );
                            }
                        }
                    } );
                    break;
                case 1:
                    es.executenew Runnable() {
                        @Override
                        public void run() {
                            try {
                                ..println"Thread " + Thread.currentThread().getName() + " has started: LRUProjectDataModelOracleCache.invalidateProjectCache(...)" );
                                .invalidateProjectCachenew InvalidateDMOProjectCacheEvent( sessionInfo,
                                                                                                            project,
                                                                                                            pomPath ) );
                                ..println"Thread " + Thread.currentThread().getName() + " has completed." );
                            } catch ( Throwable e ) {
                                result.setFailedtrue );
                                result.setMessagee.getMessage() );
                                ..printlne.getMessage() );
                            }
                        }
                    } );
                    break;
                default:
                    es.executenew Runnable() {
                        @Override
                        public void run() {
                            try {
                                ..println"Thread " + Thread.currentThread().getName() + " has started: LRUBuilderCache.assertBuilder( project ).getKieModuleIgnoringErrors();" );
                                .assertBuilder( project ).getKieModuleIgnoringErrors();
                                ..println"Thread " + Thread.currentThread().getName() + " has completed." );
                            } catch ( Throwable e ) {
                                result.setFailedtrue );
                                result.setMessagee.getMessage() );
                                ..printlne.getMessage() );
                            }
                        }
                    } );
            }
        }
        es.shutdown();
        try {
            es.awaitTermination( 5,
                                 . );
        } catch ( InterruptedException e ) {
        }
        if ( result.isFailed() ) {
            failresult.getMessage() );
        }
    }
    private static class Result {
        private boolean failed = false;
        private String message = "";
        public synchronized boolean isFailed() {
            return ;
        }
        public synchronized void setFailedboolean failed ) {
            this. = failed;
        }
        public synchronized String getMessage() {
            return ;
        }
        public synchronized void setMessageString message ) {
            this. = message;
        }
    }
New to GrepCode? Check out our FAQ X