|
20 | 20 | package org.elasticsearch.test.transport; |
21 | 21 |
|
22 | 22 | import com.carrotsearch.randomizedtesting.SysGlobals; |
| 23 | +import java.util.concurrent.TimeUnit; |
23 | 24 | import org.elasticsearch.Version; |
24 | 25 | import org.elasticsearch.action.ActionListener; |
25 | 26 | import org.elasticsearch.cluster.ClusterModule; |
@@ -599,30 +600,37 @@ public Transport.Connection openConnection(DiscoveryNode node, ConnectionProfile |
599 | 600 | Transport.Connection connection = super.openConnection(node, profile); |
600 | 601 |
|
601 | 602 | synchronized (openConnections) { |
602 | | - List<Transport.Connection> connections = openConnections.computeIfAbsent(node, |
603 | | - (n) -> new CopyOnWriteArrayList<>()); |
604 | | - connections.add(connection); |
605 | | - } |
606 | | - |
607 | | - connection.addCloseListener(ActionListener.wrap(() -> { |
608 | | - synchronized (openConnections) { |
609 | | - List<Transport.Connection> connections = openConnections.get(node); |
610 | | - boolean remove = connections.remove(connection); |
611 | | - assert remove : "Should have removed connection"; |
612 | | - if (connections.isEmpty()) { |
613 | | - openConnections.remove(node); |
| 603 | + openConnections.computeIfAbsent(node, n -> new CopyOnWriteArrayList<>()).add(connection); |
| 604 | + connection.addCloseListener(ActionListener.wrap(() -> { |
| 605 | + synchronized (openConnections) { |
| 606 | + List<Transport.Connection> connections = openConnections.get(node); |
| 607 | + boolean remove = connections.remove(connection); |
| 608 | + assert remove : "Should have removed connection"; |
| 609 | + if (connections.isEmpty()) { |
| 610 | + openConnections.remove(node); |
| 611 | + } |
| 612 | + if (openConnections.isEmpty()) { |
| 613 | + openConnections.notifyAll(); |
| 614 | + } |
614 | 615 | } |
615 | | - } |
616 | | - })); |
| 616 | + })); |
| 617 | + } |
617 | 618 |
|
618 | 619 | return connection; |
619 | 620 | } |
620 | 621 |
|
621 | 622 | @Override |
622 | 623 | protected void doClose() throws IOException { |
623 | 624 | super.doClose(); |
624 | | - synchronized (openConnections) { |
625 | | - assert openConnections.size() == 0 : "still open connections: " + openConnections; |
| 625 | + try { |
| 626 | + synchronized (openConnections) { |
| 627 | + if (openConnections.isEmpty() == false) { |
| 628 | + openConnections.wait(TimeUnit.SECONDS.toMillis(30L)); |
| 629 | + } |
| 630 | + assert openConnections.size() == 0 : "still open connections: " + openConnections; |
| 631 | + } |
| 632 | + } catch (InterruptedException e) { |
| 633 | + throw new IllegalStateException(e); |
626 | 634 | } |
627 | 635 | } |
628 | 636 |
|
|
0 commit comments