|
| 1 | +package io.ebean.datasource.test; |
| 2 | + |
| 3 | +import io.ebean.test.containers.Db2Container; |
| 4 | +import io.ebean.test.containers.MariaDBContainer; |
| 5 | +import io.ebean.test.containers.PostgresContainer; |
| 6 | +import org.junit.jupiter.api.AfterAll; |
| 7 | +import org.junit.jupiter.api.BeforeAll; |
| 8 | +import org.junit.jupiter.api.Disabled; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import java.io.IOException; |
| 12 | +import java.sql.Connection; |
| 13 | +import java.sql.PreparedStatement; |
| 14 | +import java.sql.ResultSet; |
| 15 | + |
| 16 | +/** |
| 17 | + * This testsuite demonstrates, how a driver behaves, when a network outage occurs. |
| 18 | + * |
| 19 | + * @author Roland Praml, Foconis Analytics GmbH |
| 20 | + */ |
| 21 | +public class NetworkOutageTest { |
| 22 | + |
| 23 | + static void openPort(int port) throws Exception { |
| 24 | + Runtime.getRuntime().exec("sudo iptables -D OUTPUT -p tcp --dport " + port + " -j DROP").waitFor(); // remove rule |
| 25 | + } |
| 26 | + |
| 27 | + static void closePort(int port) throws Exception { |
| 28 | + System.out.println("Closing port " + port); |
| 29 | + Runtime.getRuntime().exec("sudo iptables -A OUTPUT -p tcp --dport " + port + " -j DROP").waitFor(); // remove rule |
| 30 | + } |
| 31 | + |
| 32 | + @BeforeAll |
| 33 | + @AfterAll |
| 34 | + static void removeRules() throws Exception { |
| 35 | + openPort(3306); |
| 36 | + openPort(50000); |
| 37 | + openPort(5432); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + @Disabled |
| 42 | + void testNetworkOutageDb2() throws Exception { |
| 43 | + Db2Container container = Db2Container.builder("latest").build(); |
| 44 | + container.start(); |
| 45 | + |
| 46 | + Connection conn = container.createConnection(); |
| 47 | + PreparedStatement stmt = conn.prepareStatement("SELECT * from SYSCAT.TABLES"); |
| 48 | + stmt.setFetchSize(10); |
| 49 | + ResultSet rs = stmt.executeQuery(); |
| 50 | + rs.next(); |
| 51 | + rs.next(); // read two rows. Now simulate network outage |
| 52 | + |
| 53 | + closePort(50000); |
| 54 | + |
| 55 | + long startTime = System.currentTimeMillis(); |
| 56 | + stmt.close(); // this will block 1 minute or even longer |
| 57 | + conn.close(); |
| 58 | + System.out.println("Done in " + (System.currentTimeMillis() - startTime) + "ms"); |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + @Disabled |
| 64 | + void testNetworkOutageMariaDb() throws Exception { |
| 65 | + MariaDBContainer container = MariaDBContainer.builder("latest").build(); |
| 66 | + container.start(); |
| 67 | + |
| 68 | + Connection conn = container.createConnection(); |
| 69 | + PreparedStatement stmt = conn.prepareStatement("SELECT * from INFORMATION_SCHEMA.TABLES"); |
| 70 | + stmt.setFetchSize(10); |
| 71 | + ResultSet rs = stmt.executeQuery(); |
| 72 | + rs.next(); |
| 73 | + rs.next(); // read two rows. Now simulate network outage |
| 74 | + |
| 75 | + closePort(3306); |
| 76 | + |
| 77 | + long startTime = System.currentTimeMillis(); |
| 78 | + stmt.close(); |
| 79 | + conn.close(); |
| 80 | + // mariadb is graceful here. It slows down 3-4 ms |
| 81 | + System.out.println("Done in " + (System.currentTimeMillis() - startTime) + "ms"); |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + @Disabled |
| 87 | + void testNetworkOutagePostgres() throws Exception { |
| 88 | + PostgresContainer container = PostgresContainer.builder("latest").build(); |
| 89 | + container.start(); |
| 90 | + |
| 91 | + Connection conn = container.createConnection(); |
| 92 | + PreparedStatement stmt = conn.prepareStatement("SELECT * from INFORMATION_SCHEMA.TABLES"); |
| 93 | + stmt.setFetchSize(10); |
| 94 | + ResultSet rs = stmt.executeQuery(); |
| 95 | + rs.next(); |
| 96 | + rs.next(); // read two rows. Now simulate network outage |
| 97 | + |
| 98 | + closePort(5432); |
| 99 | + |
| 100 | + long startTime = System.currentTimeMillis(); |
| 101 | + stmt.close(); |
| 102 | + conn.close(); |
| 103 | + // there is nearly no delay in PG |
| 104 | + System.out.println("Done in " + (System.currentTimeMillis() - startTime) + "ms"); |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments