1   /**
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.monitoring;
21  
22  import static org.junit.Assert.*;
23  
24  import java.util.concurrent.atomic.AtomicBoolean;
25  
26  import org.apache.hadoop.hbase.MediumTests;
27  import org.junit.Test;
28  import org.junit.experimental.categories.Category;
29  
30  @Category(MediumTests.class)
31  public class TestTaskMonitor {
32  
33    @Test
34    public void testTaskMonitorBasics() {
35      TaskMonitor tm = new TaskMonitor();
36      assertTrue("Task monitor should start empty",
37          tm.getTasks().isEmpty());
38      
39      // Make a task and fetch it back out
40      MonitoredTask task = tm.createStatus("Test task");
41      MonitoredTask taskFromTm = tm.getTasks().get(0);
42      
43      // Make sure the state is reasonable.
44      assertEquals(task.getDescription(), taskFromTm.getDescription());
45      assertEquals(-1, taskFromTm.getCompletionTimestamp());
46      assertEquals(MonitoredTask.State.RUNNING, taskFromTm.getState());
47      
48      // Mark it as finished
49      task.markComplete("Finished!");
50      assertEquals(MonitoredTask.State.COMPLETE, task.getState());
51      
52      // It should still show up in the TaskMonitor list
53      assertEquals(1, tm.getTasks().size());
54      
55      // If we mark its completion time back a few minutes, it should get gced
56      task.expireNow();
57      assertEquals(0, tm.getTasks().size());
58    }
59    
60    @Test
61    public void testTasksGetAbortedOnLeak() throws InterruptedException {
62      final TaskMonitor tm = new TaskMonitor();
63      assertTrue("Task monitor should start empty",
64          tm.getTasks().isEmpty());
65      
66      final AtomicBoolean threadSuccess = new AtomicBoolean(false);
67      // Make a task in some other thread and leak it
68      Thread t = new Thread() {
69        @Override
70        public void run() {
71          MonitoredTask task = tm.createStatus("Test task");    
72          assertEquals(MonitoredTask.State.RUNNING, task.getState());
73          threadSuccess.set(true);
74        }
75      };
76      t.start();
77      t.join();
78      // Make sure the thread saw the correct state
79      assertTrue(threadSuccess.get());
80      
81      // Make sure the leaked reference gets cleared
82      System.gc();
83      System.gc();
84      System.gc();
85      
86      // Now it should be aborted 
87      MonitoredTask taskFromTm = tm.getTasks().get(0);
88      assertEquals(MonitoredTask.State.ABORTED, taskFromTm.getState());
89    }
90    
91    @Test
92    public void testTaskLimit() throws Exception {
93      TaskMonitor tm = new TaskMonitor();
94      for (int i = 0; i < TaskMonitor.MAX_TASKS + 10; i++) {
95        tm.createStatus("task " + i);
96      }
97      // Make sure it was limited correctly
98      assertEquals(TaskMonitor.MAX_TASKS, tm.getTasks().size());
99      // Make sure we culled the earlier tasks, not later
100     // (i.e. tasks 0 through 9 should have been deleted)
101     assertEquals("task 10", tm.getTasks().get(0).getDescription());
102   }
103 
104 
105   @org.junit.Rule
106   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
107     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
108 }
109