Add testing dirs
[proteocache.git] / testsrc / compbio / engine / PulledFileCacheTester.java
diff --git a/testsrc/compbio/engine/PulledFileCacheTester.java b/testsrc/compbio/engine/PulledFileCacheTester.java
new file mode 100644 (file)
index 0000000..9201e73
--- /dev/null
@@ -0,0 +1,97 @@
+/* Copyright (c) 2009 Peter Troshin\r
+ *  \r
+ *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     \r
+ * \r
+ *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
+ *  Apache License version 2 as published by the Apache Software Foundation\r
+ * \r
+ *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
+ *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
+ *  License for more details.\r
+ * \r
+ *  A copy of the license is in apache_license.txt. It is also available here:\r
+ * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
+ * \r
+ * Any republication or derived work distributed in source code form\r
+ * must include this copyright and license notice.\r
+ */\r
+\r
+package compbio.engine;\r
+\r
+import static org.testng.Assert.assertEquals;\r
+import static org.testng.Assert.assertNotNull;\r
+import static org.testng.Assert.fail;\r
+\r
+import java.util.concurrent.TimeUnit;\r
+\r
+import org.testng.annotations.Test;\r
+\r
+import compbio.metadata.AllTestSuit;\r
+\r
+public class PulledFileCacheTester {\r
+\r
+       @Test\r
+       public void test() {\r
+               try {\r
+                       FilePuller fp1 = FilePuller.newFilePuller(\r
+                                       AllTestSuit.TEST_DATA_PATH_ABSOLUTE + "1", 256);\r
+                       fp1.setDelay(2, TimeUnit.SECONDS);\r
+\r
+                       FilePuller fp2 = FilePuller.newFilePuller(\r
+                                       AllTestSuit.TEST_DATA_PATH_ABSOLUTE + "2", 256);\r
+                       fp2.setDelay(1, TimeUnit.SECONDS);\r
+\r
+                       FilePuller fp3 = FilePuller.newFilePuller(\r
+                                       AllTestSuit.TEST_DATA_PATH_ABSOLUTE + "3", 256);\r
+                       fp3.setDelay(1, TimeUnit.SECONDS);\r
+\r
+                       FilePuller fp4 = FilePuller.newFilePuller(\r
+                                       AllTestSuit.TEST_DATA_PATH_ABSOLUTE + "4", 256);\r
+                       fp4.setDelay(5, TimeUnit.SECONDS);\r
+\r
+                       // This only hold if tested in isolation thus clear is essential\r
+                       PulledFileCache.clear();\r
+                       assertEquals(PulledFileCache.getSize(), 0);\r
+                       PulledFileCache.put(fp1);\r
+                       PulledFileCache.put(fp2);\r
+                       PulledFileCache.put(fp3);\r
+                       PulledFileCache.put(fp4);\r
+                       assertEquals(PulledFileCache.getSize(), 4);\r
+                       Thread.sleep(1000);\r
+                       // sweep was not called yet\r
+                       assertEquals(PulledFileCache.getSize(), 4);\r
+                       // now sweep is called\r
+                       PulledFileCache.put(fp1);\r
+                       // fp1 and fp1 and fp4 only remains - copies are allowed this is\r
+                       // responsibility of the caller to ensure they are not there\r
+                       assertEquals(PulledFileCache.getSize(), 3);\r
+                       assertNotNull(PulledFileCache\r
+                                       .get(AllTestSuit.TEST_DATA_PATH_ABSOLUTE + "1"));\r
+                       assertNotNull(PulledFileCache\r
+                                       .get(AllTestSuit.TEST_DATA_PATH_ABSOLUTE + "4"));\r
+\r
+                       for (int i = 0; i < 4; i++) {\r
+                               Thread.sleep(1000);\r
+                               FilePuller fp = PulledFileCache\r
+                                               .get(AllTestSuit.TEST_DATA_PATH_ABSOLUTE + "4");\r
+                               // This will update access time\r
+                               fp.isFileCreated();\r
+                       }\r
+                       // still fp1 and fp4 only remains\r
+\r
+                       assertEquals(PulledFileCache.getSize(), 3);\r
+                       PulledFileCache.put(FilePuller.newFilePuller(\r
+                                       AllTestSuit.TEST_DATA_PATH_ABSOLUTE + "5", 256));\r
+                       // at this point only fp4 and 5 will remain\r
+                       assertEquals(PulledFileCache.getSize(), 2);\r
+                       assertNotNull(PulledFileCache\r
+                                       .get(AllTestSuit.TEST_DATA_PATH_ABSOLUTE + "4"));\r
+                       assertNotNull(PulledFileCache\r
+                                       .get(AllTestSuit.TEST_DATA_PATH_ABSOLUTE + "5"));\r
+\r
+               } catch (InterruptedException e) {\r
+                       fail(e.getMessage());\r
+               }\r
+       }\r
+\r
+}\r