Back to Repositories

Testing Thread-Based Transaction Management in CAT

This test suite demonstrates thread handling and message tracing capabilities in the CAT monitoring system. It validates the creation and management of transactions across multiple threads while maintaining trace context and message correlation.

Test Coverage Overview

The test suite covers critical aspects of multi-threaded transaction handling in CAT.

  • Transaction creation and completion in main thread
  • Child thread message correlation using TraceContextHelper
  • Event logging with remote linking capabilities
  • Message ID propagation across threads

Implementation Analysis

The testing approach utilizes JUnit to verify thread-safe transaction handling and message correlation. The implementation employs the Unidal thread management framework alongside CAT’s transaction and event tracking system.

  • Uses Cat.newTransaction() for transaction boundaries
  • Implements TraceContextHelper for message ID management
  • Leverages Threads.forGroup() for controlled thread execution

Technical Details

  • JUnit test framework
  • CAT monitoring system APIs
  • Unidal thread management library
  • Transaction and Event message types
  • TraceContextHelper for context propagation
  • Thread sleep for execution timing control

Best Practices Demonstrated

The test exemplifies robust concurrent testing practices with proper transaction management and context propagation.

  • Clean separation of thread execution logic
  • Proper transaction lifecycle management
  • Explicit message correlation across threads
  • Structured error handling and resource cleanup

dianping/cat

cat-home/src/test/java/com/dianping/cat/demo/ThreadTest.java

            
/*
 * Copyright (c) 2011-2018, Meituan Dianping. All Rights Reserved.
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.dianping.cat.demo;

import org.junit.Test;
import org.unidal.helper.Threads;

import com.dianping.cat.Cat;
import com.dianping.cat.message.Event;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.message.context.TraceContextHelper;

public class ThreadTest {

	@Test
	public void test() throws InterruptedException {
		Transaction t = Cat.newTransaction("test3", "test3");

		String id = TraceContextHelper.createMessageId();

		Threads.forGroup("cat").start(new Task(id));

		Cat.logEvent("RemoteLink", "ChildThread3", Event.SUCCESS, id);

		t.complete();

		Thread.sleep(1000);
	}

	public static class Task implements Runnable {

		private String m_messageId;

		public Task(String id) {
			m_messageId = id;
		}

		@Override
		public void run() {

			Transaction t = Cat.newTransaction("test2", "test2");

			TraceContextHelper.threadLocal().getMessageTree().setMessageId(m_messageId);

			t.complete();
		}
	}
}