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.hadoop.hbase.http;
19  
20  import org.apache.log4j.Logger;
21  import org.junit.Test;
22  import org.junit.Ignore;
23  import org.apache.hadoop.hbase.testclassification.SmallTests;
24  import org.junit.experimental.categories.Category;
25  
26  @Category(SmallTests.class)
27  public class TestHttpServerLifecycle extends HttpServerFunctionalTest {
28  
29    /**
30     * Check that a server is alive by probing the {@link HttpServer#isAlive()} method
31     * and the text of its toString() description
32     * @param server server
33     */
34    private void assertAlive(HttpServer server) {
35      assertTrue("Server is not alive", server.isAlive());
36      assertToStringContains(server, HttpServer.STATE_DESCRIPTION_ALIVE);
37    }
38  
39    private void assertNotLive(HttpServer server) {
40      assertTrue("Server should not be live", !server.isAlive());
41      assertToStringContains(server, HttpServer.STATE_DESCRIPTION_NOT_LIVE);
42    }
43  
44    /**
45     * Test that the server is alive once started
46     *
47     * @throws Throwable on failure
48     */
49    @Ignore ("Hangs on occasion; see HBASE-14430") @Test(timeout=60000)
50    public void testCreatedServerIsNotAlive() throws Throwable {
51      HttpServer server = createTestServer();
52      assertNotLive(server);
53    }
54  
55    @Ignore ("Hangs on occasion; see HBASE-14430") @Test(timeout=60000)
56    public void testStopUnstartedServer() throws Throwable {
57      HttpServer server = createTestServer();
58      stop(server);
59    }
60  
61    /**
62     * Test that the server is alive once started
63     *
64     * @throws Throwable on failure
65     */
66    @Ignore ("Hangs on occasion; see HBASE-14430") @Test(timeout=60000)
67    public void testStartedServerIsAlive() throws Throwable {
68      HttpServer server = null;
69      server = createTestServer();
70      assertNotLive(server);
71      server.start();
72      assertAlive(server);
73      stop(server);
74    }
75  
76    /**
77     * Assert that the result of {@link HttpServer#toString()} contains the specific text
78     * @param server server to examine
79     * @param text text to search for
80     */
81    private void assertToStringContains(HttpServer server, String text) {
82      String description = server.toString();
83      assertTrue("Did not find \"" + text + "\" in \"" + description + "\"",
84                 description.contains(text));
85    }
86  
87    /**
88     * Test that the server is not alive once stopped
89     *
90     * @throws Throwable on failure
91     */
92    @Ignore ("Hangs on occasion; see HBASE-14430") @Test(timeout=60000)
93    public void testStoppedServerIsNotAlive() throws Throwable {
94      HttpServer server = createAndStartTestServer();
95      assertAlive(server);
96      stop(server);
97      assertNotLive(server);
98    }
99  
100   /**
101    * Test that the server is not alive once stopped
102    *
103    * @throws Throwable on failure
104    */
105   @Ignore ("Hangs on occasion; see HBASE-14430") @Test(timeout=60000)
106   public void testStoppingTwiceServerIsAllowed() throws Throwable {
107     HttpServer server = createAndStartTestServer();
108     assertAlive(server);
109     stop(server);
110     assertNotLive(server);
111     stop(server);
112     assertNotLive(server);
113   }
114 
115   /**
116    * Test that the server is alive once started
117    * 
118    * @throws Throwable
119    *           on failure
120    */
121   @Ignore ("Hangs on occasion; see HBASE-14430") @Test(timeout=60000)
122   public void testWepAppContextAfterServerStop() throws Throwable {
123     HttpServer server = null;
124     String key = "test.attribute.key";
125     String value = "test.attribute.value";
126     server = createTestServer();
127     assertNotLive(server);
128     server.start();
129     server.setAttribute(key, value);
130     assertAlive(server);
131     assertEquals(value, server.getAttribute(key));
132     stop(server);
133     assertNull("Server context should have cleared", server.getAttribute(key));
134   }
135 }