Elffuss BraidHub

kikocis/java-driver public

JAVA-797 Add tests for prepareOnAllHosts.
Changeset5f1d9b61739a83ff9df2049b799b5e162fcf78e68992561ce92aadb0bea34243
AuthorClint Ascencio <clint.ascencio@datastax.com> · 2015-08-07 10:30 -05:00
Committerolim7t <omichallat+github@gmail.com> · 2015-08-09 20:19 +02:00
Parentsccad81a3b795

Browse the files at this changeset

1 file changed

added driver-core/src/test/java/com/datastax/driver/core/QueryOptionsTest.java +148 −0
@@ -0,0 +1,148 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.core;
17+
18+import java.util.List;
19+import com.google.common.collect.Lists;
20+import org.scassandra.http.client.PreparedStatementPreparation;
21+import org.testng.annotations.*;
22+
23+import static com.datastax.driver.core.Assertions.assertThat;
24+
25+public class QueryOptionsTest {
26+ SCassandraCluster scassandra;
27+
28+ QueryOptions queryOptions = new QueryOptions();
29+
30+ SortingLoadBalancingPolicy loadBalancingPolicy;
31+
32+ Cluster cluster = null;
33+ Session session = null;
34+ Host host1, host2, host3;
35+
36+
37+ @BeforeClass(groups = "short")
38+ public void beforeClass() {
39+ scassandra = new SCassandraCluster(CCMBridge.IP_PREFIX, 3);
40+ }
41+
42+ @BeforeMethod(groups = "short")
43+ public void beforeMethod() {
44+ loadBalancingPolicy = new SortingLoadBalancingPolicy();
45+ cluster = Cluster.builder()
46+ .addContactPoint(CCMBridge.ipOfNode(2))
47+ .withLoadBalancingPolicy(loadBalancingPolicy)
48+ .withQueryOptions(queryOptions)
49+ .build();
50+
51+ session = cluster.connect();
52+
53+ host1 = TestUtils.findHost(cluster, 1);
54+ host2 = TestUtils.findHost(cluster, 2);
55+ host3 = TestUtils.findHost(cluster, 3);
56+
57+ // Make sure there are no prepares
58+ for(int host : Lists.newArrayList(1, 2, 3)) {
59+ assertThat(scassandra.retrievePreparedStatementPreparations(host)).hasSize(0);
60+ }
61+ }
62+
63+ public void validatePrepared(boolean expectAll) {
64+ // Prepare the statement
65+ String query = "select sansa_stark from the_known_world";
66+ session.prepare(query);
67+
68+ // Ensure prepared properly based on expectation.
69+ List<PreparedStatementPreparation> preparationOne = scassandra.retrievePreparedStatementPreparations(1);
70+ List<PreparedStatementPreparation> preparationTwo = scassandra.retrievePreparedStatementPreparations(2);
71+ List<PreparedStatementPreparation> preparationThree = scassandra.retrievePreparedStatementPreparations(3);
72+
73+ assertThat(preparationOne).hasSize(1);
74+ assertThat(preparationOne.get(0).getPreparedStatementText()).isEqualTo(query);
75+
76+ if (expectAll) {
77+ assertThat(preparationTwo).hasSize(1);
78+ assertThat(preparationTwo.get(0).getPreparedStatementText()).isEqualTo(query);
79+ assertThat(preparationThree).hasSize(1);
80+ assertThat(preparationThree.get(0).getPreparedStatementText()).isEqualTo(query);
81+ } else {
82+ assertThat(preparationTwo).isEmpty();
83+ assertThat(preparationThree).isEmpty();
84+ }
85+ }
86+
87+ /**
88+ * <p>
89+ * Validates that statements are only prepared on one node when
90+ * {@link QueryOptions#setPrepareOnAllHosts(boolean)} is set to false.
91+ * </p>
92+ *
93+ * @test_category prepared_statements:prepared
94+ * @expected_result prepare query only on the first node.
95+ * @jira_ticket JAVA-797
96+ * @since 2.0.11, 2.1.8, 2.2.1
97+ */
98+ @Test(groups = "short")
99+ public void should_prepare_once_when_prepare_on_all_hosts_false() {
100+ queryOptions.setPrepareOnAllHosts(false);
101+ validatePrepared(false);
102+ }
103+
104+ /**
105+ * <p>
106+ * Validates that statements are prepared on one node when
107+ * {@link QueryOptions#setPrepareOnAllHosts(boolean)} is set to true.
108+ * </p>
109+ *
110+ * @test_category prepared_statements:prepared
111+ * @expected_result all nodes prepared the query
112+ * @jira_ticket JAVA-797
113+ * @since 2.0.11, 2.1.8, 2.2.1
114+ */
115+ @Test(groups = "short")
116+ public void should_prepare_everywhere_when_prepare_on_all_hosts_true() {
117+ queryOptions.setPrepareOnAllHosts(true);
118+ validatePrepared(true);
119+ }
120+
121+ /**
122+ * <p>
123+ * Validates that statements are prepared on one node when
124+ * {@link QueryOptions#setPrepareOnAllHosts(boolean)} is not set.
125+ * </p>
126+ *
127+ * @test_category prepared_statements:prepared
128+ * @expected_result all nodes prepared the query.
129+ * @jira_ticket JAVA-797
130+ * @since 2.0.11
131+ */
132+ @Test(groups = "short")
133+ public void should_prepare_everywhere_when_not_configured() {
134+ validatePrepared(true);
135+ }
136+
137+ @AfterMethod(groups = "short")
138+ public void afterMethod() {
139+ scassandra.clearAllPrimes();
140+ scassandra.clearAllRecordedActivity();
141+ }
142+
143+ @AfterClass(groups = "short")
144+ public void afterClass() {
145+ if (scassandra != null)
146+ scassandra.stop();
147+ }
148+}