-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CombinedIndexAccessorTest.java
383 lines (324 loc) · 13.8 KB
/
CombinedIndexAccessorTest.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* Copyright (c) 2002-2017 "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.kernel.impl.index.schema.combined;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.Set;
import org.neo4j.helpers.collection.BoundedIterable;
import org.neo4j.helpers.collection.Iterables;
import org.neo4j.helpers.collection.Iterators;
import org.neo4j.kernel.api.index.IndexAccessor;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.sameInstance;
import static org.hamcrest.core.AnyOf.anyOf;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.internal.verification.VerificationModeFactory.times;
import static org.neo4j.kernel.impl.index.schema.combined.CombinedIndexTestHelp.verifyCombinedCloseThrowIfBothThrow;
import static org.neo4j.kernel.impl.index.schema.combined.CombinedIndexTestHelp.verifyCombinedCloseThrowOnSingleCloseThrow;
import static org.neo4j.kernel.impl.index.schema.combined.CombinedIndexTestHelp.verifyOtherIsClosedOnSingleThrow;
public class CombinedIndexAccessorTest
{
private IndexAccessor boostAccessor;
private IndexAccessor fallbackAccessor;
private CombinedIndexAccessor combinedIndexAccessor;
@Before
public void setup()
{
boostAccessor = mock( IndexAccessor.class );
fallbackAccessor = mock( IndexAccessor.class );
combinedIndexAccessor = new CombinedIndexAccessor( boostAccessor, fallbackAccessor );
}
/* drop */
@Test
public void dropMustDropBoostAndFallback() throws Exception
{
// when
// ... both drop successful
combinedIndexAccessor.drop();
// then
verify( boostAccessor, times( 1 ) ).drop();
verify( fallbackAccessor, times( 1 ) ).drop();
}
@Test
public void dropMustThrowIfDropBoostFail() throws Exception
{
// when
verifyFailOnSingleDropFailure( boostAccessor, combinedIndexAccessor );
}
@Test
public void dropMustThrowIfDropFallbackFail() throws Exception
{
// when
verifyFailOnSingleDropFailure( fallbackAccessor, combinedIndexAccessor );
}
private void verifyFailOnSingleDropFailure( IndexAccessor failingAccessor, CombinedIndexAccessor combinedIndexAccessor )
throws IOException
{
IOException expectedFailure = new IOException( "fail" );
doThrow( expectedFailure ).when( failingAccessor ).drop();
try
{
combinedIndexAccessor.drop();
fail( "Should have failed" );
}
catch ( IOException e )
{
assertSame( expectedFailure, e );
}
}
@Test
public void dropMustThrowIfBothFail() throws Exception
{
// given
IOException boostFailure = new IOException( "boost" );
IOException fallbackFailure = new IOException( "fallback" );
doThrow( boostFailure ).when( boostAccessor ).drop();
doThrow( fallbackFailure ).when( fallbackAccessor ).drop();
try
{
// when
combinedIndexAccessor.drop();
fail( "Should have failed" );
}
catch ( IOException e )
{
// then
assertThat( e, anyOf( sameInstance( boostFailure ), sameInstance( fallbackFailure ) ) );
}
}
/* close */
@Test
public void closeMustCloseBoostAndFallback() throws Exception
{
// when
// ... both drop successful
combinedIndexAccessor.close();
// then
verify( boostAccessor, times( 1 ) ).close();
verify( fallbackAccessor, times( 1 ) ).close();
}
@Test
public void closeMustThrowIfFallbackThrow() throws Exception
{
verifyCombinedCloseThrowOnSingleCloseThrow( fallbackAccessor, combinedIndexAccessor );
}
@Test
public void closeMustThrowIfBoostThrow() throws Exception
{
verifyCombinedCloseThrowOnSingleCloseThrow( boostAccessor, combinedIndexAccessor );
}
@Test
public void closeMustCloseBoostIfFallbackThrow() throws Exception
{
verifyOtherIsClosedOnSingleThrow( fallbackAccessor, boostAccessor, combinedIndexAccessor );
}
@Test
public void closeMustCloseFallbackIfBoostThrow() throws Exception
{
verifyOtherIsClosedOnSingleThrow( boostAccessor, fallbackAccessor, combinedIndexAccessor );
}
@Test
public void closeMustThrowIfBothFail() throws Exception
{
verifyCombinedCloseThrowIfBothThrow( boostAccessor, fallbackAccessor, combinedIndexAccessor );
}
// newAllEntriesReader
@Test
public void allEntriesReaderMustCombineResultFromBoostAndFallback() throws Exception
{
// given
long[] boostEntries = {0, 1, 2, 5, 6};
long[] fallbackEntries = {3, 4, 7, 8};
mockAllEntriesReaders( boostEntries, fallbackEntries );
// when
Set<Long> result = Iterables.asSet( combinedIndexAccessor.newAllEntriesReader() );
// then
assertResultContainsAll( result, boostEntries );
assertResultContainsAll( result, fallbackEntries );
}
@Test
public void allEntriesReaderMustCombineResultFromBoostAndFallbackWithEmptyBoost() throws Exception
{
// given
long[] boostEntries = new long[0];
long[] fallbackEntries = {3, 4, 7, 8};
mockAllEntriesReaders( boostEntries, fallbackEntries );
// when
Set<Long> result = Iterables.asSet( combinedIndexAccessor.newAllEntriesReader() );
// then
assertResultContainsAll( result, boostEntries );
assertResultContainsAll( result, fallbackEntries );
}
@Test
public void allEntriesReaderMustCombineResultFromBoostAndFallbackWithEmptyFallback() throws Exception
{
// given
long[] boostEntries = {0, 1, 2, 5, 6};
long[] fallbackEntries = new long[0];
mockAllEntriesReaders( boostEntries, fallbackEntries );
// when
Set<Long> result = Iterables.asSet( combinedIndexAccessor.newAllEntriesReader() );
// then
assertResultContainsAll( result, boostEntries );
assertResultContainsAll( result, fallbackEntries );
}
@Test
public void allEntriesReaderMustCombineResultFromBoostAndFallbackBothEmpty() throws Exception
{
// given
long[] boostEntries = new long[0];
long[] fallbackEntries = new long[0];
mockAllEntriesReaders( boostEntries, fallbackEntries );
// when
Set<Long> result = Iterables.asSet( combinedIndexAccessor.newAllEntriesReader() );
// then
assertResultContainsAll( result, boostEntries );
assertResultContainsAll( result, fallbackEntries );
assertTrue( result.isEmpty() );
}
@Test
public void allEntriesReaderMustCloseBothBoostAndFallback() throws Exception
{
// given
BoundedIterable<Long> boostAllEntriesReader = mockSingleAllEntriesReader( boostAccessor, new long[0] );
BoundedIterable<Long> fallbackAllEntriesReader = mockSingleAllEntriesReader( fallbackAccessor, new long[0] );
// when
combinedIndexAccessor.newAllEntriesReader().close();
// then
verify( boostAllEntriesReader, times( 1 ) ).close();
verify( fallbackAllEntriesReader, times( 1 ) ).close();
}
@Test
public void allEntriesReaderMustCloseBoostIfFallbackThrow() throws Exception
{
// given
BoundedIterable<Long> boostAllEntriesReader = mockSingleAllEntriesReader( boostAccessor, new long[0] );
BoundedIterable<Long> fallbackAllEntriesReader = mockSingleAllEntriesReader( fallbackAccessor, new long[0] );
// then
BoundedIterable<Long> combinedAllEntriesReader = combinedIndexAccessor.newAllEntriesReader();
verifyOtherIsClosedOnSingleThrow( fallbackAllEntriesReader, boostAllEntriesReader, combinedAllEntriesReader );
}
@Test
public void allEntriesReaderMustCloseFallbackIfBoostThrow() throws Exception
{
// given
BoundedIterable<Long> boostAllEntriesReader = mockSingleAllEntriesReader( boostAccessor, new long[0] );
BoundedIterable<Long> fallbackAllEntriesReader = mockSingleAllEntriesReader( fallbackAccessor, new long[0] );
// then
BoundedIterable<Long> combinedAllEntriesReader = combinedIndexAccessor.newAllEntriesReader();
verifyOtherIsClosedOnSingleThrow( boostAllEntriesReader, fallbackAllEntriesReader, combinedAllEntriesReader );
}
@Test
public void allEntriesReaderMustThrowIfFallbackThrow() throws Exception
{
// given
mockSingleAllEntriesReader( boostAccessor, new long[0] );
BoundedIterable<Long> fallbackAllEntriesReader = mockSingleAllEntriesReader( fallbackAccessor, new long[0] );
// then
BoundedIterable<Long> combinedAllEntriesReader = combinedIndexAccessor.newAllEntriesReader();
CombinedIndexTestHelp.verifyCombinedCloseThrowOnSingleCloseThrow( fallbackAllEntriesReader, combinedAllEntriesReader );
}
@Test
public void allEntriesReaderMustThrowIfBoostThrow() throws Exception
{
// given
BoundedIterable<Long> boostAllEntriesReader = mockSingleAllEntriesReader( boostAccessor, new long[0] );
mockSingleAllEntriesReader( fallbackAccessor, new long[0] );
// then
BoundedIterable<Long> combinedAllEntriesReader = combinedIndexAccessor.newAllEntriesReader();
CombinedIndexTestHelp.verifyCombinedCloseThrowOnSingleCloseThrow( boostAllEntriesReader, combinedAllEntriesReader );
}
@Test
public void allEntriesReaderMustReportUnknownMaxCountIfBoostReportUnknownMaxCount() throws Exception
{
// given
mockSingleAllEntriesReaderWithUnknownMaxCount( boostAccessor, new long[0] );
mockSingleAllEntriesReader( fallbackAccessor, new long[0] );
// then
BoundedIterable<Long> combinedAllEntriesReader = combinedIndexAccessor.newAllEntriesReader();
assertThat( combinedAllEntriesReader.maxCount(), is( BoundedIterable.UNKNOWN_MAX_COUNT ) );
}
@Test
public void allEntriesReaderMustReportUnknownMaxCountIfFallbackReportUnknownMaxCount() throws Exception
{
// given
mockSingleAllEntriesReaderWithUnknownMaxCount( fallbackAccessor, new long[0] );
mockSingleAllEntriesReader( boostAccessor, new long[0] );
// then
BoundedIterable<Long> combinedAllEntriesReader = combinedIndexAccessor.newAllEntriesReader();
assertThat( combinedAllEntriesReader.maxCount(), is( BoundedIterable.UNKNOWN_MAX_COUNT ) );
}
@Test
public void allEntriesReaderMustReportCombinedMaxCountOfBoostAndFallback() throws Exception
{
mockSingleAllEntriesReader( boostAccessor, new long[]{1, 2} );
mockSingleAllEntriesReader( fallbackAccessor, new long[]{3, 4} );
// then
BoundedIterable<Long> combinedAllEntriesReader = combinedIndexAccessor.newAllEntriesReader();
assertThat( combinedAllEntriesReader.maxCount(), is( 4L ) );
}
private void assertResultContainsAll( Set<Long> result, long[] boostEntries )
{
for ( long boostEntry : boostEntries )
{
assertTrue( "Expected to contain " + boostEntry + ", but was " + result, result.contains( boostEntry ) );
}
}
private void mockAllEntriesReaders( long[] boostEntries, long[] fallbackEntries )
{
mockSingleAllEntriesReader( boostAccessor, boostEntries );
mockSingleAllEntriesReader( fallbackAccessor, fallbackEntries );
}
private BoundedIterable<Long> mockSingleAllEntriesReader( IndexAccessor targetAccessor, long[] entries )
{
BoundedIterable<Long> allEntriesReader = mockedAllEntriesReader( entries );
when( targetAccessor.newAllEntriesReader() ).thenReturn( allEntriesReader );
return allEntriesReader;
}
private BoundedIterable<Long> mockedAllEntriesReader( long... entries )
{
return mockedAllEntriesReader( true, entries );
}
private BoundedIterable<Long> mockSingleAllEntriesReaderWithUnknownMaxCount( IndexAccessor targetAccessor, long[] entries )
{
BoundedIterable<Long> allEntriesReader = mockedAllEntriesReaderUnknownMaxCount( entries );
when( targetAccessor.newAllEntriesReader() ).thenReturn( allEntriesReader );
return allEntriesReader;
}
private BoundedIterable<Long> mockedAllEntriesReaderUnknownMaxCount( long... entries )
{
return mockedAllEntriesReader( false, entries );
}
private BoundedIterable<Long> mockedAllEntriesReader( boolean knownMaxCount, long... entries )
{
BoundedIterable<Long> mockedAllEntriesReader = mock( BoundedIterable.class );
when( mockedAllEntriesReader.maxCount() ).thenReturn( knownMaxCount ? entries.length : BoundedIterable.UNKNOWN_MAX_COUNT );
when( mockedAllEntriesReader.iterator() ).thenReturn( Iterators.asIterator(entries ) );
return mockedAllEntriesReader;
}
}