Skip to content

Commit 9bd5a3a

Browse files
committed
Add tests
1 parent 3236717 commit 9bd5a3a

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

src/java/org/apache/cassandra/metrics/AllRequestsMetrics.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public AllRequestsMetrics(String scope, String prefix)
3838
otherErrors = Metrics.meter(factory.createMetricName(namePrefix + "OtherErrors"));
3939
}
4040

41+
@Override
4142
public void release()
4243
{
4344
super.release();
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.cassandra.metrics;
20+
21+
import java.util.Collections;
22+
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
27+
import com.codahale.metrics.Meter;
28+
import com.datastax.driver.core.BoundStatement;
29+
import com.datastax.driver.core.PreparedStatement;
30+
import com.datastax.driver.core.Session;
31+
import org.apache.cassandra.cql3.CQLStatement;
32+
import org.apache.cassandra.cql3.CQLTester;
33+
import org.apache.cassandra.cql3.QueryEvents;
34+
import org.apache.cassandra.cql3.QueryOptions;
35+
import org.apache.cassandra.db.ConsistencyLevel;
36+
import org.apache.cassandra.exceptions.ReadFailureException;
37+
import org.apache.cassandra.exceptions.ReadTimeoutException;
38+
import org.apache.cassandra.exceptions.UnavailableException;
39+
import org.apache.cassandra.service.QueryState;
40+
41+
import static org.junit.Assert.assertEquals;
42+
43+
public class ClientRequestsMetricsAllRequestsTest extends CQLTester
44+
{
45+
AllRequestsMetrics metrics = ClientRequestsMetricsProvider.instance.metrics(KEYSPACE).allRequestsMetrics;
46+
47+
@Before
48+
public void setup()
49+
{
50+
createTable("CREATE TABLE %s (id INT PRIMARY KEY, v TEXT)");
51+
}
52+
53+
@After
54+
public void teardown()
55+
{
56+
dropTable("DROP TABLE %s");
57+
}
58+
59+
@Test
60+
public void testInvalidRequest()
61+
{
62+
long before = metrics.invalid.getCount();
63+
try
64+
{
65+
executeNet("INSERT INTO %s (id, v) VALUES (1, ?)");
66+
}
67+
catch (Throwable t)
68+
{
69+
// expected
70+
}
71+
assertEquals(1, metrics.invalid.getCount() - before);
72+
}
73+
74+
@Test
75+
public void testInvalidPreparedRequest()
76+
{
77+
try(Session session = sessionNet())
78+
{
79+
PreparedStatement prepare1 = session.prepare(formatQuery("DELETE FROM %s WHERE id = ?"));
80+
BoundStatement bind = prepare1.bind();
81+
long before = metrics.invalid.getCount();
82+
try
83+
{
84+
session.execute(bind);
85+
}
86+
catch (Throwable t)
87+
{
88+
// expected
89+
}
90+
assertEquals(1, metrics.invalid.getCount() - before);
91+
}
92+
}
93+
94+
@Test
95+
public void testTimeoutRequest()
96+
{
97+
testNotifyQueryFailure(metrics.timeouts, new ReadTimeoutException(ConsistencyLevel.ONE));
98+
}
99+
100+
@Test
101+
public void testUnavailableRequest()
102+
{
103+
testNotifyQueryFailure(metrics.unavailables, UnavailableException.create(ConsistencyLevel.ONE, 1, 0));
104+
}
105+
106+
@Test
107+
public void testFailureRequest()
108+
{
109+
testNotifyQueryFailure(metrics.failures, new ReadFailureException(ConsistencyLevel.ONE, 0, 0, false, Collections.emptyMap()));
110+
}
111+
112+
@Test
113+
public void testOtherErrorRequest()
114+
{
115+
testNotifyQueryFailure(metrics.otherErrors, new RuntimeException());
116+
}
117+
118+
public void testNotifyQueryFailure(Meter meter, Exception exception)
119+
{
120+
CQLStatement cqlStatement = parseStatement("CREATE TABLE %s (id INT PRIMARY KEY, v TEXT)");
121+
long before = meter.getCount();
122+
123+
QueryEvents.instance.notifyQueryFailure(cqlStatement, cqlStatement.getRawCQLStatement(), QueryOptions.DEFAULT,
124+
QueryState.forInternalCalls(), exception);
125+
126+
assertEquals(1, meter.getCount() - before);
127+
}
128+
129+
130+
}

0 commit comments

Comments
 (0)