35
35
import java .lang .reflect .Method ;
36
36
import java .net .InetSocketAddress ;
37
37
import java .net .URL ;
38
+ import java .nio .file .Files ;
39
+ import java .util .Arrays ;
38
40
import java .util .Collections ;
39
41
import java .util .HashMap ;
40
42
import java .util .HashSet ;
43
45
import java .util .Set ;
44
46
import java .util .concurrent .TimeUnit ;
45
47
48
+ import org .apache .commons .io .FileUtils ;
46
49
import org .apache .pulsar .broker .PulsarService ;
47
50
import org .apache .pulsar .broker .ServiceConfiguration ;
48
51
import org .apache .pulsar .broker .ServiceConfigurationUtils ;
@@ -106,7 +109,6 @@ public class PulsarFunctionLocalRunTest {
106
109
PulsarAdmin admin ;
107
110
PulsarClient pulsarClient ;
108
111
BrokerStats brokerStatsClient ;
109
- PulsarWorkerService functionsWorkerService ;
110
112
final String tenant = "external-repl-prop" ;
111
113
String pulsarFunctionsNamespace = tenant + "/pulsar-function-admin" ;
112
114
String primaryHost ;
@@ -177,9 +179,25 @@ void setup(Method method) throws Exception {
177
179
config .setBrokerClientTlsEnabled (true );
178
180
config .setAllowAutoTopicCreationType ("non-partitioned" );
179
181
180
- functionsWorkerService = createPulsarFunctionWorker (config );
182
+ workerConfig = createWorkerConfig (config );
181
183
182
- Optional <WorkerService > functionWorkerService = Optional .of (functionsWorkerService );
184
+ // populate builtin connectors folder
185
+ if (Arrays .asList (method .getAnnotation (Test .class ).groups ()).contains ("builtin" )) {
186
+ File connectorsDir = new File (workerConfig .getConnectorsDirectory ());
187
+
188
+ if (connectorsDir .exists ()) {
189
+ FileUtils .deleteDirectory (connectorsDir );
190
+ }
191
+
192
+ if (connectorsDir .mkdir ()) {
193
+ File file = new File (getClass ().getClassLoader ().getResource ("pulsar-io-data-generator.nar" ).getFile ());
194
+ Files .copy (file .toPath (), new File (connectorsDir .getAbsolutePath () + "/" + file .getName ()).toPath ());
195
+ } else {
196
+ throw new RuntimeException ("Failed to create builtin connectors directory" );
197
+ }
198
+ }
199
+
200
+ Optional <WorkerService > functionWorkerService = Optional .empty ();
183
201
pulsar = new PulsarService (config , workerConfig , functionWorkerService , (exitCode ) -> {});
184
202
pulsar .start ();
185
203
@@ -199,9 +217,9 @@ void setup(Method method) throws Exception {
199
217
brokerStatsClient = admin .brokerStats ();
200
218
primaryHost = pulsar .getWebServiceAddress ();
201
219
202
- // update cluster metadata
220
+ // create cluster metadata
203
221
ClusterData clusterData = new ClusterData (urlTls .toString ());
204
- admin .clusters ().updateCluster (config .getClusterName (), clusterData );
222
+ admin .clusters ().createCluster (config .getClusterName (), clusterData );
205
223
206
224
ClientBuilder clientBuilder = PulsarClient .builder ()
207
225
.serviceUrl (pulsar .getBrokerServiceUrl ());
@@ -218,7 +236,7 @@ && isNotBlank(workerConfig.getBrokerClientAuthenticationParameters())) {
218
236
TenantInfo propAdmin = new TenantInfo ();
219
237
propAdmin .getAdminRoles ().add ("superUser" );
220
238
propAdmin .setAllowedClusters (Sets .newHashSet (Lists .newArrayList (CLUSTER )));
221
- admin .tenants ().updateTenant (tenant , propAdmin );
239
+ admin .tenants ().createTenant (tenant , propAdmin );
222
240
223
241
// setting up simple web sever to test submitting function via URL
224
242
fileServer = HttpServer .create (new InetSocketAddress (0 ), 0 );
@@ -279,17 +297,21 @@ void shutdown() throws Exception {
279
297
fileServer .stop (0 );
280
298
pulsarClient .close ();
281
299
admin .close ();
282
- functionsWorkerService .stop ();
283
300
pulsar .close ();
284
301
bkEnsemble .stop ();
302
+
303
+ File connectorsDir = new File (workerConfig .getConnectorsDirectory ());
304
+ if (connectorsDir .exists ()) {
305
+ FileUtils .deleteDirectory (connectorsDir );
306
+ }
285
307
}
286
308
287
- private PulsarWorkerService createPulsarFunctionWorker (ServiceConfiguration config ) {
309
+ private WorkerConfig createWorkerConfig (ServiceConfiguration config ) {
288
310
289
311
System .setProperty (JAVA_INSTANCE_JAR_PROPERTY ,
290
312
FutureUtil .class .getProtectionDomain ().getCodeSource ().getLocation ().getPath ());
291
313
292
- workerConfig = new WorkerConfig ();
314
+ WorkerConfig workerConfig = new WorkerConfig ();
293
315
workerConfig .setPulsarFunctionsNamespace (pulsarFunctionsNamespace );
294
316
workerConfig .setSchedulerClassName (
295
317
org .apache .pulsar .functions .worker .scheduler .RoundRobinScheduler .class .getName ());
@@ -321,10 +343,7 @@ private PulsarWorkerService createPulsarFunctionWorker(ServiceConfiguration conf
321
343
322
344
workerConfig .setAuthenticationEnabled (true );
323
345
workerConfig .setAuthorizationEnabled (true );
324
-
325
- PulsarWorkerService workerService = new PulsarWorkerService ();
326
- workerService .init (workerConfig , null , false );
327
- return workerService ;
346
+ return workerConfig ;
328
347
}
329
348
330
349
protected static FunctionConfig createFunctionConfig (String tenant , String namespace , String functionName , String sourceTopic , String sinkTopic , String subscriptionName ) {
@@ -707,13 +726,17 @@ private void testPulsarSourceLocalRun(String jarFilePathUrl) throws Exception {
707
726
}
708
727
}
709
728
729
+ @ Test (timeOut = 20000 , groups = "builtin" )
730
+ public void testPulsarSourceStatsBuiltin () throws Exception {
731
+ testPulsarSourceLocalRun (String .format ("%s://data-generator" , Utils .BUILTIN ));
732
+ }
710
733
711
- @ Test
734
+ @ Test ( timeOut = 20000 )
712
735
public void testPulsarSourceLocalRunNoArchive () throws Exception {
713
736
testPulsarSourceLocalRun (null );
714
737
}
715
738
716
- @ Test
739
+ @ Test ( timeOut = 20000 )
717
740
public void testPulsarSourceLocalRunWithFile () throws Exception {
718
741
String jarFilePathUrl = Utils .FILE + ":" + getClass ().getClassLoader ().getResource ("pulsar-io-data-generator.nar" ).getFile ();
719
742
testPulsarSourceLocalRun (jarFilePathUrl );
@@ -726,7 +749,7 @@ public void testPulsarSourceLocalRunWithUrl() throws Exception {
726
749
}
727
750
728
751
729
- private void testPulsarSinkStats (String jarFilePathUrl ) throws Exception {
752
+ private void testPulsarSinkLocalRun (String jarFilePathUrl ) throws Exception {
730
753
final String namespacePortion = "io" ;
731
754
final String replNamespace = tenant + "/" + namespacePortion ;
732
755
final String sourceTopic = "persistent://" + replNamespace + "/input" ;
@@ -813,20 +836,25 @@ private void testPulsarSinkStats(String jarFilePathUrl) throws Exception {
813
836
814
837
}
815
838
839
+ @ Test (timeOut = 20000 , groups = "builtin" )
840
+ public void testPulsarSinkStatsBuiltin () throws Exception {
841
+ testPulsarSinkLocalRun (String .format ("%s://data-generator" , Utils .BUILTIN ));
842
+ }
843
+
816
844
@ Test (timeOut = 20000 )
817
845
public void testPulsarSinkStatsNoArchive () throws Exception {
818
- testPulsarSinkStats (null );
846
+ testPulsarSinkLocalRun (null );
819
847
}
820
848
821
849
@ Test (timeOut = 20000 )
822
850
public void testPulsarSinkStatsWithFile () throws Exception {
823
851
String jarFilePathUrl = Utils .FILE + ":" + getClass ().getClassLoader ().getResource ("pulsar-io-data-generator.nar" ).getFile ();
824
- testPulsarSinkStats (jarFilePathUrl );
852
+ testPulsarSinkLocalRun (jarFilePathUrl );
825
853
}
826
854
827
855
@ Test (timeOut = 40000 )
828
856
public void testPulsarSinkStatsWithUrl () throws Exception {
829
857
String jarFilePathUrl = String .format ("http://127.0.0.1:%d/pulsar-io-data-generator.nar" , fileServer .getAddress ().getPort ());
830
- testPulsarSinkStats (jarFilePathUrl );
858
+ testPulsarSinkLocalRun (jarFilePathUrl );
831
859
}
832
860
}
0 commit comments