FileDocCategorySizeDatePackage
TestSingelHostConcurrencyLock.javaAPI DocApache Lucene 2.1.04642Wed Feb 14 10:46:02 GMT 2007org.apache.lucene.gdata.storage.lucenestorage

TestSingelHostConcurrencyLock

public class TestSingelHostConcurrencyLock extends TestCase
Copyright 2004 The Apache Software Foundation 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.

Fields Summary
SingleHostConcurrentStorageLock
lock
boolean
threadResult
Constructors Summary
Methods Summary
protected voidsetUp()

         
        this.lock = (SingleHostConcurrentStorageLock)SingleHostConcurrentStorageLock.getConcurrentStorageLock();
        super.setUp();
    
protected voidtearDown()

        super.tearDown();
        threadResult = false;
        this.lock.close();
    
public voidtestClose()


    
public voidtestGetConcurrentStorageLock()

        ConcurrentStorageLock lock = SingleHostConcurrentStorageLock.getConcurrentStorageLock();
        assertEquals(lock,SingleHostConcurrentStorageLock.getConcurrentStorageLock() );
        
    
public voidtestIsKeyLocked()


    
public voidtestReleaseLock()

        final String key = "someKey";
        final String nextKey = "fooKey";
        assertTrue(lock.setLock(key));
        assertTrue(lock.isKeyLocked(key));
        assertTrue(lock.releaseLock(key));
        assertTrue(this.lock.setLock(key));
        try{
            assertTrue(lock.setLock(nextKey));
            fail("thread has already locked the key");
        }catch (Exception e) {
            // TODO: handle exception
        }
        
        Thread t = new Thread(new Runnable(){
            public void run(){
                threadResult = lock.setLock(nextKey);
            }
        });
        t.start();
        t.join(300);
        assertTrue(threadResult);
        try{
        this.lock.releaseLock(nextKey);
           fail("current thread is not owner");
        }catch (ConcurrencyException e) {
            // TODO: handle exception
        }

    
public voidtestReleaseThreadLocks()

        
    
public voidtestSetLock()

        final String key = "someKey";
        final String nextKey = "fooKey";
        assertTrue(lock.setLock(key));
        assertTrue(lock.isKeyLocked(key));
        
        try{
            this.lock.setLock(key);
            fail("thread has already locked the key");
        }catch (Exception e) {
            // TODO: handle exception
        }
        
        try{
            assertTrue(lock.setLock(nextKey));
            fail("thread has already locked the key");
        }catch (Exception e) {
            // TODO: handle exception
        }
        
        Thread t = new Thread(new Runnable(){
            public void run(){
                threadResult = lock.setLock(key);
              
                
                
            }
        });
        t.start();
        t.join(300);
        assertFalse(threadResult);
        
        t = new Thread(new Runnable(){
            public void run(){
                threadResult = lock.setLock(nextKey);
              
                
                
            }
        });
        t.start();
        t.join(300);
        assertTrue(threadResult);