-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
ByteBuddyExamplesTest.java
130 lines (116 loc) · 5.03 KB
/
ByteBuddyExamplesTest.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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.cassandra.distributed.test;
import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import org.junit.Test;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.bind.annotation.SuperCall;
import org.apache.cassandra.cql3.ColumnSpecification;
import org.apache.cassandra.cql3.QueryOptions;
import org.apache.cassandra.cql3.statements.ModificationStatement;
import org.apache.cassandra.cql3.statements.SelectStatement;
import org.apache.cassandra.distributed.Cluster;
import org.apache.cassandra.distributed.api.ConsistencyLevel;
import org.apache.cassandra.service.QueryState;
import org.apache.cassandra.transport.messages.ResultMessage;
import org.apache.cassandra.transport.messages.ResultMessage.Rows;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class ByteBuddyExamplesTest extends TestBaseImpl
{
@Test
public void writeFailureTest() throws Throwable
{
try(Cluster cluster = init(Cluster.build(1)
.withInstanceInitializer(BBFailHelper::install)
.start()))
{
cluster.schemaChange("create table " + KEYSPACE + ".tbl (id int primary key, t int)");
try
{
cluster.coordinator(1).execute("insert into " + KEYSPACE + ".tbl (id, t) values (1, 1)", ConsistencyLevel.ALL);
fail("Should fail");
}
catch (RuntimeException e)
{
// expected
}
}
}
public static class BBFailHelper
{
static void install(ClassLoader cl, int nodeNumber)
{
new ByteBuddy().redefine(ModificationStatement.class)
.method(named("execute"))
.intercept(MethodDelegation.to(BBFailHelper.class))
.make()
.load(cl, ClassLoadingStrategy.Default.INJECTION);
}
public static ResultMessage execute()
{
throw new RuntimeException();
}
}
@Test
public void countTest() throws IOException
{
try(Cluster cluster = init(Cluster.build(2)
.withInstanceInitializer(BBCountHelper::install)
.start()))
{
cluster.schemaChange("create table " + KEYSPACE + ".tbl (id int primary key, bytebuddy_test_column int)");
cluster.coordinator(1).execute("select * from " + KEYSPACE + ".tbl;", ConsistencyLevel.ALL);
cluster.coordinator(2).execute("select * from " + KEYSPACE + ".tbl;", ConsistencyLevel.ALL);
cluster.get(1).runOnInstance(() -> {
assertEquals(1, BBCountHelper.count.get());
});
cluster.get(2).runOnInstance(() -> {
assertEquals(0, BBCountHelper.count.get());
});
}
}
public static class BBCountHelper
{
static AtomicInteger count = new AtomicInteger();
static void install(ClassLoader cl, int nodeNumber)
{
if (nodeNumber != 1)
return;
new ByteBuddy().rebase(SelectStatement.class)
.method(named("execute").and(takesArguments(3)))
.intercept(MethodDelegation.to(BBCountHelper.class))
.make()
.load(cl, ClassLoadingStrategy.Default.INJECTION);
}
public static ResultMessage.Rows execute(QueryState state, QueryOptions options, long queryStartNanoTime, @SuperCall Callable<ResultMessage.Rows> r) throws Exception
{
Rows res = r.call();
if (res.result.metadata.names.stream().map(ColumnSpecification::toString).collect(Collectors.toList()).contains("bytebuddy_test_column"))
count.incrementAndGet();
return res;
}
}
}