Back to Repositories

Testing Multi-threaded Regex Filter Implementation in Alibaba Canal

This test suite validates the multi-threaded functionality of the AviaterRegexFilter in Alibaba Canal, focusing on concurrent regex pattern matching operations. The tests ensure reliable pattern matching behavior under parallel execution conditions.

Test Coverage Overview

The test suite covers concurrent execution of regex pattern matching using multiple threads.

  • Tests parallel execution with 5 concurrent threads
  • Verifies pattern matching against fixed and random patterns
  • Validates thread safety of AviaterRegexFilter implementation
  • Ensures consistent results across multiple iterations

Implementation Analysis

The implementation employs JUnit for test orchestration and Java’s concurrent utilities for multi-threaded testing.

Key patterns include:
  • ExecutorService for thread pool management
  • CountDownLatch for synchronization
  • AtomicInteger for thread-safe counting
  • Combination of fixed and random test patterns

Technical Details

Testing infrastructure includes:

  • JUnit 4 test framework
  • Java Concurrent utilities (java.util.concurrent)
  • Apache Commons Lang for random string generation
  • AviaterRegexFilter as the primary test subject
  • Thread pool configuration of 5 threads

Best Practices Demonstrated

The test suite exemplifies robust concurrent testing practices.

  • Proper thread pool management and cleanup
  • Effective use of synchronization primitives
  • Deterministic success criteria validation
  • Combination of fixed and randomized test data
  • Clear separation of test setup and execution logic

alibaba/canal

filter/src/test/java/com/alibaba/otter/canal/filter/MutliAviaterFilterTest.java

            
package com.alibaba.otter.canal.filter;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.lang.RandomStringUtils;
import org.junit.Assert;
import org.junit.Test;

import com.alibaba.otter.canal.filter.aviater.AviaterRegexFilter;

public class MutliAviaterFilterTest {

    @Test
    public void test_simple() {
        int count = 5;
        ExecutorService executor = Executors.newFixedThreadPool(count);

        final CountDownLatch countDown = new CountDownLatch(count);
        final AtomicInteger successed = new AtomicInteger(0);
        for (int i = 0; i < count; i++) {
            executor.submit(() -> {
                try {
                    for (int i1 = 0; i1 < 100; i1++) {
                        doRegexTest();
                        // try {
                        // Thread.sleep(10);
                        // } catch (InterruptedException e) {
                        // }
                    }

                    successed.incrementAndGet();
                } finally {
                    countDown.countDown();
                }
            });
        }

        try {
            countDown.await();
        } catch (InterruptedException e) {
        }

        Assert.assertEquals(count, successed.get());
        executor.shutdownNow();
    }

    private void doRegexTest() {
        AviaterRegexFilter filter3 = new AviaterRegexFilter("otter2.otter_stability1|otter1.otter_stability1|"
                                                            + RandomStringUtils.randomAlphabetic(200));
        boolean result = filter3.filter("otter1.otter_stability1");
        Assert.assertEquals(true, result);
        result = filter3.filter("otter2.otter_stability1");
        Assert.assertEquals(true, result);
    }

}