Back to Repositories

Testing Period-Mixed Log Position Management in Canal

This test suite validates the PeriodMixedLogPositionManager component in Canal, focusing on log position management across memory and ZooKeeper implementations. It ensures reliable position tracking and synchronization between different manager instances.

Test Coverage Overview

The test suite provides comprehensive coverage of the PeriodMixedLogPositionManager functionality.

Key areas tested include:
  • Memory and ZooKeeper position manager integration
  • Position persistence and retrieval
  • Manager initialization and cleanup
  • Periodic synchronization between instances
Edge cases covered include timing-dependent operations and multiple manager instance coordination.

Implementation Analysis

The testing approach employs JUnit framework features for lifecycle management and assertion validation. The implementation uses @Before and @After annotations for setup/teardown, ensuring clean test environments.

Notable patterns include:
  • Dual manager instance testing
  • Timed synchronization verification
  • Clean state management using ZooKeeper paths

Technical Details

Testing tools and configuration:
  • JUnit 4 testing framework
  • ZkClientx for ZooKeeper interaction
  • Custom AbstractLogPositionManagerTest base class
  • Configurable synchronization periods (1000ms)
  • Multiple cluster setup (cluster1 and cluster2)

Best Practices Demonstrated

The test suite exemplifies several testing best practices in distributed systems testing.

Notable practices include:
  • Proper resource cleanup
  • Isolation between test runs
  • Explicit timing control
  • Component integration verification
  • Hierarchical test organization

alibaba/canal

parse/src/test/java/com/alibaba/otter/canal/parse/index/PeriodMixedLogPositionManagerTest.java

            
package com.alibaba.otter.canal.parse.index;

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

import com.alibaba.otter.canal.common.zookeeper.ZkClientx;
import com.alibaba.otter.canal.common.zookeeper.ZookeeperPathUtils;
import com.alibaba.otter.canal.protocol.position.LogPosition;
@Ignore
public class PeriodMixedLogPositionManagerTest extends AbstractLogPositionManagerTest {

    private ZkClientx zkclientx = new ZkClientx(cluster1 + ";" + cluster2);

    @Before
    public void setUp() {
        String path = ZookeeperPathUtils.getDestinationPath(destination);
        zkclientx.deleteRecursive(path);
    }

    @After
    public void tearDown() {
        String path = ZookeeperPathUtils.getDestinationPath(destination);
        zkclientx.deleteRecursive(path);
    }

    @Test
    public void testAll() {
        MemoryLogPositionManager memoryLogPositionManager = new MemoryLogPositionManager();
        ZooKeeperLogPositionManager zookeeperLogPositionManager = new ZooKeeperLogPositionManager(zkclientx);

        PeriodMixedLogPositionManager logPositionManager = new PeriodMixedLogPositionManager(memoryLogPositionManager,
            zookeeperLogPositionManager,
            1000L);

        logPositionManager.start();

        LogPosition position2 = doTest(logPositionManager);
        sleep(1500);

        PeriodMixedLogPositionManager logPositionManager2 = new PeriodMixedLogPositionManager(memoryLogPositionManager,
            zookeeperLogPositionManager,
            1000L);
        logPositionManager2.start();

        LogPosition getPosition2 = logPositionManager2.getLatestIndexBy(destination);
        Assert.assertEquals(position2, getPosition2);

        logPositionManager.stop();
        logPositionManager2.stop();
    }
}