Back to Repositories

Testing Message Index Storage Workflow in CAT System

This test suite validates the functionality of message indexing and mapping in the CAT (Central Application Tracking) system, focusing on storage and retrieval of message IDs across different IP addresses and configurations.

Test Coverage Overview

The test suite provides comprehensive coverage of the Index and IndexManager components, focusing on message ID mapping and lookup operations.

  • Tests basic mapping and lookup functionality for single IP scenarios
  • Validates large-scale operations with 15,000 message entries
  • Tests multi-IP scenarios with 10 different IP addresses
  • Verifies data persistence and retrieval accuracy

Implementation Analysis

The testing approach utilizes JUnit framework with component-based testing architecture.

  • Implements ComponentTestCase for dependency injection and component lookup
  • Uses Before annotations for test setup and configuration
  • Employs systematic validation through Assert statements
  • Implements scalability testing through iteration-based test cases

Technical Details

  • JUnit 4.x testing framework
  • Custom StorageConfiguration for test data directory management
  • NetworkInterfaceManager for IP address handling
  • File-based storage configuration with target directory
  • MessageId parsing and mapping functionality

Best Practices Demonstrated

The test suite exemplifies robust testing practices for distributed systems and data storage components.

  • Proper test isolation through dedicated test directories
  • Comprehensive setup and teardown procedures
  • Systematic validation of expected vs actual results
  • Scale testing with large datasets
  • Multi-scenario testing covering different use cases

dianping/cat

cat-hadoop/src/test/java/org/unidal/cat/message/storage/IndexTest.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 org.unidal.cat.message.storage;

import java.io.File;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.unidal.helper.Files;
import org.unidal.lookup.ComponentTestCase;

import com.dianping.cat.configuration.NetworkInterfaceManager;
import com.dianping.cat.message.tree.MessageId;

public class IndexTest extends ComponentTestCase {

	private String m_ip;

	@Before
	public void before() throws Exception {
		StorageConfiguration config = lookup(StorageConfiguration.class);

		config.setBaseDataDir(new File("target"));
		File baseDir = new File("target");
		Files.forDir().delete(new File(baseDir, "dump"), true);
		m_ip = NetworkInterfaceManager.INSTANCE.getLocalHostAddress();
	}

	@Test
	public void testMapAndLookups() throws Exception {
		int total = 15000;
		IndexManager manager = lookup(IndexManager.class, "local");
		Index index = manager.getIndex("from", m_ip, 403899, true);

		for (int i = 1; i < total; i++) {
			MessageId from = MessageId.parse("from-0a260014-403899-" + i);
			MessageId to = MessageId.parse("to-0a260015-403899-" + i);

			index.map(from, to);
		}

		index = manager.getIndex("from", m_ip, 403899, true);
		for (int i = 1; i < total; i++) {
			MessageId from = MessageId.parse("from-0a260014-403899-" + i);
			MessageId expected = MessageId.parse("to-0a260015-403899-" + i);

			MessageId actual = index.find(from);
			Assert.assertEquals(expected, actual);
		}
	}

	@Test
	public void testMapAndLookupManyIps() throws Exception {
		IndexManager manager = lookup(IndexManager.class, "local");
		Index index = manager.getIndex("from", m_ip, 403899, true);

		for (int i = 1; i < 150000; i++) {
			for (int ip = 0; ip < 10; ip++) {
				MessageId from = MessageId.parse("from-0a26000" + ip + "-403899-" + i);
				MessageId to = MessageId.parse("from-0a25000" + ip + "-403899-" + i);

				index.map(from, to);
			}
		}

		index = manager.getIndex("from", m_ip, 403899, true);
		for (int i = 1; i < 150000; i++) {

			for (int ip = 0; ip < 10; ip++) {
				MessageId from = MessageId.parse("from-0a26000" + ip + "-403899-" + i);
				MessageId expected = MessageId.parse("from-0a25000" + ip + "-403899-" + i);

				MessageId actual = index.find(from);

				Assert.assertEquals(expected, actual);
			}
		}
	}

}