|
| 1 | +package com.xiaoyu.rpc.core.extension; |
| 2 | + |
| 3 | +import com.xiaoyu.rpc.common.extension.ExtensionLoader; |
| 4 | +import com.xiaoyu.rpc.common.serialization.Serializer; |
| 5 | +import com.xiaoyu.rpc.core.loadbalancer.LoadBalancer; |
| 6 | +import com.xiaoyu.rpc.core.client.ProxyFactory; |
| 7 | +import com.xiaoyu.rpc.core.protocol.Protocol; |
| 8 | +import com.xiaoyu.rpc.core.registry.ServiceRegistry; |
| 9 | +import com.xiaoyu.rpc.core.registry.ServiceDiscovery; |
| 10 | + |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.api.DisplayName; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.*; |
| 15 | + |
| 16 | +/** |
| 17 | + * ExtensionLoader SPI 机制单元测试 |
| 18 | + */ |
| 19 | +@DisplayName("SPI ExtensionLoader 测试") |
| 20 | +public class ExtensionLoaderTest { |
| 21 | + |
| 22 | + @Test |
| 23 | + @DisplayName("测试加载 Serializer 扩展") |
| 24 | + void testLoadSerializerExtensions() { |
| 25 | + ExtensionLoader<Serializer> loader = ExtensionLoader.getExtensionLoader(Serializer.class); |
| 26 | + |
| 27 | + // 测试所有支持的序列化器 |
| 28 | + assertNotNull(loader.getExtension("java"), "Java serializer should be loaded"); |
| 29 | + assertNotNull(loader.getExtension("kryo"), "Kryo serializer should be loaded"); |
| 30 | + assertNotNull(loader.getExtension("protobuf"), "Protobuf serializer should be loaded"); |
| 31 | + assertNotNull(loader.getExtension("json"), "JSON serializer should be loaded"); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + @DisplayName("测试获取所有支持扩展名") |
| 36 | + void testGetSupportedExtensions() { |
| 37 | + ExtensionLoader<Serializer> loader = ExtensionLoader.getExtensionLoader(Serializer.class); |
| 38 | + |
| 39 | + var extensions = loader.getSupportedExtensions(); |
| 40 | + assertTrue(extensions.contains("java"), "Should contain 'java' extension"); |
| 41 | + assertTrue(extensions.contains("kryo"), "Should contain 'kryo' extension"); |
| 42 | + assertTrue(extensions.contains("protobuf"), "Should contain 'protobuf' extension"); |
| 43 | + assertTrue(extensions.contains("json"), "Should contain 'json' extension"); |
| 44 | + assertEquals(4, extensions.size(), "Should have exactly 4 serializer extensions"); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + @DisplayName("测试扩展单例缓存") |
| 49 | + void testExtensionCaching() { |
| 50 | + ExtensionLoader<Serializer> loader = ExtensionLoader.getExtensionLoader(Serializer.class); |
| 51 | + |
| 52 | + Serializer first = loader.getExtension("kryo"); |
| 53 | + Serializer second = loader.getExtension("kryo"); |
| 54 | + |
| 55 | + assertSame(first, second, "Same extension should return same instance (singleton)"); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + @DisplayName("测试加载不存在扩展时抛出异常") |
| 60 | + void testLoadNonExistentExtension() { |
| 61 | + ExtensionLoader<Serializer> loader = ExtensionLoader.getExtensionLoader(Serializer.class); |
| 62 | + |
| 63 | + assertThrows(RuntimeException.class, () -> { |
| 64 | + loader.getExtension("non_existent"); |
| 65 | + }, "Loading non-existent extension should throw exception"); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + @DisplayName("测试 LoadBalancer 扩展加载") |
| 70 | + void testLoadBalancerExtensions() { |
| 71 | + ExtensionLoader<LoadBalancer> loader = ExtensionLoader.getExtensionLoader(LoadBalancer.class); |
| 72 | + |
| 73 | + assertNotNull(loader.getExtension("random"), "Random load balancer should be loaded"); |
| 74 | + assertNotNull(loader.getExtension("roundrobin"), "RoundRobin load balancer should be loaded"); |
| 75 | + |
| 76 | + var extensions = loader.getSupportedExtensions(); |
| 77 | + assertEquals(2, extensions.size(), "Should have exactly 2 load balancer extensions"); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + @DisplayName("测试 Protocol 扩展加载") |
| 82 | + void testProtocolExtensions() { |
| 83 | + ExtensionLoader<Protocol> loader = ExtensionLoader.getExtensionLoader(Protocol.class); |
| 84 | + |
| 85 | + assertNotNull(loader.getExtension("netty"), "Netty protocol should be loaded"); |
| 86 | + assertNotNull(loader.getExtension("http"), "HTTP protocol should be loaded"); |
| 87 | + assertNotNull(loader.getExtension("http2"), "HTTP2 protocol should be loaded"); |
| 88 | + assertNotNull(loader.getExtension("grpc"), "gRPC protocol should be loaded"); |
| 89 | + |
| 90 | + var extensions = loader.getSupportedExtensions(); |
| 91 | + assertEquals(4, extensions.size(), "Should have exactly 4 protocol extensions"); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + @DisplayName("测试 ProxyFactory 扩展加载") |
| 96 | + void testProxyFactoryExtensions() { |
| 97 | + ExtensionLoader<ProxyFactory> loader = ExtensionLoader.getExtensionLoader(ProxyFactory.class); |
| 98 | + |
| 99 | + assertNotNull(loader.getExtension("jdk"), "JDK proxy factory should be loaded"); |
| 100 | + assertNotNull(loader.getExtension("bytebuddy"), "ByteBuddy proxy factory should be loaded"); |
| 101 | + |
| 102 | + var extensions = loader.getSupportedExtensions(); |
| 103 | + assertEquals(2, extensions.size(), "Should have exactly 2 proxy factory extensions"); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + @DisplayName("测试 ServiceRegistry 扩展加载") |
| 108 | + void testServiceRegistryExtensions() { |
| 109 | + ExtensionLoader<ServiceRegistry> loader = ExtensionLoader.getExtensionLoader(ServiceRegistry.class); |
| 110 | + |
| 111 | + // local registry should be loadable without external dependencies |
| 112 | + assertNotNull(loader.getExtension("local"), "Local service registry should be loaded"); |
| 113 | + |
| 114 | + var extensions = loader.getSupportedExtensions(); |
| 115 | + assertTrue(extensions.contains("local"), "Should contain 'local' extension"); |
| 116 | + assertTrue(extensions.contains("nacos"), "Should contain 'nacos' extension"); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + @DisplayName("测试 ServiceDiscovery 扩展加载") |
| 121 | + void testServiceDiscoveryExtensions() { |
| 122 | + ExtensionLoader<ServiceDiscovery> loader = ExtensionLoader.getExtensionLoader(ServiceDiscovery.class); |
| 123 | + |
| 124 | + // local registry should be loadable without external dependencies |
| 125 | + assertNotNull(loader.getExtension("local"), "Local service discovery should be loaded"); |
| 126 | + |
| 127 | + var extensions = loader.getSupportedExtensions(); |
| 128 | + assertTrue(extensions.contains("local"), "Should contain 'local' extension"); |
| 129 | + assertTrue(extensions.contains("nacos"), "Should contain 'nacos' extension"); |
| 130 | + } |
| 131 | +} |
0 commit comments