Search in sources :

Example 31 with StringResourceWrapper

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);
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 32 with StringResourceWrapper

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);
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) EntranceNode(com.alibaba.csp.sentinel.node.EntranceNode)

Example 33 with StringResourceWrapper

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();
    }
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Test(org.junit.Test)

Example 34 with StringResourceWrapper

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());
}
Also used : Context(com.alibaba.csp.sentinel.context.Context) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) Test(org.junit.Test)

Example 35 with StringResourceWrapper

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());
}
Also used : NullContext(com.alibaba.csp.sentinel.context.NullContext) Context(com.alibaba.csp.sentinel.context.Context) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) NullContext(com.alibaba.csp.sentinel.context.NullContext) Node(com.alibaba.csp.sentinel.node.Node) Test(org.junit.Test)

Aggregations

StringResourceWrapper (com.alibaba.csp.sentinel.slotchain.StringResourceWrapper)48 Test (org.junit.Test)43 ResourceWrapper (com.alibaba.csp.sentinel.slotchain.ResourceWrapper)31 Context (com.alibaba.csp.sentinel.context.Context)17 AtomicLong (java.util.concurrent.atomic.AtomicLong)12 AbstractTimeBasedTest (com.alibaba.csp.sentinel.test.AbstractTimeBasedTest)7 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)6 NullContext (com.alibaba.csp.sentinel.context.NullContext)5 DefaultNode (com.alibaba.csp.sentinel.node.DefaultNode)4 HashMap (java.util.HashMap)3 Entry (com.alibaba.csp.sentinel.Entry)2 EntranceNode (com.alibaba.csp.sentinel.node.EntranceNode)2 Node (com.alibaba.csp.sentinel.node.Node)2 Function (com.alibaba.csp.sentinel.util.function.Function)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)1 FlowException (com.alibaba.csp.sentinel.slots.block.flow.FlowException)1 ConcurrentLinkedHashMapWrapper (com.alibaba.csp.sentinel.slots.statistic.cache.ConcurrentLinkedHashMapWrapper)1 Random (java.util.Random)1