Skip to content

Commit 4b42afd

Browse files
author
Yogesh Gaikwad
committed
CLI: MultiCommand#close address review comments
Closes elastic#28953
1 parent 99dd6ce commit 4b42afd

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

server/src/test/java/org/elasticsearch/cli/MultiCommandTests.java

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,49 @@ public class MultiCommandTests extends CommandTestCase {
2929

3030
static class DummyMultiCommand extends MultiCommand {
3131

32-
AtomicBoolean closed = new AtomicBoolean();
32+
final AtomicBoolean closed = new AtomicBoolean();
33+
3334
DummyMultiCommand() {
34-
super("A dummy multi command", () -> {});
35+
super("A dummy multi command", () -> {
36+
});
3537
}
38+
3639
@Override
3740
public void close() throws IOException {
3841
super.close();
39-
this.closed.compareAndSet(false, true);
42+
if (!this.closed.compareAndSet(false, true)) {
43+
throw new IOException("DummyMultiCommand closed");
44+
}
4045
}
4146
}
4247

4348
static class DummySubCommand extends Command {
44-
AtomicBoolean throwsExceptionOnClose = new AtomicBoolean();
45-
AtomicBoolean closed = new AtomicBoolean();
49+
final boolean throwsExceptionOnClose;
50+
final AtomicBoolean closed = new AtomicBoolean();
51+
4652
DummySubCommand() {
47-
super("A dummy subcommand", () -> {});
53+
this(false);
4854
}
55+
4956
DummySubCommand(boolean throwsExceptionOnClose) {
50-
super("A dummy subcommand", () -> {});
51-
this.throwsExceptionOnClose.compareAndSet(false, throwsExceptionOnClose);
57+
super("A dummy subcommand", () -> {
58+
});
59+
this.throwsExceptionOnClose = throwsExceptionOnClose;
5260
}
61+
5362
@Override
5463
protected void execute(Terminal terminal, OptionSet options) throws Exception {
5564
terminal.println("Arguments: " + options.nonOptionArguments().toString());
5665
}
66+
5767
@Override
5868
public void close() throws IOException {
59-
if (throwsExceptionOnClose.get()) {
69+
if (throwsExceptionOnClose) {
6070
throw new IOException();
6171
} else {
62-
closed.compareAndSet(false, true);
72+
if (!this.closed.compareAndSet(false, true)) {
73+
throw new IOException("DummySubCommand closed");
74+
}
6375
}
6476
}
6577
}
@@ -133,20 +145,27 @@ public void testClose() throws Exception {
133145
multiCommand.subcommands.put("command1", subCommand1);
134146
multiCommand.subcommands.put("command2", subCommand2);
135147
multiCommand.close();
136-
assertTrue("MultiCommand must have been closed when close method is invoked", multiCommand.closed.get());
137-
assertTrue("SubCommand1 must have been closed when close method is invoked", subCommand1.closed.get());
138-
assertTrue("SubCommand2 must have been closed when close method is invoked", subCommand2.closed.get());
148+
assertTrue("MultiCommand was not closed when close method is invoked", multiCommand.closed.get());
149+
assertTrue("SubCommand1 was not closed when close method is invoked", subCommand1.closed.get());
150+
assertTrue("SubCommand2 was not closed when close method is invoked", subCommand2.closed.get());
139151
}
140152

141153
public void testCloseWhenSubCommandCloseThrowsException() {
142-
boolean throwExceptionWhenClosed = true;
143-
DummySubCommand subCommand1 = new DummySubCommand(throwExceptionWhenClosed);
144-
DummySubCommand subCommand2 = new DummySubCommand();
154+
boolean command1Throws = randomBoolean();
155+
boolean command2Throws = randomBoolean();
156+
System.out.println("c1 "+command1Throws+", c2 "+command2Throws);
157+
DummySubCommand subCommand1 = new DummySubCommand(command1Throws);
158+
DummySubCommand subCommand2 = new DummySubCommand(command2Throws);
145159
multiCommand.subcommands.put("command1", subCommand1);
146160
multiCommand.subcommands.put("command2", subCommand2);
147161
// verify exception is thrown, as well as other non failed sub-commands closed
148162
// properly.
149-
expectThrows(IOException.class, () -> multiCommand.close());
150-
assertTrue("SubCommand2 must have been closed when close method is invoked", subCommand2.closed.get());
163+
IOException ioe = expectThrows(IOException.class, () -> multiCommand.close());
164+
if (command1Throws && command2Throws) {
165+
assertEquals(1, ioe.getSuppressed().length);
166+
assertTrue("Missing suppressed exceptions", ioe.getSuppressed()[0] instanceof IOException);
167+
}
168+
assertTrue("SubCommand1 was not closed when close method is invoked", command1Throws ^ subCommand1.closed.get());
169+
assertTrue("SubCommand2 was not closed when close method is invoked", command2Throws ^ subCommand2.closed.get());
151170
}
152171
}

0 commit comments

Comments
 (0)