Back to Repositories

Testing Nacos Configuration Refresh Workflow in Spring Cloud Alibaba

This test suite validates the configuration refresh functionality in Nacos when integrated with Spring Cloud Alibaba. It focuses on testing dynamic configuration updates and verifies the proper handling of YAML configuration changes through the Nacos Config Service.

Test Coverage Overview

The test suite provides comprehensive coverage of Nacos configuration refresh mechanisms.

Key areas tested include:
  • Dynamic configuration updates using YAML format
  • Config service initialization and connection
  • Configuration publishing and retrieval
  • Refresh timing and synchronization
The suite specifically tests edge cases around configuration format handling and refresh timing.

Implementation Analysis

The testing approach utilizes JUnit 5 with Spring Cloud Alibaba’s testing support framework. The implementation employs a mock-enabled architecture for the Config Service, with explicit timing controls for refresh operations.

Key patterns include:
  • Docker-based test environment setup
  • Mockito integration for service isolation
  • Explicit thread sleep for refresh timing validation
  • AssertJ for assertion handling

Technical Details

Testing tools and configuration:
  • JUnit Jupiter test framework
  • Spring Cloud Alibaba test support
  • Nacos Config Factory and Service APIs
  • Docker compose for Nacos standalone instance
  • Custom TestExtend annotations for timing control
  • Mockito for service mocking

Best Practices Demonstrated

The test suite exemplifies several testing best practices in distributed configuration management.

Notable practices include:
  • Proper test isolation using docker containers
  • Clear test setup and teardown procedures
  • Explicit timing management for async operations
  • Comprehensive configuration verification
  • Well-structured test organization with clear separation of concerns

alibaba/spring-cloud-alibaba

spring-cloud-alibaba-tests/nacos-tests/nacos-config-test/src/test/java/com/alibaba/cloud/tests/nacos/config/NacosConfigRefreshTest.java

            
/*
 * Copyright 2013-2023 the original author or authors.
 *
 * Licensed 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
 *
 *      https://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.alibaba.cloud.tests.nacos.config;

import java.util.Properties;

import com.alibaba.cloud.testsupport.SpringCloudAlibaba;
import com.alibaba.cloud.testsupport.TestExtend;
import com.alibaba.cloud.testsupport.Tester;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;

import static com.alibaba.cloud.testsupport.Constant.TIME_OUT;
import static org.assertj.core.api.Assertions.assertThat;

/**
 *
 * Test function: Nacos config refresh.
 *
 * @author freeman
 */
// @HasDockerAndItEnabled
@SpringCloudAlibaba(composeFiles = "docker/nacos-compose-test.yml", serviceName = "nacos-standalone")
@TestExtend(time = 4 * TIME_OUT)
@Disabled("Does not work with the new nacos config load process")
public class NacosConfigRefreshTest {

	/**
	 * nacos upload conf file.
	 */
	public static final String YAML_CONTENT = "configdata:\n" + "  user:\n"
			+ "    age: 22\n" + "    name: freeman1123\n" + "    map:\n"
			+ "      hobbies:\n" + "        - art\n" + "        - programming\n"
			+ "        - movie\n" + "      intro: Hello, I'm freeman\n"
			+ "      extra: yo~\n" + "    users:\n" + "      - name: dad\n"
			+ "        age: 20\n" + "      - name: mom\n" + "        age: 18";

	@Mock
	protected ConfigService configService;

	@BeforeAll
	public static void setUp() {

	}

	@BeforeEach
	public void prepare() throws NacosException {
		Properties nacosSettings = new Properties();
		String serverAddress = "127.0.0.1:8848";
		nacosSettings.put(PropertyKeyConst.SERVER_ADDR, serverAddress);
		nacosSettings.put(PropertyKeyConst.USERNAME, "nacos");
		nacosSettings.put(PropertyKeyConst.PASSWORD, "nacos");

		configService = ConfigFactory.createConfigService(nacosSettings);

	}

	@Test
	public void testRefreshConfig() throws InterruptedException {
		Thread.sleep(2000L);
		Tester.testFunction("Dynamic refresh config", () -> {
			// update config
			updateConfig();

			// wait config refresh
			Thread.sleep(2000L);
			String content = configService.getConfig("nacos-config-refresh.yml",
					"DEFAULT_GROUP", TIME_OUT);

			assertThat(content).isEqualTo(YAML_CONTENT);
		});
	}

	private void updateConfig() throws NacosException {
		configService.publishConfig("nacos-config-refresh.yml", "DEFAULT_GROUP", YAML_CONTENT,
				"yaml");
	}
}