Back to Repositories

Testing MySQL Connector Operations and Query Execution in Canal

This test suite validates the MySQL connector functionality in Canal, focusing on basic database operations like querying and updating. It ensures reliable connectivity and proper execution of SQL commands through Canal’s MySQL driver implementation.

Test Coverage Overview

The test suite covers core MySQL connectivity and operation execution functionality.

Key areas tested include:
  • Database connection establishment and teardown
  • Query execution and result set handling
  • Update operations with character encoding support
  • Error handling and connection cleanup

Implementation Analysis

The testing approach uses JUnit to validate MySQL connector operations in isolation. The tests implement a straightforward pattern of connection setup, operation execution, and cleanup.

Key implementation aspects:
  • Direct socket-based connection testing
  • Parameterized query execution
  • Unicode character handling verification
  • Resource cleanup in finally blocks

Technical Details

Testing infrastructure includes:
  • JUnit 4 testing framework
  • Custom MySQL connector implementation
  • Local MySQL instance (127.0.0.1:3306)
  • Separate executors for queries and updates
  • IOException handling and assertions

Best Practices Demonstrated

The test suite exemplifies several testing best practices for database connectivity.

Notable practices include:
  • Proper resource management with try-finally blocks
  • Separate test methods for different operation types
  • Explicit connection cleanup
  • Comprehensive error handling and assertion
  • Isolation of query and update operations

alibaba/canal

driver/src/test/java/com/alibaba/otter/canal/parse/driver/mysql/MysqlConnectorTest.java

            
package com.alibaba.otter.canal.parse.driver.mysql;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket;

@Ignore
public class MysqlConnectorTest {

    @Test
    public void testQuery() {

        MysqlConnector connector = new MysqlConnector(new InetSocketAddress("127.0.0.1", 3306), "xxxxx", "xxxxx");
        try {
            connector.connect();
            MysqlQueryExecutor executor = new MysqlQueryExecutor(connector);
            ResultSetPacket result = executor.query("show variables like '%char%';");
            System.out.println(result);
            result = executor.query("select * from test.test1");
            System.out.println(result);
        } catch (IOException e) {
            Assert.fail(e.getMessage());
        } finally {
            try {
                connector.disconnect();
            } catch (IOException e) {
                Assert.fail(e.getMessage());
            }
        }
    }

    // @Test
    public void testUpdate() {

        MysqlConnector connector = new MysqlConnector(new InetSocketAddress("127.0.0.1", 3306), "xxxxx", "xxxxx");
        try {
            connector.connect();
            MysqlUpdateExecutor executor = new MysqlUpdateExecutor(connector);
            executor.update("insert into test.test2(id,name,score,text_value) values(null,'中文1',10,'中文2')");
        } catch (IOException e) {
            Assert.fail(e.getMessage());
        } finally {
            try {
                connector.disconnect();
            } catch (IOException e) {
                Assert.fail(e.getMessage());
            }
        }
    }
}