Skip to content

Commit abf9563

Browse files
committed
resolve errors after rebasing
1 parent 3af05f4 commit abf9563

File tree

7 files changed

+68
-103
lines changed

7 files changed

+68
-103
lines changed

src/main/java/redis/clients/jedis/Connection.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,12 @@ public void connect() {
175175

176176
outputStream = new RedisOutputStream(socket.getOutputStream());
177177
inputStream = new RedisInputStream(socket.getInputStream());
178-
} catch (Exception ex) {
178+
} catch (IOException ioe) {
179179
broken = true;
180-
throw new JedisConnectionException("Failed connecting to "
181-
+ socketFactory.getDescription(), ex);
180+
throw new JedisConnectionException("Failed connecting to " + socketFactory.getDescription(), ioe);
181+
} catch (JedisConnectionException jce) {
182+
broken = true;
183+
throw jce;
182184
}
183185
}
184186
}

src/main/java/redis/clients/jedis/DefaultJedisSocketFactory.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package redis.clients.jedis;
22

3-
import redis.clients.jedis.exceptions.JedisConnectionException;
4-
import redis.clients.jedis.util.IOUtils;
5-
3+
import java.io.IOException;
4+
import java.net.InetSocketAddress;
5+
import java.net.Socket;
66
import javax.net.ssl.HostnameVerifier;
77
import javax.net.ssl.SSLParameters;
88
import javax.net.ssl.SSLSocket;
99
import javax.net.ssl.SSLSocketFactory;
10-
import java.io.IOException;
11-
import java.net.InetSocketAddress;
12-
import java.net.Socket;
10+
11+
import redis.clients.jedis.exceptions.JedisConnectionException;
12+
import redis.clients.jedis.util.IOUtils;
1313

