📄 rawpastregrtest.java
字号:
/*************************************************************************"FreePastry" Peer-to-Peer Application Development Substrate Copyright 2002, Rice University. All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions aremet:- Redistributions of source code must retain the above copyrightnotice, this list of conditions and the following disclaimer.- Redistributions in binary form must reproduce the above copyrightnotice, this list of conditions and the following disclaimer in thedocumentation and/or other materials provided with the distribution.- Neither the name of Rice University (RICE) nor the names of itscontributors may be used to endorse or promote products derived fromthis software without specific prior written permission.This software is provided by RICE and the contributors on an "as is"basis, without any representations or warranties of any kind, expressor implied including, but not limited to, representations orwarranties of non-infringement, merchantability or fitness for aparticular purpose. In no event shall RICE or contributors be liablefor any direct, indirect, incidental, special, exemplary, orconsequential damages (including, but not limited to, procurement ofsubstitute goods or services; loss of use, data, or profits; orbusiness interruption) however caused and on any theory of liability,whether in contract, strict liability, or tort (including negligenceor otherwise) arising in any way out of the use of this software, evenif advised of the possibility of such damage.********************************************************************************/package rice.p2p.past.testing;import rice.*;import rice.environment.Environment;import rice.p2p.commonapi.*;import rice.p2p.commonapi.rawserialization.*;import rice.p2p.commonapi.testing.*;import rice.p2p.past.*;import rice.p2p.past.messaging.*;import rice.p2p.past.rawserialization.*;import rice.p2p.replication.*;import rice.persistence.*;import java.io.*;import java.util.*;import java.net.*;import java.io.Serializable;/** * @(#) PastRegrTest.java Provides regression testing for the Past service * * @version $Id: PastRegrTest.java 3157 2006-03-19 12:16:58Z jeffh $ * @author Alan Mislove */public class RawPastRegrTest extends CommonAPITest { // the storage services in the ring /** * DESCRIBE THE FIELD */ protected StorageManager storages[]; // the past impls in the ring /** * DESCRIBE THE FIELD */ protected PastImpl pasts[]; /** * DESCRIBE THE FIELD */ protected boolean running = true; // the instance name to use /** * DESCRIBE THE FIELD */ public static String INSTANCE = "PastRegrTest"; // the replication factor in Past /** * DESCRIBE THE FIELD */ public static int REPLICATION_FACTOR = 3; /** * Constructor which sets up all local variables * * @param env DESCRIBE THE PARAMETER * @exception IOException DESCRIBE THE EXCEPTION */ public RawPastRegrTest(Environment env) throws IOException { super(env); pasts = new PastImpl[NUM_NODES]; storages = new StorageManager[NUM_NODES]; if (PROTOCOL == PROTOCOL_DIRECT) { new Thread() { public void run() { while (running) { try { sleep(50); simulate(); } catch (Exception e) { System.out.println(e + " blah"); } } } }.start(); } } /** * Method which should process the given newly-created node * * @param node The newly created node * @param num The number of this node */ protected void processNode(int num, Node node) { try { storages[num] = new StorageManagerImpl(FACTORY, new PersistentStorage(FACTORY, "root-" + num, ".", 1000000, environment), new LRUCache(new MemoryStorage(FACTORY), 100000, environment)); pasts[num] = new PastImpl(node, storages[num], REPLICATION_FACTOR, INSTANCE); pasts[num].setContentDeserializer( new PastContentDeserializer() { public PastContent deserializePastContent(InputBuffer buf, Endpoint endpoint, short contentType) throws IOException { switch (contentType) { case TestPastContent.TYPE: return new TestPastContent(buf, endpoint, this); case VersionedTestPastContent.TYPE: return new VersionedTestPastContent(buf, endpoint, this); } throw new IllegalArgumentException("Unknown type:" + contentType); } }); } catch (IOException e) { throw new RuntimeException(e); } pasts[num].setContentHandleDeserializer( new PastContentHandleDeserializer() { public PastContentHandle deserializePastContentHandle(InputBuffer buf, Endpoint endpoint, short contentType) throws IOException { switch (contentType) { case TestPastContentHandle.TYPE: return new TestPastContentHandle(buf, endpoint); } throw new IllegalArgumentException("Unknown type:" + contentType); } }); } /** * Method which should run the test - this is called once all of the nodes * have been created and are ready. */ protected void runTest() { if (NUM_NODES < 2) { System.out.println("The DistPastRegrTest must be run with at least 2 nodes for proper testing. Use the '-nodes n' to specify the number of nodes."); return; } // Run each test testRouteRequest(); } /* * ---------- Test methods and classes ---------- */ /** * Tests routing a Past request to a particular node. */ protected void testRouteRequest() { final PastImpl local = pasts[environment.getRandomSource().nextInt(NUM_NODES)]; final PastImpl remote = pasts[environment.getRandomSource().nextInt(NUM_NODES)]; final Id remoteId = remote.getLocalNodeHandle().getId(); final PastContent file = new TestPastContent(remoteId); sectionStart("Simple Route Request"); // Check file doesn't exist stepStart("Initial Lookup"); local.lookup(remoteId, new TestCommand() { public void receive(Object result) throws Exception { assertTrue("File returned should be null", result == null); stepDone(); // Insert file stepStart("File Insertion"); local.insert(file, new TestCommand() { public void receive(Object result) throws Exception { assertTrue("Insert of file result should not be null", result != null); assertTrue("Insert of file should return Boolean[]", result instanceof Boolean[]); // assertTrue("Insert of file should return correct sized Boolean[]", (((Boolean[]) result).length == REPLICATION_FACTOR) || // ((NUM_NODES < REPLICATION_FACTOR) && // (((Boolean[]) result).length) == NUM_NODES)); for (int i = 0; i < ((Boolean[]) result).length; i++) { assertTrue("Insert of file should not return null at replica", ((Boolean[]) result)[i] != null); assertTrue("Insert of file should succeed at replica", ((Boolean[]) result)[i].booleanValue()); } stepDone(); // run replica maintenance runReplicaMaintence(); // Check file exists stepStart("Remote File Lookup"); local.lookup(remoteId, new TestCommand() { public void receive(Object result) throws Exception { assertTrue("File should not be null", result != null); assertEquals("Lookup of file should be correct", file, result); stepDone(); // Lookup file locally stepStart("Local File Lookup"); remote.getStorageManager().getObject(remoteId, new TestCommand() { public void receive(Object result) throws Exception { assertTrue("File should be inserted at known node", result != null); assertEquals("Retrieved local file should be the same", file, result); stepDone(); sectionDone(); testVersionControl(); } }); simulate(); } }); simulate(); } }); simulate(); } }); simulate(); } /** * Tests overwriting an exiting object with a new one */ protected void testVersionControl() { final PastImpl local = pasts[environment.getRandomSource().nextInt(NUM_NODES)]; final PastImpl remote = pasts[environment.getRandomSource().nextInt(NUM_NODES)]; final Id remoteId = remote.getLocalNodeHandle().getId(); final PastContent oldFile = new VersionedTestPastContent(remoteId, 0); final PastContent newFile = new VersionedTestPastContent(remoteId, 1); final PastContent newNewFile = new NonOverwritingTestPastContent(remoteId, 2); sectionStart("Version Control"); // Insert file stepStart("File Insertion"); local.insert(oldFile, new TestCommand() { public void receive(Object result) throws Exception { assertTrue("Insert of file result should not be null", result != null); assertTrue("Insert of file should return Boolean[]", result instanceof Boolean[]); // assertTrue("Insert of file should return correct sized Boolean[]", (((Boolean[]) result).length == REPLICATION_FACTOR) || // ((NUM_NODES < REPLICATION_FACTOR) && // (((Boolean[]) result).length) == NUM_NODES)); for (int i = 0; i < ((Boolean[]) result).length; i++) { assertTrue("Insert of file should not return null at replica", ((Boolean[]) result)[i] != null); assertTrue("Insert of file should succeed at replica", ((Boolean[]) result)[i].booleanValue()); } stepDone(); // run replica maintenance runReplicaMaintence(); // Check file exists stepStart("Remote File Lookup"); local.lookup(remoteId, new TestCommand() { public void receive(Object result) throws Exception { assertTrue("File should not be null", result != null); assertEquals("Lookup of file should be correct", oldFile, result); stepDone(); // Insert overwriting file stepStart("Overwriting File Insertion"); local.insert(newFile, new TestCommand() { public void receive(Object result) throws Exception { assertTrue("Insert of file result should not be null", result != null); assertTrue("Insert of file should return Boolean[]", result instanceof Boolean[]); // assertTrue("Insert of file should return correct sized Boolean[]", (((Boolean[]) result).length == REPLICATION_FACTOR) || // ((NUM_NODES < REPLICATION_FACTOR) && // (((Boolean[]) result).length) == NUM_NODES)); for (int i = 0; i < ((Boolean[]) result).length; i++) { assertTrue("Insert of file should not return null at replica", ((Boolean[]) result)[i] != null); assertTrue("Insert of file should succeed at replica", ((Boolean[]) result)[i].booleanValue()); } stepDone(); // run replica maintenance runReplicaMaintence(); // Check correct file exists stepStart("Remote Overwriting File Lookup"); local.lookup(remoteId, new TestCommand() { public void receive(Object result) throws Exception { assertTrue("Overwriting file should not be null", result != null); assertEquals("Lookup of overwriting file should be correct version", newFile, result); stepDone(); // Insert overwriting file stepStart("Non-overwriting File Insertion"); local.insert(newNewFile, new TestCommand() { public void receive(Object result) throws Exception { assertTrue("Insert of file result should not be null", result != null); assertTrue("Insert of file should return Boolean[]", result instanceof Boolean[]); // assertTrue("Insert of file should return correct sized Boolean[]", (((Boolean[]) result).length == REPLICATION_FACTOR) || // ((NUM_NODES < REPLICATION_FACTOR) && // (((Boolean[]) result).length) == NUM_NODES)); for (int i = 0; i < ((Boolean[]) result).length; i++) { assertTrue("Insert of file should not return null at replica", ((Boolean[]) result)[i] != null); assertTrue("Insert of file should succeed at replica", ((Boolean[]) result)[i].booleanValue()); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -