* <ul>
* <li>First byte will contains the state 0x00 until READY 0x01</li>
* <li>The second byte will contains the request for stopping 0x00 or STOP (0xFF)</li>
+ * <li>The second byte will contains the request for restarting 0x00 or RESTART (0xAA)</li>
* <li>The next 8 bytes contains a long (System.currentTimeInMillis for ping)</li>
* </ul>
*/
final MappedByteBuffer mappedByteBuffer;
private final RandomAccessFile sharedMemory;
- private static final int BYTE_LENGTH_FOR_ONE_PROCESS = 1 + 1 + 8;
+ private static final int BYTE_LENGTH_FOR_ONE_PROCESS = 1 + 1 + 1 + 8;
// With this shared memory we can handle up to MAX_PROCESSES processes
private static final int MAX_SHARED_MEMORY = BYTE_LENGTH_FOR_ONE_PROCESS * MAX_PROCESSES;
public static final byte STOP = (byte) 0xFF;
+ public static final byte RESTART = (byte) 0xAA;
public static final byte READY = (byte) 0x01;
public static final byte EMPTY = (byte) 0x00;
return mappedByteBuffer.get(offset() + 1) == STOP;
}
+ @Override
+ public void askForRestart() {
+ mappedByteBuffer.put(offset() + 3, RESTART);
+ }
+
+ @Override
+ public boolean askedForRestart() {
+ return mappedByteBuffer.get(offset() + 3) == RESTART;
+ }
+
+ @Override
+ public void acknowledgeAskForRestart() {
+ mappedByteBuffer.put(offset() + 3, EMPTY);
+ }
+
@Override
public void endWatch() {
IOUtils.closeQuietly(sharedMemory);
public class DefaultProcessCommandsTest {
+ private static final int PROCESS_NUMBER = 1;
+
@Rule
public TemporaryFolder temp = new TemporaryFolder();
FileUtils.deleteQuietly(dir);
try {
- new DefaultProcessCommands(dir, 1);
+ new DefaultProcessCommands(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, 1);
+ DefaultProcessCommands commands = new DefaultProcessCommands(dir, PROCESS_NUMBER);
assertThat(commands.isReady()).isFalse();
assertThat(commands.mappedByteBuffer.get(commands.offset())).isEqualTo(DefaultProcessCommands.EMPTY);
assertThat(commands.mappedByteBuffer.getLong(2 + commands.offset())).isEqualTo(0L);
public void ask_for_stop() throws Exception {
File dir = temp.newFolder();
- DefaultProcessCommands commands = new DefaultProcessCommands(dir, 1);
- assertThat(commands.mappedByteBuffer.get(commands.offset() + 1)).isNotEqualTo(DefaultProcessCommands.STOP);
+ DefaultProcessCommands commands = new DefaultProcessCommands(dir, PROCESS_NUMBER);
+ assertThat(commands.mappedByteBuffer.get(commands.offset() + PROCESS_NUMBER)).isNotEqualTo(DefaultProcessCommands.STOP);
assertThat(commands.askedForStop()).isFalse();
commands.askForStop();
assertThat(commands.askedForStop()).isTrue();
- assertThat(commands.mappedByteBuffer.get(commands.offset() + 1)).isEqualTo(DefaultProcessCommands.STOP);
+ assertThat(commands.mappedByteBuffer.get(commands.offset() + PROCESS_NUMBER)).isEqualTo(DefaultProcessCommands.STOP);
+ }
+
+ @Test
+ public void ask_for_restart() throws Exception {
+ File dir = temp.newFolder();
+
+ DefaultProcessCommands commands = new DefaultProcessCommands(dir, PROCESS_NUMBER);
+ assertThat(commands.mappedByteBuffer.get(commands.offset() + 3)).isNotEqualTo(DefaultProcessCommands.RESTART);
+ assertThat(commands.askedForRestart()).isFalse();
+
+ commands.askForRestart();
+ assertThat(commands.askedForRestart()).isTrue();
+ assertThat(commands.mappedByteBuffer.get(commands.offset() + 3)).isEqualTo(DefaultProcessCommands.RESTART);
+ }
+
+ @Test
+ public void acknowledgeAskForRestart_has_no_effect_when_no_restart_asked() throws Exception {
+ File dir = temp.newFolder();
+
+ DefaultProcessCommands commands = new DefaultProcessCommands(dir, PROCESS_NUMBER);
+ assertThat(commands.mappedByteBuffer.get(commands.offset() + 3)).isNotEqualTo(DefaultProcessCommands.RESTART);
+ assertThat(commands.askedForRestart()).isFalse();
+
+ commands.acknowledgeAskForRestart();
+ assertThat(commands.mappedByteBuffer.get(commands.offset() + 3)).isNotEqualTo(DefaultProcessCommands.RESTART);
+ assertThat(commands.askedForRestart()).isFalse();
+ }
+
+ @Test
+ 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);
+
+ commands.askForRestart();
+ assertThat(commands.askedForRestart()).isTrue();
+ assertThat(commands.mappedByteBuffer.get(commands.offset() + 3)).isEqualTo(DefaultProcessCommands.RESTART);
+
+ commands.acknowledgeAskForRestart();
+ assertThat(commands.mappedByteBuffer.get(commands.offset() + 3)).isNotEqualTo(DefaultProcessCommands.RESTART);
+ assertThat(commands.askedForRestart()).isFalse();
}
@Test