-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
KernelTransactionsTest.java
240 lines (200 loc) · 8.94 KB
/
KernelTransactionsTest.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
/*
* 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.kernel.impl.api;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import java.util.Collection;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.exceptions.TransactionFailureException;
import org.neo4j.kernel.impl.locking.Locks;
import org.neo4j.kernel.impl.proc.Procedures;
import org.neo4j.kernel.impl.transaction.TransactionHeaderInformationFactory;
import org.neo4j.kernel.impl.transaction.TransactionMonitor;
import org.neo4j.kernel.impl.transaction.TransactionRepresentation;
import org.neo4j.kernel.impl.transaction.log.TransactionIdStore;
import org.neo4j.kernel.impl.transaction.tracing.CommitEvent;
import org.neo4j.kernel.impl.util.JobScheduler;
import org.neo4j.kernel.lifecycle.LifeSupport;
import org.neo4j.kernel.monitoring.Monitors;
import org.neo4j.kernel.monitoring.tracing.Tracers;
import org.neo4j.logging.NullLog;
import org.neo4j.storageengine.api.StorageCommand;
import org.neo4j.storageengine.api.StorageEngine;
import org.neo4j.storageengine.api.StorageStatement;
import org.neo4j.storageengine.api.StoreReadLayer;
import org.neo4j.storageengine.api.TransactionApplicationMode;
import org.neo4j.storageengine.api.lock.ResourceLocker;
import org.neo4j.storageengine.api.txstate.ReadableTransactionState;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyCollection;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.neo4j.helpers.collection.IteratorUtil.asSet;
import static org.neo4j.helpers.collection.IteratorUtil.asUniqueSet;
public class KernelTransactionsTest
{
@Test
public void shouldListActiveTransactions() throws Exception
{
// Given
KernelTransactions registry = newKernelTransactions();
// When
KernelTransaction first = registry.newInstance();
KernelTransaction second = registry.newInstance();
KernelTransaction third = registry.newInstance();
first.close();
// Then
assertThat( asUniqueSet( registry.activeTransactions() ), equalTo( asSet( second, third ) ) );
}
@Test
public void shouldDisposeTransactionsWhenAsked() throws Exception
{
// Given
KernelTransactions registry = newKernelTransactions();
registry.disposeAll();
KernelTransaction first = registry.newInstance();
KernelTransaction second = registry.newInstance();
KernelTransaction leftOpen = registry.newInstance();
first.close();
second.close();
// When
registry.disposeAll();
// Then
KernelTransaction postDispose = registry.newInstance();
assertThat( postDispose, not( equalTo( first ) ) );
assertThat( postDispose, not( equalTo( second ) ) );
assertTrue( leftOpen.shouldBeTerminated() );
}
@Test
public void shouldIncludeRandomBytesInAdditionalHeader() throws Exception
{
// Given
TransactionRepresentation[] transactionRepresentation = new TransactionRepresentation[1];
KernelTransactions registry = newKernelTransactions( newRememberingCommitProcess( transactionRepresentation ) );
// When
try ( KernelTransaction transaction = registry.newInstance() )
{
// Just pick anything that can flag that changes have been made to this transaction
((KernelTransactionImplementation) transaction).txState().nodeDoCreate( 0 );
transaction.success();
}
// Then
byte[] additionalHeader = transactionRepresentation[0].additionalHeader();
assertNotNull( additionalHeader );
assertTrue( additionalHeader.length > 0 );
}
@Test
public void transactionCloseRemovesTxFromActiveTransactions() throws Exception
{
KernelTransactions kernelTransactions = newKernelTransactions();
KernelTransaction tx1 = kernelTransactions.newInstance();
KernelTransaction tx2 = kernelTransactions.newInstance();
KernelTransaction tx3 = kernelTransactions.newInstance();
tx1.close();
tx3.close();
assertEquals( asSet( tx2 ), kernelTransactions.activeTransactions() );
}
@Test
public void transactionRemovesItselfFromActiveTransactions() throws Exception
{
KernelTransactions kernelTransactions = newKernelTransactions();
KernelTransaction tx1 = kernelTransactions.newInstance();
KernelTransaction tx2 = kernelTransactions.newInstance();
KernelTransaction tx3 = kernelTransactions.newInstance();
tx2.close();
assertEquals( asSet( tx1, tx3 ), kernelTransactions.activeTransactions() );
}
@Test
public void disposeAllMarksAllTransactionsForTermination() throws Exception
{
KernelTransactions kernelTransactions = newKernelTransactions();
KernelTransaction tx1 = kernelTransactions.newInstance();
KernelTransaction tx2 = kernelTransactions.newInstance();
KernelTransaction tx3 = kernelTransactions.newInstance();
kernelTransactions.disposeAll();
assertTrue( tx1.shouldBeTerminated() );
assertTrue( tx2.shouldBeTerminated() );
assertTrue( tx3.shouldBeTerminated() );
}
private static KernelTransactions newKernelTransactions() throws Exception
{
return newKernelTransactions( mock( TransactionCommitProcess.class ) );
}
private static KernelTransactions newKernelTransactions( TransactionCommitProcess commitProcess ) throws Exception
{
LifeSupport life = new LifeSupport();
life.start();
Locks locks = mock( Locks.class );
when( locks.newClient() ).thenReturn( mock( Locks.Client.class ) );
StoreReadLayer readLayer = mock( StoreReadLayer.class );
when( readLayer.acquireStatement() ).thenAnswer( new Answer<StorageStatement>()
{
@Override
public StorageStatement answer( InvocationOnMock invocation ) throws Throwable
{
return mock( StorageStatement.class );
}
} );
StorageEngine storageEngine = mock( StorageEngine.class );
when( storageEngine.storeReadLayer() ).thenReturn( readLayer );
doAnswer( new Answer<Void>()
{
@Override
public Void answer( InvocationOnMock invocation ) throws Throwable
{
invocation.getArgumentAt( 0, Collection.class ).add( mock( StorageCommand.class ) );
return null;
}
} ).when( storageEngine ).createCommands(
anyCollection(),
any( ReadableTransactionState.class ),
any( ResourceLocker.class ),
anyLong() );
Tracers tracers =
new Tracers( "null", NullLog.getInstance(), mock( Monitors.class ), mock( JobScheduler.class ) );
return new KernelTransactions( locks,
null, null, null, TransactionHeaderInformationFactory.DEFAULT,
commitProcess, null,
null, new TransactionHooks(), mock( TransactionMonitor.class ), life,
tracers, storageEngine, new Procedures(), mock( TransactionIdStore.class ) );
}
private static TransactionCommitProcess newRememberingCommitProcess( final TransactionRepresentation[] slot )
throws TransactionFailureException
{
TransactionCommitProcess commitProcess = mock( TransactionCommitProcess.class );
when( commitProcess.commit(
any( TransactionToApply.class ), any( CommitEvent.class ),
any( TransactionApplicationMode.class ) ) )
.then( invocation -> {
slot[0] = ((TransactionToApply) invocation.getArguments()[0]).transactionRepresentation();
return 1L;
} );
return commitProcess;
}
}