|
| 1 | +package com.bencodez.simpleapi.sql.mysql; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | +import static org.mockito.Mockito.*; |
| 5 | + |
| 6 | +import java.sql.SQLException; |
| 7 | +import java.util.concurrent.ExecutorService; |
| 8 | +import java.util.concurrent.Executors; |
| 9 | +import java.util.stream.Stream; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +class MySQLDisconnectTest { |
| 13 | + static Stream<Throwable[]> failures() { |
| 14 | + return Stream.of( |
| 15 | + pair(null, null), |
| 16 | + pair(new IllegalStateException("pool close"), null), |
| 17 | + pair(new LinkageError("pool close"), null), |
| 18 | + pair(null, new IllegalStateException("worker shutdown")), |
| 19 | + pair(null, new LinkageError("worker shutdown")), |
| 20 | + pair(new IllegalStateException("pool close"), new LinkageError("worker shutdown")), |
| 21 | + pair(new LinkageError("pool close"), new IllegalStateException("worker shutdown"))); |
| 22 | + } |
| 23 | + private static Throwable[] pair(Throwable pool, Throwable worker) { return new Throwable[] {pool, worker}; } |
| 24 | + @Test void everyCleanupIsAttemptedAndPrimaryFailureIsPreserved() throws Exception { |
| 25 | + for (Throwable[] failures : failures().toList()) checkCleanup(failures[0], failures[1]); |
| 26 | + } |
| 27 | + private void checkCleanup(Throwable poolFailure, Throwable workerFailure) throws Exception { |
| 28 | + var worker = mock(ExecutorService.class); var manager = mock(ConnectionManager.class); |
| 29 | + if (poolFailure != null) doThrow(poolFailure).when(manager).close(); |
| 30 | + if (workerFailure != null) doThrow(workerFailure).when(worker).shutdown(); |
| 31 | + try (var executors = mockStatic(Executors.class)) { |
| 32 | + executors.when(() -> Executors.newFixedThreadPool(anyInt())).thenReturn(worker); |
| 33 | + var mysql = mysql(); |
| 34 | + var field = MySQL.class.getDeclaredField("connectionManager"); field.setAccessible(true); field.set(mysql, manager); |
| 35 | + Throwable primary = poolFailure == null ? workerFailure : poolFailure; |
| 36 | + if (primary == null) assertDoesNotThrow(mysql::disconnect); |
| 37 | + else { |
| 38 | + Throwable thrown = assertThrows(primary.getClass(), mysql::disconnect); |
| 39 | + assertSame(primary, thrown); |
| 40 | + if (poolFailure != null && workerFailure != null) |
| 41 | + assertArrayEquals(new Throwable[] { workerFailure }, thrown.getSuppressed()); |
| 42 | + else assertEquals(0, thrown.getSuppressed().length); |
| 43 | + } |
| 44 | + verify(manager).close(); verify(worker).shutdown(); |
| 45 | + } |
| 46 | + } |
| 47 | + @Test void disconnectBeforeConnectAndRepeatedDisconnectAreSafe() { |
| 48 | + var mysql = mysql(); |
| 49 | + assertDoesNotThrow(mysql::disconnect); assertDoesNotThrow(mysql::disconnect); |
| 50 | + assertTrue(mysql.getThreadPool().isShutdown()); |
| 51 | + } |
| 52 | + private static MySQL mysql() { |
| 53 | + return new MySQL(1) { |
| 54 | + public void debug(SQLException failure) { fail("Unexpected diagnostics"); } |
| 55 | + public void debug(String message) { fail("Unexpected diagnostics"); } |
| 56 | + public void severe(String message) { fail("Unexpected diagnostics"); } |
| 57 | + }; |
| 58 | + } |
| 59 | +} |
0 commit comments