View Javadoc

1   /*
2    * Copyright 2007-2008 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ksb.cache;
17  
18  import javax.xml.namespace.QName;
19  
20  import org.junit.Test;
21  import org.kuali.rice.ksb.cache.RiceCacheAdministrator;
22  import org.kuali.rice.ksb.test.KSBTestCase;
23  
24  public class RiceCacheAdministratorTest extends KSBTestCase {
25  	
26  	String key = "_key";
27  	Object value = "_value";
28  	String group = "_group";
29  	
30  	@Override
31  	public boolean startClient1() {
32  		return true;
33  	}
34  
35  	@Test public void testCache() throws Exception {
36  		
37  		loadCaches();
38  		assertCachesNotEmpty();
39  		
40  		//drive invalidation from this cache
41  		RiceCacheAdministrator cache = (RiceCacheAdministrator)getSpringContextResourceLoader().getService(new QName("cache"));
42  		cache.flushAll();
43  		//these waits are extreme and things rarely take this long but when CI bogs down it can take a while
44  		Thread.sleep(4000);
45  		assertCachesEmpty();
46  		
47  		loadCaches();
48  		assertCachesNotEmpty();
49  		
50  		cache.flushEntry(this.key);
51  		Thread.sleep(4000);
52  		assertCachesEmpty();
53  		
54  		loadCaches();
55  		assertCachesNotEmpty();
56  		
57  		cache.flushGroup(this.group);
58  		Thread.sleep(4000);
59  		assertCachesEmpty();
60  		
61  		loadCaches();
62  		assertCachesNotEmpty();
63  
64  	}
65  	
66  	@Test public void testCacheRefreshPeriod() throws Exception {
67  				
68  		loadCaches();
69  		assertCachesAreRefreshed();
70  	}
71  	
72  	private void assertCachesNotEmpty() throws Exception {
73  		RiceCacheAdministrator cache = (RiceCacheAdministrator)getSpringContextResourceLoader().getService(new QName("cache"));
74  		RiceCacheAdministrator client1Cache = (RiceCacheAdministrator)getServiceFromTestClient1SpringContext("cache");
75  		
76  		assertEquals(this.value, cache.getFromCache(this.key));
77  		assertEquals(this.value, client1Cache.getFromCache(this.key));
78  	}
79  		
80  	private void assertCachesEmpty() throws Exception {
81  		RiceCacheAdministrator cache = (RiceCacheAdministrator)getSpringContextResourceLoader().getService(new QName("cache"));
82  		RiceCacheAdministrator client1Cache = (RiceCacheAdministrator)getServiceFromTestClient1SpringContext("cache");
83  		
84  		assertNull(cache.getFromCache(this.key));
85  		assertNull(client1Cache.getFromCache(this.key));
86  	}
87  	
88  	private void loadCaches() throws Exception {	
89  		RiceCacheAdministrator cache = (RiceCacheAdministrator)getSpringContextResourceLoader().getService(new QName("cache"));
90  		RiceCacheAdministrator client1Cache = (RiceCacheAdministrator)getServiceFromTestClient1SpringContext("cache");
91  		
92  		cache.putInCache(this.key, this.value, this.group);
93  		client1Cache.putInCache(this.key, this.value, this.group);
94  	}
95  	
96  	private void assertCachesAreRefreshed() throws Exception {
97  		int refreshSeconds = 10;
98  		RiceCacheAdministrator cache = (RiceCacheAdministrator)getSpringContextResourceLoader().getService(new QName("cache"));
99  		RiceCacheAdministrator client1Cache = (RiceCacheAdministrator)getServiceFromTestClient1SpringContext("cache");
100 		
101 		assertEquals(this.value, cache.getFromCache(this.key, refreshSeconds));
102 		assertEquals(this.value, client1Cache.getFromCache(this.key, refreshSeconds));
103 		
104 		// Sleep for 12 seconds, because roles should only be cached for 10 seconds in this situation.
105 		Thread.sleep(12000);
106 		
107 		assertNull(cache.getFromCache(this.key, refreshSeconds));
108 		assertNull(client1Cache.getFromCache(this.key, refreshSeconds));
109 	}
110 }
111