Thursday, June 12, 2014

Threading Test

package ThreadingTest;

import org.testng.annotations.Test;

public class TestNGAnnotationTestMethodTimeOutExample {
@Test(timeOut = 2000)
public void timeOutTestMethodOne() throws InterruptedException {
Thread.sleep(5000);
System.out.println("TimeOut Test Method One!!");
}

@Test(timeOut = 5000)
public void timeOutTestMethodTwo() throws InterruptedException {
Thread.sleep(3000);
System.out.println("TimeOut Test Method Two!!");
}

@Test(timeOut = 2000)
public void timeOutTestMethodRunForEver() {
while (true) {
// Do nothing
}
}
}



package ThreadingTest;

import org.testng.annotations.*;

public class TestNGTimeTest {

@Test(timeOut = 1000, invocationCount = 100, successPercentage = 98)
public void testInvocationCount() throws Exception {
Thread.sleep(100);
System.out.println("waitForAnswer");
}
}


package ThreadingTest;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.testng.annotations.Test;

public class ThreadExecutorServiceDemo {
private static final String ERROR_MSG = "From thread you asked me throw an exception";

@Test
public void testUsingExecutorService() throws InterruptedException,
ExecutionException {
MyCallableService service = new MyCallableService(false);
Future<String> returnValue = Executors
.newSingleThreadScheduledExecutor().submit(service);
System.out.println(returnValue.get());
}

@Test(expectedExceptions = ExecutionException.class, expectedExceptionsMessageRegExp = ".*"
+ ERROR_MSG + ".*")
public void testUsingExecutorServiceWithException()
throws InterruptedException, ExecutionException {
MyCallableService anotherService = new MyCallableService(true);
Future<String> anotherReturnValue = Executors
.newSingleThreadScheduledExecutor().submit(anotherService);
System.out.println(anotherReturnValue.get());
}

@Test
public void testUsingThreads() throws InterruptedException {
MyThreadService mt = new MyThreadService(false);
mt.start();
while (mt.isAlive() == true) {
Thread.sleep(10000);
}
System.out.println(mt.getServiceName());
}

@Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = ERROR_MSG)
public void testUsingThreadsWithExceptions() throws InterruptedException {
MyThreadService mt = new MyThreadService(true);
mt.start();
while (mt.isAlive() == true) {
Thread.sleep(10000);
}
System.out.println(mt.getServiceName());
}

public class MyThreadService extends Thread {
private boolean throwException;
private String serviceName;

public String getServiceName() {
return serviceName;
}

public MyThreadService(boolean throwException) {
this.throwException = throwException;
}

public void run() {
try {
sleep(25000);
this.serviceName = "TestNG threaded Service";
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (throwException) {
throw new RuntimeException(ERROR_MSG);
}
}
}

public class MyCallableService implements Callable<String> {
private boolean throwException;

public MyCallableService(boolean throwException) {
this.throwException = throwException;
}

@Override
public String call() throws Exception {
Thread.sleep(25000);
if (throwException) {
throw new RuntimeException(ERROR_MSG);
}
return "Callable Service Invoked";
}
}
}


package ThreadingTest;

import org.testng.annotations.Test;

public class ThreadProgram1 {

@Test(threadPoolSize = 3, invocationCount = 20)
public void concurrencyTest() {
System.out.print(" " + Thread.currentThread().getId());
}

}


package ThreadingTest;

import org.testng.annotations.Test;

public class ThreadUnsafe {
private static int[] accounts = new int[] { 0, 0 };
private static int MAX = 1000;

@Test(threadPoolSize = 1000, invocationCount = 1000, timeOut = 2000)
public void swap() {
int amount = (int) (MAX * Math.random());
accounts[1] += amount;
accounts[0] -= amount;
System.out.println("Account[0]: " + accounts[0]);
System.out.println("Account[1]: " + accounts[1]);
int balance = checkBalance();
assert (balance == 0);
}

public int checkBalance() {
int sum = 0;
for (int i = 0; i < accounts.length; i++) {
sum += accounts[i];
}
System.out.println("Balance : " + sum);
return sum;
}

public static void main(String[] args) {
ThreadUnsafe tu = new ThreadUnsafe();
tu.swap();
}
}

No comments:

Post a Comment