Browse Source

Update test cases in Sentinel dashboard

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
master
Eric Zhao 6 years ago
parent
commit
13de41ba6f
2 changed files with 54 additions and 27 deletions
  1. +18
    -2
      sentinel-dashboard/src/test/java/com/alibaba/csp/sentinel/dashboard/discovery/AppInfoTest.java
  2. +36
    -25
      sentinel-dashboard/src/test/java/com/alibaba/csp/sentinel/dashboard/repository/metric/InMemoryMetricsRepositoryTest.java

+ 18
- 2
sentinel-dashboard/src/test/java/com/alibaba/csp/sentinel/dashboard/discovery/AppInfoTest.java View File

@@ -1,3 +1,18 @@
/*
* Copyright 1999-2019 Alibaba Group Holding Ltd.
*
* 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 com.alibaba.csp.sentinel.dashboard.discovery; package com.alibaba.csp.sentinel.dashboard.discovery;


import java.util.ConcurrentModificationException; import java.util.ConcurrentModificationException;
@@ -8,6 +23,7 @@ import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;


public class AppInfoTest { public class AppInfoTest {

@Test @Test
public void testConcurrentGetMachines() throws Exception { public void testConcurrentGetMachines() throws Exception {
AppInfo appInfo = new AppInfo("testApp"); AppInfo appInfo = new AppInfo("testApp");
@@ -25,7 +41,7 @@ public class AppInfoTest {
} }
} catch (ConcurrentModificationException e) { } catch (ConcurrentModificationException e) {
e.printStackTrace(); e.printStackTrace();
assertTrue(false);
fail();
} }


}).start(); }).start();
@@ -34,7 +50,7 @@ public class AppInfoTest {
appInfo.addMachine(genMachineInfo("hostName3", "10.18.129.93")); appInfo.addMachine(genMachineInfo("hostName3", "10.18.129.93"));
} catch (ConcurrentModificationException e) { } catch (ConcurrentModificationException e) {
e.printStackTrace(); e.printStackTrace();
assertTrue(false);
fail();
} }
Thread.sleep(1000); Thread.sleep(1000);
} }


+ 36
- 25
sentinel-dashboard/src/test/java/com/alibaba/csp/sentinel/dashboard/repository/metric/InMemoryMetricsRepositoryTest.java View File

@@ -1,8 +1,24 @@
/*
* Copyright 1999-2019 Alibaba Group Holding Ltd.
*
* 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 com.alibaba.csp.sentinel.dashboard.repository.metric; package com.alibaba.csp.sentinel.dashboard.repository.metric;


import com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity; import com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity;

import org.assertj.core.util.Lists; import org.assertj.core.util.Lists;
import org.junit.Assert;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
@@ -15,33 +31,36 @@ import java.util.concurrent.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;


/** /**
* InMemoryMetricsRepository Test
* Test cases for {@link InMemoryMetricsRepository}.
* *
* @author Nick Tan * @author Nick Tan
*/ */
public class InMemoryMetricsRepositoryTest { public class InMemoryMetricsRepositoryTest {


private static final int AVAILABLE_CPU_PROCESSORS = Runtime.getRuntime().availableProcessors();

private static final String DEFAULT_APP = "default"; private static final String DEFAULT_APP = "default";
private static final String DEFAULT_EXPIRE_APP = "default_expire_app"; private static final String DEFAULT_EXPIRE_APP = "default_expire_app";
private static final String DEFAULT_RESOURCE = "test"; private static final String DEFAULT_RESOURCE = "test";
private static final long EXPIRE_TIME = 1000 * 60 * 5L; private static final long EXPIRE_TIME = 1000 * 60 * 5L;
private InMemoryMetricsRepository inMemoryMetricsRepository;


private static final int AVAILABLE_CPU_PROCESSORS = Runtime.getRuntime().availableProcessors();
private InMemoryMetricsRepository inMemoryMetricsRepository;


private ExecutorService executorService = Executors.newFixedThreadPool(AVAILABLE_CPU_PROCESSORS);
private ExecutorService executorService;


@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {

inMemoryMetricsRepository = new InMemoryMetricsRepository(); inMemoryMetricsRepository = new InMemoryMetricsRepository();
executorService = Executors.newFixedThreadPool(AVAILABLE_CPU_PROCESSORS);
} }


@Test
public void save() throws InterruptedException {
@After
public void tearDown() {
executorService.shutdownNow();
}


private void testSave() {
for (int i = 0; i < 1000000; i++) { for (int i = 0; i < 1000000; i++) {

MetricEntity entry = new MetricEntity(); MetricEntity entry = new MetricEntity();
entry.setApp(DEFAULT_APP); entry.setApp(DEFAULT_APP);
entry.setResource(DEFAULT_RESOURCE); entry.setResource(DEFAULT_RESOURCE);
@@ -51,14 +70,11 @@ public class InMemoryMetricsRepositoryTest {
entry.setBlockQps(0L); entry.setBlockQps(0L);
entry.setSuccessQps(1L); entry.setSuccessQps(1L);
inMemoryMetricsRepository.save(entry); inMemoryMetricsRepository.save(entry);

} }

} }


@Test @Test
public void testExpireMetric() throws InterruptedException {

public void testExpireMetric() {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
MetricEntity expireEntry = new MetricEntity(); MetricEntity expireEntry = new MetricEntity();
expireEntry.setApp(DEFAULT_EXPIRE_APP); expireEntry.setApp(DEFAULT_EXPIRE_APP);
@@ -83,24 +99,21 @@ public class InMemoryMetricsRepositoryTest {
List<MetricEntity> list = inMemoryMetricsRepository.queryByAppAndResourceBetween( List<MetricEntity> list = inMemoryMetricsRepository.queryByAppAndResourceBetween(
DEFAULT_EXPIRE_APP, DEFAULT_RESOURCE, now - 2 * EXPIRE_TIME, now + EXPIRE_TIME); DEFAULT_EXPIRE_APP, DEFAULT_RESOURCE, now - 2 * EXPIRE_TIME, now + EXPIRE_TIME);


Assert.assertEquals(false, CollectionUtils.isEmpty(list));

assertTrue(list.size() == 1);

assertFalse(CollectionUtils.isEmpty(list));
assertEquals(1, list.size());
} }


@Test @Test
public void listResourcesOfApp() throws InterruptedException {
public void testListResourcesOfApp() {
// prepare basic test data // prepare basic test data
save();
System.out.println(System.currentTimeMillis() + "[basic test data ready]");
testSave();
System.out.println( "[" + System.currentTimeMillis() + "] Basic test data ready in testListResourcesOfApp");


List<CompletableFuture> futures = Lists.newArrayList(); List<CompletableFuture> futures = Lists.newArrayList();


// concurrent query resources of app // concurrent query resources of app
final CyclicBarrier cyclicBarrier = new CyclicBarrier(AVAILABLE_CPU_PROCESSORS); final CyclicBarrier cyclicBarrier = new CyclicBarrier(AVAILABLE_CPU_PROCESSORS);
for (int j = 0; j < 10000; j++) { for (int j = 0; j < 10000; j++) {

futures.add( futures.add(
CompletableFuture.runAsync(() -> { CompletableFuture.runAsync(() -> {
try { try {
@@ -117,7 +130,6 @@ public class InMemoryMetricsRepositoryTest {


// batch add metric entity // batch add metric entity
for (int i = 0; i < 10000; i++) { for (int i = 0; i < 10000; i++) {

MetricEntity entry = new MetricEntity(); MetricEntity entry = new MetricEntity();
entry.setApp(DEFAULT_APP); entry.setApp(DEFAULT_APP);
entry.setResource(DEFAULT_RESOURCE); entry.setResource(DEFAULT_RESOURCE);
@@ -127,15 +139,14 @@ public class InMemoryMetricsRepositoryTest {
entry.setBlockQps(0L); entry.setBlockQps(0L);
entry.setSuccessQps(1L); entry.setSuccessQps(1L);
inMemoryMetricsRepository.save(entry); inMemoryMetricsRepository.save(entry);

} }


CompletableFuture all = CompletableFuture.allOf(futures.toArray((new CompletableFuture[futures.size()])));
CompletableFuture all = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
try { try {
all.join(); all.join();
} catch (ConcurrentModificationException e) { } catch (ConcurrentModificationException e) {
e.printStackTrace(); e.printStackTrace();
assertFalse("concurrent error", e instanceof ConcurrentModificationException);
fail("concurrent error occurred");
} }
} }



Loading…
Cancel
Save