View Javadoc
1   package org.apache.onami.lifecycle.core;
2   
3   import java.util.concurrent.atomic.AtomicBoolean;
4   
5   import org.junit.Assert;
6   import org.junit.Test;
7   
8   /*
9    * Licensed to the Apache Software Foundation (ASF) under one
10   * or more contributor license agreements.  See the NOTICE file
11   * distributed with this work for additional information
12   * regarding copyright ownership.  The ASF licenses this file
13   * to you under the Apache License, Version 2.0 (the
14   * "License"); you may not use this file except in compliance
15   * with the License.  You may obtain a copy of the License at
16   *
17   *   http://www.apache.org/licenses/LICENSE-2.0
18   *
19   * Unless required by applicable law or agreed to in writing,
20   * software distributed under the License is distributed on an
21   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
22   * KIND, either express or implied.  See the License for the
23   * specific language governing permissions and limitations
24   * under the License.
25   */
26  
27  public class DefaultStagerTestCase
28  {
29  
30      @Test
31      public void stagerShouldStageObjectsRegisteredWhileStaging()
32      {
33          final Stager<TestAnnotationA> stager =
34                  new DefaultStager<TestAnnotationA>( TestAnnotationA.class );
35          final AtomicBoolean staged = new AtomicBoolean();
36          stager.register( new Stageable()
37          {
38              @Override
39              public void stage( StageHandler stageHandler )
40              {
41                  stager.register( new Stageable()
42                  {
43                      @Override
44                      public void stage( StageHandler stageHandler )
45                      {
46                          staged.set( true );
47                      }
48                  } );
49              }
50          } );
51  
52          stager.stage();
53  
54          Assert.assertTrue( staged.get() );
55      }
56  
57      /*
58       * Deadlock scenario:
59       * 1. DefaultStager holds lock while calling Stageable.stage();
60       * 2. Stageable.stage() blocks on some thread
61       * 3. the thread blocks on the lock in DefaultStager.register()
62       */
63      @Test
64      public void stagerShouldNotDeadlockWhileStagingObjectChains()
65      {
66          final AtomicBoolean staged = new AtomicBoolean();
67          final Stager<TestAnnotationA> stager =
68                  new DefaultStager<TestAnnotationA>( TestAnnotationA.class );
69          stager.register( new Stageable()
70          {
71              @Override
72              public void stage( StageHandler stageHandler )
73              {
74                  Thread thread = new Thread( new Runnable()
75                  {
76                      @Override
77                      public void run()
78                      {
79                          stager.register( new Stageable()
80                          {
81                              @Override
82                              public void stage( StageHandler stageHandler )
83                              {
84                                  staged.set( true );
85                              }
86                          } );
87                      }
88                  } );
89                  thread.start();
90                  try
91                  {
92                      thread.join();
93                  }
94                  catch ( InterruptedException e )
95                  {
96                      Thread.currentThread().interrupt();
97                  }
98              }
99          } );
100 
101         stager.stage();
102 
103         Assert.assertTrue( staged.get() );
104     }
105 }