blob: 331e0e60fe17e03a22449f186e72ea5fbeb82396 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001package net.floodlightcontroller.util;
2
Jonathan Harta88fd242014-04-03 11:24:54 -07003import static org.junit.Assert.assertEquals;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08004
5import org.junit.Before;
6import org.junit.Test;
7
8public class TimedCacheTest {
9 public static class CacheEntry {
10 public int key;
Ray Milkey269ffb92014-04-03 14:43:30 -070011
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080012 public CacheEntry(int key) {
13 this.key = key;
14 }
15
16 @Override
17 public int hashCode() {
18 final int prime = 31;
19 int result = 1;
20 result = prime * result + key;
21 return result;
22 }
23
24 @Override
25 public boolean equals(Object obj) {
26 if (this == obj)
27 return true;
28 if (obj == null)
29 return false;
30 if (getClass() != obj.getClass())
31 return false;
32 CacheEntry other = (CacheEntry) obj;
33 if (key != other.key)
34 return false;
35 return true;
36 }
37 }
Ray Milkey269ffb92014-04-03 14:43:30 -070038
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080039 protected TimedCache<CacheEntry> cache;
Ray Milkey269ffb92014-04-03 14:43:30 -070040
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080041 @Before
42 public void setUp() {
43 //
44 }
Ray Milkey269ffb92014-04-03 14:43:30 -070045
46
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080047 @Test
48 public void testCaching() throws InterruptedException {
49 int timeout = 50;
50 int timeToSleep = 60;
51 cache = new TimedCache<TimedCacheTest.CacheEntry>(100, timeout);
Ray Milkey269ffb92014-04-03 14:43:30 -070052
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080053 CacheEntry e1a = new CacheEntry(1);
54 CacheEntry e1b = new CacheEntry(1);
55 CacheEntry e1c = new CacheEntry(1);
56 CacheEntry e2 = new CacheEntry(2);
Ray Milkey269ffb92014-04-03 14:43:30 -070057
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080058 assertEquals(false, cache.update(e1a));
59 assertEquals(true, cache.update(e1a));
60 assertEquals(true, cache.update(e1b));
61 assertEquals(true, cache.update(e1c));
62 assertEquals(false, cache.update(e2));
63 assertEquals(true, cache.update(e2));
Ray Milkey269ffb92014-04-03 14:43:30 -070064
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080065 Thread.sleep(timeToSleep);
66 assertEquals(false, cache.update(e1a));
67 assertEquals(false, cache.update(e2));
68 }
Ray Milkey269ffb92014-04-03 14:43:30 -070069
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080070 @Test
71 public void testCapacity() throws InterruptedException {
72 int timeout = 5000;
73 cache = new TimedCache<TimedCacheTest.CacheEntry>(2, timeout);
Ray Milkey269ffb92014-04-03 14:43:30 -070074
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080075 // Testing the capacity is tricky since the capacity can be
76 // exceeded for short amounts of time, so we try to flood the cache
77 // to make sure the first entry is expired
78 CacheEntry e1 = new CacheEntry(1);
Ray Milkey269ffb92014-04-03 14:43:30 -070079 for (int i = 0; i < 100; i++) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080080 CacheEntry e = new CacheEntry(i);
81 cache.update(e);
82 }
Ray Milkey269ffb92014-04-03 14:43:30 -070083
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080084 // entry 1 should have been expired due to capacity limits
85 assertEquals(false, cache.update(e1));
86 }
Ray Milkey269ffb92014-04-03 14:43:30 -070087
88
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080089}