Added ObjectCache class and fixed som hashmap iterator issues
This commit is contained in:
parent
6dd2210be1
commit
2fcc8e98c5
6 changed files with 252 additions and 19 deletions
105
test/zutil/struct/ObjectCacheTest.java
Executable file
105
test/zutil/struct/ObjectCacheTest.java
Executable file
|
|
@ -0,0 +1,105 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2015 Ziver Koc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.struct;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
|
||||
/**
|
||||
* Created by Ziver on 2015-11-20.
|
||||
*/
|
||||
public class ObjectCacheTest {
|
||||
public static final String KEY = "key";
|
||||
public static final String OBJECT = "object";
|
||||
public static final String KEY2 = "key2";
|
||||
public static final String OBJECT2 = "object2";
|
||||
|
||||
|
||||
@Test
|
||||
public void emptyCache() throws InterruptedException {
|
||||
ObjectCache cache = new ObjectCache(10);
|
||||
assertFalse(cache.containsKey(KEY));
|
||||
assertEquals(0, cache.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zeroTTL() throws InterruptedException {
|
||||
ObjectCache cache = new ObjectCache(0);
|
||||
cache.put(KEY, OBJECT);
|
||||
assertEquals(1, cache.size());
|
||||
Thread.sleep(1);
|
||||
assertFalse(cache.containsKey(KEY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tenMsTTL() throws InterruptedException {
|
||||
ObjectCache cache = new ObjectCache(10);
|
||||
cache.put(KEY, OBJECT);
|
||||
assertEquals(OBJECT, cache.get(KEY));
|
||||
Thread.sleep(1);
|
||||
assertTrue(cache.containsKey(KEY));
|
||||
Thread.sleep(10);
|
||||
assertFalse(cache.containsKey(KEY));
|
||||
assertEquals(0, cache.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void oneSecTTL() throws InterruptedException {
|
||||
ObjectCache cache = new ObjectCache(1000);
|
||||
cache.put(KEY, OBJECT);
|
||||
Thread.sleep(1);
|
||||
assertTrue(cache.containsKey(KEY));
|
||||
}
|
||||
|
||||
//@Test
|
||||
// This TC does not work
|
||||
public void javaGRC() throws InterruptedException {
|
||||
ObjectCache cache = new ObjectCache(10000);
|
||||
{
|
||||
String tmp = new String("temporary obj");
|
||||
cache.put(KEY, tmp);
|
||||
assertEquals(1, cache.size());
|
||||
}
|
||||
System.gc(); // will probably not do anything
|
||||
Thread.sleep(10);
|
||||
assertEquals(1, cache.garbageCollect());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void grc() throws InterruptedException {
|
||||
ObjectCache cache = new ObjectCache(10);
|
||||
cache.put(KEY, OBJECT);
|
||||
cache.put(KEY2, OBJECT2);
|
||||
assertEquals(2, cache.size());
|
||||
Thread.sleep(12);
|
||||
assertEquals(2, cache.size());
|
||||
assertEquals(2, cache.garbageCollect());
|
||||
assertEquals(0, cache.size());
|
||||
}
|
||||
}
|
||||
|
|
@ -26,6 +26,8 @@ package zutil.struct;
|
|||
|
||||
import org.junit.Test;
|
||||
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
|
||||
|
|
@ -34,6 +36,7 @@ import static org.junit.Assert.assertFalse;
|
|||
*/
|
||||
public class TimedHashSetTest {
|
||||
public static final String ENTRY = "key";
|
||||
public static final String ENTRY2 = "key2";
|
||||
|
||||
@Test
|
||||
public void zeroTTL() throws InterruptedException {
|
||||
|
|
@ -48,7 +51,7 @@ public class TimedHashSetTest {
|
|||
TimedHashSet set = new TimedHashSet(10);
|
||||
set.add(ENTRY);
|
||||
Thread.sleep(1);
|
||||
assert(set.contains(ENTRY));
|
||||
assertTrue(set.contains(ENTRY));
|
||||
Thread.sleep(10);
|
||||
assertFalse(set.contains(ENTRY));
|
||||
}
|
||||
|
|
@ -58,6 +61,18 @@ public class TimedHashSetTest {
|
|||
TimedHashSet set = new TimedHashSet(1000);
|
||||
set.add(ENTRY);
|
||||
Thread.sleep(1);
|
||||
assert(set.contains(ENTRY));
|
||||
assertTrue(set.contains(ENTRY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void grc() throws InterruptedException {
|
||||
TimedHashSet set = new TimedHashSet(10);
|
||||
set.add(ENTRY);
|
||||
set.add(ENTRY2);
|
||||
assertEquals(2, set.size());
|
||||
Thread.sleep(12);
|
||||
assertEquals(2, set.size());
|
||||
assertEquals(2, set.garbageCollect());
|
||||
assertEquals(0, set.size());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue