1
+ package com .lowtuna .jsonblob .core ;
2
+
3
+ import com .fasterxml .jackson .databind .ObjectMapper ;
4
+ import io .dropwizard .util .Duration ;
5
+ import lombok .extern .java .Log ;
6
+ import org .apache .commons .io .FileUtils ;
7
+ import org .bson .types .ObjectId ;
8
+ import org .joda .time .DateTime ;
9
+ import org .junit .Assert ;
10
+ import org .junit .Before ;
11
+ import org .junit .Test ;
12
+
13
+ import java .io .File ;
14
+ import java .io .IOException ;
15
+ import java .nio .file .Files ;
16
+ import java .util .UUID ;
17
+ import java .util .concurrent .Executors ;
18
+
19
+ /**
20
+ * Created by tburch on 8/16/17.
21
+ */
22
+ @ Log
23
+ public class BlobCleanupJobTest {
24
+
25
+ private static final File TEMP ;
26
+ static {
27
+ File temp = FileUtils .getTempDirectory ();
28
+ File dir = new File (temp , UUID .randomUUID ().toString ());
29
+ dir .deleteOnExit ();
30
+ TEMP = dir ;
31
+ }
32
+
33
+ private final Duration blobTtl = Duration .minutes (1 );
34
+
35
+ private FileSystemJsonBlobManager blobManager ;
36
+
37
+ @ Before
38
+ public void initBlobManage () {
39
+ this .blobManager = new FileSystemJsonBlobManager (TEMP , Executors .newSingleThreadScheduledExecutor (), new ObjectMapper (), blobTtl , true );
40
+ }
41
+
42
+ @ Test
43
+ public void testCleanup () throws Exception {
44
+ DateTime now = DateTime .now ();
45
+
46
+ Assert .assertEquals (0 , countFiles ());
47
+ blobManager .createBlob ("{\" foo\" :|\" bar\" }" , (new ObjectId (now .minusDays ((int ) (blobTtl .toMinutes () * 2 )).toDate ())).toString ());
48
+ Assert .assertEquals (1 , countFiles ());
49
+ blobManager .createBlob ("{\" foo\" :|\" bar\" }" , (new ObjectId (now .toDate ())).toString ());
50
+ Assert .assertEquals (2 , countFiles ());
51
+
52
+ log .info ("Starting blob manager" );
53
+ blobManager .start ();
54
+
55
+ Thread .sleep (200000 );
56
+
57
+ Assert .assertEquals (1 , countFiles ());
58
+ }
59
+
60
+ private long countFiles () throws IOException {
61
+ return Files .find (TEMP .toPath (), 999 , (p , bfa ) -> bfa .isRegularFile ()).count ();
62
+ }
63
+
64
+ }
0 commit comments