Skip to content

Commit

Permalink
feat: TgSessionOption: Added setKeepAlive()
Browse files Browse the repository at this point in the history
  • Loading branch information
hishidama committed Aug 22, 2024
1 parent c32f746 commit b60fa8a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ protected SessionBuilder createLowSessionBuilder(@Nullable Credential credential
lowBuilder.withApplicationName(name);
});

sessionOption.findKeepAlive().ifPresent(keepAlive -> {
lowBuilder.withKeepAlive(keepAlive);
});

return lowBuilder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.EnumMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -123,6 +124,7 @@ public enum TgTimeoutKey {

private String sessionLabel;
private String applicationName;
private Optional<Boolean> keepAlive = Optional.empty();
private final Map<TgTimeoutKey, TgTimeValue> timeoutMap = Collections.synchronizedMap(new EnumMap<>(TgTimeoutKey.class));
private TgCommitType commitType = TgCommitType.DEFAULT;
private TgSessionShutdownType closeShutdownType = TgSessionShutdownType.NOTHING;
Expand Down Expand Up @@ -176,6 +178,28 @@ public TgSessionOption setApplicationName(@Nullable String name) {
return this.applicationName;
}

/**
* set session keep-alive.
*
* @param enabled {@code true} to enable session keep-alive, or {@code false} to disable it
* @return this
* @since X.X.X
*/
public TgSessionOption setKeepAlive(@Nullable Boolean enabled) {
this.keepAlive = Optional.ofNullable(enabled);
return this;
}

/**
* get session keep-alive.
*
* @return session keep-alive
* @since X.X.X
*/
public Optional<Boolean> findKeepAlive() {
return this.keepAlive;
}

/**
* set timeout.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertNull;

import java.net.URI;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -51,12 +52,45 @@ void applicationName() {
assertEquals("test-app", actual);
}

@Test
void keepAlive() {
{
var sessionOption = new TgSessionOption();
testKeepAlive(Optional.empty(), sessionOption);
}
{
var sessionOption = new TgSessionOption().setKeepAlive(true);
testKeepAlive(Optional.of(true), sessionOption);
}
{
var sessionOption = new TgSessionOption().setKeepAlive(false);
testKeepAlive(Optional.of(false), sessionOption);
}
{
var sessionOption = new TgSessionOption().setKeepAlive(null);
testKeepAlive(Optional.empty(), sessionOption);
}
}

private void testKeepAlive(Optional<Boolean> expected, TgSessionOption sessionOption) {
assertEquals(expected, sessionOption.findKeepAlive());

var connector = new SessionOptionTestConnector();
var lowBuilder = connector.createLowSessionBuilder(null, sessionOption);
boolean actual = getField(lowBuilder, "doKeepAlive");
assertEquals(expected.orElseGet(() -> {
var defaultBuilder = SessionBuilder.connect("tcp://test:12345");
return getField(defaultBuilder, "doKeepAlive");
}), actual);
}

// FIXME リフレクションを使わずにSessionBuilderから取得したい
private static String getField(SessionBuilder lowBuilder, String name) {
@SuppressWarnings("unchecked")
private static <T> T getField(SessionBuilder lowBuilder, String name) {
try {
var field = lowBuilder.getClass().getDeclaredField(name);
field.setAccessible(true);
return (String) field.get(lowBuilder);
return (T) field.get(lowBuilder);
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ void label() {
// $TSURUGI_HOME/bin/tgctl session list --verbose
}

void keepAlive() {
var sessionOption = TgSessionOption.of();
sessionOption.setKeepAlive(true);
}

void timeout() {
var sessionOption = TgSessionOption.of();
sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 1, TimeUnit.MINUTES);
Expand Down

0 comments on commit b60fa8a

Please sign in to comment.