Elffuss BraidHub

kikocis/java-driver public

9b39ff6a48bf · 6.1 KiBRawPermalink
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 */
16package com.datastax.driver.mapping;
17
18import com.datastax.driver.core.CCMTestsSupport;
19import com.datastax.driver.core.Cluster;
20import com.datastax.driver.core.Session;
21import com.datastax.driver.core.utils.MoreFutures.SuccessCallback;
22import com.datastax.driver.core.utils.UUIDs;
23import com.datastax.driver.mapping.annotations.Column;
24import com.datastax.driver.mapping.annotations.PartitionKey;
25import com.datastax.driver.mapping.annotations.Table;
26import com.google.common.base.Function;
27import com.google.common.base.Objects;
28import com.google.common.util.concurrent.Futures;
29import com.google.common.util.concurrent.ListenableFuture;
30import com.google.common.util.concurrent.Uninterruptibles;
31import org.testng.annotations.Test;
32
33import java.util.UUID;
34import java.util.concurrent.CountDownLatch;
35
36import static com.datastax.driver.core.TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT;
37import static java.util.concurrent.TimeUnit.MINUTES;
38import static org.testng.Assert.fail;
39
40/**
41 * Basic tests for the mapping module.
42 */
43@SuppressWarnings("unused")
44public class MapperAsyncTest extends CCMTestsSupport {
45
46 private static final String KEYSPACE = "mapper_async_test_ks";
47
48 private final User paul = new User("Paul", "paul@yahoo.com");
49
50 @Override
51 public void onTestContextInitialized() {
52 execute(
53 String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, KEYSPACE, 1),
54 String.format("CREATE TABLE %s.users (user_id uuid PRIMARY KEY, name text, email text)", KEYSPACE)
55 );
56 }
57
58 /**
59 * Validates that when using save, get and deleteAsync using the same executor as the caller that the driver
60 * does not block indefinitely in a netty worker thread.
61 *
62 * @jira_ticket JAVA-1070
63 * @test_category queries:async
64 * @test_category object_mapper
65 */
66 @Test(groups = "short")
67 public void should_get_query_async_without_blocking() {
68
69 final CountDownLatch latch = new CountDownLatch(1);
70
71 // create a second cluster to perform everything asynchronously,
72 // including session initialization
73 Cluster cluster2 = register(
74 createClusterBuilder()
75 .addContactPoints(getContactPoints())
76 .withPort(ccm().getBinaryPort())
77 .build());
78
79 ListenableFuture<MappingManager> mappingManagerFuture = Futures.transform(
80 cluster2.connectAsync(),
81 new Function<Session, MappingManager>() {
82 @Override
83 public MappingManager apply(Session session) {
84 return new MappingManager(session);
85 }
86 });
87
88 Futures.addCallback(mappingManagerFuture, new SuccessCallback<MappingManager>() {
89
90 @Override
91 public void onSuccess(MappingManager manager) {
92
93 final Mapper<User> mapper = manager.mapper(User.class);
94 ListenableFuture<Void> saveFuture = mapper.saveAsync(paul);
95 Futures.addCallback(saveFuture, new SuccessCallback<Void>() {
96
97 @Override
98 public void onSuccess(Void result) {
99 ListenableFuture<User> getFuture = mapper.getAsync(paul.getUserId());
100 Futures.addCallback(getFuture, new SuccessCallback<User>() {
101
102 @Override
103 public void onSuccess(User paul) {
104
105 Futures.addCallback(mapper.deleteAsync(paul), new SuccessCallback<Void>() {
106 @Override
107 public void onSuccess(Void result) {
108 latch.countDown();
109 }
110 });
111 }
112 });
113 }
114 });
115 }
116 });
117
118 try {
119 Uninterruptibles.awaitUninterruptibly(latch, 5, MINUTES);
120 } catch (Exception e) {
121 fail("Operation did not complete normally within 5 minutes");
122 }
123 }
124
125 @Table(name = "users", keyspace = KEYSPACE)
126 public static class User {
127
128 @PartitionKey
129 @Column(name = "user_id")
130 private UUID userId;
131
132 private String name;
133
134 private String email;
135
136 public User() {
137 }
138
139 public User(String name, String email) {
140 this.userId = UUIDs.random();
141 this.name = name;
142 this.email = email;
143 }
144
145 public UUID getUserId() {
146 return userId;
147 }
148
149 public void setUserId(UUID userId) {
150 this.userId = userId;
151 }
152
153 public String getName() {
154 return name;
155 }
156
157 public void setName(String name) {
158 this.name = name;
159 }
160
161 public String getEmail() {
162 return email;
163 }
164
165 public void setEmail(String email) {
166 this.email = email;
167 }
168
169 @Override
170 public boolean equals(Object other) {
171 if (other == null || other.getClass() != this.getClass())
172 return false;
173
174 User that = (User) other;
175 return Objects.equal(userId, that.userId)
176 && Objects.equal(name, that.name)
177 && Objects.equal(email, that.email);
178 }
179
180 @Override
181 public int hashCode() {
182 return Objects.hashCode(userId, name, email);
183 }
184 }
185
186}