@@ -1801,18 +1801,24 @@ func TestClient_ReadWriteTransaction(t *testing.T) {
1801
1801
}
1802
1802
}
1803
1803
1804
- func validateIsolationLevelForRWTransactions (t * testing.T , server * MockedSpannerInMemTestServer , expected sppb.TransactionOptions_IsolationLevel ) {
1804
+ func validateIsolationLevelForRWTransactions (t * testing.T , server * MockedSpannerInMemTestServer , expected sppb.TransactionOptions_IsolationLevel , beginTransactionOption BeginTransactionOption ) {
1805
1805
found := false
1806
1806
requests := drainRequestsFromServer (server .TestSpanner )
1807
1807
for _ , req := range requests {
1808
1808
switch sqlReq := req .(type ) {
1809
1809
case * sppb.ExecuteSqlRequest :
1810
+ if beginTransactionOption == ExplicitBeginTransaction {
1811
+ t .Fatalf ("got TransactionOptions on ExecuteSqlRequest in combination with ExplicitBeginTransaction" )
1812
+ }
1810
1813
found = true
1811
1814
if sqlReq .GetTransaction ().GetBegin ().GetIsolationLevel () != expected {
1812
1815
t .Fatalf ("Invalid IsolationLevel\n Expected: %v\n Got: %v\n " , expected , sqlReq .GetTransaction ().GetBegin ().GetIsolationLevel ())
1813
1816
}
1814
1817
break
1815
1818
case * sppb.BeginTransactionRequest :
1819
+ if beginTransactionOption == InlinedBeginTransaction {
1820
+ t .Fatalf ("got BeginTransaction RPC in combination with InlinedBeginTransaction" )
1821
+ }
1816
1822
found = true
1817
1823
if sqlReq .GetOptions ().GetIsolationLevel () != expected {
1818
1824
t .Fatalf ("Invalid IsolationLevel\n Expected: %v\n Got: %v\n " , expected , sqlReq .GetOptions ().GetIsolationLevel ())
@@ -1843,7 +1849,7 @@ func TestClient_ReadWriteTransactionWithNoIsolationLevelForRWTransactionAtClient
1843
1849
t .Fatal (err )
1844
1850
}
1845
1851
defer teardown ()
1846
- validateIsolationLevelForRWTransactions (t , server , sppb .TransactionOptions_ISOLATION_LEVEL_UNSPECIFIED )
1852
+ validateIsolationLevelForRWTransactions (t , server , sppb .TransactionOptions_ISOLATION_LEVEL_UNSPECIFIED , InlinedBeginTransaction )
1847
1853
}
1848
1854
1849
1855
func TestClient_ReadWriteTransactionWithIsolationLevelForRWTransactionAtClientConfig (t * testing.T ) {
@@ -1853,7 +1859,7 @@ func TestClient_ReadWriteTransactionWithIsolationLevelForRWTransactionAtClientCo
1853
1859
t .Fatal (err )
1854
1860
}
1855
1861
defer teardown ()
1856
- validateIsolationLevelForRWTransactions (t , server , sppb .TransactionOptions_REPEATABLE_READ )
1862
+ validateIsolationLevelForRWTransactions (t , server , sppb .TransactionOptions_REPEATABLE_READ , InlinedBeginTransaction )
1857
1863
}
1858
1864
1859
1865
func TestClient_ReadWriteTransactionWithIsolationLevelForRWTransactionAtTransactionLevel (t * testing.T ) {
@@ -1863,7 +1869,7 @@ func TestClient_ReadWriteTransactionWithIsolationLevelForRWTransactionAtTransact
1863
1869
t .Fatal (err )
1864
1870
}
1865
1871
defer teardown ()
1866
- validateIsolationLevelForRWTransactions (t , server , sppb .TransactionOptions_REPEATABLE_READ )
1872
+ validateIsolationLevelForRWTransactions (t , server , sppb .TransactionOptions_REPEATABLE_READ , InlinedBeginTransaction )
1867
1873
}
1868
1874
1869
1875
func TestClient_ReadWriteTransactionWithIsolationLevelForRWTransactionAtTransactionLevelWithAbort (t * testing.T ) {
@@ -1882,7 +1888,7 @@ func TestClient_ReadWriteTransactionWithIsolationLevelForRWTransactionAtTransact
1882
1888
if err != nil {
1883
1889
t .Fatal (err )
1884
1890
}
1885
- validateIsolationLevelForRWTransactions (t , server , sppb .TransactionOptions_REPEATABLE_READ )
1891
+ validateIsolationLevelForRWTransactions (t , server , sppb .TransactionOptions_REPEATABLE_READ , InlinedBeginTransaction )
1886
1892
}
1887
1893
1888
1894
func TestClient_ApplyMutationsWithAtLeastOnceIsolationLevel (t * testing.T ) {
@@ -1897,7 +1903,7 @@ func TestClient_ApplyMutationsWithAtLeastOnceIsolationLevel(t *testing.T) {
1897
1903
if err != nil {
1898
1904
t .Fatal (err )
1899
1905
}
1900
- validateIsolationLevelForRWTransactions (t , server , sppb .TransactionOptions_REPEATABLE_READ )
1906
+ validateIsolationLevelForRWTransactions (t , server , sppb .TransactionOptions_REPEATABLE_READ , ExplicitBeginTransaction )
1901
1907
}
1902
1908
1903
1909
func TestClient_ApplyMutationsWithIsolationLevel (t * testing.T ) {
@@ -1912,61 +1918,111 @@ func TestClient_ApplyMutationsWithIsolationLevel(t *testing.T) {
1912
1918
if err != nil {
1913
1919
t .Fatal (err )
1914
1920
}
1915
- validateIsolationLevelForRWTransactions (t , server , sppb .TransactionOptions_SERIALIZABLE )
1921
+ validateIsolationLevelForRWTransactions (t , server , sppb .TransactionOptions_SERIALIZABLE , ExplicitBeginTransaction )
1916
1922
}
1917
1923
1918
- func TestClient_ReadWriteStmtBasedTransactionWithIsolationLevelAtTransactionLevel (t * testing.T ) {
1924
+ func consumeIterator (iter * RowIterator ) error {
1925
+ defer iter .Stop ()
1926
+ for {
1927
+ _ , err := iter .Next ()
1928
+ if errors .Is (err , iterator .Done ) {
1929
+ break
1930
+ }
1931
+ if err != nil {
1932
+ return err
1933
+ }
1934
+ }
1935
+ return nil
1936
+ }
1937
+
1938
+ func TestClient_ReadWriteStmtBasedTransactionWithIsolationLevelAtTransactionLevelWithExplicitBegin (t * testing.T ) {
1939
+ t .Parallel ()
1940
+ testClientReadWriteStmtBasedTransactionWithIsolationLevelAtTransactionLevel (t , ExplicitBeginTransaction )
1941
+ }
1942
+
1943
+ func TestClient_ReadWriteStmtBasedTransactionWithIsolationLevelAtTransactionLevelWithInlineBegin (t * testing.T ) {
1944
+ t .Parallel ()
1945
+ testClientReadWriteStmtBasedTransactionWithIsolationLevelAtTransactionLevel (t , InlinedBeginTransaction )
1946
+ }
1947
+
1948
+ func testClientReadWriteStmtBasedTransactionWithIsolationLevelAtTransactionLevel (t * testing.T , beginTransactionOption BeginTransactionOption ) {
1919
1949
server , client , teardown := setupMockedTestServer (t )
1920
1950
defer teardown ()
1921
1951
ctx := context .Background ()
1922
1952
tx , err := NewReadWriteStmtBasedTransactionWithOptions (
1923
1953
ctx ,
1924
1954
client ,
1925
- TransactionOptions {IsolationLevel : sppb .TransactionOptions_REPEATABLE_READ })
1955
+ TransactionOptions {IsolationLevel : sppb .TransactionOptions_REPEATABLE_READ , BeginTransactionOption : beginTransactionOption })
1926
1956
if err != nil {
1927
1957
t .Fatalf ("Unexpected error when creating transaction: %v" , err )
1928
1958
}
1929
1959
1930
1960
iter := tx .Query (ctx , NewStatement (SelectSingerIDAlbumIDAlbumTitleFromAlbums ))
1931
- defer iter .Stop ()
1961
+ if err := consumeIterator (iter ); err != nil {
1962
+ t .Fatal (err )
1963
+ }
1964
+
1965
+ validateIsolationLevelForRWTransactions (t , server , sppb .TransactionOptions_REPEATABLE_READ , beginTransactionOption )
1966
+ }
1967
+
1968
+ func TestClient_ReadWriteStmtBasedTransactionWithIsolationLevelAtClientConfigLevelWithExplicitBegin (t * testing.T ) {
1969
+ t .Parallel ()
1970
+ testClientReadWriteStmtBasedTransactionWithIsolationLevelAtClientConfigLevel (t , ExplicitBeginTransaction )
1971
+ }
1932
1972
1933
- validateIsolationLevelForRWTransactions (t , server , sppb .TransactionOptions_REPEATABLE_READ )
1973
+ func TestClient_ReadWriteStmtBasedTransactionWithIsolationLevelAtClientConfigLevelWithInlineBegin (t * testing.T ) {
1974
+ t .Parallel ()
1975
+ testClientReadWriteStmtBasedTransactionWithIsolationLevelAtClientConfigLevel (t , InlinedBeginTransaction )
1934
1976
}
1935
1977
1936
- func TestClient_ReadWriteStmtBasedTransactionWithIsolationLevelAtClientConfigLevel (t * testing.T ) {
1978
+ func testClientReadWriteStmtBasedTransactionWithIsolationLevelAtClientConfigLevel (t * testing.T , beginTransactionOption BeginTransactionOption ) {
1937
1979
server , client , teardown := setupMockedTestServerWithConfig (t , ClientConfig {TransactionOptions : TransactionOptions {IsolationLevel : sppb .TransactionOptions_SERIALIZABLE }})
1938
1980
defer teardown ()
1939
1981
ctx := context .Background ()
1940
1982
tx , err := NewReadWriteStmtBasedTransactionWithOptions (
1941
1983
ctx ,
1942
1984
client ,
1943
- TransactionOptions {})
1985
+ TransactionOptions {BeginTransactionOption : beginTransactionOption })
1944
1986
if err != nil {
1945
1987
t .Fatalf ("Unexpected error when creating transaction: %v" , err )
1946
1988
}
1947
1989
1948
1990
iter := tx .Query (ctx , NewStatement (SelectSingerIDAlbumIDAlbumTitleFromAlbums ))
1949
- defer iter .Stop ()
1991
+ if err := consumeIterator (iter ); err != nil {
1992
+ t .Fatal (err )
1993
+ }
1994
+
1995
+ validateIsolationLevelForRWTransactions (t , server , sppb .TransactionOptions_SERIALIZABLE , beginTransactionOption )
1996
+ }
1997
+
1998
+ func TestClient_ReadWriteStmtBasedTransactionWithNoIsolationLevelWithExplicitBegin (t * testing.T ) {
1999
+ t .Parallel ()
2000
+ testClientReadWriteStmtBasedTransactionWithNoIsolationLevel (t , ExplicitBeginTransaction )
2001
+ }
1950
2002
1951
- validateIsolationLevelForRWTransactions (t , server , sppb .TransactionOptions_SERIALIZABLE )
2003
+ func TestClient_ReadWriteStmtBasedTransactionWithNoIsolationLevelWithInlineBegin (t * testing.T ) {
2004
+ t .Parallel ()
2005
+ testClientReadWriteStmtBasedTransactionWithNoIsolationLevel (t , InlinedBeginTransaction )
1952
2006
}
1953
2007
1954
- func TestClient_ReadWriteStmtBasedTransactionWithNoIsolationLevel (t * testing.T ) {
2008
+ func testClientReadWriteStmtBasedTransactionWithNoIsolationLevel (t * testing.T , beginTransactionOption BeginTransactionOption ) {
1955
2009
server , client , teardown := setupMockedTestServerWithConfig (t , ClientConfig {TransactionOptions : TransactionOptions {}})
1956
2010
defer teardown ()
1957
2011
ctx := context .Background ()
1958
2012
tx , err := NewReadWriteStmtBasedTransactionWithOptions (
1959
2013
ctx ,
1960
2014
client ,
1961
- TransactionOptions {})
2015
+ TransactionOptions {BeginTransactionOption : beginTransactionOption })
1962
2016
if err != nil {
1963
2017
t .Fatalf ("Unexpected error when creating transaction: %v" , err )
1964
2018
}
1965
2019
1966
2020
iter := tx .Query (ctx , NewStatement (SelectSingerIDAlbumIDAlbumTitleFromAlbums ))
1967
- defer iter .Stop ()
2021
+ if err := consumeIterator (iter ); err != nil {
2022
+ t .Fatal (err )
2023
+ }
1968
2024
1969
- validateIsolationLevelForRWTransactions (t , server , sppb .TransactionOptions_ISOLATION_LEVEL_UNSPECIFIED )
2025
+ validateIsolationLevelForRWTransactions (t , server , sppb .TransactionOptions_ISOLATION_LEVEL_UNSPECIFIED , beginTransactionOption )
1970
2026
}
1971
2027
1972
2028
func TestClient_ReadWriteTransactionCommitAborted (t * testing.T ) {
0 commit comments