Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.nesscomputing.concurrent;
Copyright (C) 2012 Ness Computing, 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.
 
 
 
 
 import org.junit.Test;
 
 
 {
     @Inject
     private Injector injector = null;
 
     private ThreadPoolExecutor unwrappedExecutor = null;
 
 
     @Before
     public void setUp()
     {
         ..changeScope(null);
 
         final Injector injector = Guice.createInjector(.,
                                                        new ThreadDelegatedScopeModule(),
                                                        new AbstractModule() {
             @Override
             public void configure() {
                 bind(ScopedObject.class).toProvider(ScopedObject.TestObjectProvider.class).in(.);
             }
         });
 
         injector.injectMembers(this);
         Assert.assertNotNull(injector);
 
          = new ThreadPoolExecutor(5, 5, 1, .new LinkedBlockingQueue<Runnable>());
         Assert.assertFalse(.isShutdown());
         Assert.assertFalse(.isTerminated());
     }
 
     @After
     public void tearDown()
     {
         Assert.assertNotNull();
         .shutdown();
          = null;
 
         Assert.assertNotNull();
         // Get rid of the stupid "duplicate Servlet module warning"
         final GuiceFilter filter = .getInstance(GuiceFilter.class);
         filter.destroy();
          = null;
 
         ..changeScope(null);
     }
 
     @Test
     public void testUnwrappedExecutor() throws Exception
     {
         final ScopedObject t1 = .getInstance(ScopedObject.class);
         Assert.assertNotNull(t1);
 
         final CountDownLatch latch = new CountDownLatch(1);
 
         final ScopedRunnable runnable = new ScopedRunnable(latch);
         .execute(runnable);
 
         Assert.assertTrue("Some threads got stuck!"latch.await(1, .));
 
         Assert.assertNotSame(t1runnable.getTestObject());
     }
    @Test
    public void testWrappedExecutor() throws Exception
    {
        final ScopedObject t1 = .getInstance(ScopedObject.class);
        Assert.assertNotNull(t1);
        final CountDownLatch latch = new CountDownLatch(1);
        final ScopedRunnable runnable = new ScopedRunnable(latch);
        ThreadDelegatingDecorator.wrapExecutor().execute(runnable);
        Assert.assertTrue("Some threads got stuck!"latch.await(1, .));
        Assert.assertSame(t1runnable.getTestObject());
    }
    @Test
    public void testMultipleUnwrappedExecutor() throws Exception
    {
        final ScopedObject t1 = .getInstance(ScopedObject.class);
        Assert.assertNotNull(t1);
        final int testCount = 10;
        final ScopedRunnable [] runnables = new ScopedRunnable[testCount];
        final CountDownLatch latch = new CountDownLatch(testCount);
        for (int i = 0; i < testCounti++) {
            runnables[i] = new ScopedRunnable(latch);
            .execute(runnables[i]);
        }
        Assert.assertTrue("Some threads got stuck!"latch.await(1, .));
        for (int i  = 0; i < testCounti++) {
            Assert.assertNotSame(t1runnables[i].getTestObject());
        }
    }
    @Test
    public void testMultipleWrappedExecutor() throws Exception
    {
        final ScopedObject t1 = .getInstance(ScopedObject.class);
        Assert.assertNotNull(t1);
        final int testCount = 10;
        final ScopedRunnable [] runnables = new ScopedRunnable[testCount];
        final CountDownLatch latch = new CountDownLatch(testCount);
        final Executor wrappedExecutor = ThreadDelegatingDecorator.wrapExecutor();
        for (int i = 0; i < testCounti++) {
            runnables[i] = new ScopedRunnable(latch);
            wrappedExecutor.execute(runnables[i]);
        }
        Assert.assertTrue("Some threads got stuck!"latch.await(10000, .));
        for (int i  = 0; i < testCounti++) {
            Assert.assertSame(t1runnables[i].getTestObject());
        }
    }
    public class ScopedRunnable implements Runnable
    {
        private volatile ScopedObject testObject;
        private final CountDownLatch latch;
        public ScopedRunnable(final CountDownLatch latch)
        {
            this. = latch;
        }
        @Override
        public void run()
        {
             = .getInstance(ScopedObject.class);
            .countDown();
        }
        public ScopedObject getTestObject()
        {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X