Skip to content

Commit

Permalink
Merge pull request #1624 from chengyouling/develop-ut
Browse files Browse the repository at this point in the history
add rocketmq grayscale UT test
  • Loading branch information
Sherlockhan authored Sep 24, 2024
2 parents 146cd69 + 2fb55b2 commit cecf011
Show file tree
Hide file tree
Showing 24 changed files with 1,212 additions and 28 deletions.
10 changes: 10 additions & 0 deletions report/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,16 @@
<artifactId>tag-transmission-sofarpc-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.sermant</groupId>
<artifactId>mq-config-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.sermant</groupId>
<artifactId>mq-grayscale-rocketmq-plugin</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.sermant</groupId>
<artifactId>sermant-backend</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static void setCacheConfig(MqGrayscaleConfig config, DynamicConfigEventTy
}
boolean isAllowRefresh = isAllowRefreshChangeFlag(cacheConfig, config);
if (isAllowRefresh) {
cacheConfig.updateGrayscaleConfig(MqGrayConfigCache.getCacheConfig());
cacheConfig.updateGrayscaleConfig(config);
RocketMqConfigUtils.updateChangeFlag();
RocketMqConfigUtils.recordTrafficTagsSet(config);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. All rights reserved.
*
* 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
*
* 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 io.sermant.mq.grayscale.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* common config utils
*
* @author chengyouling
* @since 2024-09-18
**/
public class CommonConfigUtils {
public static GrayTagItem getGrayTagItem() {
GrayTagItem item = new GrayTagItem();
item.setConsumerGroupTag("gray");
Map<String, String> serviceMeta = new HashMap<>();
serviceMeta.put("x_lane_tag", "gray" );
item.setServiceMeta(serviceMeta);
Map<String, String> trafficTag = new HashMap<>();
trafficTag.put("x_lane_canary", "gray");
item.setTrafficTag(trafficTag);
return item;
}

public static MqGrayscaleConfig getMqGrayscaleConfig() {
MqGrayscaleConfig config = new MqGrayscaleConfig();
config.setGrayscale(getGrayscale(getGrayTagItem()));
config.setEnabled(true);
return config;
}


public static List<GrayTagItem> getGrayscale(GrayTagItem item) {
List<GrayTagItem> grayscale = new ArrayList<>();
grayscale.add(item);
return grayscale;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. All rights reserved.
*
* 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
*
* 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 io.sermant.mq.grayscale.config;

import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* GrayTagItem test
*
* @author chengyouling
* @since 2024-09-18
**/
public class GrayTagItemTest {
@Test
public void testMatchPropertiesByServiceMeta() {
Map<String, String> properties = new HashMap<>();
properties.put("x_lane_tag", "gray");
GrayTagItem item = CommonConfigUtils.getGrayTagItem();
Assert.assertTrue(item.matchPropertiesByServiceMeta(properties));

properties.put("x_lane_tag", "red");
Assert.assertFalse(item.matchPropertiesByServiceMeta(properties));
}

@Test
public void testUpdateTrafficTags() {
GrayTagItem itemResource = CommonConfigUtils.getGrayTagItem();
List<GrayTagItem> grayscale = new ArrayList<>();
GrayTagItem item = CommonConfigUtils.getGrayTagItem();
item.getTrafficTag().put("x_lane_red", "red");
grayscale.add(item);
itemResource.updateTrafficTags(grayscale);
Assert.assertSame(2, itemResource.getTrafficTag().size());

itemResource = CommonConfigUtils.getGrayTagItem();
itemResource.setConsumerGroupTag("red");
itemResource.updateTrafficTags(grayscale);
Assert.assertSame(1, itemResource.getTrafficTag().size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. All rights reserved.
*
* 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
*
* 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 io.sermant.mq.grayscale.config;

import io.sermant.core.service.dynamicconfig.common.DynamicConfigEventType;

import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
* MqGrayConfigCache test
*
* @author chengyouling
* @since 2024-09-18
**/
public class MqGrayConfigCacheTest {
@Test
public void testSetCacheConfig() {
MqGrayscaleConfig config = getMqGrayscaleConfig(CommonConfigUtils.getGrayTagItem());
MqGrayConfigCache.setCacheConfig(config, DynamicConfigEventType.CREATE);
Assert.assertTrue(MqGrayConfigCache.getCacheConfig().isEnabled());

// consumeMode changed will not update config
config.getBase().setConsumeMode(ConsumeModeEnum.BASE);
MqGrayConfigCache.setCacheConfig(config, DynamicConfigEventType.MODIFY);
Assert.assertSame(ConsumeModeEnum.BASE, MqGrayConfigCache.getCacheConfig().getBase().getConsumeMode());

// serviceMeta changed will not update config
GrayTagItem item = CommonConfigUtils.getGrayTagItem();
item.getServiceMeta().put("x_lane_tag", "red" );
MqGrayscaleConfig config1 = getMqGrayscaleConfig(item);
MqGrayConfigCache.setCacheConfig(config1, DynamicConfigEventType.MODIFY);
Assert.assertEquals("gray",
MqGrayConfigCache.getCacheConfig().getGrayscale().get(0).getServiceMeta().get("x_lane_tag"));

// trafficTag changed will not update config
GrayTagItem item1 = CommonConfigUtils.getGrayTagItem();
item1.getTrafficTag().put("x_lane_canary", "red");
item1.getServiceMeta().put("x_lane_tag", "red" );
MqGrayscaleConfig config2 = getMqGrayscaleConfig(item1);
MqGrayConfigCache.setCacheConfig(config2, DynamicConfigEventType.MODIFY);
Assert.assertEquals("red",
MqGrayConfigCache.getCacheConfig().getGrayscale().get(0).getTrafficTag().get("x_lane_canary"));
}

private MqGrayscaleConfig getMqGrayscaleConfig(GrayTagItem item) {
MqGrayscaleConfig config = new MqGrayscaleConfig();
config.setEnabled(true);
config.setGrayscale(CommonConfigUtils.getGrayscale(item));
return config;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. All rights reserved.
*
* 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
*
* 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 io.sermant.mq.grayscale.config;

import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
* MqGrayscaleConfig test
*
* @author chengyouling
* @since 2024-09-18
**/
public class MqGrayscaleConfigTest {
@Test
public void testGetGrayTagsByServiceMeta() {
MqGrayscaleConfig config = CommonConfigUtils.getMqGrayscaleConfig();
Map<String, String> properties = new HashMap<>();
properties.put("x_lane_tag", "gray" );
Assert.assertSame(1, config.getGrayTagsByServiceMeta(properties).size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2024-2024 Sermant Authors. All rights reserved.
*
* 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
*
* 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 io.sermant.mq.grayscale.config.rocketmq;

import io.sermant.mq.grayscale.config.CommonConfigUtils;

import org.junit.Assert;
import org.junit.Test;

/**
* RocketMqConfigUtils test
*
* @author chengyouling
* @since 2024-09-18
**/
public class RocketMqConfigUtilsTest {
@Test
public void testBaseGroupTagChangeMap() {
RocketMqConfigUtils.setBaseGroupTagChangeMap("127.0.0.1@TEST@base", true);
Assert.assertTrue(RocketMqConfigUtils.getBaseGroupTagChangeMap("127.0.0.1@TEST@base"));
Assert.assertFalse(RocketMqConfigUtils.getBaseGroupTagChangeMap("127.0.0.1@TEST@base1"));
}

@Test
public void testGrayGroupTagChangeMap() {
RocketMqConfigUtils.setGrayGroupTagChangeMap("127.0.0.1@TEST@base-gray", true);
Assert.assertTrue(RocketMqConfigUtils.getGrayGroupTagChangeMap("127.0.0.1@TEST@base-gray"));
Assert.assertFalse(RocketMqConfigUtils.getGrayGroupTagChangeMap("127.0.0.1@TEST@base-gray1"));
}

@Test
public void testUpdateChangeFlag() {
RocketMqConfigUtils.setBaseGroupTagChangeMap("127.0.0.1@TEST@base", false);
RocketMqConfigUtils.setGrayGroupTagChangeMap("127.0.0.1@TEST@base-gray", false);
RocketMqConfigUtils.updateChangeFlag();
Assert.assertTrue(RocketMqConfigUtils.getBaseGroupTagChangeMap("127.0.0.1@TEST@base"));
Assert.assertTrue(RocketMqConfigUtils.getGrayGroupTagChangeMap("127.0.0.1@TEST@base-gray"));
}

@Test
public void testRecordTrafficTagsSet() {
RocketMqConfigUtils.recordTrafficTagsSet(CommonConfigUtils.getMqGrayscaleConfig());
Assert.assertSame(1, RocketMqConfigUtils.getGrayTagsSet().size());
}
}
20 changes: 10 additions & 10 deletions sermant-plugins/sermant-mq-grayscale/mq-config-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<config.skip.flag>false</config.skip.flag>
<package.plugin.type>service</package.plugin.type>
</properties>

Expand All @@ -33,15 +34,14 @@
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public static void injectTrafficTagByServiceMeta(Message message) {
return;
}
for (Map.Entry<String, String> entry : grayTags.entrySet()) {
if (!message.getProperties().containsKey(entry.getKey())) {
if (message.getProperties() == null || !message.getProperties().containsKey(entry.getKey())) {
message.putUserProperty(entry.getKey(), entry.getValue());
}
}
Expand Down
Loading

0 comments on commit cecf011

Please sign in to comment.