pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / util / TestTwoPhaseCommitTool.java
1 package org.apache.lucene.util;
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.HashMap;
22 import java.util.Map;
23
24 import org.apache.lucene.util.TwoPhaseCommitTool.TwoPhaseCommitWrapper;
25
26 public class TestTwoPhaseCommitTool extends LuceneTestCase {
27
28   private static class TwoPhaseCommitImpl implements TwoPhaseCommit {
29     static boolean commitCalled = false;
30     final boolean failOnPrepare;
31     final boolean failOnCommit;
32     final boolean failOnRollback;
33     boolean rollbackCalled = false;
34     Map<String, String> prepareCommitData = null;
35     Map<String, String> commitData = null;
36
37     public TwoPhaseCommitImpl(boolean failOnPrepare, boolean failOnCommit, boolean failOnRollback) {
38       this.failOnPrepare = failOnPrepare;
39       this.failOnCommit = failOnCommit;
40       this.failOnRollback = failOnRollback;
41     }
42
43     public void prepareCommit() throws IOException {
44       prepareCommit(null);
45     }
46
47     public void prepareCommit(Map<String, String> commitData) throws IOException {
48       this.prepareCommitData = commitData;
49       assertFalse("commit should not have been called before all prepareCommit were", commitCalled);
50       if (failOnPrepare) {
51         throw new IOException("failOnPrepare");
52       }
53     }
54
55     public void commit() throws IOException {
56       commit(null);
57     }
58
59     public void commit(Map<String, String> commitData) throws IOException {
60       this.commitData = commitData;
61       commitCalled = true;
62       if (failOnCommit) {
63         throw new RuntimeException("failOnCommit");
64       }
65     }
66
67     public void rollback() throws IOException {
68       rollbackCalled = true;
69       if (failOnRollback) {
70         throw new Error("failOnRollback");
71       }
72     }
73   }
74
75   @Override
76   public void setUp() throws Exception {
77     super.setUp();
78     TwoPhaseCommitImpl.commitCalled = false; // reset count before every test
79   }
80
81   public void testPrepareThenCommit() throws Exception {
82     // tests that prepareCommit() is called on all objects before commit()
83     TwoPhaseCommitImpl[] objects = new TwoPhaseCommitImpl[2];
84     for (int i = 0; i < objects.length; i++) {
85       objects[i] = new TwoPhaseCommitImpl(false, false, false);
86     }
87
88     // following call will fail if commit() is called before all prepare() were
89     TwoPhaseCommitTool.execute(objects);
90   }
91
92   public void testRollback() throws Exception {
93     // tests that rollback is called if failure occurs at any stage
94     int numObjects = random.nextInt(8) + 3; // between [3, 10]
95     TwoPhaseCommitImpl[] objects = new TwoPhaseCommitImpl[numObjects];
96     for (int i = 0; i < objects.length; i++) {
97       boolean failOnPrepare = random.nextBoolean();
98       // we should not hit failures on commit usually
99       boolean failOnCommit = random.nextDouble() < 0.05;
100       boolean railOnRollback = random.nextBoolean();
101       objects[i] = new TwoPhaseCommitImpl(failOnPrepare, failOnCommit, railOnRollback);
102     }
103
104     boolean anyFailure = false;
105     try {
106       TwoPhaseCommitTool.execute(objects);
107     } catch (Throwable t) {
108       anyFailure = true;
109     }
110
111     if (anyFailure) {
112       // if any failure happened, ensure that rollback was called on all.
113       for (TwoPhaseCommitImpl tpc : objects) {
114         assertTrue("rollback was not called while a failure occurred during the 2-phase commit", tpc.rollbackCalled);
115       }
116     }
117   }
118
119   public void testWrapper() throws Exception {
120     // tests that TwoPhaseCommitWrapper delegates prepare/commit w/ commitData
121     TwoPhaseCommitImpl impl = new TwoPhaseCommitImpl(false, false, false);
122     HashMap<String, String> commitData = new HashMap<String, String>();
123     TwoPhaseCommitWrapper wrapper = new TwoPhaseCommitWrapper(impl, commitData);
124
125     wrapper.prepareCommit();
126     assertSame(commitData, impl.prepareCommitData);
127
128     // wrapper should ignore passed commitData
129     wrapper.prepareCommit(new HashMap<String, String>());
130     assertSame(commitData, impl.prepareCommitData);
131
132     wrapper.commit();
133     assertSame(commitData, impl.commitData);
134
135     // wrapper should ignore passed commitData
136     wrapper.commit(new HashMap<String, String>());
137     assertSame(commitData, impl.commitData);
138   }
139
140   public void testNullTPCs() throws Exception {
141     int numObjects = random.nextInt(4) + 3; // between [3, 6]
142     TwoPhaseCommit[] tpcs = new TwoPhaseCommit[numObjects];
143     boolean setNull = false;
144     for (int i = 0; i < tpcs.length; i++) {
145       boolean isNull = random.nextDouble() < 0.3;
146       if (isNull) {
147         setNull = true;
148         tpcs[i] = null;
149       } else {
150         tpcs[i] = new TwoPhaseCommitImpl(false, false, false);
151       }
152     }
153
154     if (!setNull) {
155       // none of the TPCs were picked to be null, pick one at random
156       int idx = random.nextInt(numObjects);
157       tpcs[idx] = null;
158     }
159
160     // following call would fail if TPCTool won't handle null TPCs properly
161     TwoPhaseCommitTool.execute(tpcs);
162   }
163
164 }