| 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.mapping; |
| 17 | |
| 18 | import com.datastax.driver.core.CCMConfig; |
| 19 | import com.datastax.driver.core.CCMTestsSupport; |
| 20 | import com.datastax.driver.core.Cluster; |
| 21 | import com.datastax.driver.core.CreateCCM; |
| 22 | import com.datastax.driver.core.exceptions.NoHostAvailableException; |
| 23 | import com.datastax.driver.core.policies.ConstantReconnectionPolicy; |
| 24 | import org.testng.annotations.Test; |
| 25 | |
| 26 | import static com.datastax.driver.core.CreateCCM.TestMode.PER_METHOD; |
| 27 | import static com.datastax.driver.core.TestUtils.*; |
| 28 | import static com.datastax.driver.mapping.MapperTest.User; |
| 29 | import static org.testng.Assert.fail; |
| 30 | |
| 31 | @CreateCCM(PER_METHOD) |
| 32 | @CCMConfig(dirtiesContext = true) |
| 33 | public class MapperReconnectionTest extends CCMTestsSupport { |
| 34 | |
| 35 | @Override |
| 36 | public Cluster.Builder createClusterBuilder() { |
| 37 | return super.createClusterBuilder().withReconnectionPolicy(new ConstantReconnectionPolicy(1000)); |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public void onTestContextInitialized() { |
| 42 | execute("CREATE TABLE users (user_id uuid PRIMARY KEY, name text, email text, year int, gender text)"); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Ensures that when the driver looses connectivity, |
| 47 | * if a mapper query preparation is attempted in the meanwhile, |
| 48 | * the failed future will not be kept in cache, so that when |
| 49 | * connectivity comes back again, |
| 50 | * the same query preparation can be reattempted. |
| 51 | * |
| 52 | * @jira_ticket JAVA-1283 |
| 53 | * @test_category object_mapper |
| 54 | */ |
| 55 | @Test(groups = "long") |
| 56 | public void should_not_keep_failed_future_in_query_cache() throws Exception { |
| 57 | MappingManager manager = new MappingManager(session()); |
| 58 | Mapper<User> m = manager.mapper(User.class); |
| 59 | User u1 = new User("Paul", "paul@gmail.com"); |
| 60 | m.save(u1); |
| 61 | ccm().stop(1); |
| 62 | ccm().waitForDown(1); |
| 63 | waitForDown(ipOfNode(1), cluster()); |
| 64 | try { |
| 65 | m.get(u1.getUserId()); |
| 66 | fail("Should have thrown NoHostAvailableException"); |
| 67 | } catch (NoHostAvailableException e) { |
| 68 | // ok |
| 69 | } |
| 70 | ccm().start(1); |
| 71 | ccm().waitForUp(1); |
| 72 | waitForUp(ipOfNode(1), cluster()); |
| 73 | try { |
| 74 | m.get(u1.getUserId()); |
| 75 | } catch (NoHostAvailableException e) { |
| 76 | fail("Should not have thrown NoHostAvailableException"); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | } |