Skip to content

Commit 627b1ad

Browse files
Bouncheckavelanarius
authored andcommitted
Fix flakiness in ScyllaSniProxyTest
Sometimes listener gets notified twice, causing assertion checking for exactly 1 call to fail. Now we permit at most 2 calls, but both have to refer to correct keyspace and table names.
1 parent 3e2d8a1 commit 627b1ad

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

driver-core/src/test/java/com/datastax/driver/core/ScyllaSniProxyTest.java

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import static org.mockito.Mockito.atLeast;
88
import static org.mockito.Mockito.atMost;
99
import static org.mockito.Mockito.mock;
10-
import static org.mockito.Mockito.times;
1110
import static org.mockito.Mockito.verify;
1211

1312
import com.datastax.driver.core.utils.ScyllaOnly;
13+
import java.util.ArrayList;
1414
import java.util.Collection;
1515
import org.testng.annotations.Test;
1616

@@ -19,6 +19,7 @@
1919
public class ScyllaSniProxyTest extends CCMTestsSupport {
2020

2121
private void test_ccm_cluster(int testNodes) {
22+
ccm().setKeepLogs(true);
2223
Cluster c = cluster().init();
2324
Session s = c.connect();
2425
TestUtils.waitForUp(TestUtils.ipOfNode(1), c);
@@ -35,15 +36,60 @@ private void test_ccm_cluster(int testNodes) {
3536
((SessionManager) s).cluster.manager.controlConnection.triggerReconnect();
3637

3738
SchemaChangeListener listener = mock(SchemaChangeListenerBase.class);
39+
final ArrayList<String> keyspaceMismatches = new ArrayList<String>();
40+
final ArrayList<String> tableMismatches = new ArrayList<String>();
41+
42+
SchemaChangeListener assertingListener =
43+
new SchemaChangeListenerBase() {
44+
@Override
45+
public void onKeyspaceAdded(KeyspaceMetadata keyspace) {
46+
if (!keyspace.getName().equals("testks")) {
47+
keyspaceMismatches.add(keyspace.getName());
48+
}
49+
}
50+
51+
@Override
52+
public void onTableAdded(TableMetadata table) {
53+
if (!table.getName().equals("testtab")) {
54+
tableMismatches.add(table.getName());
55+
}
56+
}
57+
};
58+
3859
c.register(listener);
60+
c.register(assertingListener);
3961

4062
s.execute(String.format(TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT, "testks", testNodes));
4163
s.execute("CREATE TABLE testks.testtab (a int PRIMARY KEY, b int);");
4264

43-
verify(listener, times(1)).onTableAdded(any(TableMetadata.class));
65+
// Sometimes (probably due to reconnection) both events can be read twice
66+
// assertingListener ensures we deal with the same keyspace and table
67+
verify(listener, atLeast(1)).onTableAdded(any(TableMetadata.class));
68+
verify(listener, atMost(2)).onTableAdded(any(TableMetadata.class));
4469
verify(listener, atLeast(1)).onKeyspaceAdded(any(KeyspaceMetadata.class));
4570
verify(listener, atMost(2)).onKeyspaceAdded(any(KeyspaceMetadata.class));
4671

72+
if (!keyspaceMismatches.isEmpty()) {
73+
StringBuilder ksNames = new StringBuilder();
74+
for (String str : keyspaceMismatches) {
75+
ksNames.append(", " + ksNames + str);
76+
}
77+
throw new RuntimeException(
78+
"assertingListener registered keyspace added event with keyspaces named: ["
79+
+ ksNames.substring(2)
80+
+ "] which is not \"testks\"");
81+
}
82+
if (!tableMismatches.isEmpty()) {
83+
StringBuilder tabNames = new StringBuilder();
84+
for (String str : tableMismatches) {
85+
tabNames.append(", " + tabNames + str);
86+
}
87+
throw new RuntimeException(
88+
"assertingListener registered table added event with tables named: ["
89+
+ tabNames.substring(2)
90+
+ "] which is not \"testtab\"");
91+
}
92+
4793
s.close();
4894
c.close();
4995
}

0 commit comments

Comments
 (0)