|
From: Bryan T. <tho...@us...> - 2007-04-23 13:09:35
|
Update of /cvsroot/cweb/bigdata/src/test/com/bigdata/service In directory sc8-pr-cvs4.sourceforge.net:/tmp/cvs-serv20436/src/test/com/bigdata/service Modified Files: TestDataServer0.java TestAll.java Added Files: AbstractServerTestCase.java TestMetadataServer0.java Log Message: The metadata server now tracks join/leaves of data services within a managed set of registrars and has a test case for this feature. --- NEW FILE: AbstractServerTestCase.java --- /** The Notice below must appear in each file of the Source Code of any copy you distribute of the Licensed Product. Contributors to any Modifications may add their own copyright notices to identify their own contributions. License: The contents of this file are subject to the CognitiveWeb Open Source License Version 1.1 (the License). You may not copy or use this file, in either source code or executable form, except in compliance with the License. You may obtain a copy of the License from http://www.CognitiveWeb.org/legal/license/ Software distributed under the License is distributed on an AS IS basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyrights: Portions created by or assigned to CognitiveWeb are Copyright (c) 2003-2003 CognitiveWeb. All Rights Reserved. Contact information for CognitiveWeb is available at http://www.CognitiveWeb.org Portions Copyright (c) 2002-2003 Bryan Thompson. Acknowledgements: Special thanks to the developers of the Jabber Open Source License 1.0 (JOSL), from which this License was derived. This License contains terms that differ from JOSL. Special thanks to the CognitiveWeb Open Source Contributors for their suggestions and support of the Cognitive Web. Modifications: */ /* * Created on Apr 22, 2007 */ package com.bigdata.service; import java.io.IOException; import java.net.InetAddress; import junit.framework.AssertionFailedError; import junit.framework.TestCase2; import net.jini.core.discovery.LookupLocator; import net.jini.core.lookup.ServiceID; import net.jini.core.lookup.ServiceRegistrar; import net.jini.core.lookup.ServiceTemplate; /** * @author <a href="mailto:tho...@us...">Bryan Thompson</a> * @version $Id$ */ public abstract class AbstractServerTestCase extends TestCase2 { /** * */ public AbstractServerTestCase() { } /** * @param arg0 */ public AbstractServerTestCase(String arg0) { super(arg0); } /** * Return the {@link ServiceID} of a server that we started ourselves. The * method waits until the {@link ServiceID} becomes available on * {@link AbstractServer#getServiceID()}. * * @exception AssertionFailedError * If the {@link ServiceID} can not be found after a timeout. * * @exception InterruptedException * if the thread is interrupted while it is waiting to retry. */ protected ServiceID getServiceID(AbstractServer server) throws AssertionFailedError, InterruptedException { ServiceID serviceID = null; for(int i=0; i<10 && serviceID == null; i++) { /* * Note: This can be null since the serviceID is not assigned * synchonously by the registrar. */ serviceID = server.getServiceID(); if(serviceID == null) { /* * We wait a bit and retry until we have it or timeout. */ Thread.sleep(200); } } assertNotNull("serviceID",serviceID); return serviceID; } /** * Lookup a {@link DataService} by its {@link ServiceID} using unicast * discovery on localhost. * * @param serviceID * The {@link ServiceID}. * * @return The service. * * @todo Modify to return the service item? * * @todo Modify to not be specific to {@link DataService} vs * {@link MetadataService} (we need a common base interface for both * that carries most of the functionality but allows us to make * distinctions easily during discovery). */ public IDataService lookupDataService(ServiceID serviceID) throws IOException, ClassNotFoundException, InterruptedException { /* * Lookup the discover service (unicast on localhost). */ // get the hostname. InetAddress addr = InetAddress.getLocalHost(); String hostname = addr.getHostName(); // Find the service registrar (unicast protocol). final int timeout = 4*1000; // seconds. System.err.println("hostname: "+hostname); LookupLocator lookupLocator = new LookupLocator("jini://"+hostname); ServiceRegistrar serviceRegistrar = lookupLocator.getRegistrar( timeout ); /* * Prepare a template for lookup search. * * Note: The client needs a local copy of the interface in order to be * able to invoke methods on the service without using reflection. The * implementation class will be downloaded from the codebase identified * by the server. */ ServiceTemplate template = new ServiceTemplate(// /* * use this to request the service by its serviceID. */ serviceID, /* * Use this to filter services by an interface that they expose. */ // new Class[] { IDataService.class }, null, /* * use this to filter for services by Entry attributes. */ null); /* * Lookup a service. This can fail if the service registrar has not * finished processing the service registration. If it does, you can * generally just retry the test and it will succeed. However this * points out that the client may need to wait and retry a few times if * you are starting everthing up at once (or just register for * notification events for the service if it is not found and enter a * wait state). */ IDataService service = null; for (int i = 0; i < 10 && service == null; i++) { service = (IDataService) serviceRegistrar .lookup(template /* , maxMatches */); if (service == null) { System.err.println("Service not found: sleeping..."); Thread.sleep(200); } } if(service!=null) { System.err.println("Service found."); } return service; } } --- NEW FILE: TestMetadataServer0.java --- /** The Notice below must appear in each file of the Source Code of any copy you distribute of the Licensed Product. Contributors to any Modifications may add their own copyright notices to identify their own contributions. License: The contents of this file are subject to the CognitiveWeb Open Source License Version 1.1 (the License). You may not copy or use this file, in either source code or executable form, except in compliance with the License. You may obtain a copy of the License from http://www.CognitiveWeb.org/legal/license/ Software distributed under the License is distributed on an AS IS basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyrights: Portions created by or assigned to CognitiveWeb are Copyright (c) 2003-2003 CognitiveWeb. All Rights Reserved. Contact information for CognitiveWeb is available at http://www.CognitiveWeb.org Portions Copyright (c) 2002-2003 Bryan Thompson. Acknowledgements: Special thanks to the developers of the Jabber Open Source License 1.0 (JOSL), from which this License was derived. This License contains terms that differ from JOSL. Special thanks to the CognitiveWeb Open Source Contributors for their suggestions and support of the Cognitive Web. Modifications: */ /* * Created on Apr 22, 2007 */ package com.bigdata.service; import net.jini.core.lookup.ServiceID; /** * Test ability to launch, register, discover and use a {@link MetadataService} * * @author <a href="mailto:tho...@us...">Bryan Thompson</a> * @version $Id$ */ public class TestMetadataServer0 extends AbstractServerTestCase { /** * */ public TestMetadataServer0() { } /** * @param arg0 */ public TestMetadataServer0(String arg0) { super(arg0); } MetadataServer metadataServer0; DataServer dataServer1; /** * Starts a {@link DataServer} ({@link #dataServer1}) and then a * {@link MetadataServer} ({@link #metadataServer0}). Each runs in its own * thread. */ public void setUp() throws Exception { /* * Start up a data server before the metadata server so that we can make * sure that it is detected by the metadata server once it starts up. */ dataServer1 = new DataServer( new String[] { "src/resources/config/standalone/DataServer1.config" }); new Thread() { public void run() { dataServer1.run(); } }.start(); /* * Start the metadata server. */ metadataServer0 = new MetadataServer( new String[] { "src/resources/config/standalone/MetadataServer0.config" }); new Thread() { public void run() { metadataServer0.run(); } }.start(); } /** * destroy the test services. */ public void tearDown() throws Exception { metadataServer0.destroy(); dataServer1.destroy(); } /** * Test the ability to discover the {@link MetadataService} and the ability * of the {@link MetadataServer} to track {@link DataService}s. * <p> * Note: We start a data service both before and after the metadata server * and verify that both wind up in the service cache and that the metadata * server itself does not wind up in the cache since it should be excluded * by the service item filter. */ public void test_serverRunning() throws Exception { ServiceID dataService1ID = getServiceID(dataServer1); ServiceID metadataServiceID = getServiceID(metadataServer0); final IMetadataService proxy = (IMetadataService) lookupDataService(metadataServiceID); assertNotNull("service not discovered", proxy); /* * Start a data service and verify that the metadata service will * discover it. */ final DataServer dataServer0 = new DataServer( new String[] { "src/resources/config/standalone/DataServer0.config" }); ServiceID dataService0ID = null; try { new Thread() { public void run() { dataServer0.run(); } }.start(); /* * wait until we get the serviceID as an indication that the data * service is running. */ dataService0ID = getServiceID(dataServer0); /* * verify that both data services were discovered by the metadata * server. */ System.err.println("Sleeping"); Thread.sleep(500); assertNotNull(metadataServer0.getDataServiceByID(dataService0ID)); assertNotNull(metadataServer0.getDataServiceByID(dataService1ID)); assertEquals("#dataServices", 2, metadataServer0 .getDataServiceCount()); } finally { /* * Destroy one of the data services and verify that the metadata * server notices this event. */ System.err.println("Destroying DataServer0"); dataServer0.destroy(); if (dataService0ID != null) { System.err.println("Sleeping"); Thread.sleep(500); assertEquals("#dataServices", 1, metadataServer0 .getDataServiceCount()); assertNull(metadataServer0.getDataServiceByID(dataService0ID)); assertNotNull(metadataServer0 .getDataServiceByID(dataService1ID)); } } } } Index: TestAll.java =================================================================== RCS file: /cvsroot/cweb/bigdata/src/test/com/bigdata/service/TestAll.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** TestAll.java 20 Apr 2007 16:36:27 -0000 1.2 --- TestAll.java 23 Apr 2007 13:09:30 -0000 1.3 *************** *** 76,79 **** --- 76,85 ---- suite.addTestSuite( TestDataServer0.class ); + /* + * Test of a single client talking to a single metadata service + * instance. + */ + suite.addTestSuite( TestMetadataServer0.class ); + // suite.addTestSuite( TestServer.class ); // Does not implement TestCase. Index: TestDataServer0.java =================================================================== RCS file: /cvsroot/cweb/bigdata/src/test/com/bigdata/service/TestDataServer0.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** TestDataServer0.java 21 Apr 2007 10:37:17 -0000 1.3 --- TestDataServer0.java 23 Apr 2007 13:09:30 -0000 1.4 *************** *** 48,66 **** package com.bigdata.service; - import java.io.IOException; - import java.net.InetAddress; import java.util.UUID; - import junit.framework.AssertionFailedError; - import junit.framework.TestCase2; - import net.jini.core.discovery.LookupLocator; import net.jini.core.lookup.ServiceID; - import net.jini.core.lookup.ServiceRegistrar; - import net.jini.core.lookup.ServiceTemplate; import com.bigdata.btree.IIndex; import com.bigdata.journal.IIndexStore; - /** * Test of client-server communications. The test starts a {@link DataServer} --- 48,58 ---- *************** *** 74,86 **** * and the service to agree on interface definitions, etc. You can use * <code>build.xml</code> in the root of this module to update that JAR. * * @author <a href="mailto:tho...@us...">Bryan Thompson</a> * @version $Id$ - * - * @todo write another test class that accesses more than one - * {@link DataService} instance, e.g., by placing a different index on - * each {@link DataService}. */ ! public class TestDataServer0 extends TestCase2 { /** --- 66,79 ---- * and the service to agree on interface definitions, etc. You can use * <code>build.xml</code> in the root of this module to update that JAR. + * <p> + * Note: You MUST grant sufficient permissions for the tests to execute, e.g., + * <pre> + * -Djava.security.policy=policy.all + * </pre> * * @author <a href="mailto:tho...@us...">Bryan Thompson</a> * @version $Id$ */ ! public class TestDataServer0 extends AbstractServerTestCase { /** *************** *** 97,104 **** } - String[] args = new String[]{ - "src/resources/config/standalone/DataServer0.config" - }; - DataServer dataServer0; --- 90,93 ---- *************** *** 106,110 **** public void setUp() throws Exception { ! dataServer0 = new DataServer(args); new Thread() { --- 95,101 ---- public void setUp() throws Exception { ! dataServer0 = new DataServer(new String[]{ ! "src/resources/config/standalone/DataServer0.config" ! }); new Thread() { *************** *** 130,174 **** /** - * Return the {@link ServiceID} of the specific service that we launched in - * #setUp(). - * - * @exception AssertionFailedError - * If the {@link ServiceID} can not be found after a timeout. - * - * @exception InterruptedException - * if the thread is interrupted while it is waiting to retry. - */ - protected ServiceID getServiceID() throws AssertionFailedError, InterruptedException { - - ServiceID serviceID = null; - - for(int i=0; i<10 && serviceID == null; i++) { - - /* - * Note: This can be null since the serviceID is not assigned - * synchonously by the registrar. - */ - - serviceID = dataServer0.getServiceID(); - - if(serviceID == null) { - - /* - * We wait a bit and retry until we have it or timeout. - */ - - Thread.sleep(200); - - } - - } - - assertNotNull("serviceID",serviceID); - - return serviceID; - - } - - /** * Exercises the basic features of the {@link IDataService} interface using * unisolated operations, including creating a B+Tree, insert, contains, --- 121,124 ---- *************** *** 177,186 **** * * @throws Exception - * - * @todo test {@link IDataService#submit(long, IProcedure)}. */ public void test_serverRunning() throws Exception { ! ServiceID serviceID = getServiceID(); final IDataService proxy = lookupDataService(serviceID); --- 127,134 ---- * * @throws Exception */ public void test_serverRunning() throws Exception { ! ServiceID serviceID = getServiceID(dataServer0); final IDataService proxy = lookupDataService(serviceID); *************** *** 308,393 **** } - /** - * Lookup a {@link DataService} by its {@link ServiceID}. - * - * @param serviceID - * The {@link ServiceID}. - */ - public IDataService lookupDataService(ServiceID serviceID) - throws IOException, ClassNotFoundException, InterruptedException { - - /* - * Lookup the discover service (unicast on localhost). - */ - - // get the hostname. - InetAddress addr = InetAddress.getLocalHost(); - String hostname = addr.getHostName(); - - // Find the service registrar (unicast protocol). - final int timeout = 4*1000; // seconds. - System.err.println("hostname: "+hostname); - LookupLocator lookupLocator = new LookupLocator("jini://"+hostname); - ServiceRegistrar serviceRegistrar = lookupLocator.getRegistrar( timeout ); - - /* - * Prepare a template for lookup search. - * - * Note: The client needs a local copy of the interface in order to be - * able to invoke methods on the service without using reflection. The - * implementation class will be downloaded from the codebase identified - * by the server. - */ - ServiceTemplate template = new ServiceTemplate(// - /* - * use this to request the service by its serviceID. - */ - serviceID, - /* - * Use this to filter services by an interface that they expose. - */ - // new Class[] { IDataService.class }, - null, - /* - * use this to filter for services by Entry attributes. - */ - null); - - /* - * Lookup a service. This can fail if the service registrar has not - * finished processing the service registration. If it does, you can - * generally just retry the test and it will succeed. However this - * points out that the client may need to wait and retry a few times if - * you are starting everthing up at once (or just register for - * notification events for the service if it is not found and enter a - * wait state). - */ - - IDataService service = null; - - for (int i = 0; i < 10 && service == null; i++) { - - service = (IDataService) serviceRegistrar - .lookup(template /* , maxMatches */); - - if (service == null) { - - System.err.println("Service not found: sleeping..."); - - Thread.sleep(200); - - } - - } - - if(service!=null) { - - System.err.println("Service found."); - - } - - return service; - - } - } --- 256,258 ---- |