📄 pastregrtest.java
字号:
final PastContentHandle handle1 = new TestPastContentHandle(remote1, id); final PastContentHandle handle2 = new TestPastContentHandle(remote2, id); sectionStart("Fetch Testing"); // Insert file stepStart("File 1 Insertion"); remote1.getStorageManager().store(id, null, file1, new TestCommand() { public void receive(Object result) throws Exception { assertTrue("Storage of file 1 should succeed", ((Boolean) result).booleanValue()); stepDone(); // Insert second file stepStart("File 2 Insertion"); remote2.getStorageManager().store(id, null, file2, new TestCommand() { public void receive(Object result) throws Exception { assertTrue("Storage of file 2 should succeed", ((Boolean) result).booleanValue()); stepDone(); // Retrieve first file stepStart("File 1 Fetch"); local.fetch(handle1, new TestCommand() { public void receive(Object result) throws Exception { assertTrue("Result should be non-null", result != null); assertEquals("Result should be correct", file1, result); assertTrue("Result should not be file 2", (!file2.equals(result))); final Object received1 = result; stepDone(); // Retrieve second file stepStart("File 2 Fetch"); local.fetch(handle2, new TestCommand() { public void receive(Object result) throws Exception { assertTrue("Result should be non-null", result != null); assertEquals("Result should be correct", file2, result); assertTrue("Result should not be file 1", (!file1.equals(result))); final Object received2 = result; stepDone(); // ensure different stepStart("File 1 and 2 Different"); assertTrue("Files should not be equal", (!received1.equals(received2))); stepDone(); // remove file stepStart("File 1 Removal"); remote1.getStorageManager().unstore(id, new TestCommand() { public void receive(Object result) throws Exception { assertTrue("Removal of file 1 should succeed", ((Boolean) result).booleanValue()); stepDone(); // remove second file stepStart("File 2 Removal"); remote2.getStorageManager().unstore(id, new TestCommand() { public void receive(Object result) throws Exception { assertTrue("Removal of file 2 should succeed", ((Boolean) result).booleanValue()); stepDone(); sectionDone(); testLookupHandles(); } }); simulate(); } }); simulate(); } }); simulate(); } }); simulate(); } }); simulate(); } }); simulate(); } /** * Tests the lookup handles function in Past. */ protected void testLookupHandles() { 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("Lookup Handles Testing"); // 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)); System.out.println("PastRegrTest.testLookupHandles() insert result.length:" + ((Boolean[]) result).length); 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 (at 1 replica) stepStart("Remote Handles Lookup - 1 Replica"); local.lookupHandles(remoteId, 1, new TestCommand() { public void receive(Object result) throws Exception { assertTrue("Replicas should not be null", result != null); assertTrue("Replicas should be handle[]", result instanceof PastContentHandle[]); assertTrue("Only 1 replica should be returned", ((PastContentHandle[]) result).length == 1); if (((PastContentHandle[]) result)[0] == null) { System.out.println("PastRegrTest.problem"); } assertEquals("Replica should be for right object", remoteId, ((PastContentHandle[]) result)[0].getId()); stepDone(); // Check file exists (at all replicas) stepStart("Remote Handles Lookup - All Replicas"); local.lookupHandles(remoteId, REPLICATION_FACTOR + 1, new TestCommand() { public void receive(Object result) throws Exception { assertTrue("Replicas should not be null", result != null); assertTrue("Replicas should be handle[]", result instanceof PastContentHandle[]); PastContentHandle[] handles = (PastContentHandle[]) result; assertTrue("All replicas should be returned", (handles.length == REPLICATION_FACTOR + 1) || ((NUM_NODES < REPLICATION_FACTOR + 1) && (handles.length) == NUM_NODES)); for (int i = 0; i < handles.length; i++) { assertTrue("Replica " + i + " should not be null", handles[i] != null); assertEquals("Replica " + i + " should be for right object", remoteId, handles[i].getId()); } for (int i = 0; i < handles.length; i++) { for (int j = 0; j < handles.length; j++) { if (i != j) { assertTrue("Handles " + handles[i] + " and " + handles[j] + " should be different", (!handles[i].getNodeHandle().getId().equals(handles[j].getNodeHandle().getId()))); } } } stepDone(); // Check file exists (at a huge number of replicas) stepStart("Remote Handles Lookup - 12 Replicas"); local.lookupHandles(remoteId, 12, new TestCommand() { public void receive(Object result) throws Exception { assertTrue("Replicas should not be null", result != null); assertTrue("Replicas should be handle[]", result instanceof PastContentHandle[]); PastContentHandle[] handles = (PastContentHandle[]) result; assertTrue("All replicas should be returned, got " + handles.length, (handles.length >= REPLICATION_FACTOR + 1) || ((NUM_NODES < REPLICATION_FACTOR + 1) && (handles.length) == NUM_NODES)); int count = 0; for (int i = 0; i < handles.length; i++) { if (handles[i] != null) { assertEquals("Replica " + i + " should be for right object", remoteId, handles[i].getId()); count++; } } assertTrue("All replicas should be returned (got " + count + "/" + (REPLICATION_FACTOR + 1) + ")", count == REPLICATION_FACTOR + 1); for (int i = 0; i < handles.length; i++) { for (int j = 0; j < handles.length; j++) { if ((i != j) && (handles[i] != null) && (handles[j] != null)) { assertTrue("Handles " + handles[i] + " and " + handles[j] + " should be different", (!handles[i].getNodeHandle().getId().equals(handles[j].getNodeHandle().getId()))); } } } stepDone(); sectionDone(); testCaching(); } }); simulate(); } }); simulate(); } }); simulate(); } }); simulate(); } /** * Tests the dynamic caching function in Past. */ protected void testCaching() { final PastImpl local = pasts[environment.getRandomSource().nextInt(NUM_NODES)]; final Id id1 = generateId(); final Id id2 = generateId(); final PastContent file1 = new TestPastContent(id1); final PastContent file2 = new TestPastContent(id2); final PastContent file3 = new NonMutableTestPastContent(id2); sectionStart("Caching Testing"); // Manually insert file stepStart("Manually Inserting Object Into Cache"); // check cache local.getStorageManager().getCache().cache(id1, null, file1, new TestCommand() { public void receive(Object result) throws Exception { assertTrue("Object should not be null", result != null); assertTrue("Object should be True", result.equals(new Boolean(true))); stepDone(); // Check file exists stepStart("Local Lookup Satisfied by Cache"); local.lookup(id1, new TestCommand() { public void receive(Object result) throws Exception { assertTrue("File should not be null", result != null); assertEquals("Lookup of file should be correct", file1, result); stepDone(); // Insert file stepStart("Caching Mutable Object"); final LookupMessage lmsg = new LookupMessage(1, id2, local.getLocalNodeHandle(), id2); lmsg.receiveResult(file2); assertTrue("Message should continue to be routed", local.forward(new TestRouteMessage(id2, null, lmsg))); stepDone(); stepStart("Cache Shouldn't Contain Object"); // check cache local.getStorageManager().getObject(id2, new TestCommand() { public void receive(Object result) throws Exception { assertTrue("Object should be null", result == null); stepDone(); stepStart("Caching Non-Mutable Object"); lmsg.receiveResult(file3); assertTrue("Message should continue to be routed", local.forward(new TestRouteMessage(id2, null, lmsg))); stepDone(); stepStart("Cache Should Contain Object"); // check cache local.getStorageManager().getObject(id2, new TestCommand() { public void receive(Object result) throws Exception { // assertTrue("Object should not be null", result != null); // assertTrue("Object should be correct", result.equals(file3)); stepDone(); // check lookup LookupMessage lmsg = new LookupMessage(-1, id2, local.getLocalNodeHandle(), id2); stepStart("Lookup Satisfied By Cache"); // assertTrue("Message should not continue to be routed", // ! local.forward(new TestRouteMessage(id2, null, lmsg))); stepDone(); sectionDone(); cleanUp(); } }); simulate(); } }); simulate(); } }); simulate(); } }); simulate(); } /** * Private method which initiates the replica maintenance on all of the nodes */ private void runReplicaMaintence() { for (int i = 0; i < NUM_NODES; i++) { pasts[i].getReplication().replicate(); } simulate(); } /** * Private method which generates a random Id * * @return A new random Id */ private Id generateId() { byte[] data = new byte[20]; environment.getRandomSource().nextBytes(data); return FACTORY.buildId(data); } /** * DESCRIBE THE METHOD */ protected void cleanUp() { running = false; environment.destroy();// synchronized(this) {// this.notifyAll();// } } /** * Usage: DistPastTest [-port p] [-bootstrap host[:port]] [-nodes n] * [-protocol (direct|socket)] [-help] * * @param args DESCRIBE THE PARAMETER * @exception Exception DESCRIBE THE EXCEPTION */ public static void main(String args[]) throws Exception {// System.setOut(new PrintStream(new FileOutputStream("pastrtest.txt")));// System.setErr(System.out);// while(true) { LinkedList delme = new LinkedList(); delme.add(new File("FreePastry-Storage-Root")); while (!delme.isEmpty()) { File f = (File) delme.removeFirst(); if (f.isDirectory()) { File[] subs = f.listFiles(); if (subs.length == 0) { f.delete(); } else { delme.addAll(Arrays.asList(subs)); delme.addLast(f); } } else { f.delete(); } } Environment env = parseArgs(args); env.getParameters().setDouble("p2p_past_successfulInsertThreshold", 1.0); PastRegrTest pastTest = new PastRegrTest(env); pastTest.start();// synchronized(pastTest) {// pastTest.wait();// }// } } /** * Common superclass for test commands. * * @version $Id: pretty.settings 2305 2005-03-11 20:22:33Z jeffh $ * @author jeffh */ protected class TestCommand implements Continuation { /** * DESCRIBE THE METHOD * * @param result DESCRIBE THE PARAMETER */ public void receiveResult(Object result) { try { receive(result); } catch (Exception e) { receiveException(e); } } /** * DESCRIBE THE METHOD *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -