1   /* StoredQueueTest.java
2    *
3    * $Id: StoredQueueTest.java 5197 2007-06-06 01:31:46Z gojomo $
4    *
5    * Created on Jun 14, 2007
6    *
7    * Copyright (C) 2007 Internet Archive
8    *
9    * This file is part of the Heritrix web crawler (crawler.archive.org).
10   *
11   * Heritrix is free software; you can redistribute it and/or modify
12   * it under the terms of the GNU Lesser Public License as published by
13   * the Free Software Foundation; either version 2.1 of the License, or
14   * any later version.
15   *
16   * Heritrix is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU Lesser Public License for more details.
20   *
21   * You should have received a copy of the GNU Lesser Public License
22   * along with Heritrix; if not, write to the Free Software
23   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24   */
25  package org.archive.queue;
26  
27  import java.io.File;
28  import java.util.NoSuchElementException;
29  import java.util.concurrent.LinkedBlockingQueue;
30  
31  import org.archive.util.FileUtils;
32  import org.archive.util.TmpDirTestCase;
33  import org.archive.util.bdbje.EnhancedEnvironment;
34  
35  import com.sleepycat.je.Database;
36  import com.sleepycat.je.DatabaseConfig;
37  import com.sleepycat.je.DatabaseException;
38  import com.sleepycat.je.EnvironmentConfig;
39  
40  public class StoredQueueTest extends TmpDirTestCase {
41      StoredQueue<String> queue;
42      EnhancedEnvironment env;
43      Database db; 
44      File envDir; 
45  
46      protected void setUp() throws Exception {
47          super.setUp();
48          this.envDir = new File(getTmpDir(),"StoredMapTest");
49          this.envDir.mkdirs();
50          try {
51              EnvironmentConfig envConfig = new EnvironmentConfig();
52              envConfig.setTransactional(false);
53              envConfig.setAllowCreate(true);
54              env = new EnhancedEnvironment(envDir,envConfig);
55              DatabaseConfig dbConfig = StoredQueue.databaseConfig();
56              db = env.openDatabase(null, "StoredMapTest", dbConfig);
57          } catch (DatabaseException e) {
58              throw new RuntimeException(e);
59          }
60          this.queue = new StoredQueue<String>(db, String.class, env.getClassCatalog());
61      }
62      
63      protected void tearDown() throws Exception {
64          db.close();
65          env.close(); 
66          FileUtils.deleteDir(this.envDir);
67          super.tearDown();
68      }
69      
70      public void testAdd() {
71          assertEquals("not empty at start",0,queue.size());
72          fill(queue, 10);
73          assertEquals("unexpected size at full",10,queue.size());
74      }
75  
76      /***
77       * @deprecated Use {@link #fill(Queue,int)} instead
78       */
79      protected void fill(int size) {
80          fill(queue, size);
81      }
82  
83      protected void fill(java.util.Queue<String> q, int size) {
84          for(int i = 1; i <= size; i++) {
85              q.add("item-"+i);
86          }
87      }
88      
89      protected int drain(java.util.Queue<String> q) {
90          int count = 0; 
91          while(true) {
92              try {
93                  q.remove();
94                  count++;
95              } catch(NoSuchElementException nse) {
96                  return count;
97              }
98          }
99      }
100 
101     public void testClear() {
102         fill(queue, 10);
103         queue.clear();
104         assertEquals("unexpected size after clear",0,queue.size());
105     }
106 
107     public void testRemove() {
108         fill(queue, 10);
109         assertEquals("unexpected remove value","item-1",queue.remove());
110         assertEquals("improper count of removed items",9,drain(queue));
111         try {
112             queue.remove();
113             fail("expected NoSuchElementException not received");
114         } catch (NoSuchElementException nse) {
115             // do nothing
116         }
117     }
118     
119     public void testOrdering() {
120         fill(queue, 10);
121         for(int i = 1; i <= 10; i++) {
122             assertEquals("unexpected remove value","item-"+i,queue.remove());
123         }
124     }
125 
126     public void testElement() {
127         fill(queue, 10);
128         assertEquals("unexpected element value","item-1",queue.element());
129         assertEquals("unexpected element value",queue.peek(),queue.element());
130         queue.clear();
131         try {
132             queue.element();
133             fail("expected NoSuchElementException not received");
134         } catch (NoSuchElementException nse) {
135             // do nothing
136         }
137     }
138 
139     public void xestTimingsAgainstLinkedBlockingQueue() {
140         tryTimings(50000);
141         tryTimings(500000);
142     }
143 
144     private void tryTimings(int i) {
145         LinkedBlockingQueue<String> lbq = new LinkedBlockingQueue<String>();
146         long start = System.currentTimeMillis();
147         fill(lbq,i);
148         drain(lbq);
149         long finish = System.currentTimeMillis();
150         System.out.println("LBQ - "+i+":"+(finish-start));
151         start = System.currentTimeMillis();
152         fill(queue,i);
153         drain(queue);
154         finish = System.currentTimeMillis();
155         System.out.println("SQ - "+i+":"+(finish-start));
156     }
157 }