-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge from master and use @SpiOrder and SpiLoader.loadInstanceListSor…
…ted(Class<T> clazz) method.
- Loading branch information
Showing
17 changed files
with
230 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...ore/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.slotchain.ProcessorSlot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Sentinel default ProcessorSlots | ||
com.alibaba.csp.sentinel.slots.nodeselector.NodeSelectorSlot | ||
com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot | ||
com.alibaba.csp.sentinel.slots.logger.LogSlot | ||
com.alibaba.csp.sentinel.slots.statistic.StatisticSlot | ||
com.alibaba.csp.sentinel.slots.system.SystemSlot | ||
com.alibaba.csp.sentinel.slots.block.authority.AuthoritySlot | ||
com.alibaba.csp.sentinel.slots.block.flow.FlowSlot | ||
com.alibaba.csp.sentinel.slots.block.degrade.DegradeSlot |
89 changes: 89 additions & 0 deletions
89
sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/DefaultSlotChainBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright 1999-2018 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.slots; | ||
|
||
import com.alibaba.csp.sentinel.slotchain.AbstractLinkedProcessorSlot; | ||
import com.alibaba.csp.sentinel.slotchain.ProcessorSlotChain; | ||
import com.alibaba.csp.sentinel.slots.block.authority.AuthoritySlot; | ||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeSlot; | ||
import com.alibaba.csp.sentinel.slots.block.flow.FlowSlot; | ||
import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot; | ||
import com.alibaba.csp.sentinel.slots.logger.LogSlot; | ||
import com.alibaba.csp.sentinel.slots.nodeselector.NodeSelectorSlot; | ||
import com.alibaba.csp.sentinel.slots.statistic.StatisticSlot; | ||
import com.alibaba.csp.sentinel.slots.system.SystemSlot; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Test cases for {@link DefaultSlotChainBuilder}. | ||
* | ||
* @author cdfive | ||
*/ | ||
public class DefaultSlotChainBuilderTest { | ||
|
||
@Test | ||
public void testBuild() { | ||
DefaultSlotChainBuilder builder = new DefaultSlotChainBuilder(); | ||
ProcessorSlotChain slotChain = builder.build(); | ||
assertNotNull(slotChain); | ||
|
||
// Verify the order of slot | ||
AbstractLinkedProcessorSlot<?> next = slotChain.getNext(); | ||
assertTrue(next instanceof NodeSelectorSlot); | ||
|
||
// Store the first NodeSelectorSlot instance | ||
NodeSelectorSlot nodeSelectorSlot = (NodeSelectorSlot) next; | ||
|
||
next = next.getNext(); | ||
assertTrue(next instanceof ClusterBuilderSlot); | ||
|
||
next = next.getNext(); | ||
assertTrue(next instanceof LogSlot); | ||
|
||
next = next.getNext(); | ||
assertTrue(next instanceof StatisticSlot); | ||
|
||
next = next.getNext(); | ||
assertTrue(next instanceof SystemSlot); | ||
|
||
next = next.getNext(); | ||
assertTrue(next instanceof AuthoritySlot); | ||
|
||
next = next.getNext(); | ||
assertTrue(next instanceof FlowSlot); | ||
|
||
next = next.getNext(); | ||
assertTrue(next instanceof DegradeSlot); | ||
|
||
next = next.getNext(); | ||
assertNull(next); | ||
|
||
// Build again to verify different instances | ||
ProcessorSlotChain slotChain2 = builder.build(); | ||
assertNotNull(slotChain2); | ||
// Verify the two ProcessorSlotChain instances are different | ||
assertNotSame(slotChain, slotChain2); | ||
|
||
next = slotChain2.getNext(); | ||
assertTrue(next instanceof NodeSelectorSlot); | ||
// Store the second NodeSelectorSlot instance | ||
NodeSelectorSlot nodeSelectorSlot2 = (NodeSelectorSlot) next; | ||
// Verify the two NodeSelectorSlot instances are different | ||
assertNotSame(nodeSelectorSlot, nodeSelectorSlot2); | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/SpiLoaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 1999-2018 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.util; | ||
|
||
import com.alibaba.csp.sentinel.slotchain.ProcessorSlot; | ||
import com.alibaba.csp.sentinel.slotchain.SlotChainBuilder; | ||
import com.alibaba.csp.sentinel.slots.DefaultSlotChainBuilder; | ||
import com.alibaba.csp.sentinel.slots.block.authority.AuthoritySlot; | ||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeSlot; | ||
import com.alibaba.csp.sentinel.slots.block.flow.FlowSlot; | ||
import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot; | ||
import com.alibaba.csp.sentinel.slots.logger.LogSlot; | ||
import com.alibaba.csp.sentinel.slots.nodeselector.NodeSelectorSlot; | ||
import com.alibaba.csp.sentinel.slots.statistic.StatisticSlot; | ||
import com.alibaba.csp.sentinel.slots.system.SystemSlot; | ||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Test cases for {@link SpiLoader}. | ||
* | ||
* @author cdfive | ||
*/ | ||
public class SpiLoaderTest { | ||
|
||
@Test | ||
public void testLoadFirstInstance() { | ||
ProcessorSlot processorSlot = SpiLoader.loadFirstInstance(ProcessorSlot.class); | ||
assertNotNull(processorSlot); | ||
|
||
SlotChainBuilder slotChainBuilder = SpiLoader.loadFirstInstance(SlotChainBuilder.class); | ||
assertNotNull(slotChainBuilder); | ||
assertTrue(slotChainBuilder instanceof DefaultSlotChainBuilder); | ||
} | ||
|
||
@Test | ||
public void testLoadHighestPriorityInstance() { | ||
ProcessorSlot processorSlot = SpiLoader.loadHighestPriorityInstance(ProcessorSlot.class); | ||
assertNotNull(processorSlot); | ||
|
||
// NodeSelectorSlot is highest order with @SpiOrder(-9000), among all slots | ||
assertTrue(processorSlot instanceof NodeSelectorSlot); | ||
} | ||
|
||
@Test | ||
public void testLoadInstanceListSorted() { | ||
List<ProcessorSlot> sortedSlots = SpiLoader.loadInstanceListSorted(ProcessorSlot.class); | ||
assertNotNull(sortedSlots); | ||
|
||
// Total 8 default slot in sentinel-core | ||
assertEquals(8, sortedSlots.size()); | ||
|
||
// Verify the order of slot | ||
int index = 0; | ||
assertTrue(sortedSlots.get(index++) instanceof NodeSelectorSlot); | ||
assertTrue(sortedSlots.get(index++) instanceof ClusterBuilderSlot); | ||
assertTrue(sortedSlots.get(index++) instanceof LogSlot); | ||
assertTrue(sortedSlots.get(index++) instanceof StatisticSlot); | ||
assertTrue(sortedSlots.get(index++) instanceof SystemSlot); | ||
assertTrue(sortedSlots.get(index++) instanceof AuthoritySlot); | ||
assertTrue(sortedSlots.get(index++) instanceof FlowSlot); | ||
assertTrue(sortedSlots.get(index++) instanceof DegradeSlot); | ||
|
||
// Verify each call return different instances | ||
List<ProcessorSlot> sortedSlots2 = SpiLoader.loadInstanceListSorted(ProcessorSlot.class); | ||
assertNotSame(sortedSlots, sortedSlots2); | ||
assertEquals(sortedSlots.size(), sortedSlots2.size()); | ||
for (int i = 0; i < sortedSlots.size(); i++) { | ||
ProcessorSlot slot = sortedSlots.get(i); | ||
ProcessorSlot slot2 = sortedSlots2.get(i); | ||
assertNotSame(slot, slot2); | ||
assertNotEquals(slot, slot2); | ||
assertEquals(slot.getClass(), slot2.getClass()); | ||
} | ||
} | ||
} |
Oops, something went wrong.