提交 6973b767 编写于 作者: M marcelmhs@gmail.com

- Added ConsoleLogger and ConsoleTest to version control.

上级 ff16f40d
package navigators.smart.tom.demo.keyvalue;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
public class ConsoleLogger extends Thread {
private InputStream in = null;
private PrintStream out = null;
private int index;
public InputStream getIn() {
return in;
}
public void setIn(InputStream in) {
this.in = in;
}
public PrintStream getOut() {
return out;
}
public void setOut(PrintStream out) {
this.out = out;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public void run() {
BufferedReader stdInput = new BufferedReader(new InputStreamReader(in));
String s;
try {
while ((s = stdInput.readLine()) != null) {
out.println("Replica " + index + ")" +s);
}
} catch(IOException ioe) {
System.out.println("----------- Exception writing replica log: " + ioe.getMessage());
}
}
}
package navigators.smart.tom.demo.keyvalue;
import static org.junit.Assert.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class ConsoleTest {
private static Process replica0;
private static Process replica1;
private static Process replica2;
private static Process replica3;
private static String[] command = new String[5];
@BeforeClass
public static void startServers() {
try {
System.out.println("Starting the servers");
command[0] = "java";
command[1] = "-cp";
command[2] = "bin/SMaRt.jar:lib/slf4j-api-1.5.8.jar:lib/slf4j-jdk14-1.5.8.jar:lib/netty-3.1.1.GA.jar:lib/commons-codec-1.5.jar";
command[3] = "navigators.smart.tom.demo.keyvalue.BFTMapImpl";
command[4] = "0";
replica0 = new ProcessBuilder(command).redirectErrorStream(true).start();
command[4] = "1";
replica1 = new ProcessBuilder(command).redirectErrorStream(true).start();
command[4] = "2";
replica2 = new ProcessBuilder(command).redirectErrorStream(true).start();
command[4] = "3";
replica3 = new ProcessBuilder(command).redirectErrorStream(true).start();
ConsoleLogger log0 = new ConsoleLogger();
log0.setIndex(0);
log0.setIn(replica0.getInputStream());
log0.setOut(System.out);
log0.start();
ConsoleLogger log1 = new ConsoleLogger();
log1.setIndex(1);
log1.setIn(replica1.getInputStream());
log1.setOut(System.out);
log1.start();
ConsoleLogger log2 = new ConsoleLogger();
log2.setIndex(2);
log2.setIn(replica2.getInputStream());
log2.setOut(System.out);
log2.start();
ConsoleLogger log3 = new ConsoleLogger();
log3.setIndex(3);
log3.setIn(replica3.getInputStream());
log3.setOut(System.out);
log3.start();
System.out.println("Servers started");
} catch(IOException ioe) {
System.out.println("Exception during KVClient test: ");
System.out.println(ioe.getMessage());
}
}
@AfterClass
public static void stopServers() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, IOException {
System.out.println("Stopping servers");
replica0.destroy();
replica1.destroy();
replica2.destroy();
replica3.destroy();
System.out.println("Servers stopped");
}
/**
* Test regular case where there is the creation of a table, insert of
* data and search for size of the table.
* No servers are killed nor behaves different than expected.
*/
@Test
public void testRegularCase() {
try{
Thread.sleep(1000);
BFTMap bftMap = new BFTMap(1001);
bftMap.put("TestTable1", new HashMap<String,byte[]>());
bftMap.putEntry("TestTable1", "key1", "value1".getBytes());
assertEquals("Main table size should be 1", 1, bftMap.size1("TestTable1"));
for(int i = 0; i < 100; i++) {
String key = "key" + (2+i);
String value = "value" + (2+i);
bftMap.putEntry("TestTable1", key, value.getBytes());
}
assertEquals("Main table size should be 101", 101, bftMap.size1("TestTable1"));
bftMap.putEntry("TestTable1", "key102", "value102".getBytes());
assertEquals("Main table size should be 102", 102, bftMap.size1("TestTable1"));
} catch(InterruptedException ie) {
System.out.println("Exception during Thread sleep: " + ie.getMessage());
}
}
public static void main(String[] args) {
ConsoleTest test = new ConsoleTest();
startServers();
test.testStopAndStartNonLeader();
try {
stopServers();
} catch(Exception e) {
e.printStackTrace();
}
}
/**
* This test insert and retrieve data.
* During this process a replica that is not the leader is killed.
* After that the replica is started back.
* During the whole process messages keep being sent, to test if
* the application works as expected.
*/
@Test
public void testStopAndStartNonLeader() {
try{
Thread.sleep(1000);
BFTMap bftMap = new BFTMap(1001);
bftMap.put("TestTable2", new HashMap<String,byte[]>());
for(int i = 0; i < 65; i++) {
String key = "key" + (1+i);
String value = "value" + (2+i);
bftMap.putEntry("TestTable2", key, value.getBytes());
}
System.out.println("---------Sleep1: " + new java.util.Date());
Thread.sleep(5000);
System.out.println("---------Wakeup1: " + new java.util.Date());
System.out.println("---------Killing replica 1");
replica1.destroy(); // Killing a non-leader replica, replica1
System.out.println("---------Sleep2: " + new java.util.Date());
Thread.sleep(5000);
System.out.println("---------Wakeup2: " + new java.util.Date());
for(int i = 0; i < 35; i++) {
String key = "key" + (66+i);
String value = "value" + (66+i);
bftMap.putEntry("TestTable2", key, value.getBytes());
}
System.out.println("---------Sleep3: " + new java.util.Date());
Thread.sleep(5000);
System.out.println("---------Wakeup3: " + new java.util.Date());
command[4] = "1";
System.out.println("---------Starting replica 1");
replica1 = new ProcessBuilder(command).start(); // Starting replica1 back
ConsoleLogger log1 = new ConsoleLogger();
log1.setIndex(11);
log1.setIn(replica1.getInputStream());
log1.setOut(System.out);
log1.start();
System.out.println("---------Sleep4: " + new java.util.Date());
Thread.sleep(20000);
System.out.println("---------Wakeup4: " + new java.util.Date());
for(int i = 0; i < 35; i++) {
String key = "key" + (101+i);
String value = "value" + (101+i);
bftMap.putEntry("TestTable2", key, value.getBytes());
}
System.out.println("---------Sleep2: " + new java.util.Date());
Thread.sleep(20000);
System.out.println("---------Wakeup2: " + new java.util.Date());
replica2.destroy(); // Killing another non-leader replica, replica3
for(int i = 0; i < 35; i++) {
String key = "key" + (136+i);
String value = "value" + (136+i);
bftMap.putEntry("TestTable2", key, value.getBytes());
}
System.out.println("----------Table size:" + bftMap.size1("TestTable2"));
} catch(InterruptedException ie) {
System.out.println("Exception during Thread sleep: " + ie.getMessage());
} catch(IOException ioe) {
System.out.println("Exception when starting replica 2: " + ioe.getMessage());
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册