1414
public class DefaultJedisSocketFactory implements JedisSocketFactory {
1515

@@ -40,7 +40,7 @@ public DefaultJedisSocketFactory(String host, int port, JedisSocketConfig socket
4040
}
4141

4242
@Override
43-
public Socket createSocket() throws IOException {
43+
public Socket createSocket() throws JedisConnectionException {
4444
Socket socket = null;
4545
try {
4646
socket = new Socket();
@@ -77,15 +77,11 @@ public Socket createSocket() throws IOException {
7777

7878
return socket;
7979

80-
} catch (Exception ex) {
80+
} catch (IOException ex) {
8181

8282
IOUtils.closeQuietly(socket);
8383

84-
if (ex instanceof JedisConnectionException) {
85-
throw ex;
86-
} else {
87-
throw new JedisConnectionException("Failed to create socket.", ex);
88-
}
84+
throw new JedisConnectionException("Failed to create socket.", ex);
8985
}
9086
}
9187

src/main/java/redis/clients/jedis/JedisSocketFactory.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44
import java.net.Socket;
5+
import redis.clients.jedis.exceptions.JedisConnectionException;
56

67
/**
78
* JedisSocketFactory: responsible for creating socket connections
@@ -15,7 +16,14 @@
1516
*/
1617
public interface JedisSocketFactory {
1718

18-
Socket createSocket() throws IOException;
19+
/**
20+
* @return Socket
21+
* @throws IOException this will be removed in future
22+
* @throws JedisConnectionException
23+
* @deprecated throwing IOException will not be supported in future
24+
*/
25+
@Deprecated
26+
Socket createSocket() throws IOException, JedisConnectionException;
1927

2028
String getDescription();
2129

src/test/java/redis/clients/jedis/tests/JedisTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public void uriWithDBindexShouldUseTimeout() throws URISyntaxException {
193193
long startTime = System.nanoTime();
194194
try (Jedis jedis = new Jedis(uri, 5000)) {
195195
jedis.ping();
196-
} catch(Exception ex) {
196+
} catch (Exception ex) {
197197
assertEquals(JedisConnectionException.class, ex.getClass());
198198
assertEquals(java.net.UnknownHostException.class, ex.getCause().getClass());
199199
}

src/test/java/redis/clients/jedis/tests/SSLJedisTest.java

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,9 @@ public void connectWithShardInfo() throws Exception {
111111
JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, null);
112112
shardInfo.setPassword("foobared");
113113

114-
Jedis jedis = new Jedis(shardInfo);
115-
assertEquals("PONG", jedis.ping());
116-
jedis.disconnect();
117-
jedis.close();
114+
try (Jedis jedis = new Jedis(shardInfo)) {
115+
assertEquals("PONG", jedis.ping());
116+
}
118117
}
119118

120119
/**
@@ -140,8 +139,7 @@ public void connectWithShardInfoByIpAddress() throws Exception {
140139
JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, null);
141140
shardInfo.setPassword("foobared");
142141

143-
Jedis jedis = new Jedis(shardInfo);
144-
try {
142+
try (Jedis jedis = new Jedis(shardInfo)) {
145143
assertEquals("PONG", jedis.ping());
146144
fail("The code did not throw the expected JedisConnectionException.");
147145
} catch (JedisConnectionException e) {
@@ -150,12 +148,6 @@ public void connectWithShardInfoByIpAddress() throws Exception {
150148
assertEquals("Unexpected second inner exception.",
151149
CertificateException.class, e.getCause().getCause().getClass());
152150
}
153-
154-
try {
155-
jedis.close();
156-
} catch (Throwable e1) {
157-
// Expected.
158-
}
159151
}
160152

161153
/**
@@ -172,10 +164,9 @@ public void connectWithShardInfoAndCustomHostnameVerifier() {
172164
JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier);
173165
shardInfo.setPassword("foobared");
174166

175-
Jedis jedis = new Jedis(shardInfo);
176-
assertEquals("PONG", jedis.ping());
177-
jedis.disconnect();
178-
jedis.close();
167+
try (Jedis jedis = new Jedis(shardInfo)) {
168+
assertEquals("PONG", jedis.ping());
169+
}
179170
}
180171

181172
/**
@@ -191,10 +182,9 @@ public void connectWithShardInfoAndCustomSocketFactory() throws Exception {
191182
JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier);
192183
shardInfo.setPassword("foobared");
193184

194-
Jedis jedis = new Jedis(shardInfo);
195-
assertEquals("PONG", jedis.ping());
196-
jedis.disconnect();
197-
jedis.close();
185+
try (Jedis jedis = new Jedis(shardInfo)) {
186+
assertEquals("PONG", jedis.ping());
187+
}
198188
}
199189

200190
/**
@@ -213,20 +203,13 @@ public void connectWithShardInfoAndCustomHostnameVerifierByIpAddress() {
213203
JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier);
214204
shardInfo.setPassword("foobared");
215205

216-
Jedis jedis = new Jedis(shardInfo);
217-
try {
206+
try (Jedis jedis = new Jedis(shardInfo)) {
218207
assertEquals("PONG", jedis.ping());
219208
fail("The code did not throw the expected JedisConnectionException.");
220209
} catch (JedisConnectionException e) {
221210
assertEquals("The JedisConnectionException does not contain the expected message.",
222211
"The connection to '127.0.0.1' failed ssl/tls hostname verification.", e.getMessage());
223212
}
224-
225-
try {
226-
jedis.close();
227-
} catch (Throwable e1) {
228-
// Expected.
229-
}
230213
}
231214

232215
/**
@@ -245,8 +228,7 @@ public void connectWithShardInfoAndEmptyTrustStore() throws Exception {
245228
JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, null, null);
246229
shardInfo.setPassword("foobared");
247230

248-
Jedis jedis = new Jedis(shardInfo);
249-
try {
231+
try (Jedis jedis = new Jedis(shardInfo)) {
250232
assertEquals("PONG", jedis.ping());
251233
fail("The code did not throw the expected JedisConnectionException.");
252234
} catch (JedisConnectionException e) {
@@ -257,12 +239,6 @@ public void connectWithShardInfoAndEmptyTrustStore() throws Exception {
257239
assertEquals("Unexpected third inner exception.", InvalidAlgorithmParameterException.class,
258240
e.getCause().getCause().getCause().getClass());
259241
}
260-
261-
try {
262-
jedis.close();
263-
} catch (Throwable e1) {
264-
// Expected.
265-
}
266242
}
267243

268244
/**

src/test/java/redis/clients/jedis/tests/SSLJedisWithCompleteCredentialsTest.java

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,9 @@ public void connectWithShardInfo() throws Exception {
115115
shardInfo.setUser("acljedis");
116116
shardInfo.setPassword("fizzbuzz");
117117

118-
Jedis jedis = new Jedis(shardInfo);
119-
assertEquals("PONG", jedis.ping());
120-
jedis.disconnect();
121-
jedis.close();
118+
try (Jedis jedis = new Jedis(shardInfo)) {
119+
assertEquals("PONG", jedis.ping());
120+
}
122121
}
123122

124123
/**
@@ -145,8 +144,7 @@ public void connectWithShardInfoByIpAddress() throws Exception {
145144
shardInfo.setUser("acljedis");
146145
shardInfo.setPassword("fizzbuzz");
147146

148-
Jedis jedis = new Jedis(shardInfo);
149-
try {
147+
try (Jedis jedis = new Jedis(shardInfo)) {
150148
assertEquals("PONG", jedis.ping());
151149
fail("The code did not throw the expected JedisConnectionException.");
152150
} catch (JedisConnectionException e) {
@@ -155,12 +153,6 @@ public void connectWithShardInfoByIpAddress() throws Exception {
155153
assertEquals("Unexpected second inner exception.",
156154
CertificateException.class, e.getCause().getCause().getClass());
157155
}
158-
159-
try {
160-
jedis.close();
161-
} catch (Throwable e1) {
162-
// Expected.
163-
}
164156
}
165157

166158
/**
@@ -178,10 +170,9 @@ public void connectWithShardInfoAndCustomHostnameVerifier() {
178170
shardInfo.setUser("acljedis");
179171
shardInfo.setPassword("fizzbuzz");
180172

181-
Jedis jedis = new Jedis(shardInfo);
182-
assertEquals("PONG", jedis.ping());
183-
jedis.disconnect();
184-
jedis.close();
173+
try (Jedis jedis = new Jedis(shardInfo)) {
174+
assertEquals("PONG", jedis.ping());
175+
}
185176
}
186177

187178
/**
@@ -198,10 +189,9 @@ public void connectWithShardInfoAndCustomSocketFactory() throws Exception {
198189
shardInfo.setUser("acljedis");
199190
shardInfo.setPassword("fizzbuzz");
200191

201-
Jedis jedis = new Jedis(shardInfo);
202-
assertEquals("PONG", jedis.ping());
203-
jedis.disconnect();
204-
jedis.close();
192+
try (Jedis jedis = new Jedis(shardInfo)) {
193+
assertEquals("PONG", jedis.ping());
194+
}
205195
}
206196

207197
/**
@@ -221,19 +211,14 @@ public void connectWithShardInfoAndCustomHostnameVerifierByIpAddress() {
221211
shardInfo.setUser("acljedis");
222212
shardInfo.setPassword("fizzbuzz");
223213

224-
Jedis jedis = new Jedis(shardInfo);
225-
try {
226-
assertEquals("PONG", jedis.ping());
227-
fail("The code did not throw the expected JedisConnectionException.");
228-
} catch (JedisConnectionException e) {
229-
assertEquals("The JedisConnectionException does not contain the expected message.",
230-
"The connection to '127.0.0.1' failed ssl/tls hostname verification.", e.getMessage());
231-
}
232-
233-
try {
234-
jedis.close();
235-
} catch (Throwable e1) {
236-
// Expected.
214+
try (Jedis jedis = new Jedis(shardInfo)) {
215+
try {
216+
assertEquals("PONG", jedis.ping());
217+
fail("The code did not throw the expected JedisConnectionException.");
218+
} catch (JedisConnectionException e) {
219+
assertEquals("The JedisConnectionException does not contain the expected message.",
220+
"The connection to '127.0.0.1' failed ssl/tls hostname verification.", e.getMessage());
221+
}
237222
}
238223
}
239224

@@ -254,8 +239,7 @@ public void connectWithShardInfoAndEmptyTrustStore() throws Exception {
254239
shardInfo.setUser("acljedis");
255240
shardInfo.setPassword("fizzbuzz");
256241

257-
Jedis jedis = new Jedis(shardInfo);
258-
try {
242+
try (Jedis jedis = new Jedis(shardInfo)) {
259243
assertEquals("PONG", jedis.ping());
260244
fail("The code did not throw the expected JedisConnectionException.");
261245
} catch (JedisConnectionException e) {
@@ -266,12 +250,6 @@ public void connectWithShardInfoAndEmptyTrustStore() throws Exception {
266250
assertEquals("Unexpected third inner exception.", InvalidAlgorithmParameterException.class,
267251
e.getCause().getCause().getCause().getClass());
268252
}
269-
270-
try {
271-
jedis.close();
272-
} catch (Throwable e1) {
273-
// Expected.
274-
}
275253
}
276254

277255
/**

src/test/java/redis/clients/jedis/tests/UdsTest.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package redis.clients.jedis.tests;
22

3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.net.Socket;
36
import org.junit.Test;
47
import org.newsclub.net.unix.AFUNIXSocket;
58
import org.newsclub.net.unix.AFUNIXSocketAddress;
9+
610
import redis.clients.jedis.Jedis;
711
import redis.clients.jedis.JedisSocketFactory;
812
import redis.clients.jedis.Protocol;
9-
10-
import java.io.File;
11-
import java.io.IOException;
12-
import java.net.Socket;
13+
import redis.clients.jedis.exceptions.JedisConnectionException;
1314

1415
import static org.junit.Assert.assertEquals;
1516

@@ -27,10 +28,14 @@ private static class UdsJedisSocketFactory implements JedisSocketFactory {
2728
private static final File UDS_SOCKET = new File("/tmp/redis_uds.sock");
2829

2930
@Override
30-
public Socket createSocket() throws IOException {
31-
Socket socket = AFUNIXSocket.newStrictInstance();
32-
socket.connect(new AFUNIXSocketAddress(UDS_SOCKET), Protocol.DEFAULT_TIMEOUT);
33-
return socket;
31+
public Socket createSocket() throws JedisConnectionException {
32+
try {
33+
Socket socket = AFUNIXSocket.newStrictInstance();
34+
socket.connect(new AFUNIXSocketAddress(UDS_SOCKET), Protocol.DEFAULT_TIMEOUT);
35+
return socket;
36+
} catch (IOException ioe) {
37+
throw new JedisConnectionException("Failed to create UDS connection.", ioe);
38+
}
3439
}
3540

3641
@Override

0 commit comments

Comments
 (0)