11package com .alicloud .openservices .tablestore .tunnel .functiontest ;
22
3- import java .io .FileInputStream ;
4- import java .util .ArrayList ;
5- import java .util .List ;
6- import java .util .Properties ;
7-
8- import com .alicloud .openservices .tablestore .SyncClient ;
93import com .alicloud .openservices .tablestore .TunnelClient ;
10- import com .alicloud .openservices .tablestore .model .Column ;
11- import com .alicloud .openservices .tablestore .model .ColumnValue ;
12- import com .alicloud .openservices .tablestore .model .CreateTableRequest ;
13- import com .alicloud .openservices .tablestore .model .DeleteTableRequest ;
14- import com .alicloud .openservices .tablestore .model .PrimaryKeyBuilder ;
15- import com .alicloud .openservices .tablestore .model .PrimaryKeyType ;
16- import com .alicloud .openservices .tablestore .model .PrimaryKeyValue ;
17- import com .alicloud .openservices .tablestore .model .PutRowRequest ;
18- import com .alicloud .openservices .tablestore .model .RowPutChange ;
19- import com .alicloud .openservices .tablestore .model .TableMeta ;
20- import com .alicloud .openservices .tablestore .model .TableOptions ;
21- import com .alicloud .openservices .tablestore .model .tunnel .CreateTunnelRequest ;
22- import com .alicloud .openservices .tablestore .model .tunnel .CreateTunnelResponse ;
23- import com .alicloud .openservices .tablestore .model .tunnel .DeleteTunnelRequest ;
24- import com .alicloud .openservices .tablestore .model .tunnel .DeleteTunnelResponse ;
25- import com .alicloud .openservices .tablestore .model .tunnel .TunnelType ;
264import com .alicloud .openservices .tablestore .tunnel .worker .IChannelProcessor ;
275import com .alicloud .openservices .tablestore .tunnel .worker .ProcessRecordsInput ;
286import com .alicloud .openservices .tablestore .tunnel .worker .TunnelWorker ;
297import com .alicloud .openservices .tablestore .tunnel .worker .TunnelWorkerConfig ;
30- import org .junit .Assert ;
31- import org .junit .BeforeClass ;
32- import org .junit .Test ;
338
349public class TestTunnelWorkerReconnect {
35- private static final String CONF_PATH = "tunnel.config" ;
36- private static String endpoint = "" ;
37- private static String accessId = "" ;
38- private static String accessKey = "" ;
39- private static String instanceName = "" ;
40-
41- @ BeforeClass
42- public static void loadConfig () {
43- System .out .println ("here" );
44- try {
45- Properties properties = new Properties ();
46- properties .load (new FileInputStream (CONF_PATH ));
47- endpoint = properties .getProperty ("OtsEndpoint" );
48- accessId = properties .getProperty ("AccessId" );
49- accessKey = properties .getProperty ("AccessKey" );
50- instanceName = properties .getProperty ("InstanceName" );
51- } catch (Exception e ) {
52- e .printStackTrace ();
53- }
54- }
10+ private static final String Endpoint = "" ;
11+ private static final String AccessId = "" ;
12+ private static final String AccessKey = "" ;
13+ private static final String InstanceName = "" ;
5514
56- class SimpleProcessor implements IChannelProcessor {
15+ static class SimpleProcessor implements IChannelProcessor {
5716 @ Override
5817 public void process (ProcessRecordsInput input ) {
5918 System .out .println ("Default record processor, would print records count" );
@@ -73,100 +32,16 @@ public void shutdown() {
7332 }
7433 }
7534
76- static void createTunnel (TunnelClient client , String tableName , String tunnelName ) {
77- CreateTunnelRequest request = new CreateTunnelRequest (tableName , tunnelName , TunnelType .Stream );
78- CreateTunnelResponse resp = client .createTunnel (request );
79- System .out .println ("RequestId: " + resp .getRequestId ());
80- System .out .println ("TunnelId: " + resp .getTunnelId ());
81- }
82-
83- static void createTable (SyncClient client , String tableName ) {
84- TableMeta meta = new TableMeta (tableName );
85- meta .addPrimaryKeyColumn ("int" , PrimaryKeyType .INTEGER );
86- meta .addPrimaryKeyColumn ("str" , PrimaryKeyType .STRING );
87- CreateTableRequest request = new CreateTableRequest (meta , new TableOptions (-1 , 1 ));
88- client .createTable (request );
89-
90- }
91-
92- static void deleteTable (SyncClient client , String tableName ) {
93- DeleteTableRequest request = new DeleteTableRequest (tableName );
94- client .deleteTable (request );
95- }
96-
97- static List <RowPutChange > putRows (SyncClient client , String tableName , int rowCount ) {
98- List <RowPutChange > changes = new ArrayList <RowPutChange >();
99- for (int i = 0 ; i < rowCount ; i ++) {
100- PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder .createPrimaryKeyBuilder ();
101- primaryKeyBuilder .addPrimaryKeyColumn ("int" , PrimaryKeyValue .fromLong (i ));
102- primaryKeyBuilder .addPrimaryKeyColumn ("str" , PrimaryKeyValue .fromString ("string" + i ));
103- RowPutChange rowPutChange = new RowPutChange (tableName , primaryKeyBuilder .build ());
104-
105- for (int j = 0 ; j < 10 ; j ++) {
106- rowPutChange .addColumn (new Column ("test" + j , ColumnValue .fromLong (i )));
107- }
108- client .putRow (new PutRowRequest (rowPutChange ));
109- changes .add (rowPutChange );
110- }
111- System .out .println (String .format ("Put %d rows succeed." , rowCount ));
112- return changes ;
113- }
114-
115- static void deleteTunnel (TunnelClient client , String tableName , String tunnelName ) {
116- DeleteTunnelRequest request = new DeleteTunnelRequest (tableName , tunnelName );
117- DeleteTunnelResponse resp = client .deleteTunnel (request );
118- System .out .println ("RequestId: " + resp .getRequestId ());
119- }
120-
121- @ Test
122- public void testTunnelWorkerReconnect_WithTunnelInvalid () {
123- TunnelClient tunnelClient = new TunnelClient (endpoint , accessId , accessKey , instanceName );
124- SyncClient syncClient = new SyncClient (endpoint , accessId , accessKey , instanceName );
125-
126- String tableName = "test_zr" + System .currentTimeMillis ();
127- String tunnelName = "test_zr" + System .currentTimeMillis ();
128- // 1. create table
129- System .out .println ("Begin Create Table: " + tableName );
130- createTable (syncClient , tableName );
131- System .out .println ("++++++++++++++++++++++++++++++++++++" );
132-
133- // 2. create tunnel
134- System .out .println ("Begin Create Tunnel: " + tunnelName );
135- CreateTunnelResponse resp = tunnelClient .createTunnel (
136- new CreateTunnelRequest (tableName , tunnelName , TunnelType .Stream ));
137- String tunnelId = resp .getTunnelId ();
138- System .out .println ("Create Tunnel, Id: " + tunnelId );
139- System .out .println ("++++++++++++++++++++++++++++++++++++" );
140-
141- // 3. put data
142- System .out .println ("Begin Put Data in backend." );
143- putRows (syncClient , tableName , 5000 );
144- System .out .println ("++++++++++++++++++++++++++++++++++++" );
145-
146- // 4. new tunnel worker and consume.
35+ public static void main (String [] args ) {
36+ TunnelClient client = new TunnelClient (Endpoint , AccessId , AccessKey , InstanceName );
14737 TunnelWorkerConfig config = new TunnelWorkerConfig (new SimpleProcessor ());
148- TunnelWorker worker = new TunnelWorker (tunnelId , tunnelClient , config );
38+ TunnelWorker worker1 = new TunnelWorker ("cfea9e1d-19f3-4835-8551-698a5c5c0773" , client , config );
14939 try {
150- worker .connectAndWorking ();
151- Thread .sleep (50000 );
152- System .out .println ("Begin Delete Tunnel: " + tunnelName );
153- deleteTunnel (tunnelClient , tableName , tunnelName );
154- System .out .println ("++++++++++++++++++++++++++++++++++++" );
155- Thread .sleep (50000 );
156- // cannot achieve here.
157- Assert .fail ();
40+ System .out .println ("worker running...." );
41+ worker1 .connectAndWorking ();
15842 } catch (Exception e ) {
15943 e .printStackTrace ();
160- worker .shutdown ();
161- } finally {
162- // delete table
163- System .out .println ("Begin Delete Table: " + tableName );
164- deleteTable (syncClient , tableName );
165- System .out .println ("++++++++++++++++++++++++++++++++++++" );
166- worker .shutdown ();
167- syncClient .shutdown ();
168- tunnelClient .shutdown ();
44+ worker1 .shutdown ();
16945 }
17046 }
171-
17247}
0 commit comments