view src/treecms/proto/test/IncrementalNodeIDTest1.java @ 30:8d733b98c5de

added Node API setLinkedNode,getLinkedNode for create link to other node. but not implemented yet.
author Shoshi TAMAKI
date Wed, 10 Nov 2010 00:36:51 +0900
parents 45881237e777
children
line wrap: on
line source

package treecms.proto.test;

/**
 * IncrementalNodeIDTest1
 * 
 * testIDstartsWithZero()
 * 	the id must be start from zero.
 * 
 * testUpdateIDFromLatestID()
 * 	check that id able to update correctly from latest node id.
 * 
 * testUpdateIDFromOldID()
 *  check that id able to update correctly from old node id.
 *  
 * testMultiThreadUpdateID()
 *  execute concurrent update using shared object.
 */

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.junit.Assert;
import org.junit.runner.JUnitCore;
import treecms.proto.api.NodeID;
import treecms.proto.id.IncrementalNodeID;

public class IncrementalNodeIDTest1
{
	public static void main(String _args[])
	{
		JUnitCore.main(IncrementalNodeIDTest1.class.getName());
	}
	
	public IncrementalNodeIDTest1()
	{
	}
	
	@Test
	public void testIDstartsWithZero()
	{
		NodeID id = new IncrementalNodeID();
		long ver = Long.parseLong(id.toString().split("@")[1]);
		Assert.assertEquals(ver,0);
	}
	
	@Test
	public void testUpdateIDFromLatestID()
	{
		int count = 100;
		NodeID id = new IncrementalNodeID();
		
		for(int i = 1;i <= count;i ++){
			id = id.update();
			long ver = Long.parseLong(id.toString().split("@")[1]);
			Assert.assertEquals(ver,i);
		}
	}
	
	@Test
	public void testUpdateIDFromOldID()
	{
		int count = 100;
		NodeID id0 = new IncrementalNodeID();
		
		for(int i = 1;i <= count;i ++){
			NodeID id = id0.update();
			long ver = Long.parseLong(id.toString().split("@")[1]);
			Assert.assertEquals(ver,i);
		}
	}
	
	@Test
	public void testMultiThreadedUpdateID()
	{
		final int threads = 10; //number of threads
		final int count = 100; //modification count
		ExecutorService service = Executors.newFixedThreadPool(threads);
		
		final NodeID id = new IncrementalNodeID();
		
		for(int i = 0;i < threads;i ++){
			service.execute(new Runnable(){
				@Override
				public void run()
				{
					for(int j = 0;j < count;j ++){
						id.update();
					}
				}
			});
		}
		
		service.shutdown();
		
		try {
			service.awaitTermination(Long.MAX_VALUE,TimeUnit.DAYS);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
		//check id
		NodeID tip = id.update();
		long ver = Long.parseLong(tip.toString().split("@")[1]);
		Assert.assertEquals(ver,threads*count + 1);
	}
}