FileDocCategorySizeDatePackage
TestThreadSafety.javaAPI DocphoneME MR2 API (J2ME)11831Wed May 02 18:00:40 BST 2007com.sun.j2me.location

TestThreadSafety

public class TestThreadSafety extends com.sun.midp.i3test.TestCase
Thread safety tests.

Fields Summary
Constructors Summary
Methods Summary
public voidrunTests()
Entry point.

	declare("Multiple threads querying locations.");
        testMultipleGetLocation();
	declare("Provider reset while querying locations.");
	testResetInterruptedException();
	declare("Accessing landmark store from multiple threads.");
	testMultipleStoreAccess();
    
voidtestMultipleGetLocation()
Body of the test 1. This test creates several threads querying locations simultaneously. No deadlock situations or exception throws are expected.

        LocationProviderImpl[] providers = 
            LocationProviderImpl.getProviders();
        for (int i = 0; i < providers.length; i++) {
            ((PlatformLocationProvider) providers[i]).
                    i3test_setState(LocationProvider.AVAILABLE);
        }
        
	LocationQueryThread t1, t2, t3;
	t1 = t2 = t3 = null;
	try {
	    // t1 and t2 use the same timeout, t3 uses a different one
	    t1 = new LocationQueryThread(null, 2, 10000);
	    t2 = new LocationQueryThread(null, 2, 10000);
	    t3 = new LocationQueryThread(null, 3, 15000);
	    // start threads
	    t1.start();
	    t2.start();
	    t3.start();
	    // wait
	    Thread.sleep(5000);
	    // no exceptions should be thrown within threads
	    assertNull("Exception occured in thread 1", t1.getException());
	    if (t1.getException() != null) {
		t1.getException().printStackTrace();
	    }
	    assertNull("Exception occured in thread 2", t2.getException());
	    if (t2.getException() != null) {
		t2.getException().printStackTrace();
	    }
	    assertNull("Exception occured in thread 3", t3.getException());
	    if (t3.getException() != null) {
		t3.getException().printStackTrace();
	    }
	    // by this time, each thread should have at least 2 locations
	    assertTrue("Thread 1 did not receive any location",
		       t1.getLocationCount() > 0);
	    assertTrue("Thread 2 did not receive any location",
		       t2.getLocationCount() > 0);
	    assertTrue("Thread 3 did not receive any location",
		       t3.getLocationCount() > 0);
	} catch (InterruptedException e) {
	    fail("Interrupted Exception caught");
	    e.printStackTrace();
	} finally {
	    t1.interrupt();
	    t2.interrupt();
	    t3.interrupt();
	}
    
voidtestMultipleStoreAccess()
Body of the test 3. This test starts two thread which access the same landmark store and perform various actions on that store. No deadlocks should occur.

	StoreAccessThread t1, t2;
	t1 = t2 = null;
	try {
	    t1 = new StoreAccessThread(5000);
	    t2 = new StoreAccessThread(5000);
	    t1.start();
	    t2.start();
	    Thread.sleep(5500);
	    assertNull("Exception occured in thread 1", t1.getException());
	    if (t1.getException() != null) {
		t1.getException().printStackTrace();
	    }
	    assertNull("Exception occured in thread 2", t2.getException());
	    if (t2.getException() != null) {
		t2.getException().printStackTrace();
	    }
	} catch (Exception e) {
	    fail("Unexpected exception caught");
	    e.printStackTrace();
	} finally {
	    t1.interrupt();
	    t2.interrupt();
	}
    
voidtestResetInterruptedException()
Body of the test 2. This test resets the provider while several threads querying locations. This should result in InterruptedException being thrown in all threads.

        LocationProviderImpl[] providers = 
            LocationProviderImpl.getProviders();
        for (int i = 0; i < providers.length; i++) {
            ((PlatformLocationProvider) providers[i]).
                    i3test_setState(LocationProvider.AVAILABLE);
        }
	LocationQueryThread t1, t2;
	t1 = t2 = null;
	try {
	    LocationProvider lp = LocationProvider.getInstance(null);
	    // two threads seem enough
	    t1 = new LocationQueryThread(lp, 2, 10000);
	    t2 = new LocationQueryThread(lp, 3, 15000);
	    // start threads
	    t1.start();
	    t2.start();
	    // wait 1/2 of standard response time
	    Thread.sleep(
		((PlatformLocationProvider)lp).getResponseTime() * 1000 / 2);
	    lp.reset();
	    // wait for exceptions to be caught in threads
	    Thread.sleep(100);
	    assertTrue("Exception did not occur in thread 1",
			 t1.getException() instanceof InterruptedException);
	    assertTrue("Exception did not occur in thread 2",
			 t2.getException() instanceof InterruptedException);
	} catch (Exception e) {
	    assertFalse("Unexpected exception caught: " + e, true);
	} finally {
	    t1.interrupt();
	    t2.interrupt();
	}