All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gemstone.gemfire.internal.util.SingletonCallableJUnitTest Maven / Gradle / Ivy

There is a newer version: 2.0-BETA
Show newest version
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * 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. See accompanying
 * LICENSE file.
 */
package com.gemstone.gemfire.internal.util;

import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

import junit.framework.TestCase;

/**
 * Test for {@link SingletonCallable}
 * 
 * @author sbawaskar
 */
public class SingletonCallableJUnitTest extends TestCase {

  public void testAllThreadsThrowException() throws Exception {
    final SingletonCallable callable = new SingletonCallable();
    // start 10 threads
    int numThreads = 10;
    Thread[] threads = new Thread[numThreads];
    final AtomicInteger count = new AtomicInteger();
    final AtomicInteger numExceptions = new AtomicInteger();
    final CountDownLatch waitForAllThreads = new CountDownLatch(1);
    
    for (int i=0; i() {
              @Override
              public Boolean call() throws Exception {
                count.incrementAndGet();
                Thread.sleep(500);
                throw new IOException("SWAP");
              }
            });
          } catch (Exception e) {
            // expected
            numExceptions.incrementAndGet();
          }
        }
      });
      threads[i] = t;
      t.start();
    }
    waitForAllThreads.countDown();
    
    for (int i=0; i callable = new SingletonCallable();
    // start 10 threads
    int numThreads = 10;
    Thread[] threads = new Thread[numThreads];
    final AtomicInteger count = new AtomicInteger();
    final CountDownLatch waitForAllThreads = new CountDownLatch(1);
    
    for (int i=0; i() {
              @Override
              public Integer call() throws Exception {
                Thread.sleep(500);
                return count.incrementAndGet();
              }
            });
          } catch (Exception e) {
            // expected
          }
        }
      });
      threads[i] = t;
      t.start();
    }
    waitForAllThreads.countDown();
    
    for (int i=0; i extends SingletonCallable {
    @Override
    public boolean ignoreException(Exception e) {
      return true;
    }
  }

  public void testExOverride() throws Exception {
    final SingletonCallableNoException callable = new SingletonCallableNoException();
    // start 10 threads
    int numThreads = 10;
    Thread[] threads = new Thread[numThreads];
    final AtomicInteger count = new AtomicInteger();
    final AtomicInteger numExceptions = new AtomicInteger();
    final CountDownLatch waitForAllThreads = new CountDownLatch(1);
    
    for (int i=0; i() {
              @Override
              public Boolean call() throws Exception {
                count.incrementAndGet();
                Thread.sleep(500);
                throw new IOException("SWAP");
              }
            });
          } catch (Exception e) {
            // expected
            numExceptions.incrementAndGet();
          }
        }
      });
      threads[i] = t;
      t.start();
    }
    waitForAllThreads.countDown();
    
    for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy