@@ -91,6 +91,8 @@ func (suite *RestTestSuite) SetupTest() {
9191 r .HandleFunc ("/rules/{name}/topo" , getTopoRuleHandler ).Methods (http .MethodGet )
9292 r .HandleFunc ("/rules/{name}/reset_state" , ruleStateHandler ).Methods (http .MethodPut )
9393 r .HandleFunc ("/rules/{name}/explain" , explainRuleHandler ).Methods (http .MethodGet )
94+ r .HandleFunc ("/rules/bulkstart" , rulesBulkOperationsHandler ).Methods (http .MethodPost )
95+ r .HandleFunc ("/rules/bulkstop" , rulesBulkOperationsHandler ).Methods (http .MethodPost )
9496 r .HandleFunc ("/rules/{name}/trace/start" , enableRuleTraceHandler ).Methods (http .MethodPost )
9597 r .HandleFunc ("/rules/{name}/trace/stop" , disableRuleTraceHandler ).Methods (http .MethodPost )
9698 r .HandleFunc ("/rules/validate" , validateRuleHandler ).Methods (http .MethodPost )
@@ -1024,3 +1026,64 @@ func (suite *RestTestSuite) TestWaitStopRule() {
10241026 require .True (suite .T (), end .Sub (now ) >= 300 * time .Millisecond )
10251027 waitAllRuleStop ()
10261028}
1029+
1030+ func (suite * RestTestSuite ) TestRulesBulkStartAndStop () {
1031+
1032+ timestamp := time .Now ().UnixNano ()
1033+ mockRules := []string {
1034+ fmt .Sprintf (`{"id":"r1_%d","sql":"SELECT * FROM demo","actions":[{"log":{}}], "tags": ["mock-tag"], "triggered": true}` , timestamp ),
1035+ fmt .Sprintf (`{"id":"r2_%d","sql":"SELECT color FROM demo","actions":[{"log":{}}], "tags": ["mock-tag"], "triggered": true}` , timestamp ),
1036+ fmt .Sprintf (`{"id":"r3_%d","sql":"SELECT size FROM demo","actions":[{"log":{}}], "tags": ["mock-tag"], "triggered": true}` , timestamp ),
1037+ fmt .Sprintf (`{"id":"r4_%d","sql":"SELECT ts FROM demo","actions":[{"log":{}}], "tags": ["mock-tag"], "triggered": true}` , timestamp ),
1038+ fmt .Sprintf (`{"id":"r5_%d","sql":"SELECT color,size FROM demo","actions":[{"log":{}}], "tags": ["mock-tag"], "triggered": true}` , timestamp ),
1039+ fmt .Sprintf (`{"id":"r6_%d","sql":"SELECT color,ts FROM demo","actions":[{"log":{}}], "tags": ["mock-tag"], "triggered": true}` , timestamp ),
1040+ fmt .Sprintf (`{"id":"r7_%d","sql":"SELECT size,ts FROM demo","actions":[{"log":{}}], "tags": ["mock-tag"], "triggered": true}` , timestamp ),
1041+ fmt .Sprintf (`{"id":"r8_%d","sql":"SELECT color,size,ts FROM demo","actions":[{"log":{}}], "tags": ["mock-tag"], "triggered": true}` , timestamp ),
1042+ fmt .Sprintf (`{"id":"r9_%d","sql":"SELECT * FROM demo WHERE size>0","actions":[{"log":{}}], "tags": ["mock-tag"], "triggered": true}` , timestamp ),
1043+ fmt .Sprintf (`{"id":"r10_%d","sql":"SELECT color FROM demo","actions":[{"log":{}}], "tags": ["mock-tag"], "triggered": true}` , timestamp ),
1044+ }
1045+
1046+ // create stream
1047+ buf := bytes .NewBuffer ([]byte (`{"sql":"CREATE STREAM demo (color STRING, size BIGINT, ts BIGINT) WITH (DATASOURCE=\"/data1\", TYPE=\"websocket\", FORMAT=\"json\", KEY=\"ts\")"}` ))
1048+ req , _ := http .NewRequest (http .MethodPost , "http://localhost:8080/streams" , buf )
1049+ w := httptest .NewRecorder ()
1050+ suite .r .ServeHTTP (w , req )
1051+ require .Equal (suite .T (), http .StatusCreated , w .Code )
1052+
1053+ // create rules
1054+ for i , rule := range mockRules {
1055+ buf := bytes .NewBuffer ([]byte (rule ))
1056+ req , _ := http .NewRequest (http .MethodPost , "http://localhost:8080/rules" , buf )
1057+ w := httptest .NewRecorder ()
1058+ suite .r .ServeHTTP (w , req )
1059+
1060+ if w .Code != http .StatusCreated {
1061+ body , _ := io .ReadAll (w .Result ().Body )
1062+ fmt .Printf ("Error: %d (index %d): %s\n " , i + 1 , i , string (body ))
1063+ }
1064+
1065+ require .Equal (suite .T (), http .StatusCreated , w .Code )
1066+ }
1067+
1068+ // bulk start
1069+ buf = bytes .NewBuffer ([]byte (`{"tags": ["mock-tag"]}` ))
1070+ req , _ = http .NewRequest (http .MethodPost , "http://localhost:8080/rules/bulkstart" , buf )
1071+ w = httptest .NewRecorder ()
1072+ suite .r .ServeHTTP (w , req )
1073+ if w .Code != http .StatusOK {
1074+ body , _ := io .ReadAll (w .Result ().Body )
1075+ fmt .Println (string (body ))
1076+ }
1077+ require .Equal (suite .T (), http .StatusOK , w .Code )
1078+
1079+ // bulk stop
1080+ buf = bytes .NewBuffer ([]byte (`{"tags": ["mock-tag"]}` ))
1081+ req , _ = http .NewRequest (http .MethodPost , "http://localhost:8080/rules/bulkstop" , buf )
1082+ w = httptest .NewRecorder ()
1083+ suite .r .ServeHTTP (w , req )
1084+ if w .Code != http .StatusOK {
1085+ body , _ := io .ReadAll (w .Result ().Body )
1086+ fmt .Println (string (body ))
1087+ }
1088+ require .Equal (suite .T (), http .StatusOK , w .Code )
1089+ }
0 commit comments