Back to Repositories

Testing ZooKeeper Metadata Management Implementation in Alibaba Canal

This test suite validates the ZooKeeper-based metadata management functionality in Alibaba Canal, focusing on subscription handling, batch processing, and cursor operations. It ensures reliable metadata persistence and retrieval through ZooKeeper integration.

Test Coverage Overview

The test suite provides comprehensive coverage of ZooKeeperMetaManager’s core operations:
  • Subscription management and verification
  • Batch processing operations including creation and clearing
  • Cursor handling and position tracking
  • ZooKeeper path management and cleanup
Integration points include ZooKeeper client initialization, path utils, and position range handling.

Implementation Analysis

The testing approach utilizes JUnit 4 framework with a hierarchical structure extending AbstractMetaManagerTest. Each test case follows a consistent pattern of setup, execution, and cleanup:
  • ZooKeeper client initialization with cluster configuration
  • MetaManager lifecycle management (start/stop)
  • Isolated test paths for each operation
  • Systematic teardown and cleanup

Technical Details

Testing infrastructure includes:
  • JUnit 4 test framework
  • ZkClientx for ZooKeeper interaction
  • Custom ZookeeperPathUtils for path management
  • PositionRange for batch operation tracking
  • Before/After hooks for test isolation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Proper test isolation through setUp/tearDown methods
  • Systematic resource cleanup after each test
  • Clear separation of concerns between test cases
  • Effective use of inheritance for shared test functionality
  • Comprehensive verification of operation outcomes

alibaba/canal

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

            
package com.alibaba.otter.canal.meta;

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.position.PositionRange;
@Ignore
public class ZooKeeperMetaManagerTest 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() {
        ZooKeeperMetaManager metaManager = new ZooKeeperMetaManager();
        metaManager.setZkClientx(zkclientx);
        metaManager.start();

        doSubscribeTest(metaManager);
        metaManager.stop();
    }

    @Test
    public void testBatchAll() {
        ZooKeeperMetaManager metaManager = new ZooKeeperMetaManager();
        metaManager.setZkClientx(zkclientx);
        metaManager.start();
        doBatchTest(metaManager);

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

    @Test
    public void testCursorhAll() {
        ZooKeeperMetaManager metaManager = new ZooKeeperMetaManager();
        metaManager.setZkClientx(zkclientx);
        metaManager.start();
        doCursorTest(metaManager);
        metaManager.stop();
    }
}