42
42
import java .util .Optional ;
43
43
import java .util .Set ;
44
44
import java .util .concurrent .TimeUnit ;
45
+ import java .util .concurrent .atomic .AtomicBoolean ;
45
46
import java .util .concurrent .atomic .AtomicInteger ;
46
47
import java .util .function .Function ;
47
48
import java .util .function .LongSupplier ;
48
49
import java .util .stream .Collector ;
49
50
import java .util .stream .Collectors ;
50
51
import java .util .stream .Stream ;
51
52
53
+ import static org .hamcrest .Matchers .containsInAnyOrder ;
52
54
import static org .hamcrest .Matchers .equalTo ;
53
55
54
56
public class PublicationTests extends ESTestCase {
@@ -97,6 +99,7 @@ abstract class MockPublication extends Publication {
97
99
98
100
Map <DiscoveryNode , ActionListener <PublishWithJoinResponse >> pendingPublications = new HashMap <>();
99
101
Map <DiscoveryNode , ActionListener <TransportResponse .Empty >> pendingCommits = new HashMap <>();
102
+ Map <DiscoveryNode , PublishWithJoinResponse > possibleJoins = new HashMap <>();
100
103
101
104
MockPublication (Settings settings , PublishRequest publishRequest , Discovery .AckListener ackListener ,
102
105
LongSupplier currentTimeSupplier ) {
@@ -106,13 +109,14 @@ abstract class MockPublication extends Publication {
106
109
107
110
@ Override
108
111
protected void onCompletion (boolean committed ) {
112
+ assertFalse (completed );
109
113
completed = true ;
110
114
this .committed = committed ;
111
115
}
112
116
113
117
@ Override
114
118
protected void onPossibleJoin (DiscoveryNode sourceNode , PublishWithJoinResponse response ) {
115
-
119
+ assertNull ( possibleJoins . put ( sourceNode , response ));
116
120
}
117
121
118
122
@ Override
@@ -166,25 +170,39 @@ public void testSimpleClusterStatePublishing() throws InterruptedException {
166
170
167
171
assertThat (publication .pendingPublications .keySet (), equalTo (discoNodes ));
168
172
assertTrue (publication .pendingCommits .isEmpty ());
173
+ AtomicBoolean processedNode1PublishResponse = new AtomicBoolean ();
169
174
publication .pendingPublications .entrySet ().stream ().collect (shuffle ()).forEach (e -> {
170
175
PublishResponse publishResponse = nodeResolver .apply (e .getKey ()).coordinationState .handlePublishRequest (
171
176
publication .publishRequest );
172
- e .getValue ().onResponse (new PublishWithJoinResponse (publishResponse , Optional .empty ()));
177
+ assertNotEquals (processedNode1PublishResponse .get (), publication .pendingCommits .isEmpty ());
178
+ assertFalse (publication .possibleJoins .containsKey (e .getKey ()));
179
+ PublishWithJoinResponse publishWithJoinResponse = new PublishWithJoinResponse (publishResponse ,
180
+ randomBoolean () ? Optional .empty () : Optional .of (new Join (e .getKey (), n1 , randomNonNegativeLong (),
181
+ randomNonNegativeLong (), randomNonNegativeLong ())));
182
+ e .getValue ().onResponse (publishWithJoinResponse );
183
+ assertTrue (publication .possibleJoins .containsKey (e .getKey ()));
184
+ assertEquals (publishWithJoinResponse , publication .possibleJoins .get (e .getKey ()));
185
+ if (e .getKey ().equals (n1 )) {
186
+ processedNode1PublishResponse .set (true );
187
+ }
188
+ assertNotEquals (processedNode1PublishResponse .get (), publication .pendingCommits .isEmpty ());
173
189
});
174
190
175
191
assertThat (publication .pendingCommits .keySet (), equalTo (discoNodes ));
176
192
assertNotNull (publication .applyCommit );
177
193
assertEquals (publication .applyCommit .getTerm (), publication .publishRequest .getAcceptedState ().term ());
178
194
assertEquals (publication .applyCommit .getVersion (), publication .publishRequest .getAcceptedState ().version ());
179
195
publication .pendingCommits .entrySet ().stream ().collect (shuffle ()).forEach (e -> {
196
+ assertFalse (publication .completed );
197
+ assertFalse (publication .committed );
180
198
nodeResolver .apply (e .getKey ()).coordinationState .handleCommit (publication .applyCommit );
181
199
e .getValue ().onResponse (TransportResponse .Empty .INSTANCE );
182
200
});
183
201
184
202
assertTrue (publication .completed );
185
203
assertTrue (publication .committed );
186
204
187
- assertThat (ackListener .await (0L , TimeUnit .SECONDS ). size (), equalTo ( 3 ));
205
+ assertThat (ackListener .await (0L , TimeUnit .SECONDS ), containsInAnyOrder ( n1 , n2 , n3 ));
188
206
}
189
207
190
208
public void testClusterStatePublishingWithFaultyNode () throws InterruptedException {
0 commit comments