pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / index / TestCrash.java
1 package org.apache.lucene.index;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.IOException;
21 import java.util.Random;
22
23 import org.apache.lucene.util.LuceneTestCase;
24 import org.apache.lucene.store.Directory;
25 import org.apache.lucene.store.MockDirectoryWrapper;
26 import org.apache.lucene.store.NoLockFactory;
27 import org.apache.lucene.analysis.MockAnalyzer;
28 import org.apache.lucene.document.Document;
29 import org.apache.lucene.document.Field;
30
31 public class TestCrash extends LuceneTestCase {
32
33   private IndexWriter initIndex(Random random, boolean initialCommit) throws IOException {
34     return initIndex(random, newDirectory(), initialCommit);
35   }
36
37   private IndexWriter initIndex(Random random, MockDirectoryWrapper dir, boolean initialCommit) throws IOException {
38     dir.setLockFactory(NoLockFactory.getNoLockFactory());
39
40     IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
41         TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMaxBufferedDocs(10)
42         .setMergeScheduler(new ConcurrentMergeScheduler())
43         .setMergePolicy(newLogMergePolicy()));
44     ((ConcurrentMergeScheduler) writer.getConfig().getMergeScheduler()).setSuppressExceptions();
45     if (initialCommit) {
46       writer.commit();
47     }
48     
49     Document doc = new Document();
50     doc.add(newField("content", "aaa", Field.Store.YES, Field.Index.ANALYZED));
51     doc.add(newField("id", "0", Field.Store.YES, Field.Index.ANALYZED));
52     for(int i=0;i<157;i++)
53       writer.addDocument(doc);
54
55     return writer;
56   }
57
58   private void crash(final IndexWriter writer) throws IOException {
59     final MockDirectoryWrapper dir = (MockDirectoryWrapper) writer.getDirectory();
60     ConcurrentMergeScheduler cms = (ConcurrentMergeScheduler) writer.getConfig().getMergeScheduler();
61     cms.sync();
62     dir.crash();
63     cms.sync();
64     dir.clearCrash();
65   }
66
67   public void testCrashWhileIndexing() throws IOException {
68     // This test relies on being able to open a reader before any commit
69     // happened, so we must create an initial commit just to allow that, but
70     // before any documents were added.
71     IndexWriter writer = initIndex(random, true);
72     Directory dir = writer.getDirectory();
73     crash(writer);
74     IndexReader reader = IndexReader.open(dir, false);
75     assertTrue(reader.numDocs() < 157);
76     reader.close();
77     dir.close();
78   }
79
80   public void testWriterAfterCrash() throws IOException {
81     // This test relies on being able to open a reader before any commit
82     // happened, so we must create an initial commit just to allow that, but
83     // before any documents were added.
84     IndexWriter writer = initIndex(random, true);
85     MockDirectoryWrapper dir = (MockDirectoryWrapper) writer.getDirectory();
86     dir.setPreventDoubleWrite(false);
87     crash(writer);
88     writer = initIndex(random, dir, false);
89     writer.close();
90
91     IndexReader reader = IndexReader.open(dir, false);
92     assertTrue(reader.numDocs() < 314);
93     reader.close();
94     dir.close();
95   }
96
97   public void testCrashAfterReopen() throws IOException {
98     IndexWriter writer = initIndex(random, false);
99     MockDirectoryWrapper dir = (MockDirectoryWrapper) writer.getDirectory();
100     writer.close();
101     writer = initIndex(random, dir, false);
102     assertEquals(314, writer.maxDoc());
103     crash(writer);
104
105     /*
106     System.out.println("\n\nTEST: open reader");
107     String[] l = dir.list();
108     Arrays.sort(l);
109     for(int i=0;i<l.length;i++)
110       System.out.println("file " + i + " = " + l[i] + " " +
111     dir.fileLength(l[i]) + " bytes");
112     */
113
114     IndexReader reader = IndexReader.open(dir, false);
115     assertTrue(reader.numDocs() >= 157);
116     reader.close();
117     dir.close();
118   }
119
120   public void testCrashAfterClose() throws IOException {
121     
122     IndexWriter writer = initIndex(random, false);
123     MockDirectoryWrapper dir = (MockDirectoryWrapper) writer.getDirectory();
124
125     writer.close();
126     dir.crash();
127
128     /*
129     String[] l = dir.list();
130     Arrays.sort(l);
131     for(int i=0;i<l.length;i++)
132       System.out.println("file " + i + " = " + l[i] + " " + dir.fileLength(l[i]) + " bytes");
133     */
134
135     IndexReader reader = IndexReader.open(dir, false);
136     assertEquals(157, reader.numDocs());
137     reader.close();
138     dir.close();
139   }
140
141   public void testCrashAfterCloseNoWait() throws IOException {
142     
143     IndexWriter writer = initIndex(random, false);
144     MockDirectoryWrapper dir = (MockDirectoryWrapper) writer.getDirectory();
145
146     writer.close(false);
147
148     dir.crash();
149
150     /*
151     String[] l = dir.list();
152     Arrays.sort(l);
153     for(int i=0;i<l.length;i++)
154       System.out.println("file " + i + " = " + l[i] + " " + dir.fileLength(l[i]) + " bytes");
155     */
156     IndexReader reader = IndexReader.open(dir, false);
157     assertEquals(157, reader.numDocs());
158     reader.close();
159     dir.close();
160   }
161
162   public void testCrashReaderDeletes() throws IOException {
163     
164     IndexWriter writer = initIndex(random, false);
165     MockDirectoryWrapper dir = (MockDirectoryWrapper) writer.getDirectory();
166
167     writer.close(false);
168     IndexReader reader = IndexReader.open(dir, false);
169     reader.deleteDocument(3);
170
171     dir.crash();
172
173     /*
174     String[] l = dir.list();
175     Arrays.sort(l);
176     for(int i=0;i<l.length;i++)
177       System.out.println("file " + i + " = " + l[i] + " " + dir.fileLength(l[i]) + " bytes");
178     */
179     reader = IndexReader.open(dir, false);
180     assertEquals(157, reader.numDocs());
181     reader.close();
182     dir.clearCrash();
183     dir.close();
184   }
185
186   public void testCrashReaderDeletesAfterClose() throws IOException {
187     
188     IndexWriter writer = initIndex(random, false);
189     MockDirectoryWrapper dir = (MockDirectoryWrapper) writer.getDirectory();
190
191     writer.close(false);
192     IndexReader reader = IndexReader.open(dir, false);
193     reader.deleteDocument(3);
194     reader.close();
195
196     dir.crash();
197
198     /*
199     String[] l = dir.list();
200     Arrays.sort(l);
201     for(int i=0;i<l.length;i++)
202       System.out.println("file " + i + " = " + l[i] + " " + dir.fileLength(l[i]) + " bytes");
203     */
204     reader = IndexReader.open(dir, false);
205     assertEquals(156, reader.numDocs());
206     reader.close();
207     dir.close();
208   }
209 }