Skip to content

Commit bb779d3

Browse files
加入测试
1 parent fa8fa2c commit bb779d3

5 files changed

Lines changed: 238 additions & 0 deletions

File tree

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,8 @@ dependencies {
3535
}
3636

3737
test {
38+
dependencies {
39+
testCompile "junit:junit:4.12"
40+
}
3841
useJUnitPlatform()
3942
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package fybug.nulll.pdcache;
2+
import org.junit.Assert;
3+
import org.junit.runner.RunWith;
4+
import org.junit.runners.Suite;
5+
6+
import java.io.IOException;
7+
import java.io.PrintWriter;
8+
import java.io.StringWriter;
9+
10+
import fybug.nulll.pdcache.memory.memoryTest;
11+
12+
@RunWith( Suite.class )
13+
@Suite.SuiteClasses( {memoryTest.class} )
14+
public
15+
class RunTest {
16+
public static PrintWriter from;
17+
public static PrintWriter to;
18+
19+
private static StringWriter from_s;
20+
private static StringWriter to_s;
21+
22+
public static
23+
void init() {
24+
from = new PrintWriter(from_s = new StringWriter());
25+
to = new PrintWriter(to_s = new StringWriter());
26+
}
27+
28+
public static
29+
void destruction() throws IOException {
30+
from.close();
31+
from = null;
32+
from_s.close();
33+
from_s = null;
34+
35+
to.close();
36+
to = null;
37+
to_s.close();
38+
to_s = null;
39+
}
40+
41+
public static
42+
void check() { Assert.assertEquals(from_s.toString(), to_s.toString()); }
43+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package fybug.nulll.pdcache.memory;
2+
import org.jetbrains.annotations.NotNull;
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.io.IOException;
8+
import java.lang.ref.WeakReference;
9+
10+
import fybug.nulll.pdcache.CanClean;
11+
12+
import static fybug.nulll.pdcache.RunTest.check;
13+
import static fybug.nulll.pdcache.RunTest.destruction;
14+
import static fybug.nulll.pdcache.RunTest.from;
15+
import static fybug.nulll.pdcache.RunTest.init;
16+
import static fybug.nulll.pdcache.RunTest.to;
17+
18+
public
19+
class CacheTest {
20+
private Cache<Object> cache;
21+
22+
@Before
23+
public
24+
void setUp() {
25+
init();
26+
cache = Cache.build(Object.class).refernce(WeakReference.class).build();
27+
}
28+
29+
@After
30+
public
31+
void tearDown() throws IOException {
32+
destruction();
33+
cache.clear();
34+
}
35+
36+
@Test
37+
public
38+
void cache() throws Exception {
39+
var o = new Object();
40+
41+
from.println(o);
42+
cache.set(o);
43+
cache.get(to::println);
44+
45+
o = null;
46+
System.gc();
47+
from.println("null");
48+
cache.get(to::println);
49+
50+
o = new Object();
51+
52+
from.println(o);
53+
cache.set(o);
54+
cache.get(to::println);
55+
56+
check();
57+
}
58+
59+
@Test
60+
public
61+
void cache1() throws Exception {
62+
CanClean o = new CanClean() {
63+
public @NotNull
64+
Runnable getclean() { return () -> to.println("des:"); }
65+
};
66+
67+
from.println(o);
68+
cache.set(o);
69+
cache.get(to::println);
70+
71+
o = null;
72+
System.gc();
73+
from.println("des:");
74+
from.println("null");
75+
cache.get(to::println);
76+
77+
o = new CanClean() {
78+
public @NotNull
79+
Runnable getclean() { return () -> {}; }
80+
};
81+
82+
from.println(o);
83+
cache.set(o);
84+
cache.get(to::println);
85+
86+
check();
87+
}
88+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package fybug.nulll.pdcache.memory;
2+
import org.jetbrains.annotations.NotNull;
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.io.IOException;
8+
import java.lang.ref.WeakReference;
9+
10+
import fybug.nulll.pdcache.CanClean;
11+
12+
import static fybug.nulll.pdcache.RunTest.check;
13+
import static fybug.nulll.pdcache.RunTest.destruction;
14+
import static fybug.nulll.pdcache.RunTest.from;
15+
import static fybug.nulll.pdcache.RunTest.init;
16+
import static fybug.nulll.pdcache.RunTest.to;
17+
public
18+
class MapCacheTest {
19+
private MapCache<String, Object> cache;
20+
21+
@Before
22+
public
23+
void setUp() {
24+
init();
25+
cache = MapCache.build(String.class, Object.class).refernce(WeakReference.class).build();
26+
}
27+
28+
@After
29+
public
30+
void tearDown() throws IOException {
31+
destruction();
32+
cache.clear();
33+
}
34+
35+
@Test
36+
public
37+
void cache() throws Exception {
38+
var o = new Object();
39+
40+
from.println(o);
41+
cache.put("asd", o);
42+
cache.get("asd", (k, v) -> to.println(v));
43+
44+
// 模拟回收
45+
o = null;
46+
System.gc();
47+
from.println("null");
48+
cache.get("asd", (k, v) -> to.println(v));
49+
50+
o = new Object();
51+
52+
from.println(o);
53+
cache.put("asd", o);
54+
cache.get("asd", (k, v) -> to.println(v));
55+
56+
check();
57+
}
58+
59+
@Test
60+
public
61+
void cache1() throws Exception {
62+
CanClean o = new CanClean() {
63+
public @NotNull
64+
Runnable getclean() {
65+
return () -> to.println("des:");
66+
}
67+
};
68+
69+
from.println(o);
70+
cache.put("asd", o);
71+
cache.get("asd", (k, v) -> to.println(v));
72+
73+
// 模拟回收
74+
o = null;
75+
System.gc();
76+
from.println("des:");
77+
from.println("null");
78+
cache.get("asd", (k, v) -> to.println(v));
79+
80+
o = new CanClean() {
81+
public @NotNull
82+
Runnable getclean() {
83+
return () -> {};
84+
}
85+
};
86+
87+
from.println(o);
88+
cache.put("asd", o);
89+
cache.get("asd", (k, v) -> to.println(v));
90+
91+
check();
92+
}
93+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package fybug.nulll.pdcache.memory;
2+
import org.junit.runner.RunWith;
3+
import org.junit.runners.Suite;
4+
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.Executors;
7+
8+
@RunWith( Suite.class )
9+
@Suite.SuiteClasses( {CacheTest.class, MapCacheTest.class} )
10+
public
11+
class memoryTest {}

0 commit comments

Comments
 (0)