| 1 | package faultToleranceTest; |
|---|
| 2 | |
|---|
| 3 | import static org.junit.Assert.assertEquals; |
|---|
| 4 | |
|---|
| 5 | import java.util.Properties; |
|---|
| 6 | |
|---|
| 7 | import omq.common.broker.Broker; |
|---|
| 8 | import omq.common.util.ParameterQueue; |
|---|
| 9 | import omq.common.util.Serializer; |
|---|
| 10 | |
|---|
| 11 | import org.junit.BeforeClass; |
|---|
| 12 | import org.junit.Test; |
|---|
| 13 | |
|---|
| 14 | import calculatorTest.Calculator; |
|---|
| 15 | |
|---|
| 16 | public class ClientTest { |
|---|
| 17 | private static Calculator remoteCalc; |
|---|
| 18 | |
|---|
| 19 | @BeforeClass |
|---|
| 20 | public static void startClient() throws Exception { |
|---|
| 21 | Properties env = new Properties(); |
|---|
| 22 | env.setProperty(ParameterQueue.USER_NAME, "guest"); |
|---|
| 23 | env.setProperty(ParameterQueue.USER_PASS, "guest"); |
|---|
| 24 | |
|---|
| 25 | // Set host info of rabbimq (where it is) |
|---|
| 26 | env.setProperty(ParameterQueue.SERVER_HOST, "127.0.0.1"); |
|---|
| 27 | env.setProperty(ParameterQueue.SERVER_PORT, "5672"); |
|---|
| 28 | env.setProperty(ParameterQueue.DURABLE_QUEUES, "false"); |
|---|
| 29 | env.setProperty(ParameterQueue.SERIALIZER_NAME, Serializer.java); |
|---|
| 30 | env.setProperty(ParameterQueue.ENABLECOMPRESSION, "false"); |
|---|
| 31 | |
|---|
| 32 | // Set info about where the message will be sent |
|---|
| 33 | env.setProperty(ParameterQueue.RPC_EXCHANGE, "rpc_exchange"); |
|---|
| 34 | // env.setProperty(ParameterQueue.DEBUGFILE, "c:\\middlewareDebug"); |
|---|
| 35 | |
|---|
| 36 | // Set info about the queue & the exchange where the ResponseListener |
|---|
| 37 | // will listen to. |
|---|
| 38 | env.setProperty(ParameterQueue.RPC_REPLY_QUEUE, "reply_queue"); |
|---|
| 39 | env.setProperty(ParameterQueue.EVENT_REPLY_QUEUE, "event_queue"); |
|---|
| 40 | env.setProperty(ParameterQueue.RETRY_TIME_CONNECTION, "5000"); |
|---|
| 41 | |
|---|
| 42 | Broker.initBroker(env); |
|---|
| 43 | remoteCalc = (Calculator) Broker.lookup("calculator1", Calculator.class); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | @Test |
|---|
| 47 | public void toleranceTest() throws Exception { |
|---|
| 48 | int x = 10; |
|---|
| 49 | int y = 20; |
|---|
| 50 | int sum = 10 + 20; |
|---|
| 51 | |
|---|
| 52 | int sync = remoteCalc.add(x, y); |
|---|
| 53 | |
|---|
| 54 | String password = "unpc"; |
|---|
| 55 | String[] command = { "/bin/bash", "-c", "echo " + password + " | sudo -S service rabbitmq-server restart" }; |
|---|
| 56 | |
|---|
| 57 | Runtime runtime = Runtime.getRuntime(); |
|---|
| 58 | runtime.exec(command); |
|---|
| 59 | |
|---|
| 60 | Thread.sleep(15000); |
|---|
| 61 | int resp = remoteCalc.add(x, y); |
|---|
| 62 | |
|---|
| 63 | assertEquals(sum, sync); |
|---|
| 64 | assertEquals(sum, resp); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | } |
|---|