Skip to content

Commit

Permalink
test: Added session label
Browse files Browse the repository at this point in the history
  • Loading branch information
hishidama committed Jun 19, 2024
1 parent 66bb755 commit 2f07320
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ void limit() throws Exception {
var sessionList = new ArrayList<TsurugiSession>();
Throwable occurred = null;
try {
String baseLabel = DbTestConnector.getSessionLabel();
for (int i = 0; i < ATTEMPT_SIZE; i++) {
TsurugiSession session;
try {
session = DbTestConnector.createSession();
session = DbTestConnector.createSession(baseLabel + "[" + i + "]");
} catch (IOException e) {
assertEqualsMessage("the server has declined the connection request", e);
int count = sessionList.size();
Expand All @@ -60,23 +61,7 @@ void limit() throws Exception {
occurred = e;
throw e;
} finally {
var exceptionList = new ArrayList<Exception>();
for (var session : sessionList) {
try {
session.close();
} catch (Exception e) {
exceptionList.add(e);
}
}
if (!exceptionList.isEmpty()) {
var e = new Exception("session close error. errorCount=" + exceptionList.size());
exceptionList.forEach(e::addSuppressed);
if (occurred != null) {
occurred.addSuppressed(e);
} else {
throw e;
}
}
closeSession(sessionList, occurred);
}
}

Expand Down Expand Up @@ -121,11 +106,13 @@ void manySession3() throws Exception {

private void manySession(boolean sqlClient, boolean transaction) throws IOException, InterruptedException {
LOG.debug("create session start");
var list = new ArrayList<TsurugiSession>();
var sessionList = new ArrayList<TsurugiSession>();
Throwable occurred = null;
try {
String baseLabel = DbTestConnector.getSessionLabel();
for (int i = 0; i < 60; i++) {
var session = DbTestConnector.createSession();
list.add(session);
var session = DbTestConnector.createSession(baseLabel + "[" + i + "]");
sessionList.add(session);

if (sqlClient) {
session.getLowSqlClient();
Expand All @@ -136,35 +123,37 @@ private void manySession(boolean sqlClient, boolean transaction) throws IOExcept
if (transaction) {
LOG.debug("createTransaction start");
int i = 0;
for (var session : list) {
for (var session : sessionList) {
LOG.debug("createTransaction {}", i++);
try (var tx = session.createTransaction(TgTxOption.ofOCC())) {
tx.getLowTransaction();
}
}
LOG.debug("createTransaction end");
}
} catch (Throwable e) {
occurred = e;
throw e;
} finally {
LOG.debug("close session start");
for (var session : list) {
session.close();
}
LOG.debug("close session end");
closeSession(sessionList, occurred);
}
}

@Test
void multiThread() {
LOG.debug("create session start");
var sessionList = new CopyOnWriteArrayList<TsurugiSession>();
Throwable occurred = null;
try {
var threadList = new ArrayList<Thread>();
var alive = new AtomicBoolean(true);
String baseLabel = DbTestConnector.getSessionLabel();
for (int i = 0; i < 60; i++) {
String label = baseLabel + "[" + i + "]";
var thread = new Thread(() -> {
TsurugiSession session;
try {
session = DbTestConnector.createSession();
session = DbTestConnector.createSession(label);
sessionList.add(session);
session.getLowSqlClient();
} catch (Exception e) {
Expand Down Expand Up @@ -196,16 +185,36 @@ void multiThread() {
}
}
LOG.debug("thread join end");
} catch (Throwable e) {
occurred = e;
throw e;
} finally {
LOG.debug("close session start");
for (var session : sessionList) {
try {
session.close();
} catch (Exception e) {
LOG.warn("close error", e);
}
closeSession(sessionList, occurred);
}
}

private void closeSession(List<TsurugiSession> sessionList, Throwable occurred) {
LOG.debug("close session start");

var exceptionList = new ArrayList<Exception>();
for (var session : sessionList) {
try {
session.close();
} catch (Exception e) {
exceptionList.add(e);
}
LOG.debug("close session end");
}

if (!exceptionList.isEmpty()) {
var e = new RuntimeException("session close error. errorCount=" + exceptionList.size());
exceptionList.forEach(e::addSuppressed);
if (occurred != null) {
occurred.addSuppressed(e);
} else {
throw e;
}
}

LOG.debug("close session end");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class DbTestConnector {
private static URI staticEndpoint;
private static final List<TsurugiSession> staticSessionList = new CopyOnWriteArrayList<>();

private static String sessionLabel;

public static URI getEndPoint() {
if (staticEndpoint == null) {
String endpoint = System.getProperty(SYSPROP_DBTEST_ENDPOINT, "tcp://localhost:12345");
Expand Down Expand Up @@ -57,27 +59,40 @@ public static URI assumeEndpointTcp() {
return endpoint;
}

public static void setSessionLabel(String label) {
sessionLabel = label;
}

public static String getSessionLabel() {
return sessionLabel;
}

public static TsurugiConnector createConnector() {
URI endpoint = getEndPoint();
return TsurugiConnector.of(endpoint);
return TsurugiConnector.of(endpoint).setApplicationName("iceaxe-dbtest");
}

private static final TgSessionShutdownType CLOSE_SHUTDOWN_TYPE = TgSessionShutdownType.GRACEFUL;

public static TsurugiSession createSession() throws IOException {
return createSession(20, TimeUnit.SECONDS);
return createSession(sessionLabel);
}

public static TsurugiSession createSession(String label) throws IOException {
return createSession(label, 20, TimeUnit.SECONDS, CLOSE_SHUTDOWN_TYPE);
}

public static TsurugiSession createSession(long time, TimeUnit unit) throws IOException {
return createSession(time, unit, CLOSE_SHUTDOWN_TYPE);
return createSession(sessionLabel, time, unit, CLOSE_SHUTDOWN_TYPE);
}

public static TsurugiSession createSession(TgSessionShutdownType shutdownType) throws IOException {
return createSession(20, TimeUnit.SECONDS, shutdownType);
return createSession(sessionLabel, 20, TimeUnit.SECONDS, shutdownType);
}

public static TsurugiSession createSession(long time, TimeUnit unit, TgSessionShutdownType shutdownType) throws IOException {
public static TsurugiSession createSession(String label, long time, TimeUnit unit, TgSessionShutdownType shutdownType) throws IOException {
var sessionOption = TgSessionOption.of();
sessionOption.setLabel(label);
sessionOption.setTimeout(TgTimeoutKey.DEFAULT, time, unit);
sessionOption.setCloseShutdownType(shutdownType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ static void testerAfterAll() throws IOException, InterruptedException, ServerExc
private static final boolean START_END_LOG_INFO = true;

protected static void logInitStart(Logger log, TestInfo info) {
setSessionLabel(info, null, "init all");

if (START_END_LOG_INFO) {
log.info("init all start");
} else {
Expand All @@ -119,6 +121,8 @@ protected static void logInitStart(Logger log, TestInfo info) {
}

protected static void logInitEnd(Logger log, TestInfo info) {
DbTestConnector.setSessionLabel(null);

if (START_END_LOG_INFO) {
log.info("init all end");
} else {
Expand All @@ -129,6 +133,8 @@ protected static void logInitEnd(Logger log, TestInfo info) {

protected void logInitStart(TestInfo info) {
String displayName = getDisplayName(info);
setSessionLabel(info, displayName, "init");

if (START_END_LOG_INFO) {
LOG.info("{} init start", displayName);
} else {
Expand All @@ -138,6 +144,8 @@ protected void logInitStart(TestInfo info) {
}

protected void logInitEnd(TestInfo info) {
DbTestConnector.setSessionLabel(null);

String displayName = getDisplayName(info);
if (START_END_LOG_INFO) {
LOG.info("{} init end", displayName);
Expand All @@ -150,6 +158,8 @@ protected void logInitEnd(TestInfo info) {
@BeforeEach
void tetsterBeforeEach(TestInfo info) {
String displayName = getDisplayName(info);
setSessionLabel(info, displayName, null);

if (START_END_LOG_INFO) {
LOG.info("{} start", displayName);
} else {
Expand All @@ -160,6 +170,8 @@ void tetsterBeforeEach(TestInfo info) {

@AfterEach
void testerAfterEach(TestInfo info) {
DbTestConnector.setSessionLabel(null);

String displayName = getDisplayName(info);
if (START_END_LOG_INFO) {
LOG.info("{} end", displayName);
Expand All @@ -169,6 +181,18 @@ void testerAfterEach(TestInfo info) {
serverLog(LOG, displayName, "end");
}

private static void setSessionLabel(TestInfo info, String displayName, String suffix) {
String className = info.getTestClass().map(c -> c.getSimpleName()).orElse("Unknown");
String label = className;
if (displayName != null) {
label += "." + displayName;
}
if (suffix != null) {
label += " " + suffix;
}
DbTestConnector.setSessionLabel(label);
}

private static String getDisplayName(TestInfo info) {
String d = info.getDisplayName();
String m = info.getTestMethod().map(Method::getName).orElse(null);
Expand Down

0 comments on commit 2f07320

Please sign in to comment.