2626import co .elastic .apm .agent .impl .error .ErrorCapture ;
2727import co .elastic .apm .agent .impl .transaction .Span ;
2828import co .elastic .apm .agent .impl .transaction .Transaction ;
29- import co .elastic .apm .agent .testutils .TestPort ;
3029import com .alibaba .dubbo .config .ApplicationConfig ;
3130import com .alibaba .dubbo .config .MethodConfig ;
3231import com .alibaba .dubbo .config .ProtocolConfig ;
4544
4645public class AlibabaDubboInstrumentationTest extends AbstractDubboInstrumentationTest {
4746
48- private ReferenceConfig <DubboTestApi > testApiReferenceConfig ;
49-
50- private ServiceConfig <DubboTestApi > testApiServiceConfig ;
51-
52- private ServiceConfig <AnotherApi > anotherApiServiceConfig ;
53-
54- private ReferenceConfig <AnotherApi > anotherApiReferenceConfig ;
55-
5647 @ Override
5748 protected DubboTestApi buildDubboTestApi () {
58- RegistryConfig registryConfig = new RegistryConfig ();
59- registryConfig . setAddress ( "N/A" );
49+ RegistryConfig registryConfig = createRegistryConfig ();
50+ ApplicationConfig appConfig = createApplicationConfig ( );
6051
6152 //build AnotherApi provider
62- ApplicationConfig anotherApiAppConfig = new ApplicationConfig ();
63- anotherApiAppConfig .setName ("another-api-provider" );
64-
65- ProtocolConfig anotherApiProtocolConfig = new ProtocolConfig ();
66- anotherApiProtocolConfig .setName ("dubbo" );
67- anotherApiProtocolConfig .setPort (getAnotherApiPort ());
68- anotherApiProtocolConfig .setThreads (10 );
69-
70- anotherApiServiceConfig = new ServiceConfig <>();
71- anotherApiServiceConfig .setApplication (anotherApiAppConfig );
72- anotherApiServiceConfig .setProtocol (anotherApiProtocolConfig );
73- anotherApiServiceConfig .setInterface (AnotherApi .class );
74- anotherApiServiceConfig .setRef (new AnotherApiImpl ());
75- anotherApiServiceConfig .setRegistry (registryConfig );
76- anotherApiServiceConfig .export ();
77-
78- //build AnotherApi consumer
79- ApplicationConfig providerAppConfig = new ApplicationConfig ();
80- providerAppConfig .setName ("dubbo-provider" );
53+ ProtocolConfig anotherApiProtocolConfig = createProtocolConfig (getAnotherApiPort ());
54+ createAndExportServiceConfig (registryConfig , AnotherApi .class , new AnotherApiImpl (), appConfig , anotherApiProtocolConfig );
8155
8256 //build AnotherApi consumer
83- anotherApiReferenceConfig = new ReferenceConfig <>();
84- anotherApiReferenceConfig .setApplication (providerAppConfig );
85- anotherApiReferenceConfig .setInterface (AnotherApi .class );
86- anotherApiReferenceConfig .setUrl ("dubbo://localhost:" + getAnotherApiPort ());
87- anotherApiReferenceConfig .setTimeout (3000 );
57+ ReferenceConfig <AnotherApi > anotherApiReferenceConfig = createReferenceConfig (AnotherApi .class , appConfig , anotherApiProtocolConfig .getPort ());
8858
89- //build test api provider
90- ProtocolConfig protocolConfig = new ProtocolConfig ();
91- protocolConfig .setName ("dubbo" );
92- protocolConfig .setPort (getPort ());
93- protocolConfig .setThreads (10 );
59+ // build DubboTestApi provider
60+ ProtocolConfig protocolConfig = createProtocolConfig (getPort ());
61+ createAndExportServiceConfig (registryConfig , DubboTestApi .class , new DubboTestApiImpl (anotherApiReferenceConfig .get ()), appConfig , protocolConfig );
9462
95- testApiServiceConfig = new ServiceConfig <>();
96- testApiServiceConfig .setApplication (providerAppConfig );
97- testApiServiceConfig .setProtocol (protocolConfig );
98- testApiServiceConfig .setInterface (DubboTestApi .class );
99- testApiServiceConfig .setRef (new DubboTestApiImpl (anotherApiReferenceConfig .get ()));
100- testApiServiceConfig .setRegistry (registryConfig );
101- testApiServiceConfig .export ();
102-
103- //build test api consumer
104- ApplicationConfig consumerApp = new ApplicationConfig ();
105- consumerApp .setName ("dubbo-consumer" );
106-
107- testApiReferenceConfig = new ReferenceConfig <>();
108- testApiReferenceConfig .setApplication (consumerApp );
109- testApiReferenceConfig .setInterface (DubboTestApi .class );
110- testApiReferenceConfig .setUrl ("dubbo://localhost:" + getPort ());
111- testApiReferenceConfig .setTimeout (3000 );
63+ // build DubboTestApi consumer
64+ ReferenceConfig <DubboTestApi > testApiReferenceConfig = createReferenceConfig (DubboTestApi .class , appConfig , protocolConfig .getPort ());
11265
11366 List <MethodConfig > methodConfigList = new LinkedList <>();
11467 testApiReferenceConfig .setMethods (methodConfigList );
@@ -126,6 +79,50 @@ protected DubboTestApi buildDubboTestApi() {
12679 return testApiReferenceConfig .get ();
12780 }
12881
82+ private static RegistryConfig createRegistryConfig () {
83+ RegistryConfig registryConfig = new RegistryConfig ();
84+ registryConfig .setAddress ("N/A" );
85+ return registryConfig ;
86+ }
87+
88+ private static ApplicationConfig createApplicationConfig () {
89+ ApplicationConfig appConfig = new ApplicationConfig ();
90+ appConfig .setName ("all-in-one-app" );
91+ return appConfig ;
92+ }
93+
94+ private static ProtocolConfig createProtocolConfig (int port ){
95+ ProtocolConfig protocolConfig = new ProtocolConfig ();
96+ protocolConfig .setName ("dubbo" );
97+ protocolConfig .setPort (port );
98+ protocolConfig .setThreads (10 );
99+ return protocolConfig ;
100+ }
101+
102+ private static <T > void createAndExportServiceConfig (RegistryConfig registryConfig ,
103+ Class <T > interfaceClass ,
104+ T interfaceImpl ,
105+ ApplicationConfig applicationConfig ,
106+ ProtocolConfig protocolConfig ) {
107+
108+ ServiceConfig <T > serviceConfig = new ServiceConfig <T >();
109+ serviceConfig .setApplication (applicationConfig );
110+ serviceConfig .setProtocol (protocolConfig );
111+ serviceConfig .setInterface (interfaceClass );
112+ serviceConfig .setRef (interfaceImpl );
113+ serviceConfig .setRegistry (registryConfig );
114+ serviceConfig .export ();
115+ }
116+
117+ private static <T > ReferenceConfig <T > createReferenceConfig (Class <T > interfaceClass , ApplicationConfig applicationConfig , int port ) {
118+ ReferenceConfig <T > referenceConfig = new ReferenceConfig <>();
119+ referenceConfig .setApplication (applicationConfig );
120+ referenceConfig .setInterface (interfaceClass );
121+ referenceConfig .setUrl (String .format ("dubbo://localhost:%d" , port ));
122+ referenceConfig .setTimeout (3000 );
123+ return referenceConfig ;
124+ }
125+
129126 @ Test
130127 public void testAsync () throws Exception {
131128 String arg = "hello" ;
0 commit comments