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.junit.Test;
27  
28  public class TestTaskMonitor {
29  
30    @Test
31    public void testTaskMonitorBasics() {
32      TaskMonitor tm = new TaskMonitor();
33      assertTrue("Task monitor should start empty",
34          tm.getTasks().isEmpty());
35      
36      // Make a task and fetch it back out
37      MonitoredTask task = tm.createStatus("Test task");
38      MonitoredTask taskFromTm = tm.getTasks().get(0);
39      
40      // Make sure the state is reasonable.
41      assertEquals(task.getDescription(), taskFromTm.getDescription());
42      assertEquals(-1, taskFromTm.getCompletionTimestamp());
43      assertEquals(MonitoredTask.State.RUNNING, taskFromTm.getState());
44      
45      // Mark it as finished
46      task.markComplete("Finished!");
47      assertEquals(MonitoredTask.State.COMPLETE, task.getState());
48      
49      // It should still show up in the TaskMonitor list
50      assertEquals(1, tm.getTasks().size());
51      
52      // If we mark its completion time back a few minutes, it should get gced
53      task.expireNow();
54      assertEquals(0, tm.getTasks().size());
55    }
56    
57    @Test
58    public void testTasksGetAbortedOnLeak() throws InterruptedException {
59      final TaskMonitor tm = new TaskMonitor();
60      assertTrue("Task monitor should start empty",
61          tm.getTasks().isEmpty());
62      
63      final AtomicBoolean threadSuccess = new AtomicBoolean(false);
64      // Make a task in some other thread and leak it
65      Thread t = new Thread() {
66        @Override
67        public void run() {
68          MonitoredTask task = tm.createStatus("Test task");    
69          assertEquals(MonitoredTask.State.RUNNING, task.getState());
70          threadSuccess.set(true);
71        }
72      };
73      t.start();
74      t.join();
75      // Make sure the thread saw the correct state
76      assertTrue(threadSuccess.get());
77      
78      // Make sure the leaked reference gets cleared
79      System.gc();
80      System.gc();
81      System.gc();
82      
83      // Now it should be aborted 
84      MonitoredTask taskFromTm = tm.getTasks().get(0);
85      assertEquals(MonitoredTask.State.ABORTED, taskFromTm.getState());
86    }
87    
88    @Test
89    public void testTaskLimit() throws Exception {
90      TaskMonitor tm = new TaskMonitor();
91      for (int i = 0; i < TaskMonitor.MAX_TASKS + 10; i++) {
92        tm.createStatus("task " + i);
93      }
94      // Make sure it was limited correctly
95      assertEquals(TaskMonitor.MAX_TASKS, tm.getTasks().size());
96      // Make sure we culled the earlier tasks, not later
97      // (i.e. tasks 0 through 9 should have been deleted)
98      assertEquals("task 10", tm.getTasks().get(0).getDescription());
99    }
100 
101 }