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.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertNull; 022import static org.junit.jupiter.api.Assertions.assertTrue; 023 024import org.apache.hadoop.hbase.testclassification.MiscTests; 025import org.apache.hadoop.hbase.testclassification.SmallTests; 026import org.junit.jupiter.api.Disabled; 027import org.junit.jupiter.api.Tag; 028import org.junit.jupiter.api.Test; 029 030@Disabled("Hangs on occasion; see HBASE-14430") 031@Tag(MiscTests.TAG) 032@Tag(SmallTests.TAG) 033public class TestHttpServerLifecycle extends HttpServerFunctionalTest { 034 035 /** 036 * Check that a server is alive by probing the {@link HttpServer#isAlive()} method and the text of 037 * its toString() description 038 * @param server server 039 */ 040 private void assertAlive(HttpServer server) { 041 assertTrue(server.isAlive(), "Server is not alive"); 042 assertToStringContains(server, HttpServer.STATE_DESCRIPTION_ALIVE); 043 } 044 045 private void assertNotLive(HttpServer server) { 046 assertTrue(!server.isAlive(), "Server should not be live"); 047 assertToStringContains(server, HttpServer.STATE_DESCRIPTION_NOT_LIVE); 048 } 049 050 /** 051 * Test that the server is alive once started 052 * @throws Throwable on failure 053 */ 054 @Test 055 public void testCreatedServerIsNotAlive() throws Throwable { 056 HttpServer server = createTestServer(); 057 assertNotLive(server); 058 } 059 060 @Test 061 public void testStopUnstartedServer() throws Throwable { 062 HttpServer server = createTestServer(); 063 stop(server); 064 } 065 066 /** 067 * Test that the server is alive once started 068 * @throws Throwable on failure 069 */ 070 @Test 071 public void testStartedServerIsAlive() throws Throwable { 072 HttpServer server = createTestServer(); 073 assertNotLive(server); 074 server.start(); 075 assertAlive(server); 076 stop(server); 077 } 078 079 /** 080 * Assert that the result of {@link HttpServer#toString()} contains the specific text 081 * @param server server to examine 082 * @param text text to search for 083 */ 084 private void assertToStringContains(HttpServer server, String text) { 085 String description = server.toString(); 086 assertTrue(description.contains(text), 087 "Did not find \"" + text + "\" in \"" + description + "\""); 088 } 089 090 /** 091 * Test that the server is not alive once stopped 092 * @throws Throwable on failure 093 */ 094 @Test 095 public void testStoppedServerIsNotAlive() throws Throwable { 096 HttpServer server = createAndStartTestServer(); 097 assertAlive(server); 098 stop(server); 099 assertNotLive(server); 100 } 101 102 /** 103 * Test that the server is not alive once stopped 104 * @throws Throwable on failure 105 */ 106 @Test 107 public void testStoppingTwiceServerIsAllowed() throws Throwable { 108 HttpServer server = createAndStartTestServer(); 109 assertAlive(server); 110 stop(server); 111 assertNotLive(server); 112 stop(server); 113 assertNotLive(server); 114 } 115 116 /** 117 * Test that the server is alive once started 118 */ 119 @Test 120 public void testWepAppContextAfterServerStop() throws Throwable { 121 String key = "test.attribute.key"; 122 String value = "test.attribute.value"; 123 HttpServer server = createTestServer(); 124 assertNotLive(server); 125 server.start(); 126 server.setAttribute(key, value); 127 assertAlive(server); 128 assertEquals(value, server.getAttribute(key)); 129 stop(server); 130 assertNull(server.getAttribute(key), "Server context should have cleared"); 131 } 132}