View Javadoc
1   package org.apache.commons.jcs.engine.control.event;
2   
3   import org.apache.commons.jcs.JCS;
4   import org.apache.commons.jcs.access.CacheAccess;
5   import org.apache.commons.jcs.engine.ElementAttributes;
6   import org.apache.commons.jcs.engine.behavior.IElementAttributes;
7   import org.apache.commons.jcs.engine.control.event.behavior.IElementEvent;
8   import org.apache.commons.jcs.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 int items = 20000;
39  
40      /**
41       * Test setup with expected configuration parameters.
42       */
43      @Override
44      public void setUp()
45      {
46          JCS.setConfigFilename( "/TestSimpleEventHandling.ccf" );
47      }
48  
49      /**
50       * Verify that the spooled event is called as expected.
51       * <p>
52       * @throws Exception Description of the Exception
53       */
54      public void testSpoolEvent()
55          throws Exception
56      {
57          // SETUP
58          MyEventHandler meh = new MyEventHandler();
59  
60          CacheAccess<String, String> jcs = JCS.getInstance( "WithDisk" );
61          // this should add the event handler to all items as they are created.
62          IElementAttributes attributes = jcs.getDefaultElementAttributes();
63          attributes.addElementEventHandler( meh );
64          jcs.setDefaultElementAttributes( attributes );
65  
66          // DO WORK
67          // put them in
68          for ( int i = 0; i <= items; i++ )
69          {
70              jcs.put( i + ":key", "data" + i );
71          }
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          CacheAccess<String, String> jcs = JCS.getInstance( "NoDisk" );
91  
92          MyEventHandler meh = new MyEventHandler();
93  
94          // this should add the event handler to all items as they are created.
95          IElementAttributes attributes = jcs.getDefaultElementAttributes();
96          attributes.addElementEventHandler( meh );
97          jcs.setDefaultElementAttributes( attributes );
98  
99          // put them in
100         for ( int i = 0; i <= items; i++ )
101         {
102             jcs.put( i + ":key", "data" + i );
103         }
104 
105         // wait a bit for it to finish
106         Thread.sleep( items / 20 );
107 
108         // test to see if the count is right
109         assertTrue( "The number of ELEMENT_EVENT_SPOOLED_DISK_NOT_AVAILABLE events  [" + meh.getSpoolNoDiskCount()
110             + "] does not equal the number expected.", meh.getSpoolNoDiskCount() >= items );
111 
112     }
113 
114     /**
115      * Test the ELEMENT_EVENT_SPOOLED_NOT_ALLOWED event.
116      * @throws Exception
117      */
118     public void testSpoolNotAllowedEvent()
119         throws Exception
120     {
121         MyEventHandler meh = new MyEventHandler();
122 
123         CacheAccess<String, String> jcs = JCS.getInstance( "DiskButNotAllowed" );
124         // this should add the event handler to all items as they are created.
125         IElementAttributes attributes = jcs.getDefaultElementAttributes();
126         attributes.addElementEventHandler( meh );
127         jcs.setDefaultElementAttributes( attributes );
128 
129         // put them in
130         for ( int i = 0; i <= items; i++ )
131         {
132             jcs.put( i + ":key", "data" + i );
133         }
134 
135         // wait a bit for it to finish
136         Thread.sleep( items / 20 );
137 
138         // test to see if the count is right
139         assertTrue( "The number of ELEMENT_EVENT_SPOOLED_NOT_ALLOWED events [" + meh.getSpoolNotAllowedCount()
140             + "] does not equal the number expected.", meh.getSpoolNotAllowedCount() >= items );
141 
142     }
143 
144     /**
145      * Test the ELEMENT_EVENT_SPOOLED_NOT_ALLOWED event.
146      * @throws Exception
147      */
148     public void testSpoolNotAllowedEventOnItem()
149         throws Exception
150     {
151         MyEventHandler meh = new MyEventHandler();
152 
153         CacheAccess<String, String> jcs = JCS.getInstance( "DiskButNotAllowed" );
154         // this should add the event handler to all items as they are created.
155         //IElementAttributes attributes = jcs.getDefaultElementAttributes();
156         //attributes.addElementEventHandler( meh );
157         //jcs.setDefaultElementAttributes( attributes );
158 
159         // put them in
160         for ( int i = 0; i <= items; i++ )
161         {
162             IElementAttributes attributes = jcs.getDefaultElementAttributes();
163             attributes.addElementEventHandler( meh );
164             jcs.put( i + ":key", "data" + i, attributes );
165         }
166 
167         // wait a bit for it to finish
168         Thread.sleep( items / 20 );
169 
170         // test to see if the count is right
171         assertTrue( "The number of ELEMENT_EVENT_SPOOLED_NOT_ALLOWED events [" + meh.getSpoolNotAllowedCount()
172             + "] does not equal the number expected.", meh.getSpoolNotAllowedCount() >= items );
173 
174     }
175 
176     /**
177      * Test the ELEMENT_EVENT_EXCEEDED_MAXLIFE_ONREQUEST event.
178      * @throws Exception
179      */
180     public void testExceededMaxlifeOnrequestEvent()
181         throws Exception
182     {
183         MyEventHandler meh = new MyEventHandler();
184 
185         CacheAccess<String, String> jcs = JCS.getInstance( "Maxlife" );
186         // this should add the event handler to all items as they are created.
187         IElementAttributes attributes = jcs.getDefaultElementAttributes();
188         attributes.addElementEventHandler( meh );
189         jcs.setDefaultElementAttributes( attributes );
190 
191         // put them in
192         for ( int i = 0; i < 200; i++ )
193         {
194             jcs.put( i + ":key", "data" + i);
195         }
196 
197         // wait a bit for the items to expire
198         Thread.sleep( 3000 );
199 
200         for ( int i = 0; i < 200; i++ )
201         {
202             String value = jcs.get( i + ":key");
203             assertNull("Item should be null for key " + i + ":key, but is " + value, value);
204         }
205 
206         // wait a bit for it to finish
207         Thread.sleep( 100 );
208 
209         // test to see if the count is right
210         assertTrue( "The number of ELEMENT_EVENT_EXCEEDED_MAXLIFE_ONREQUEST events [" + meh.getExceededMaxlifeCount()
211             + "] does not equal the number expected.", meh.getExceededMaxlifeCount() >= 200 );
212     }
213 
214     /**
215      * Test the ELEMENT_EVENT_EXCEEDED_IDLETIME_ONREQUEST event.
216      * @throws Exception
217      */
218     public void testExceededIdletimeOnrequestEvent()
219         throws Exception
220     {
221         MyEventHandler meh = new MyEventHandler();
222 
223         CacheAccess<String, String> jcs = JCS.getInstance( "Idletime" );
224         // this should add the event handler to all items as they are created.
225         IElementAttributes attributes = jcs.getDefaultElementAttributes();
226         attributes.addElementEventHandler( meh );
227         jcs.setDefaultElementAttributes( attributes );
228 
229         // put them in
230         for ( int i = 0; i < 200; i++ )
231         {
232             jcs.put( i + ":key", "data" + i);
233         }
234 
235         // update access time
236         for ( int i = 0; i < 200; i++ )
237         {
238             String value = jcs.get( i + ":key");
239             assertNotNull("Item should not be null for key " + i + ":key", value);
240         }
241 
242         // wait a bit for the items to expire
243         Thread.sleep( 1500 );
244 
245         for ( int i = 0; i < 200; i++ )
246         {
247             String value = jcs.get( i + ":key");
248             assertNull("Item should be null for key " + i + ":key, but is " + value, value);
249         }
250 
251         // wait a bit for it to finish
252         Thread.sleep( 100 );
253 
254         // test to see if the count is right
255         assertTrue( "The number of ELEMENT_EVENT_EXCEEDED_IDLETIME_ONREQUEST events [" + meh.getExceededIdletimeCount()
256             + "] does not equal the number expected.", meh.getExceededIdletimeCount() >= 200 );
257     }
258 
259     /**
260      * Test that cloned ElementAttributes have different creation times.
261      * @throws Exception
262      */
263     public void testElementAttributesCreationTime()
264         throws Exception
265     {
266     	ElementAttributes elem1 = new ElementAttributes();
267     	long ctime1 = elem1.getCreateTime();
268     	
269     	Thread.sleep(10);
270 
271     	IElementAttributes elem2 = elem1.clone();
272     	long ctime2 = elem2.getCreateTime();
273     	
274     	assertFalse("Creation times should be different", ctime1 == ctime2);
275     }
276     
277     /**
278      * Simple event counter used to verify test results.
279      */
280     public static class MyEventHandler
281         implements IElementEventHandler
282     {
283         /** times spool called */
284         private int spoolCount = 0;
285 
286         /** times spool not allowed */
287         private int spoolNotAllowedCount = 0;
288 
289         /** times spool without disk */
290         private int spoolNoDiskCount = 0;
291 
292         /** times exceeded maxlife */
293         private int exceededMaxlifeCount = 0;
294 
295         /** times exceeded idle time */
296         private int exceededIdletimeCount = 0;
297 
298         /**
299          * @param event
300          */
301         @Override
302         public synchronized <T> void handleElementEvent( IElementEvent<T> event )
303         {
304             //System.out.println( "Handling Event of Type " +
305             // event.getElementEvent() );
306 
307             switch (event.getElementEvent())
308             {
309                 case SPOOLED_DISK_AVAILABLE:
310                 //System.out.println( "Handling Event of Type
311                 // ELEMENT_EVENT_SPOOLED_DISK_AVAILABLE, " + getSpoolCount() );
312                 spoolCount++;
313                 break;
314 
315                 case SPOOLED_NOT_ALLOWED:
316                 spoolNotAllowedCount++;
317                 break;
318 
319                 case SPOOLED_DISK_NOT_AVAILABLE:
320                 spoolNoDiskCount++;
321                 break;
322 
323                 case EXCEEDED_MAXLIFE_ONREQUEST:
324                 exceededMaxlifeCount++;
325                 break;
326 
327                 case EXCEEDED_IDLETIME_ONREQUEST:
328                 exceededIdletimeCount++;
329                 break;
330 
331                 case EXCEEDED_IDLETIME_BACKGROUND:
332                 break;
333 
334                 case EXCEEDED_MAXLIFE_BACKGROUND:
335                 break;
336             }
337         }
338 
339         /**
340          * @return Returns the spoolCount.
341          */
342         protected int getSpoolCount()
343         {
344             return spoolCount;
345         }
346 
347         /**
348          * @return Returns the spoolNotAllowedCount.
349          */
350         protected int getSpoolNotAllowedCount()
351         {
352             return spoolNotAllowedCount;
353         }
354 
355         /**
356          * @return Returns the spoolNoDiskCount.
357          */
358         protected int getSpoolNoDiskCount()
359         {
360             return spoolNoDiskCount;
361         }
362 
363         /**
364          * @return the exceededMaxlifeCount
365          */
366         protected int getExceededMaxlifeCount()
367         {
368             return exceededMaxlifeCount;
369         }
370 
371         /**
372          * @return the exceededIdletimeCount
373          */
374         protected int getExceededIdletimeCount()
375         {
376             return exceededIdletimeCount;
377         }
378     }
379 
380 }