@@ -6,10 +6,12 @@ import (
66 "fmt"
77 "reflect"
88 "testing"
9+ "time"
910
1011 "cloud.google.com/go/spanner"
1112 "cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
1213 sppb "cloud.google.com/go/spanner/apiv1/spannerpb"
14+ "github.com/google/uuid"
1315 "github.com/googleapis/go-sql-spanner/testutil"
1416 "google.golang.org/api/option"
1517 "google.golang.org/grpc/codes"
@@ -58,6 +60,87 @@ func TestSimpleQuery(t *testing.T) {
5860 ClosePool (pool .ObjectId )
5961}
6062
63+ func generateLargeResultSet (numRows int ) * sppb.ResultSet {
64+ res := & sppb.ResultSet {
65+ Metadata : & sppb.ResultSetMetadata {
66+ RowType : & sppb.StructType {
67+ Fields : []* sppb.StructType_Field {
68+ {Type : & sppb.Type {Code : sppb .TypeCode_STRING }, Name : "col1" },
69+ {Type : & sppb.Type {Code : sppb .TypeCode_STRING }, Name : "col2" },
70+ {Type : & sppb.Type {Code : sppb .TypeCode_STRING }, Name : "col3" },
71+ {Type : & sppb.Type {Code : sppb .TypeCode_STRING }, Name : "col4" },
72+ {Type : & sppb.Type {Code : sppb .TypeCode_STRING }, Name : "col5" },
73+ },
74+ },
75+ },
76+ }
77+ rows := make ([]* structpb.ListValue , 0 , numRows )
78+ for i := 0 ; i < numRows ; i ++ {
79+ values := make ([]* structpb.Value , 0 , 5 )
80+ for j := 0 ; j < 5 ; j ++ {
81+ values = append (values , & structpb.Value {
82+ Kind : & structpb.Value_StringValue {StringValue : uuid .New ().String ()},
83+ })
84+ }
85+ rows = append (rows , & structpb.ListValue {
86+ Values : values ,
87+ })
88+ }
89+ res .Rows = rows
90+ return res
91+ }
92+
93+ func TestLargeQuery (t * testing.T ) {
94+ t .Skip ("only for manual testing" )
95+ t .Parallel ()
96+
97+ dsn , server , teardown := setupTestDBConnection (t )
98+ defer teardown ()
99+
100+ _ = server .TestSpanner .PutStatementResult ("select * from all_types" , & testutil.StatementResult {
101+ Type : testutil .StatementResultResultSet ,
102+ ResultSet : generateLargeResultSet (1000000 ),
103+ })
104+
105+ pool := CreatePool (dsn )
106+ conn := CreateConnection (pool .ObjectId )
107+ statement := sppb.ExecuteSqlRequest {
108+ Sql : "select * from all_types" ,
109+ }
110+ statementBytes , err := proto .Marshal (& statement )
111+ if err != nil {
112+ t .Fatalf ("failed to marshal statement: %v" , err )
113+ }
114+
115+ startTime := time .Now ()
116+ results := Execute (pool .ObjectId , conn .ObjectId , statementBytes )
117+ metadata := Metadata (pool .ObjectId , conn .ObjectId , results .ObjectId )
118+ if metadata .Code != 0 {
119+ t .Fatalf ("metadata.Code: %v" , metadata .Code )
120+ }
121+ for {
122+ row := Next (pool .ObjectId , conn .ObjectId , results .ObjectId )
123+ if row .Length () == 0 {
124+ break
125+ }
126+ values := structpb.ListValue {}
127+ if err := proto .Unmarshal (row .Res , & values ); err != nil {
128+ t .Fatalf ("failed to unmarshal row: %v" , err )
129+ }
130+ }
131+ stats := ResultSetStats (pool .ObjectId , conn .ObjectId , results .ObjectId )
132+ if stats .Code != 0 {
133+ t .Fatalf ("stats.Code: %v" , stats .Code )
134+ }
135+ CloseRows (pool .ObjectId , conn .ObjectId , results .ObjectId )
136+ endTime := time .Now ()
137+
138+ fmt .Printf ("Query took %v\n " , endTime .Sub (startTime ))
139+
140+ CloseConnection (pool .ObjectId , conn .ObjectId )
141+ ClosePool (pool .ObjectId )
142+ }
143+
61144func TestQueryWithTimestampBound (t * testing.T ) {
62145 t .Parallel ()
63146
@@ -285,18 +368,11 @@ func TestBufferWrite(t *testing.T) {
285368 }
286369
287370 requests := drainRequestsFromServer (server .TestSpanner )
371+ // There should not be any BeginTransaction requests yet, as we use inlined-begin.
288372 beginRequests := requestsOfType (requests , reflect .TypeOf (& sppb.BeginTransactionRequest {}))
289- if g , w := len (beginRequests ), 1 ; g != w {
373+ if g , w := len (beginRequests ), 0 ; g != w {
290374 t .Fatalf ("begin requests count mismatch\n Got: %v\n Want: %v" , g , w )
291375 }
292- req := beginRequests [0 ].(* sppb.BeginTransactionRequest )
293- if req .Options == nil {
294- t .Fatalf ("missing tx opts" )
295- }
296- if req .Options .GetReadWrite () == nil {
297- t .Fatalf ("missing tx read write" )
298- }
299-
300376 // There should not be any commit requests yet.
301377 commitRequests := requestsOfType (requests , reflect .TypeOf (& sppb.CommitRequest {}))
302378 if g , w := len (commitRequests ), 0 ; g != w {
@@ -311,6 +387,17 @@ func TestBufferWrite(t *testing.T) {
311387
312388 // Verify that we have a commit request on the server.
313389 requests = drainRequestsFromServer (server .TestSpanner )
390+ beginRequests = requestsOfType (requests , reflect .TypeOf (& sppb.BeginTransactionRequest {}))
391+ if g , w := len (beginRequests ), 1 ; g != w {
392+ t .Fatalf ("begin requests count mismatch\n Got: %v\n Want: %v" , g , w )
393+ }
394+ req := beginRequests [0 ].(* sppb.BeginTransactionRequest )
395+ if req .Options == nil {
396+ t .Fatalf ("missing tx opts" )
397+ }
398+ if req .Options .GetReadWrite () == nil {
399+ t .Fatalf ("missing tx read write" )
400+ }
314401 commitRequests = requestsOfType (requests , reflect .TypeOf (& sppb.CommitRequest {}))
315402 if g , w := len (commitRequests ), 1 ; g != w {
316403 t .Fatalf ("commit requests count mismatch\n Got: %v\n Want: %v" , g , w )
@@ -368,18 +455,6 @@ func TestBufferWrite_RetryAborted(t *testing.T) {
368455 }
369456
370457 requests := drainRequestsFromServer (server .TestSpanner )
371- beginRequests := requestsOfType (requests , reflect .TypeOf (& sppb.BeginTransactionRequest {}))
372- if g , w := len (beginRequests ), 1 ; g != w {
373- t .Fatalf ("begin requests count mismatch\n Got: %v\n Want: %v" , g , w )
374- }
375- req := beginRequests [0 ].(* sppb.BeginTransactionRequest )
376- if req .Options == nil {
377- t .Fatalf ("missing tx opts" )
378- }
379- if req .Options .GetReadWrite () == nil {
380- t .Fatalf ("missing tx read write" )
381- }
382-
383458 // There should not be any commit requests yet.
384459 commitRequests := requestsOfType (requests , reflect .TypeOf (& sppb.CommitRequest {}))
385460 if g , w := len (commitRequests ), 0 ; g != w {
@@ -397,8 +472,21 @@ func TestBufferWrite_RetryAborted(t *testing.T) {
397472 t .Fatalf ("failed to commit: %v" , res .Code )
398473 }
399474
400- // Verify that we have a commit request on the server.
475+ // Verify that we have both begin and commit requests on the server.
401476 requests = drainRequestsFromServer (server .TestSpanner )
477+ beginRequests := requestsOfType (requests , reflect .TypeOf (& sppb.BeginTransactionRequest {}))
478+ if g , w := len (beginRequests ), 2 ; g != w {
479+ t .Fatalf ("begin requests count mismatch\n Got: %v\n Want: %v" , g , w )
480+ }
481+ for _ , beginReq := range beginRequests {
482+ req := beginReq .(* sppb.BeginTransactionRequest )
483+ if req .Options == nil {
484+ t .Fatalf ("missing tx opts" )
485+ }
486+ if req .Options .GetReadWrite () == nil {
487+ t .Fatalf ("missing tx read write" )
488+ }
489+ }
402490 commitRequests = requestsOfType (requests , reflect .TypeOf (& sppb.CommitRequest {}))
403491 if g , w := len (commitRequests ), 2 ; g != w {
404492 t .Fatalf ("commit requests count mismatch\n Got: %v\n Want: %v" , g , w )
0 commit comments