Back to Repositories

Testing PeriodMixedMetaManager ZooKeeper Integration in Canal

This test suite validates the PeriodMixedMetaManager functionality in Canal, focusing on subscription management, batch processing, and cursor handling with ZooKeeper integration. The tests ensure reliable metadata persistence and retrieval across distributed systems.

Test Coverage Overview

The test suite provides comprehensive coverage of core metadata management operations.

  • Subscription management verification with multiple client identities
  • Batch processing operations and cleanup functionality
  • Cursor position tracking and persistence testing
  • ZooKeeper integration validation for metadata storage

Implementation Analysis

The testing approach employs JUnit for structured test execution with ZooKeeper integration.

Key patterns include:
  • Setup/teardown lifecycle management for test isolation
  • State verification through Assert statements
  • Simulated distributed system testing with multiple manager instances
  • Temporal testing with sleep intervals for async operations

Technical Details

Testing infrastructure includes:

  • JUnit 4.x test framework
  • ZkClientx for ZooKeeper client operations
  • Custom AbstractMetaManagerTest base class
  • ZooKeeperMetaManager for metadata persistence
  • PeriodMixedMetaManager for combined metadata handling

Best Practices Demonstrated

The test suite exemplifies robust testing practices for distributed systems.

  • Proper resource cleanup in tearDown methods
  • Isolation of test cases through path deletion
  • Verification of persistence across manager instances
  • Comprehensive state validation
  • Clear test method naming conventions

alibaba/canal

meta/src/test/java/com/alibaba/otter/canal/meta/PeriodMixedMetaManagerTest.java

            
package com.alibaba.otter.canal.meta;

import java.util.List;
import java.util.Map;

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.ClientIdentity;
import com.alibaba.otter.canal.protocol.position.Position;
import com.alibaba.otter.canal.protocol.position.PositionRange;
@Ignore
public class PeriodMixedMetaManagerTest extends AbstractMetaManagerTest {

    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 testSubscribeAll() {
        PeriodMixedMetaManager metaManager = new PeriodMixedMetaManager();

        ZooKeeperMetaManager zooKeeperMetaManager = new ZooKeeperMetaManager();
        zooKeeperMetaManager.setZkClientx(zkclientx);

        metaManager.setZooKeeperMetaManager(zooKeeperMetaManager);
        metaManager.start();
        doSubscribeTest(metaManager);

        sleep(1000L);
        // 重新构建一次,能获得上一次zk上的记录
        PeriodMixedMetaManager metaManager2 = new PeriodMixedMetaManager();
        metaManager2.setZooKeeperMetaManager(zooKeeperMetaManager);
        metaManager2.start();

        List<ClientIdentity> clients = metaManager2.listAllSubscribeInfo(destination);
        Assert.assertEquals(2, clients.size());
        metaManager.stop();
    }

    @Test
    public void testBatchAll() {
        PeriodMixedMetaManager metaManager = new PeriodMixedMetaManager();

        ZooKeeperMetaManager zooKeeperMetaManager = new ZooKeeperMetaManager();
        zooKeeperMetaManager.setZkClientx(zkclientx);

        metaManager.setZooKeeperMetaManager(zooKeeperMetaManager);
        metaManager.start();
        doBatchTest(metaManager);

        metaManager.clearAllBatchs(clientIdentity);
        Map<Long, PositionRange> ranges = metaManager.listAllBatchs(clientIdentity);
        Assert.assertEquals(0, ranges.size());
        metaManager.stop();
    }

    @Test
    public void testCursorAll() {
        PeriodMixedMetaManager metaManager = new PeriodMixedMetaManager();

        ZooKeeperMetaManager zooKeeperMetaManager = new ZooKeeperMetaManager();
        zooKeeperMetaManager.setZkClientx(zkclientx);

        metaManager.setZooKeeperMetaManager(zooKeeperMetaManager);
        metaManager.start();
        Position lastPosition = doCursorTest(metaManager);

        sleep(1000L);
        // 重新构建一次,能获得上一次zk上的记录
        PeriodMixedMetaManager metaManager2 = new PeriodMixedMetaManager();
        metaManager2.setZooKeeperMetaManager(zooKeeperMetaManager);
        metaManager2.start();

        Position position = metaManager2.getCursor(clientIdentity);
        Assert.assertEquals(position, lastPosition);
        metaManager.stop();
    }
}