11package test
22
33import (
4- "bytes"
54 "context"
65 "crypto/tls"
76 "fmt"
@@ -27,6 +26,41 @@ import (
2726
2827const shutDownTimeout = 20 * time .Second
2928
29+ func (s * GatewayOpsTestSuite ) setupDapiKvGetBarrierHook (ctx context.Context , conn * grpc.ClientConn , hooksContextID , targetPath , barrierID string ) {
30+ hooksClient := internal_hooks_v1 .NewHooksServiceClient (conn )
31+
32+ _ , err := hooksClient .CreateHooksContext (ctx , & internal_hooks_v1.CreateHooksContextRequest {
33+ Id : hooksContextID ,
34+ })
35+ require .NoError (s .T (), err , "failed to create hooks context" )
36+
37+ _ , err = hooksClient .AddHooks (ctx , & internal_hooks_v1.AddHooksRequest {
38+ HooksContextId : hooksContextID ,
39+ Hooks : []* internal_hooks_v1.Hook {
40+ {
41+ Name : "block-dapi-kv-get" ,
42+ Description : "Block HTTP document GET on a barrier to simulate a long-running request" ,
43+ TargetMethod : targetPath ,
44+ Actions : []* internal_hooks_v1.HookAction {
45+ {
46+ Action : & internal_hooks_v1.HookAction_WaitOnBarrier_ {
47+ WaitOnBarrier : & internal_hooks_v1.HookAction_WaitOnBarrier {
48+ BarrierId : barrierID ,
49+ },
50+ },
51+ },
52+ {
53+ Action : & internal_hooks_v1.HookAction_Execute_ {
54+ Execute : & internal_hooks_v1.HookAction_Execute {},
55+ },
56+ },
57+ },
58+ },
59+ },
60+ })
61+ require .NoError (s .T (), err , "failed to add hooks" )
62+ }
63+
3064func (s * GatewayOpsTestSuite ) setupKvGetBarrierHook (ctx context.Context , conn * grpc.ClientConn , hooksContextID , barrierID string ) {
3165 hooksClient := internal_hooks_v1 .NewHooksServiceClient (conn )
3266
@@ -118,26 +152,37 @@ func (s *GatewayOpsTestSuite) TestGracefulShutdown() {
118152 },
119153 }
120154
155+ connAddr := fmt .Sprintf ("%s:%d" , "127.0.0.1" , startInfo .ServicePorts .PS )
156+ conn , err := grpc .NewClient (connAddr ,
157+ grpc .WithTransportCredentials (credentials .NewTLS (& tls.Config {InsecureSkipVerify : true })),
158+ grpc .WithUserAgent ("test-client" ),
159+ )
160+ require .NoError (s .T (), err , "failed to create grpc client connection to gateway" )
161+ defer func () { _ = conn .Close () }()
162+
163+ testKey := s .testDocId ()
164+
165+ // The call to setupKvGetBarrierHook can hang indefinitely if the hooks service is unreachable so we use a context with a
166+ // generous timeout
167+ ctx , cancel := context .WithTimeout (context .Background (), 120 * time .Second )
168+ defer cancel ()
169+
170+ docPath := fmt .Sprintf ("/v1/buckets/%s/scopes/%s/collections/%s/documents/%s" , s .bucketName , s .scopeName , s .collectionName , testKey )
171+
121172 eofDapiHooksContextID := "http-eof-test-case"
122- err = startInfo .HooksManager .CreateHooksContext (eofDapiHooksContextID )
123- require .NoError (s .T (), err , "failed to create http eof hooks context" )
173+ s .setupDapiKvGetBarrierHook (ctx , conn , eofDapiHooksContextID , docPath , "eof-barrier" )
124174
125175 // Check that requests to Data API taking longer than the shutdown timeout will eventually be forcibly closed.
126176 wg .Add (1 )
127177 eofDapiReqSent := make (chan any )
128178 go func () {
129179 defer wg .Done ()
130180
131- statement := []byte (fmt .Sprintf (`{"statement": "%s"}` , "SELECT 1 == 1" ))
132- reqBody := bytes .NewReader (statement )
133-
134- req , err := http .NewRequest ("POST" , fmt .Sprintf ("https://%s/_p/query/query/service" , dapiAddr ), reqBody )
181+ req , err := http .NewRequest ("GET" , fmt .Sprintf ("https://%s%s" , dapiAddr , docPath ), nil )
135182 require .NoError (s .T (), err , "failed to create http eof request" )
136183
137184 req .SetBasicAuth (testConfig .CbUser , testConfig .CbPass )
138- req .Header .Set ("Content-Type" , "application/json" )
139185 req .Header .Set ("X-Hooks-ID" , eofDapiHooksContextID )
140- req .Header .Set ("X-Barrier-ID" , "eof-barrier" )
141186
142187 eofDapiReqSent <- struct {}{}
143188 _ , err = dapiCli .Do (req )
@@ -148,25 +193,19 @@ func (s *GatewayOpsTestSuite) TestGracefulShutdown() {
148193 }()
149194
150195 successDapiHooksContextID := "http-success-test-case"
151- err = startInfo .HooksManager .CreateHooksContext (successDapiHooksContextID )
152- require .NoError (s .T (), err , "failed to create http success hooks context" )
196+ s .setupDapiKvGetBarrierHook (ctx , conn , successDapiHooksContextID , docPath , "success-barrier" )
153197
154198 // Check that requests that start before shutdown is called but take less than the shutdown timeout complete successfully.
155199 wg .Add (1 )
156200 successDapiReqSent := make (chan any )
157201 go func () {
158202 defer wg .Done ()
159203
160- statement := []byte (fmt .Sprintf (`{"statement": "%s"}` , "SELECT 1 == 1" ))
161- reqBody := bytes .NewReader (statement )
162-
163- req , err := http .NewRequest ("POST" , fmt .Sprintf ("https://%s/_p/query/query/service" , dapiAddr ), reqBody )
204+ req , err := http .NewRequest ("GET" , fmt .Sprintf ("https://%s%s" , dapiAddr , docPath ), nil )
164205 require .NoError (s .T (), err , "failed to create http success request" )
165206
166207 req .SetBasicAuth (testConfig .CbUser , testConfig .CbPass )
167- req .Header .Set ("Content-Type" , "application/json" )
168208 req .Header .Set ("X-Hooks-ID" , successDapiHooksContextID )
169- req .Header .Set ("X-Barrier-ID" , "success-barrier" )
170209
171210 successDapiReqSent <- struct {}{}
172211 resp , err := dapiCli .Do (req )
@@ -177,21 +216,6 @@ func (s *GatewayOpsTestSuite) TestGracefulShutdown() {
177216 _ = resp .Body .Close ()
178217 }()
179218
180- connAddr := fmt .Sprintf ("%s:%d" , "127.0.0.1" , startInfo .ServicePorts .PS )
181- conn , err := grpc .NewClient (connAddr ,
182- grpc .WithTransportCredentials (credentials .NewTLS (& tls.Config {InsecureSkipVerify : true })),
183- grpc .WithUserAgent ("test-client" ),
184- )
185- require .NoError (s .T (), err , "failed to create grpc client connection to gateway" )
186- defer func () { _ = conn .Close () }()
187-
188- testKey := s .testDocId ()
189-
190- // The call to setupKvGetBarrierHook can hang indefinitely if the hooks service is unreachable so we use a context with a
191- // generous timeout
192- ctx , cancel := context .WithTimeout (context .Background (), 120 * time .Second )
193- defer cancel ()
194-
195219 // Check that grpc requests that take longer than the shutdown timeout will eventually be forcibly closed.
196220 eofGrpcReqSent := make (chan any )
197221 wg .Add (1 )
0 commit comments