-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
TransactionRepresentationReplicatedTransactionTest.java
134 lines (108 loc) · 5.42 KB
/
TransactionRepresentationReplicatedTransactionTest.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
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j Enterprise Edition. The included source
* code can be redistributed and/or modified under the terms of the
* GNU AFFERO GENERAL PUBLIC LICENSE Version 3
* (http://www.fsf.org/licensing/licenses/agpl-3.0.html) with the
* Commons Clause, as found in the associated LICENSE.txt file.
*
* 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 Affero General Public License for more details.
*
* Neo4j object code can be licensed independently from the source
* under separate terms from the AGPL. Inquiries can be directed to:
* licensing@neo4j.com
*
* More information is also available at:
* https://neo4j.com/licensing/
*/
package org.neo4j.causalclustering.core.state.machines.tx;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.neo4j.causalclustering.messaging.NetworkFlushableChannelNetty4;
import org.neo4j.causalclustering.messaging.NetworkReadableClosableChannelNetty4;
import org.neo4j.causalclustering.messaging.marshalling.ChunkedEncoder;
import org.neo4j.kernel.impl.store.record.NodeRecord;
import org.neo4j.kernel.impl.transaction.TransactionRepresentation;
import org.neo4j.kernel.impl.transaction.command.Command;
import org.neo4j.kernel.impl.transaction.log.PhysicalTransactionRepresentation;
import org.neo4j.storageengine.api.StorageCommand;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.neo4j.causalclustering.core.state.machines.tx.ReplicatedTransactionTest.encode;
public class TransactionRepresentationReplicatedTransactionTest
{
@Test
public void shouldEncodeAndMarshalSameBytes() throws IOException
{
PhysicalTransactionRepresentation expectedTx = new PhysicalTransactionRepresentation( ofSize( 10000 ) );
expectedTx.setHeader( new byte[0], 1, 2, 3, 4, 5, 6 );
ByteBuf buffer2 = Unpooled.buffer();
ByteBuf buffer3 = Unpooled.buffer();
ChunkedEncoder marshal1 = ReplicatedTransaction.from( expectedTx ).marshal();
ChunkedEncoder marshal2 = ReplicatedTransaction.from( expectedTx ).marshal();
encode( buffer3, marshal1 );
marshal2.marshal( new NetworkFlushableChannelNetty4( buffer2 ) );
assertTrue( ByteBufUtil.equals( buffer3, buffer2 ) );
NetworkReadableClosableChannelNetty4 read = new NetworkReadableClosableChannelNetty4( buffer2 );
ReplicatedTransaction unmarshal = ReplicatedTransactionSerializer.decode( buffer2 );
TransactionRepresentation storageCommands = ReplicatedTransactionFactory.extractTransactionRepresentation( unmarshal, new byte[0] );
buffer3.release();
buffer2.release();
}
@Test
public void shouldDecodeAndUnmarshalSameBytes() throws IOException
{
PhysicalTransactionRepresentation expectedTx =
new PhysicalTransactionRepresentation( Collections.singleton( new Command.NodeCommand( new NodeRecord( 1 ), new NodeRecord( 2 ) ) ) );
expectedTx.setHeader( new byte[0], 1, 2, 3, 4, 5, 6 );
ByteBuf buffer = Unpooled.buffer();
TransactionRepresentationReplicatedTransaction replicatedTransaction = ReplicatedTransaction.from( expectedTx );
replicatedTransaction.marshal().marshal( new NetworkFlushableChannelNetty4( buffer ) );
ReplicatedTransaction decoded = ReplicatedTransactionSerializer.decode( buffer );
buffer.readerIndex( 0 );
ReplicatedTransaction unmarshaled = ReplicatedTransactionSerializer.decode( buffer );
assertEquals( decoded, unmarshaled );
buffer.release();
}
private Collection<StorageCommand> ofSize( int size )
{
List<StorageCommand> commands = new ArrayList<>();
for ( int i = 0; i < size; i++ )
{
commands.add( new Command.NodeCommand( new NodeRecord( 1 ), new NodeRecord( 2 ) ) );
}
return commands;
}
@Test
public void shouldMarshalSame() throws IOException
{
PhysicalTransactionRepresentation tx = new PhysicalTransactionRepresentation( ofSize( 1 ) );
ReplicatedTransaction byteTx = ReplicatedTransactionFactory.createImmutableReplicatedTransaction( tx );
TransactionRepresentationReplicatedTransaction repreTx = ReplicatedTransaction.from( tx );
ByteBuf buffer1 = Unpooled.buffer();
ByteBuf buffer2 = Unpooled.buffer();
byteTx.marshal().marshal( new NetworkFlushableChannelNetty4( buffer1 ) );
repreTx.marshal().marshal( new NetworkFlushableChannelNetty4( buffer2 ) );
NetworkReadableClosableChannelNetty4 readable1 = new NetworkReadableClosableChannelNetty4( buffer1 );
ReplicatedTransaction unmarshalByte = ReplicatedTransactionSerializer.unmarshal( readable1 );
ReplicatedTransaction unmarshalrepr = ReplicatedTransactionSerializer.decode( buffer2 );
buffer1.readerIndex( 0 );
buffer2.readerIndex( 0 );
buffer1.setInt( 0, -1 );
buffer2.setInt( 0, -1 );
assertArrayEquals( buffer1.array(), buffer2.array() );
}
}