ConsoleTest.java 6.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
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());
		}
	}

}