1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.omid.tso.client;
19
20 import org.apache.omid.tso.util.DummyCellIdImpl;
21 import org.apache.statemachine.StateMachine.FsmImpl;
22 import org.slf4j.Logger;
23 import org.testng.annotations.Test;
24
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.TimeUnit;
27
28 import static com.google.common.collect.Sets.newHashSet;
29 import static org.slf4j.LoggerFactory.getLogger;
30 import static org.testng.Assert.assertEquals;
31 import static org.testng.Assert.fail;
32
33
34
35
36 public class TestUnconnectedTSOClient {
37
38 private static final Logger LOG = getLogger(TestUnconnectedTSOClient.class);
39
40 private static final int TSO_RECONNECTION_DELAY_IN_SECS_FOR_TEST = 2;
41
42 @Test(timeOut = 30_000)
43 public void testRequestsDoneOnAnUnconnectedTSOClientAlwaysReturn() throws Exception {
44
45 OmidClientConfiguration tsoClientConf = new OmidClientConfiguration();
46 tsoClientConf.setConnectionString("localhost:12345");
47 tsoClientConf.setReconnectionDelayInSecs(TSO_RECONNECTION_DELAY_IN_SECS_FOR_TEST);
48
49
50 TSOClient tsoClient = TSOClient.newInstance(tsoClientConf);
51
52
53 FsmImpl fsm = (FsmImpl) tsoClient.fsm;
54
55 assertEquals(fsm.getState().getClass(), TSOClient.DisconnectedState.class);
56
57
58
59 try {
60 tsoClient.getNewStartTimestamp().get();
61 fail();
62 } catch (ExecutionException e) {
63 LOG.info("Exception expected");
64 assertEquals(e.getCause().getClass(), ConnectionException.class);
65 TimeUnit.SECONDS.sleep(TSO_RECONNECTION_DELAY_IN_SECS_FOR_TEST * 2);
66 assertEquals(fsm.getState().getClass(), TSOClient.DisconnectedState.class);
67 }
68
69 try {
70 tsoClient.commit(1, newHashSet(new DummyCellIdImpl(0xdeadbeefL))).get();
71 fail();
72 } catch (ExecutionException e) {
73 LOG.info("Exception expected");
74 assertEquals(e.getCause().getClass(), ConnectionException.class);
75 TimeUnit.SECONDS.sleep(TSO_RECONNECTION_DELAY_IN_SECS_FOR_TEST * 2);
76 assertEquals(fsm.getState().getClass(), TSOClient.DisconnectedState.class);
77 }
78
79 tsoClient.close().get();
80 LOG.info("No exception expected");
81 }
82
83 }