View Javadoc
1   package org.apache.commons.jcs3.engine.control.event;
2   
3   import org.apache.commons.jcs3.JCS;
4   import org.apache.commons.jcs3.access.CacheAccess;
5   import org.apache.commons.jcs3.engine.ElementAttributes;
6   import org.apache.commons.jcs3.engine.behavior.IElementAttributes;
7   import org.apache.commons.jcs3.engine.control.event.behavior.IElementEvent;
8   import org.apache.commons.jcs3.engine.control.event.behavior.IElementEventHandler;
9   
10  /*
11   * Licensed to the Apache Software Foundation (ASF) under one
12   * or more contributor license agreements.  See the NOTICE file
13   * distributed with this work for additional information
14   * regarding copyright ownership.  The ASF licenses this file
15   * to you under the Apache License, Version 2.0 (the
16   * "License"); you may not use this file except in compliance
17   * with the License.  You may obtain a copy of the License at
18   *
19   *   http://www.apache.org/licenses/LICENSE-2.0
20   *
21   * Unless required by applicable law or agreed to in writing,
22   * software distributed under the License is distributed on an
23   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
24   * KIND, either express or implied.  See the License for the
25   * specific language governing permissions and limitations
26   * under the License.
27   */
28  
29  import junit.framework.TestCase;
30  
31  /**
32   * This test suite verifies that the basic ElementEvent are called as they should be.
33   */
34  public class SimpleEventHandlingUnitTest
35      extends TestCase
36  {
37      /** Items to test with */
38      private static final int items = 2000;
39  
40      /** Event handler instance */
41      private MyEventHandler meh;
42  
43      /**
44       * Test setup with expected configuration parameters.
45       */
46      @Override
47      public void setUp()
48      {
49          JCS.setConfigFilename( "/TestSimpleEventHandling.ccf" );
50          this.meh = new MyEventHandler();
51      }
52  
53      /**
54       * Verify that the spooled event is called as expected.
55       * <p>
56       * @throws Exception Description of the Exception
57       */
58      public void testSpoolEvent()
59          throws Exception
60      {
61          final CacheAccess<String, String> jcs = JCS.getInstance( "WithDisk" );
62          // this should add the event handler to all items as they are created.
63          final IElementAttributes attributes = jcs.getDefaultElementAttributes();
64          attributes.addElementEventHandler( meh );
65          jcs.setDefaultElementAttributes( attributes );
66  
67          // DO WORK
68          // put them in
69          for ( int i = 0; i < items; i++ )
70          {
71              jcs.put( i + ":key", "data" + i );
72          }
73          // wait a bit for it to finish
74          Thread.sleep( items / 20 );
75  
76          // VERIFY
77          // test to see if the count is right
78          assertTrue( "The number of ELEMENT_EVENT_SPOOLED_DISK_AVAILABLE events [" + meh.getSpoolCount()
79              + "] does not equal the number expected [" + items + "]", meh.getSpoolCount() >= items );
80      }
81  
82      /**
83       * Test overflow with no disk configured for the region.
84       * <p>
85       * @throws Exception
86       */
87      public void testSpoolNoDiskEvent()
88          throws Exception
89      {
90          final CacheAccess<String, String> jcs = JCS.getInstance( "NoDisk" );
91  
92          // this should add the event handler to all items as they are created.
93          final IElementAttributes attributes = jcs.getDefaultElementAttributes();
94          attributes.addElementEventHandler( meh );
95          jcs.setDefaultElementAttributes( attributes );
96  
97          // put them in
98          for ( int i = 0; i < items; i++ )
99          {
100             jcs.put( i + ":key", "data" + i );
101         }
102 
103         // wait a bit for it to finish
104         Thread.sleep( items / 20 );
105 
106         // test to see if the count is right
107         assertTrue( "The number of ELEMENT_EVENT_SPOOLED_DISK_NOT_AVAILABLE events  [" + meh.getSpoolNoDiskCount()
108             + "] does not equal the number expected.", meh.getSpoolNoDiskCount() >= items );
109 
110     }
111 
112     /**
113      * Test the ELEMENT_EVENT_SPOOLED_NOT_ALLOWED event.
114      * @throws Exception
115      */
116     public void testSpoolNotAllowedEvent()
117         throws Exception
118     {
119         final CacheAccess<String, String> jcs = JCS.getInstance( "DiskButNotAllowed" );
120         // this should add the event handler to all items as they are created.
121         final IElementAttributes attributes = jcs.getDefaultElementAttributes();
122         attributes.addElementEventHandler( meh );
123         jcs.setDefaultElementAttributes( attributes );
124 
125         // put them in
126         for ( int i = 0; i < items; i++ )
127         {
128             jcs.put( i + ":key", "data" + i );
129         }
130 
131         // wait a bit for it to finish
132         Thread.sleep( items / 20 );
133 
134         // test to see if the count is right
135         assertTrue( "The number of ELEMENT_EVENT_SPOOLED_NOT_ALLOWED events [" + meh.getSpoolNotAllowedCount()
136             + "] does not equal the number expected.", meh.getSpoolNotAllowedCount() >= items );
137 
138     }
139 
140     /**
141      * Test the ELEMENT_EVENT_SPOOLED_NOT_ALLOWED event.
142      * @throws Exception
143      */
144     public void testSpoolNotAllowedEventOnItem()
145         throws Exception
146     {
147         final CacheAccess<String, String> jcs = JCS.getInstance( "DiskButNotAllowed" );
148         // this should add the event handler to all items as they are created.
149         //IElementAttributes attributes = jcs.getDefaultElementAttributes();
150         //attributes.addElementEventHandler( meh );
151         //jcs.setDefaultElementAttributes( attributes );
152 
153         // put them in
154         for ( int i = 0; i < items; i++ )
155         {
156             final IElementAttributes attributes = jcs.getDefaultElementAttributes();
157             attributes.addElementEventHandler( meh );
158             jcs.put( i + ":key", "data" + i, attributes );
159         }
160 
161         // wait a bit for it to finish
162         Thread.sleep( items / 20 );
163 
164         // test to see if the count is right
165         assertTrue( "The number of ELEMENT_EVENT_SPOOLED_NOT_ALLOWED events [" + meh.getSpoolNotAllowedCount()
166             + "] does not equal the number expected.", meh.getSpoolNotAllowedCount() >= items );
167 
168     }
169 
170     /**
171      * Test the ELEMENT_EVENT_EXCEEDED_MAXLIFE_ONREQUEST event.
172      * @throws Exception
173      */
174     public void testExceededMaxlifeOnrequestEvent()
175         throws Exception
176     {
177         final CacheAccess<String, String> jcs = JCS.getInstance( "Maxlife" );
178         // this should add the event handler to all items as they are created.
179         final IElementAttributes attributes = jcs.getDefaultElementAttributes();
180         attributes.addElementEventHandler( meh );
181         jcs.setDefaultElementAttributes( attributes );
182 
183         // put them in
184         for ( int i = 0; i < 200; i++ )
185         {
186             jcs.put( i + ":key", "data" + i);
187         }
188 
189         // wait a bit for the items to expire
190         Thread.sleep(attributes.getMaxLife() * 1000 + 100);
191 
192         for ( int i = 0; i < 200; i++ )
193         {
194             final String value = jcs.get( i + ":key");
195             assertNull("Item should be null for key " + i + ":key, but is " + value, value);
196         }
197 
198         // wait a bit for it to finish
199         Thread.sleep( 100 );
200 
201         // test to see if the count is right
202         assertTrue( "The number of ELEMENT_EVENT_EXCEEDED_MAXLIFE_ONREQUEST events [" + meh.getExceededMaxlifeCount()
203             + "] does not equal the number expected.", meh.getExceededMaxlifeCount() >= 200 );
204     }
205 
206     /**
207      * Test the ELEMENT_EVENT_EXCEEDED_IDLETIME_ONREQUEST event.
208      * @throws Exception
209      */
210     public void testExceededIdletimeOnrequestEvent()
211         throws Exception
212     {
213         final CacheAccess<String, String> jcs = JCS.getInstance( "Idletime" );
214         // this should add the event handler to all items as they are created.
215         final IElementAttributes attributes = jcs.getDefaultElementAttributes();
216         attributes.addElementEventHandler( meh );
217         jcs.setDefaultElementAttributes( attributes );
218 
219         // put them in
220         for ( int i = 0; i < 200; i++ )
221         {
222             jcs.put( i + ":key", "data" + i);
223         }
224 
225         // update access time
226         for ( int i = 0; i < 200; i++ )
227         {
228             final String value = jcs.get( i + ":key");
229             assertNotNull("Item should not be null for key " + i + ":key", value);
230         }
231 
232         // wait a bit for the items to expire
233         Thread.sleep(attributes.getIdleTime() * 1000 + 100);
234 
235         for ( int i = 0; i < 200; i++ )
236         {
237             final String value = jcs.get( i + ":key");
238             assertNull("Item should be null for key " + i + ":key, but is " + value, value);
239         }
240 
241         // wait a bit for it to finish
242         Thread.sleep( 100 );
243 
244         // test to see if the count is right
245         assertTrue( "The number of ELEMENT_EVENT_EXCEEDED_IDLETIME_ONREQUEST events [" + meh.getExceededIdletimeCount()
246             + "] does not equal the number expected.", meh.getExceededIdletimeCount() >= 200 );
247     }
248 
249     /**
250      * Test that cloned ElementAttributes have different creation times.
251      * @throws Exception
252      */
253     public void testElementAttributesCreationTime()
254         throws Exception
255     {
256     	final ElementAttributes elem1 = new ElementAttributes();
257     	final long ctime1 = elem1.getCreateTime();
258 
259     	Thread.sleep(10);
260 
261     	final IElementAttributes elem2 = elem1.clone();
262     	final long ctime2 = elem2.getCreateTime();
263 
264     	assertFalse("Creation times should be different", ctime1 == ctime2);
265     }
266 
267     /**
268      * Simple event counter used to verify test results.
269      */
270     public static class MyEventHandler
271         implements IElementEventHandler
272     {
273         /** times spool called */
274         private int spoolCount;
275 
276         /** times spool not allowed */
277         private int spoolNotAllowedCount;
278 
279         /** times spool without disk */
280         private int spoolNoDiskCount;
281 
282         /** times exceeded maxlife */
283         private int exceededMaxlifeCount;
284 
285         /** times exceeded idle time */
286         private int exceededIdletimeCount;
287 
288         /**
289          * @param event
290          */
291         @Override
292         public synchronized <T> void handleElementEvent( final IElementEvent<T> event )
293         {
294             //System.out.println( "Handling Event of Type " +
295             // event.getElementEvent() );
296 
297             switch (event.getElementEvent())
298             {
299                 case SPOOLED_DISK_AVAILABLE:
300                 //System.out.println( "Handling Event of Type
301                 // ELEMENT_EVENT_SPOOLED_DISK_AVAILABLE, " + getSpoolCount() );
302                 spoolCount++;
303                 break;
304 
305                 case SPOOLED_NOT_ALLOWED:
306                 spoolNotAllowedCount++;
307                 break;
308 
309                 case SPOOLED_DISK_NOT_AVAILABLE:
310                 spoolNoDiskCount++;
311                 break;
312 
313                 case EXCEEDED_MAXLIFE_ONREQUEST:
314                 exceededMaxlifeCount++;
315                 break;
316 
317                 case EXCEEDED_IDLETIME_ONREQUEST:
318                 exceededIdletimeCount++;
319                 break;
320 
321                 case EXCEEDED_IDLETIME_BACKGROUND:
322                 break;
323 
324                 case EXCEEDED_MAXLIFE_BACKGROUND:
325                 break;
326             }
327         }
328 
329         /**
330          * @return Returns the spoolCount.
331          */
332         protected int getSpoolCount()
333         {
334             return spoolCount;
335         }
336 
337         /**
338          * @return Returns the spoolNotAllowedCount.
339          */
340         protected int getSpoolNotAllowedCount()
341         {
342             return spoolNotAllowedCount;
343         }
344 
345         /**
346          * @return Returns the spoolNoDiskCount.
347          */
348         protected int getSpoolNoDiskCount()
349         {
350             return spoolNoDiskCount;
351         }
352 
353         /**
354          * @return the exceededMaxlifeCount
355          */
356         protected int getExceededMaxlifeCount()
357         {
358             return exceededMaxlifeCount;
359         }
360 
361         /**
362          * @return the exceededIdletimeCount
363          */
364         protected int getExceededIdletimeCount()
365         {
366             return exceededIdletimeCount;
367         }
368     }
369 
370 }