-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ConsistencyCheckServiceIntegrationTest.java
219 lines (191 loc) · 8.81 KB
/
ConsistencyCheckServiceIntegrationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.legacy.consistency;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.collection.MapUtil;
import org.neo4j.helpers.progress.ProgressMonitorFactory;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.kernel.api.exceptions.TransactionFailureException;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.configuration.Settings;
import org.neo4j.kernel.impl.store.record.NodeRecord;
import org.neo4j.legacy.consistency.ConsistencyCheckService.Result;
import org.neo4j.legacy.consistency.checking.GraphStoreFixture;
import org.neo4j.legacy.consistency.checking.full.ConsistencyCheckIncompleteException;
import org.neo4j.logging.NullLogProvider;
import org.neo4j.test.TargetDirectory;
import org.neo4j.test.TestGraphDatabaseFactory;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.neo4j.helpers.collection.MapUtil.stringMap;
import static org.neo4j.legacy.consistency.ConsistencyCheckService.defaultLogFileName;
import static org.neo4j.test.Property.property;
import static org.neo4j.test.Property.set;
@Ignore("Label scan store to be upgraded")
public class ConsistencyCheckServiceIntegrationTest
{
@Test
public void shouldSucceedIfStoreIsConsistent() throws Exception
{
// given
Date timestamp = new Date();
ConsistencyCheckService service = new ConsistencyCheckService( timestamp );
Config configuration = new Config( settings(), GraphDatabaseSettings.class, ConsistencyCheckSettings.class );
// when
ConsistencyCheckService.Result result = service.runFullConsistencyCheck( fixture.directory(),
configuration, ProgressMonitorFactory.NONE, NullLogProvider.getInstance(),
new DefaultFileSystemAbstraction() );
// then
assertEquals( ConsistencyCheckService.Result.SUCCESS, result );
File reportFile = new File( fixture.directory(), defaultLogFileName( timestamp ) );
assertFalse( "Unexpected generation of consistency check report file: " + reportFile, reportFile.exists() );
}
@Test
public void shouldFailIfTheStoreInNotConsistent() throws Exception
{
// given
breakNodeStore();
Date timestamp = new Date();
ConsistencyCheckService service = new ConsistencyCheckService( timestamp );
Config configuration = new Config( settings(), GraphDatabaseSettings.class, ConsistencyCheckSettings.class );
// when
ConsistencyCheckService.Result result = service.runFullConsistencyCheck( fixture.directory(),
configuration, ProgressMonitorFactory.NONE, NullLogProvider.getInstance(),
new DefaultFileSystemAbstraction() );
// then
assertEquals( ConsistencyCheckService.Result.FAILURE, result );
File reportFile = new File( fixture.directory(), defaultLogFileName( timestamp ) );
assertTrue( "Inconsistency report file " + reportFile + " not generated", reportFile.exists() );
}
@Test
public void shouldWriteInconsistenciesToLogFileAtSpecifiedLocation() throws Exception
{
// given
breakNodeStore();
ConsistencyCheckService service = new ConsistencyCheckService();
File specificLogFile = new File( testDirectory.directory(), "specific_logfile.txt" );
Config configuration = new Config(
settings( ConsistencyCheckSettings.consistency_check_report_file.name(), specificLogFile.getPath() ),
GraphDatabaseSettings.class, ConsistencyCheckSettings.class
);
// when
service.runFullConsistencyCheck( fixture.directory(), configuration,
ProgressMonitorFactory.NONE, NullLogProvider.getInstance(), new DefaultFileSystemAbstraction() );
// then
assertTrue( "Inconsistency report file " + specificLogFile + " not generated", specificLogFile.exists() );
}
@Test
public void shouldNotReportDuplicateForHugeLongValues() throws Exception
{
// given
ConsistencyCheckService service = new ConsistencyCheckService();
Config configuration = new Config( settings(), GraphDatabaseSettings.class, ConsistencyCheckSettings.class );
GraphDatabaseService db = new TestGraphDatabaseFactory().newEmbeddedDatabase( testDirectory.graphDbDir() );
String propertyKey = "itemId";
Label label = Label.label( "Item" );
try ( Transaction tx = db.beginTx() )
{
db.schema().constraintFor( label ).assertPropertyIsUnique( propertyKey ).create();
tx.success();
}
try ( Transaction tx = db.beginTx() )
{
set( db.createNode( label ), property( propertyKey, 973305894188596880L ) );
set( db.createNode( label ), property( propertyKey, 973305894188596864L ) );
tx.success();
}
db.shutdown();
// when
Result result = service.runFullConsistencyCheck( testDirectory.graphDbDir(), configuration,
ProgressMonitorFactory.NONE, NullLogProvider.getInstance(), new DefaultFileSystemAbstraction() );
// then
assertEquals( ConsistencyCheckService.Result.SUCCESS, result );
}
@Test
public void shouldAllowGraphCheckDisabled() throws IOException, ConsistencyCheckIncompleteException
{
GraphDatabaseService gds = new GraphDatabaseFactory().newEmbeddedDatabase( testDirectory.absolutePath() );
try ( Transaction tx = gds.beginTx() )
{
gds.createNode();
tx.success();
}
gds.shutdown();
ConsistencyCheckService service = new ConsistencyCheckService();
Config configuration = new Config( settings(), GraphDatabaseSettings.class, ConsistencyCheckSettings.class );
configuration.applyChanges( MapUtil.stringMap( ConsistencyCheckSettings.consistency_check_graph.name(),
Settings.FALSE ) );
// when
Result result = service.runFullConsistencyCheck( testDirectory.graphDbDir(), configuration,
ProgressMonitorFactory.NONE, NullLogProvider.getInstance(), new DefaultFileSystemAbstraction() );
// then
assertEquals( ConsistencyCheckService.Result.SUCCESS, result );
}
protected Map<String,String> settings( String... strings )
{
Map<String, String> defaults = new HashMap<>();
defaults.put( GraphDatabaseSettings.pagecache_memory.name(), "8m" );
return stringMap( defaults, strings );
}
private void breakNodeStore() throws TransactionFailureException
{
fixture.apply( new GraphStoreFixture.Transaction()
{
@Override
protected void transactionData( GraphStoreFixture.TransactionDataBuilder tx,
GraphStoreFixture.IdGenerator next )
{
tx.create( new NodeRecord( next.node(), false, next.relationship(), -1 ) );
}
} );
}
@Rule
public final GraphStoreFixture fixture = new GraphStoreFixture()
{
@Override
protected void generateInitialData( GraphDatabaseService graphDb )
{
try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() )
{
Node node1 = set( graphDb.createNode() );
Node node2 = set( graphDb.createNode(), property( "key", "value" ) );
node1.createRelationshipTo( node2, RelationshipType.withName( "C" ) );
tx.success();
}
}
};
@Rule
public final TargetDirectory.TestDirectory testDirectory = TargetDirectory.testDirForTest( getClass() );
}