private final AllProcessesCommands allProcessesCommands;
private final ProcessCommands delegate;
- public DefaultProcessCommands(File directory, int processNumber) {
- this(directory, processNumber, true);
- }
-
- public DefaultProcessCommands(File directory, int processNumber, boolean clean) {
+ private DefaultProcessCommands(File directory, int processNumber, boolean clean) {
this.allProcessesCommands = new AllProcessesCommands(directory);
this.delegate = clean ? allProcessesCommands.createAfterClean(processNumber) : allProcessesCommands.create(processNumber);
}
+ /**
+ * Main DefaultProcessCommands will clear the shared memory space of the specified process number when created and will
+ * then write and/or read to it.
+ * Therefor there should be only one main DefaultProcessCommands.
+ */
+ public static DefaultProcessCommands main(File directory, int processNumber) {
+ return new DefaultProcessCommands(directory, processNumber, true);
+ }
+
+ /**
+ * Secondary DefaultProcessCommands will read and write to the shared memory space but will not clear it. Therefor, there
+ * can be any number of them.
+ */
+ public static DefaultProcessCommands secondary(File directory, int processNumber) {
+ return new DefaultProcessCommands(directory, processNumber, false);
+ }
+
@Override
public boolean isUp() {
return delegate.isUp();
FileUtils.deleteQuietly(dir);
try {
- new DefaultProcessCommands(dir, PROCESS_NUMBER);
+ DefaultProcessCommands.main(dir, PROCESS_NUMBER);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("Not a valid directory: " + dir.getAbsolutePath());
public void child_process_update_the_mapped_memory() throws Exception {
File dir = temp.newFolder();
- DefaultProcessCommands commands = new DefaultProcessCommands(dir, PROCESS_NUMBER);
+ DefaultProcessCommands commands = DefaultProcessCommands.main(dir, PROCESS_NUMBER);
assertThat(commands.isUp()).isFalse();
commands.setUp();
public void ask_for_stop() throws Exception {
File dir = temp.newFolder();
- DefaultProcessCommands commands = new DefaultProcessCommands(dir, PROCESS_NUMBER);
+ DefaultProcessCommands commands = DefaultProcessCommands.main(dir, PROCESS_NUMBER);
assertThat(commands.askedForStop()).isFalse();
commands.askForStop();
public void ask_for_restart() throws Exception {
File dir = temp.newFolder();
- DefaultProcessCommands commands = new DefaultProcessCommands(dir, PROCESS_NUMBER);
+ DefaultProcessCommands commands = DefaultProcessCommands.main(dir, PROCESS_NUMBER);
assertThat(commands.askedForRestart()).isFalse();
commands.askForRestart();
public void acknowledgeAskForRestart_has_no_effect_when_no_restart_asked() throws Exception {
File dir = temp.newFolder();
- DefaultProcessCommands commands = new DefaultProcessCommands(dir, PROCESS_NUMBER);
+ DefaultProcessCommands commands = DefaultProcessCommands.main(dir, PROCESS_NUMBER);
assertThat(commands.askedForRestart()).isFalse();
commands.acknowledgeAskForRestart();
public void acknowledgeAskForRestart_resets_askForRestart_has_no_effect_when_no_restart_asked() throws Exception {
File dir = temp.newFolder();
- DefaultProcessCommands commands = new DefaultProcessCommands(dir, PROCESS_NUMBER);
+ DefaultProcessCommands commands = DefaultProcessCommands.main(dir, PROCESS_NUMBER);
commands.askForRestart();
assertThat(commands.askedForRestart()).isTrue();
}
@Test
- public void constructor_fails_if_processNumber_is_less_than_0() throws Exception {
- File dir = temp.newFolder();
+ public void main_fails_if_processNumber_is_less_than_0() throws Exception {
int processNumber = -2;
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("Process number " + processNumber + " is not valid");
+ expectProcessNumberNoValidIAE(processNumber);
- new DefaultProcessCommands(dir, processNumber);
+ DefaultProcessCommands.main(temp.newFolder(), processNumber);
}
@Test
- public void getProcessCommands_fails_if_processNumber_is_higher_than_MAX_PROCESSES() throws Exception {
- File dir = temp.newFolder();
+ public void main_fails_if_processNumber_is_higher_than_MAX_PROCESSES() throws Exception {
+ int processNumber = MAX_PROCESSES + 1;
+
+ expectProcessNumberNoValidIAE(processNumber);
+
+ DefaultProcessCommands.main(temp.newFolder(), processNumber);
+ }
+
+ @Test
+ public void secondary_fails_if_processNumber_is_less_than_0() throws Exception {
+ int processNumber = -2;
+
+ expectProcessNumberNoValidIAE(processNumber);
+
+ DefaultProcessCommands.secondary(temp.newFolder(), processNumber);
+ }
+
+ @Test
+ public void secondary_fails_if_processNumber_is_higher_than_MAX_PROCESSES() throws Exception {
int processNumber = MAX_PROCESSES + 1;
+ expectProcessNumberNoValidIAE(processNumber);
+
+ DefaultProcessCommands.secondary(temp.newFolder(), processNumber);
+ }
+
+ private void expectProcessNumberNoValidIAE(int processNumber) {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Process number " + processNumber + " is not valid");
-
- new DefaultProcessCommands(dir, processNumber);
}
}