⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scriberegrtest.java

📁 pastry的java实现的2.0b版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*************************************************************************"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.scribe.testing;import java.io.IOException;import java.util.*;import rice.environment.Environment;import rice.environment.params.simple.SimpleParameters;import rice.environment.time.simulated.DirectTimeSource;import rice.p2p.commonapi.*;import rice.p2p.commonapi.rawserialization.*;import rice.p2p.commonapi.testing.CommonAPITest;import rice.p2p.scribe.*;import rice.p2p.scribe.messaging.SubscribeMessage;/** * @(#) DistScribeRegrTest.java Provides regression testing for the Scribe * service using distributed nodes. * * @version $Id: ScribeRegrTest.java 3274 2006-05-15 16:17:47Z jeffh $ * @author Alan Mislove */public class ScribeRegrTest extends CommonAPITest {  // the scribe impls in the ring  /**   * DESCRIBE THE FIELD   */  protected ScribeImpl[] scribes;  /**   * The scribe policies   */  protected TestScribePolicy policies[];  // the instance name to use  /**   * DESCRIBE THE FIELD   */  public static String INSTANCE = "ScribeRegrTest";  /**   * Constructor which sets up all local variables   *   * @param env DESCRIBE THE PARAMETER   * @exception IOException DESCRIBE THE EXCEPTION   */  public ScribeRegrTest(Environment env) throws IOException {    super(env);    scribes = new ScribeImpl[NUM_NODES];    policies = new TestScribePolicy[NUM_NODES];  }  /**   * 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) {    scribes[num] = new ScribeImpl(node, INSTANCE);    policies[num] = new TestScribePolicy(scribes[num]);    scribes[num].setPolicy(policies[num]);  }  /**   * 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 DistScribeRegrTest 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    testBasic(1, "Basic");    testBasic(2, "Partial (1)");    testBasic(4, "Partial (2)");    testSingleRoot("Single rooted Trees");    testAPI();    testFailureNotification();    testMaintenance();  }  /*   *  ---------- Test methods and classes ----------   */  /**   * Tests basic functionality   *   * @param skip DESCRIBE THE PARAMETER   * @param name DESCRIBE THE PARAMETER   */  protected void testBasic(int skip, String name) {    sectionStart(name + " Scribe Networks");    int NUM_MESSAGES = 5;    int SKIP = skip;    Topic topic = new Topic(generateId());    TestScribeClient[] clients = new TestScribeClient[NUM_NODES / SKIP];    stepStart(name + " Tree Construction");    for (int i = 0; i < NUM_NODES / SKIP; i++) {      clients[i] = new TestScribeClient(scribes[i], topic, i);      scribes[i].subscribe(topic, clients[i]);      simulate();    }    int numWithParent = 0;    for (int i = 0; i < NUM_NODES / SKIP; i++) {      if (scribes[i].getParent(topic) != null) {        numWithParent++;      }    }    if (numWithParent < (NUM_NODES / SKIP) - 1) {      stepDone(FAILURE, "Expected at least " + (NUM_NODES / SKIP - 1) + " nodes with parents, found " + numWithParent);    } else {      stepDone(SUCCESS);    }    stepStart(name + " Publish");    ScribeImpl local = scribes[environment.getRandomSource().nextInt(NUM_NODES / SKIP)];    for (int i = 0; i < NUM_MESSAGES; i++) {      local.publish(topic, new TestScribeContent(topic, i));      simulate();    }    boolean failed = false;    for (int i = 0; i < NUM_NODES / SKIP; i++) {      if (clients[i].getPublishMessages().length != NUM_MESSAGES) {        stepDone(FAILURE, "Expected client " + clients[i] + " to receive all messages, received " + clients[i].getPublishMessages().length);        failed = true;      }    }    if (!failed) {      stepDone(SUCCESS);    }    stepStart(name + " Anycast - No Accept");    local = scribes[environment.getRandomSource().nextInt(NUM_NODES)];    local.anycast(topic, new TestScribeContent(topic, 59));    simulate();    failed = false;    for (int i = 0; i < NUM_NODES / SKIP; i++) {      if (clients[i].getAnycastMessages().length != 0) {        stepDone(FAILURE, "Expected no accepters for anycast, found one at " + scribes[i]);        failed = true;      }    }    if (!failed) {      stepDone(SUCCESS);    }    stepStart(name + " Anycast - 1 Accept");    TestScribeClient client = clients[environment.getRandomSource().nextInt(NUM_NODES / SKIP)];    client.acceptAnycast(true);    local = scribes[environment.getRandomSource().nextInt(NUM_NODES)];    local.anycast(topic, new TestScribeContent(topic, 59));    simulate();    failed = false;    for (int i = 0; i < NUM_NODES / SKIP; i++) {      if (clients[i].equals(client)) {        if (clients[i].getAnycastMessages().length != 1) {          stepDone(FAILURE, "Expected node to accept anycast at " + client + " accepted " + clients[i].getAnycastMessages().length);          failed = true;        }      } else {        if (clients[i].getAnycastMessages().length != 0) {          stepDone(FAILURE, "Expected no accepters for anycast, found one at " + scribes[i]);          failed = true;        }      }    }    if (!failed) {      stepDone(SUCCESS);    }    stepStart(name + " Anycast - All Accept");    for (int i = 0; i < NUM_NODES / SKIP; i++) {      clients[i].acceptAnycast(true);    }    local = scribes[environment.getRandomSource().nextInt(NUM_NODES / SKIP)];    local.anycast(topic, new TestScribeContent(topic, 59));    simulate();    int total = 0;    for (int i = 0; i < NUM_NODES / SKIP; i++) {      total += clients[i].getAnycastMessages().length;    }    if (total != 2) {      stepDone(FAILURE, "Expected 2 anycast messages to be found, found " + total);    } else {      stepDone(SUCCESS);    }    stepStart(name + " Unsubscribe");    for (int i = 0; i < NUM_NODES / SKIP; i++) {      scribes[i].unsubscribe(topic, clients[i]);      simulate();    }    local = scribes[environment.getRandomSource().nextInt(NUM_NODES)];    local.publish(topic, new TestScribeContent(topic, 100));    simulate();    failed = false;    for (int i = 0; i < NUM_NODES / SKIP; i++) {      if (clients[i].getPublishMessages().length != NUM_MESSAGES) {        stepDone(FAILURE, "Expected client " + clients[i] + " to receive no additional messages, received " + clients[i].getPublishMessages().length);        failed = true;      }    }    if (!failed) {      stepDone(SUCCESS);    }    stepStart(name + " Tree Completely Demolished");    failed = false;    for (int i = 0; i < NUM_NODES; i++) {      if (scribes[i].getClients(topic).length > 0) {        stepDone(FAILURE, "Expected scribe " + scribes[i] + " to have no clients, had " + scribes[i].getClients(topic).length);        failed = true;      }      if (scribes[i].getChildren(topic).length > 0) {        stepDone(FAILURE, "Expected scribe " + scribes[i] + " to have no children, had " + scribes[i].getChildren(topic).length);        failed = true;      }      if (scribes[i].getParent(topic) != null) {        stepDone(FAILURE, "Expected scribe " + scribes[i] + " to have no parent, had " + scribes[i].getParent(topic));        failed = true;      }    }    if (!failed) {      stepDone(SUCCESS);    }    sectionDone();  }  /**   * Tests basic publish functionality   */  protected void testAPI() {    sectionStart("Scribe API Functionality");    int NUM_MESSAGES = 5;    Topic topic = new Topic(generateId());    TestScribeClient[] clients = new TestScribeClient[NUM_NODES];    stepStart("Tree Construction");    for (int i = 0; i < NUM_NODES; i++) {      policies[i].allowSubscribe(false);    }    for (int i = 0; i < NUM_NODES / 2; i++) {      clients[i] = new TestScribeClient(scribes[i], topic, i);      scribes[i].subscribe(topic, clients[i]);      simulate();    }    int numWithParent = 0;    for (int i = 0; i < NUM_NODES; i++) {      if (scribes[i].getParent(topic) != null) {        numWithParent++;      }    }    if (numWithParent < (NUM_NODES / 2) - 1) {      stepDone(FAILURE, "Expected at least " + (NUM_NODES / 2 - 1) + " nodes with parents, found " + numWithParent);    } else      if (numWithParent > (NUM_NODES / 2)) {      stepDone(FAILURE, "Expected no more than " + (NUM_NODES / 2) + " nodes with parents, due to policy, found " + numWithParent);    } else {      stepDone(SUCCESS);    }    stepStart("Drop Child");    // now, find a scribe with a child    ScribeImpl scribe = null;    TestScribeClient client = null;    TestScribePolicy policy = null;    for (int i = 0; (i < NUM_NODES) && (scribe == null); i++) {      if (scribes[i].getChildren(topic).length > 0) {        scribe = scribes[i];        client = clients[i];        policy = policies[i];      }    }    if (scribe == null) {      stepDone(FAILURE, "Could not find any scribes with children");    } else {      NodeHandle child = scribe.getChildren(topic)[0];      // set this client to never allow subscribes      policy.neverAllowSubscribe(true);      // drop the handle now      scribe.removeChild(topic, child);      simulate();      ScribeImpl local = scribes[environment.getRandomSource().nextInt(NUM_NODES)];      for (int i = 0; i < NUM_MESSAGES; i++) {        local.publish(topic, new TestScribeContent(topic, i));        simulate();      }      boolean failed = false;      for (int i = 0; i < NUM_NODES / 2; i++) {        if (clients[i].getPublishMessages().length != NUM_MESSAGES) {          stepDone(FAILURE, "Expected client " + clients[i] + " to receive all messages, received " + clients[i].getPublishMessages().length);          failed = true;        }      }      NodeHandle[] children = scribe.getChildren(topic);      if (Arrays.asList(children).contains(child)) {        stepDone(FAILURE, "Child resubscribed to previous node, policy should prevent this.");        failed = true;      }      if (!failed) {        stepDone(SUCCESS);      }    }    stepStart("Reset Policies");    for (int i = 0; i < NUM_NODES; i++) {      policies[i].allowSubscribe(true);      policies[i].neverAllowSubscribe(false);    }    stepDone(SUCCESS);    sectionDone();  }  /**   * Tests failure notification   */  protected void testFailureNotification() {    sectionStart("Subscribe Failure Notification");    Topic topic = new Topic(generateId());    TestScribeClient client;    stepStart("Policy Change");    for (int i = 0; i < NUM_NODES; i++) {      policies[i].neverAllowSubscribe(true);    }    stepDone(SUCCESS);    stepStart("Subscribe Attempt");    int i = environment.getRandomSource().nextInt(NUM_NODES);    while (scribes[i].isRoot(topic)) {      i = environment.getRandomSource().nextInt(NUM_NODES);    }    client = new TestScribeClient(scribes[i], topic, i);    scribes[i].subscribe(topic, client);    simulate();    stepDone(SUCCESS);    stepStart("Failure Notification Delivered");    if (!client.getSubscribeFailed()) {      stepDone(FAILURE, "Expected subscribe to fail, but did not.");    } else {      stepDone(SUCCESS);    }    stepStart("Policy Reset");    for (int j = 0; j < NUM_NODES; j++) {      policies[j].neverAllowSubscribe(false);    }    stepDone(SUCCESS);    sectionDone();  }  /**   * A unit test for JUnit   *   * @param name DESCRIBE THE PARAMETER   */  protected void testSingleRoot(String name) {    sectionStart(name + "");    int numTrees = 10;    boolean failed = false;    for (int num = 0; num < numTrees; num++) {      Topic topic = new Topic(generateId());      TestScribeClient[] clients = new TestScribeClient[NUM_NODES];      stepStart(name + " TopicId=" + topic.getId());      for (int i = 0; i < NUM_NODES; i++) {        clients[i] = new TestScribeClient(scribes[i], topic, i);        scribes[i].subscribe(topic, clients[i]);        simulate();      }      int numRoot = 0;      for (int i = 0; i < NUM_NODES; i++) {        if (scribes[i].isRoot(topic)) {          numRoot++;          //System.out.println("myId= " + scribes[i].getId());        }      }      if (numRoot != 1) {        stepDone(FAILURE, "Number of roots= " + numRoot);        failed = true;      } else {        stepDone(SUCCESS);      }    }    sectionDone();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -