Skip to content

Commit

Permalink
[HZ-1202] Make TcpIpConnection.equals more robust (3.12.z) (#21885)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwart committed May 18, 2023
1 parent 71ac661 commit eeb392a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
35 changes: 25 additions & 10 deletions hazelcast/src/main/java/com/hazelcast/nio/tcp/TcpIpConnection.java
Expand Up @@ -31,6 +31,8 @@
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.channels.CancelledKeyException;
import java.util.Arrays;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;

Expand Down Expand Up @@ -187,20 +189,24 @@ public boolean write(OutboundFrame frame) {
}

@Override
public boolean equals(Object o) {
if (this == o) {
public int hashCode() {
return hash(channel.isClientMode(), connectionId);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(o instanceof TcpIpConnection)) {
if (obj == null) {
return false;
}
TcpIpConnection that = (TcpIpConnection) o;
return connectionId == that.getConnectionId();
}

@Override
public int hashCode() {
return connectionId;
if (getClass() != obj.getClass()) {
return false;
}
TcpIpConnection other = (TcpIpConnection) obj;
return channel.isClientMode() == other.channel.isClientMode() && connectionId == other.connectionId
&& equals(endPoint, other.endPoint);
}

@Override
Expand Down Expand Up @@ -297,4 +303,13 @@ public String toString() {
+ ", type=" + type
+ "]";
}

private static int hash(Object... values) {
return Arrays.hashCode(values);
}

private static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}

}
Expand Up @@ -16,11 +16,14 @@

package com.hazelcast.nio.tcp;

import com.hazelcast.internal.networking.Channel;
import com.hazelcast.nio.ConnectionLifecycleListener;
import com.hazelcast.nio.Packet;
import com.hazelcast.test.AssertTask;
import com.hazelcast.util.function.Consumer;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;

import java.net.InetAddress;
import java.net.InetSocketAddress;
Expand All @@ -44,6 +47,9 @@ public abstract class TcpIpConnection_AbstractBasicTest extends TcpIpConnection_

private List<Packet> packetsB;

@Mock
private ConnectionLifecycleListener<TcpIpConnection> mockedListener;

@Before
public void setup() throws Exception {
super.setup();
Expand Down Expand Up @@ -235,5 +241,19 @@ public void test_equals() {
assertNotEquals(connAB, connAC);
assertNotEquals(connAC, connAB);
assertNotEquals(connAB, "foo");

// don't mock if you don't need to
TcpIpEndpointManager em = connAB.getEndpointManager();
Channel channel = connAB.getChannel();
TcpIpConnection conn1 = new TcpIpConnection(em, mockedListener, 0, channel);
TcpIpConnection conn2 = new TcpIpConnection(em, mockedListener, 0, channel);
assertEquals(conn1, conn2);
conn1.setEndPoint(addressA);
assertNotEquals(conn1, conn2);
conn2.setEndPoint(addressB);
assertNotEquals(conn1, conn2);
conn2.setEndPoint(addressA);
assertEquals(conn1, conn2);
}

}

0 comments on commit eeb392a

Please sign in to comment.