Skip to content

Commit

Permalink
fix(spanner): context timeout should be wrapped correctly (#7744)
Browse files Browse the repository at this point in the history
* fix(spanner): context timeout should be wrapped correctly

* add test
  • Loading branch information
rahul2393 committed Apr 17, 2023
1 parent 68b39df commit f8e22f6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion spanner/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,8 @@ func (c *Client) rwTransaction(ctx context.Context, f func(context.Context, *Rea
}
if t.shouldExplicitBegin(attempt) {
if err = t.begin(ctx); err != nil {
return spannerErrorf(codes.Internal, "error while BeginTransaction during retrying a ReadWrite transaction: %v", err)
trace.TracePrintf(ctx, nil, "Error while BeginTransaction during retrying a ReadWrite transaction: %v", ToSpannerError(err))
return ToSpannerError(err)
}
} else {
t = &ReadWriteTransaction{
Expand Down
35 changes: 35 additions & 0 deletions spanner/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1838,6 +1838,41 @@ func TestClient_ReadWriteTransaction_MultipleReadsWithoutNext(t *testing.T) {
}
}

func TestClient_ReadWriteTransaction_WithCancelledContext(t *testing.T) {
t.Parallel()
server, client, teardown := setupMockedTestServer(t)
defer teardown()
server.TestSpanner.AddPartialResultSetError(
SelectSingerIDAlbumIDAlbumTitleFromAlbums,
PartialResultSetExecutionTime{
ResumeToken: EncodeResumeToken(2),
Err: status.Errorf(codes.Internal, "stream terminated by RST_STREAM"),
},
)
ctx, cancel := context.WithCancel(context.Background())
_, err := client.ReadWriteTransaction(ctx, func(ctx context.Context, tx *ReadWriteTransaction) error {
iter := tx.Read(ctx, "Albums", KeySets(Key{"foo"}), []string{"SingerId", "AlbumId", "AlbumTitle"})
if _, err := iter.Next(); err != nil {
return err
}
return nil
})
if err != nil {
panic(err)
}
cancel()
_, err = client.ReadWriteTransaction(ctx, func(ctx context.Context, tx *ReadWriteTransaction) error {
iter := tx.Read(ctx, "Albums", KeySets(Key{"foo"}), []string{"SingerId", "AlbumId", "AlbumTitle"})
if _, err := iter.Next(); err != nil {
return err
}
return nil
})
if status.Code(err) != codes.Canceled {
t.Fatal(err)
}
}

func testReadWriteTransaction(t *testing.T, executionTimes map[string]SimulatedExecutionTime, expectedAttempts int) error {
return testReadWriteTransactionWithConfig(t, ClientConfig{SessionPoolConfig: DefaultSessionPoolConfig}, executionTimes, expectedAttempts)
}
Expand Down

0 comments on commit f8e22f6

Please sign in to comment.