View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Test the behavior of requests on a TSOClient component that is not connected to a TSO server.
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) // 30 secs
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          // Component under test
50          TSOClient tsoClient = TSOClient.newInstance(tsoClientConf);
51  
52          // Internal accessor to fsm
53          FsmImpl fsm = (FsmImpl) tsoClient.fsm;
54  
55          assertEquals(fsm.getState().getClass(), TSOClient.DisconnectedState.class);
56  
57          // Test requests to the 3 relevant methods in TSO client
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  }