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+ }
0 commit comments