001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.http; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertNull; 022import static org.junit.Assert.assertTrue; 023 024import org.apache.hadoop.hbase.HBaseClassTestRule; 025import org.apache.hadoop.hbase.testclassification.MiscTests; 026import org.apache.hadoop.hbase.testclassification.SmallTests; 027import org.junit.ClassRule; 028import org.junit.Ignore; 029import org.junit.Test; 030import org.junit.experimental.categories.Category; 031 032@Ignore("Hangs on occasion; see HBASE-14430") 033@Category({ MiscTests.class, SmallTests.class }) 034public class TestHttpServerLifecycle extends HttpServerFunctionalTest { 035 036 @ClassRule 037 public static final HBaseClassTestRule CLASS_RULE = 038 HBaseClassTestRule.forClass(TestHttpServerLifecycle.class); 039 040 /** 041 * Check that a server is alive by probing the {@link HttpServer#isAlive()} method and the text of 042 * its toString() description 043 * @param server server 044 */ 045 private void assertAlive(HttpServer server) { 046 assertTrue("Server is not alive", server.isAlive()); 047 assertToStringContains(server, HttpServer.STATE_DESCRIPTION_ALIVE); 048 } 049 050 private void assertNotLive(HttpServer server) { 051 assertTrue("Server should not be live", !server.isAlive()); 052 assertToStringContains(server, HttpServer.STATE_DESCRIPTION_NOT_LIVE); 053 } 054 055 /** 056 * Test that the server is alive once started 057 * @throws Throwable on failure 058 */ 059 @Test 060 public void testCreatedServerIsNotAlive() throws Throwable { 061 HttpServer server = createTestServer(); 062 assertNotLive(server); 063 } 064 065 @Test 066 public void testStopUnstartedServer() throws Throwable { 067 HttpServer server = createTestServer(); 068 stop(server); 069 } 070 071 /** 072 * Test that the server is alive once started 073 * @throws Throwable on failure 074 */ 075 @Test 076 public void testStartedServerIsAlive() throws Throwable { 077 HttpServer server = createTestServer(); 078 assertNotLive(server); 079 server.start(); 080 assertAlive(server); 081 stop(server); 082 } 083 084 /** 085 * Assert that the result of {@link HttpServer#toString()} contains the specific text 086 * @param server server to examine 087 * @param text text to search for 088 */ 089 private void assertToStringContains(HttpServer server, String text) { 090 String description = server.toString(); 091 assertTrue("Did not find \"" + text + "\" in \"" + description + "\"", 092 description.contains(text)); 093 } 094 095 /** 096 * Test that the server is not alive once stopped 097 * @throws Throwable on failure 098 */ 099 @Test 100 public void testStoppedServerIsNotAlive() throws Throwable { 101 HttpServer server = createAndStartTestServer(); 102 assertAlive(server); 103 stop(server); 104 assertNotLive(server); 105 } 106 107 /** 108 * Test that the server is not alive once stopped 109 * @throws Throwable on failure 110 */ 111 @Test 112 public void testStoppingTwiceServerIsAllowed() throws Throwable { 113 HttpServer server = createAndStartTestServer(); 114 assertAlive(server); 115 stop(server); 116 assertNotLive(server); 117 stop(server); 118 assertNotLive(server); 119 } 120 121 /** 122 * Test that the server is alive once started 123 */ 124 @Test 125 public void testWepAppContextAfterServerStop() throws Throwable { 126 String key = "test.attribute.key"; 127 String value = "test.attribute.value"; 128 HttpServer server = createTestServer(); 129 assertNotLive(server); 130 server.start(); 131 server.setAttribute(key, value); 132 assertAlive(server); 133 assertEquals(value, server.getAttribute(key)); 134 stop(server); 135 assertNull("Server context should have cleared", server.getAttribute(key)); 136 } 137}