Search in sources :

Example 6 with JdbcPropertySetImpl

use of com.mysql.cj.jdbc.JdbcPropertySetImpl in project aws-mysql-jdbc by awslabs.

the class FailoverConnectionPluginTest method initFailoverPlugin.

private FailoverConnectionPlugin initFailoverPlugin(Properties properties) throws SQLException {
    final JdbcPropertySet propertySet = new JdbcPropertySetImpl();
    propertySet.initializeProperties(properties);
    when(mockConnection.getPropertySet()).thenReturn(propertySet);
    return new FailoverConnectionPlugin(mockCurrentConnectionProvider, propertySet, mockNextPlugin, mockLogger, mockConnectionProvider, () -> mockTopologyService, () -> mockClusterMetricContainer);
}
Also used : JdbcPropertySet(com.mysql.cj.jdbc.JdbcPropertySet) JdbcPropertySetImpl(com.mysql.cj.jdbc.JdbcPropertySetImpl)

Example 7 with JdbcPropertySetImpl

use of com.mysql.cj.jdbc.JdbcPropertySetImpl in project aws-mysql-jdbc by awslabs.

the class SimplePacketReaderTest method readPayloadErrors.

// test error handling when reading payload
@Test
public void readPayloadErrors() throws IOException {
    RuntimeProperty<Integer> maxAllowedPacket = new JdbcPropertySetImpl().getProperty(PropertyKey.maxAllowedPacket);
    MockSocketConnection connection = new FixedBufferSocketConnection(new byte[] { 5, 4 });
    MessageReader<NativePacketHeader, NativePacketPayload> reader = new SimplePacketReader(connection, maxAllowedPacket);
    // can't read 3 bytes if the buffer only has 2
    try {
        reader.readMessage(Optional.empty(), new NativePacketHeader(new byte[] { 3, 0, 0, 0 }));
        fail("Shouldn't be able to read more than 2 bytes");
    } catch (EOFException ex) {
        assertTrue(connection.forceClosed, "Connection should be force closed when payload read fails");
    }
    // any IO errors during read should hang up the connection
    connection = new MockSocketConnection() {

        @Override
        public int readFully(byte[] b, int off, int len) throws IOException {
            throw new IOException("arbitrary failure");
        }
    };
    reader = new SimplePacketReader(connection, maxAllowedPacket);
    try {
        reader.readMessage(Optional.empty(), new NativePacketHeader(new byte[] { 3, 0, 0, 0 }));
        fail("IOException should be thrown");
    } catch (IOException ex) {
        assertTrue(connection.forceClosed, "Connection should be force closed when payload read fails");
    }
}
Also used : IOException(java.io.IOException) EOFException(java.io.EOFException) JdbcPropertySetImpl(com.mysql.cj.jdbc.JdbcPropertySetImpl) Test(org.junit.jupiter.api.Test)

Example 8 with JdbcPropertySetImpl

use of com.mysql.cj.jdbc.JdbcPropertySetImpl in project aws-mysql-jdbc by awslabs.

the class SimplePacketReaderTest method heuristicTestWithRandomPackets.

// generate some random packets for the reader
@Test
public void heuristicTestWithRandomPackets() throws IOException {
    // can't exceed 127 without changing code
    int numPackets = 127;
    int maxPacketSize = 127;
    // >>>>>>>> generate random test packets <<<<<<<<
    // the sizes are random. the sequence is the packet # (in the array). payload is repeated packet #
    Random rand = new Random();
    int[] packetLengths = new int[numPackets];
    int totalBufferSize = 0;
    for (int i = 0; i < numPackets; ++i) {
        packetLengths[i] = rand.nextInt(maxPacketSize);
        totalBufferSize += packetLengths[i] + NativeConstants.HEADER_LENGTH;
    }
    ByteBuffer buffer = ByteBuffer.allocate(totalBufferSize);
    for (int i = 0; i < numPackets; ++i) {
        // i = packet number (in array of random test packets)
        // header
        buffer.put((byte) packetLengths[i]);
        buffer.put((byte) 0);
        buffer.put((byte) 0);
        buffer.put((byte) i);
        // payload
        for (int j = 0; j < packetLengths[i]; ++j) {
            buffer.put((byte) i);
        }
    }
    buffer.clear();
    // >>>>>>>> read the packets <<<<<<<<
    RuntimeProperty<Integer> maxAllowedPacket = new JdbcPropertySetImpl().getProperty(PropertyKey.maxAllowedPacket);
    MockSocketConnection connection = new FixedBufferSocketConnection(buffer.array());
    MessageReader<NativePacketHeader, NativePacketPayload> reader = new SimplePacketReader(connection, maxAllowedPacket);
    NativePacketPayload readBuffer = new NativePacketPayload(new byte[maxPacketSize]);
    for (int i = 0; i < numPackets; ++i) {
        NativePacketHeader hdr = reader.readHeader();
        // check length against generated lengths
        assertEquals(packetLengths[i], hdr.getMessageSize());
        // each packet sequence is the packet # in the array
        assertEquals(i, hdr.getMessageSequence());
        assertEquals(i, reader.getMessageSequence());
        reader.readMessage(Optional.of(readBuffer), hdr);
        // check payload bytes also match packet #
        for (int j = 0; j < packetLengths[i]; ++j) {
            assertEquals(i, readBuffer.getByteBuffer()[j]);
        }
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) Random(java.util.Random) JdbcPropertySetImpl(com.mysql.cj.jdbc.JdbcPropertySetImpl) Test(org.junit.jupiter.api.Test)

Aggregations

JdbcPropertySetImpl (com.mysql.cj.jdbc.JdbcPropertySetImpl)8 Test (org.junit.jupiter.api.Test)6 SocketConnection (com.mysql.cj.protocol.SocketConnection)2 EOFException (java.io.EOFException)2 MysqlConnection (com.mysql.cj.MysqlConnection)1 CJPacketTooBigException (com.mysql.cj.exceptions.CJPacketTooBigException)1 JdbcConnection (com.mysql.cj.jdbc.JdbcConnection)1 JdbcPropertySet (com.mysql.cj.jdbc.JdbcPropertySet)1 MysqlSQLXML (com.mysql.cj.jdbc.MysqlSQLXML)1 ServerPreparedStatement (com.mysql.cj.jdbc.ServerPreparedStatement)1 UpdatableResultSet (com.mysql.cj.jdbc.result.UpdatableResultSet)1 InternalDate (com.mysql.cj.protocol.InternalDate)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ByteBuffer (java.nio.ByteBuffer)1 Connection (java.sql.Connection)1 Date (java.sql.Date)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1