@@ -100,6 +100,12 @@ const v2_get_transaction_results_response = HTTP.Response(200, [
100100 " " ,
101101], " \r\n " ))
102102
103+ const v2_get_transaction_json_completed = """ {"id":"a3e3bc91-0a98-50ba-733c-0987e160eb7d","results_format_version":"2.0.1","state":"COMPLETED"}"""
104+ const v2_get_transaction_response_completed () = HTTP. Response (200 ,
105+ """
106+ {"transaction": $(v2_get_transaction_json_completed) }
107+ """ )
108+
103109const v2_fastpath_response = HTTP. Response (200 , [
104110 " Content-Type" => " Content-Type: multipart/form-data; boundary=8a89e52be8efe57f0b68ea75388314a3" ,
105111 " Transfer-Encoding" => " chunked" ,
@@ -111,7 +117,7 @@ const v2_fastpath_response = HTTP.Response(200, [
111117 " Content-Disposition: form-data; name=\" transaction\" ; filename=\"\" " ,
112118 " Content-Type: application/json" ,
113119 " " ,
114- """ {"id":"a3e3bc91-0a98-50ba-733c-0987e160eb7d","results_format_version":"2.0.1","state":"COMPLETED"} """ ,
120+ v2_get_transaction_json_completed ,
115121 " --8a89e52be8efe57f0b68ea75388314a3" ,
116122 " Content-Disposition: form-data; name=\" metadata.proto\" ; filename=\"\" " ,
117123 " Content-Type: application/x-protobuf" ,
@@ -208,14 +214,16 @@ end
208214end
209215
210216struct NetworkError code:: Int end
211- function make_fail_second_time_patch (first_response, fail_code)
217+ make_fail_after_second_time_patch (args... ) =
218+ make_fail_after_nth_time_patch (2 , args... )
219+ function make_fail_after_nth_time_patch (n, first_response, exception)
212220 request_idx = 0
213221 return (ctx:: Context , args... ; kw... ) -> begin
214222 request_idx += 1
215- if request_idx == 1
216- return first_response
223+ if request_idx >= n
224+ throw (exception)
217225 else
218- throw ( NetworkError (fail_code))
226+ return first_response
219227 end
220228 end
221229end
@@ -228,26 +236,49 @@ end
228236 @test_throws NetworkError (404 ) RAI. exec (ctx, " engine" , " db" , " 2+2" )
229237 end
230238
231- # Test for an error thrown _after_ the transaction is created, before it completes.
232- sync_error_patch = Mocking. Patch (RAI. request,
233- make_fail_second_time_patch (v2_async_response, 500 ))
239+ @testset " test that txn ID is logged for txn errors while polling" begin
240+ # Test for an error thrown _after_ the transaction is created, before it completes.
241+ sync_error_patch = Mocking. Patch (RAI. request,
242+ make_fail_after_second_time_patch (v2_async_response, NetworkError (500 )))
243+
244+ # See https://discourse.julialang.org/t/how-to-test-the-value-of-a-variable-from-info-log/37380/3
245+ # for an explanation of this logs-testing pattern.
246+ logs, _ = Test. collect_test_logs () do
247+ apply (sync_error_patch) do
248+ @test_throws NetworkError (500 ) RAI. exec (ctx, " engine" , " db" , " 2+2" )
249+ end
250+ end
251+ sym, val = collect (pairs (logs[1 ]. kwargs))[1 ]
252+ @test sym ≡ :transaction_id
253+ @test val == " 1fc9001b-1b88-8685-452e-c01bc6812429"
254+ end
234255
235- # See https://discourse.julialang.org/t/how-to-test-the-value-of-a-variable-from-info-log/37380/3
236- # for an explanation of this logs-testing pattern.
237- logs, _ = Test. collect_test_logs () do
238- apply (sync_error_patch) do
239- @test_throws NetworkError (500 ) RAI. exec (ctx, " engine" , " db" , " 2+2" )
256+ @testset " Handle Aborted Txns with no metadata" begin
257+ # Test for the _specific case_ of a 404 from the RelationalAI service, once the txn
258+ # completes.
259+
260+ # Attempt to wait until a txn is done. This will attempt to fetch the metadata &
261+ # results once it's finished.
262+ metadata_404_patch = Mocking. Patch (RAI. request,
263+ make_fail_after_second_time_patch (
264+ # get_transaction() returns a completed Transaction resource
265+ v2_get_transaction_response_completed (),
266+ # So then we attempt to fetch the metadata or results or problems, and error
267+ RAI. HTTPError (404 )
268+ )
269+ )
270+
271+ apply (metadata_404_patch) do
272+ RAI. wait_until_done (ctx, " <txn-id>" , start_time_ns= 0 )
240273 end
241274 end
242- sym, val = collect (pairs (logs[1 ]. kwargs))[1 ]
243- @test sym ≡ :transaction_id
244- @test val == " 1fc9001b-1b88-8685-452e-c01bc6812429"
275+
245276end
246277
247278@testset " exec with fast-path response only makes one request" begin
248279 # Throw an error if the SDK attempts to make two requests to RAI API:
249280 only_1_request_patch = Mocking. Patch (RAI. request,
250- make_fail_second_time_patch (v2_fastpath_response, 500 ))
281+ make_fail_after_second_time_patch (v2_fastpath_response, NetworkError ( 500 ) ))
251282
252283 ctx = Context (" region" , " scheme" , " host" , " 2342" , nothing , " audience" )
253284 apply (only_1_request_patch) do
0 commit comments