use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.
the class ParamFlowThrottleRateLimitingCheckerTest method testSingleValueThrottleCheckQpsMultipleThreads.
@Test
public void testSingleValueThrottleCheckQpsMultipleThreads() throws Exception {
final String resourceName = "testSingleValueThrottleCheckQpsMultipleThreads";
final ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
int paramIdx = 0;
long threshold = 5L;
final ParamFlowRule rule = new ParamFlowRule(resourceName).setCount(threshold).setParamIdx(paramIdx).setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER);
final String valueA = "valueA";
ParameterMetric metric = new ParameterMetric();
ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric);
metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000));
int threadCount = 40;
System.out.println(metric.getRuleTimeCounter(rule));
final CountDownLatch waitLatch = new CountDownLatch(threadCount);
final AtomicInteger successCount = new AtomicInteger();
for (int i = 0; i < threadCount; i++) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
if (ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA)) {
successCount.incrementAndGet();
}
waitLatch.countDown();
}
});
t.setName("sentinel-simulate-traffic-task-" + i);
t.start();
}
waitLatch.await();
assertEquals(successCount.get(), 1);
System.out.println(threadCount);
successCount.set(0);
System.out.println("testSingleValueThrottleCheckQpsMultipleThreads: sleep for 3 seconds");
TimeUnit.SECONDS.sleep(3);
successCount.set(0);
final CountDownLatch waitLatch1 = new CountDownLatch(threadCount);
final long currentTime = TimeUtil.currentTimeMillis();
final long endTime = currentTime + rule.getDurationInSec() * 1000 - 1;
for (int i = 0; i < threadCount; i++) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
long currentTime1 = currentTime;
while (currentTime1 <= endTime) {
if (ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA)) {
successCount.incrementAndGet();
}
Random random = new Random();
try {
TimeUnit.MILLISECONDS.sleep(random.nextInt(20));
} catch (InterruptedException e) {
e.printStackTrace();
}
currentTime1 = TimeUtil.currentTimeMillis();
}
waitLatch1.countDown();
}
});
t.setName("sentinel-simulate-traffic-task-" + i);
t.start();
}
waitLatch1.await();
assertEquals(successCount.get(), threshold);
}
use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.
the class ContextUtil method initDefaultContext.
private static void initDefaultContext() {
String defaultContextName = Constants.CONTEXT_DEFAULT_NAME;
EntranceNode node = new EntranceNode(new StringResourceWrapper(defaultContextName, EntryType.IN), null);
Constants.ROOT.addChild(node);
contextNameNodeMap.put(defaultContextName, node);
}
use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.
the class AsyncEntryTest method testCleanCurrentEntryInLocalError.
@Test(expected = IllegalStateException.class)
public void testCleanCurrentEntryInLocalError() {
final String contextName = "abc";
try {
ContextUtil.enter(contextName);
Context curContext = ContextUtil.getContext();
AsyncEntry entry = new AsyncEntry(new StringResourceWrapper("testCleanCurrentEntryInLocal", EntryType.OUT), null, curContext);
entry.cleanCurrentEntryInLocal();
entry.cleanCurrentEntryInLocal();
} finally {
ContextTestUtil.cleanUpContext();
}
}
use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.
the class AsyncEntryTest method testDuplicateInitAsyncContext.
@Test
public void testDuplicateInitAsyncContext() {
Context context = new Context(null, "abc");
AsyncEntry entry = new AsyncEntry(new StringResourceWrapper("testDuplicateInitAsyncContext", EntryType.OUT), null, context);
entry.initAsyncContext();
Context asyncContext = entry.getAsyncContext();
// Duplicate init.
entry.initAsyncContext();
assertSame(asyncContext, entry.getAsyncContext());
}
use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.
the class CtEntryTest method testGetLastNode.
@Test
public void testGetLastNode() {
Context context = new NullContext();
CtEntry entry = new CtEntry(new StringResourceWrapper("testGetLastNode", EntryType.IN), null, context);
assertNull(entry.parent);
assertNull(entry.getLastNode());
Entry parentEntry = mock(Entry.class);
Node node = mock(Node.class);
when(parentEntry.getCurNode()).thenReturn(node);
entry.parent = parentEntry;
assertSame(node, entry.getLastNode());
}
Aggregations