9201e73e853fbb9f9734d7cbcadef3dd70148b93
[proteocache.git] / testsrc / compbio / engine / PulledFileCacheTester.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 \r
19 package compbio.engine;\r
20 \r
21 import static org.testng.Assert.assertEquals;\r
22 import static org.testng.Assert.assertNotNull;\r
23 import static org.testng.Assert.fail;\r
24 \r
25 import java.util.concurrent.TimeUnit;\r
26 \r
27 import org.testng.annotations.Test;\r
28 \r
29 import compbio.metadata.AllTestSuit;\r
30 \r
31 public class PulledFileCacheTester {\r
32 \r
33         @Test\r
34         public void test() {\r
35                 try {\r
36                         FilePuller fp1 = FilePuller.newFilePuller(\r
37                                         AllTestSuit.TEST_DATA_PATH_ABSOLUTE + "1", 256);\r
38                         fp1.setDelay(2, TimeUnit.SECONDS);\r
39 \r
40                         FilePuller fp2 = FilePuller.newFilePuller(\r
41                                         AllTestSuit.TEST_DATA_PATH_ABSOLUTE + "2", 256);\r
42                         fp2.setDelay(1, TimeUnit.SECONDS);\r
43 \r
44                         FilePuller fp3 = FilePuller.newFilePuller(\r
45                                         AllTestSuit.TEST_DATA_PATH_ABSOLUTE + "3", 256);\r
46                         fp3.setDelay(1, TimeUnit.SECONDS);\r
47 \r
48                         FilePuller fp4 = FilePuller.newFilePuller(\r
49                                         AllTestSuit.TEST_DATA_PATH_ABSOLUTE + "4", 256);\r
50                         fp4.setDelay(5, TimeUnit.SECONDS);\r
51 \r
52                         // This only hold if tested in isolation thus clear is essential\r
53                         PulledFileCache.clear();\r
54                         assertEquals(PulledFileCache.getSize(), 0);\r
55                         PulledFileCache.put(fp1);\r
56                         PulledFileCache.put(fp2);\r
57                         PulledFileCache.put(fp3);\r
58                         PulledFileCache.put(fp4);\r
59                         assertEquals(PulledFileCache.getSize(), 4);\r
60                         Thread.sleep(1000);\r
61                         // sweep was not called yet\r
62                         assertEquals(PulledFileCache.getSize(), 4);\r
63                         // now sweep is called\r
64                         PulledFileCache.put(fp1);\r
65                         // fp1 and fp1 and fp4 only remains - copies are allowed this is\r
66                         // responsibility of the caller to ensure they are not there\r
67                         assertEquals(PulledFileCache.getSize(), 3);\r
68                         assertNotNull(PulledFileCache\r
69                                         .get(AllTestSuit.TEST_DATA_PATH_ABSOLUTE + "1"));\r
70                         assertNotNull(PulledFileCache\r
71                                         .get(AllTestSuit.TEST_DATA_PATH_ABSOLUTE + "4"));\r
72 \r
73                         for (int i = 0; i < 4; i++) {\r
74                                 Thread.sleep(1000);\r
75                                 FilePuller fp = PulledFileCache\r
76                                                 .get(AllTestSuit.TEST_DATA_PATH_ABSOLUTE + "4");\r
77                                 // This will update access time\r
78                                 fp.isFileCreated();\r
79                         }\r
80                         // still fp1 and fp4 only remains\r
81 \r
82                         assertEquals(PulledFileCache.getSize(), 3);\r
83                         PulledFileCache.put(FilePuller.newFilePuller(\r
84                                         AllTestSuit.TEST_DATA_PATH_ABSOLUTE + "5", 256));\r
85                         // at this point only fp4 and 5 will remain\r
86                         assertEquals(PulledFileCache.getSize(), 2);\r
87                         assertNotNull(PulledFileCache\r
88                                         .get(AllTestSuit.TEST_DATA_PATH_ABSOLUTE + "4"));\r
89                         assertNotNull(PulledFileCache\r
90                                         .get(AllTestSuit.TEST_DATA_PATH_ABSOLUTE + "5"));\r
91 \r
92                 } catch (InterruptedException e) {\r
93                         fail(e.getMessage());\r
94                 }\r
95         }\r
96 \r
97 }\r