github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/tests/integration/database_sql_with_tx_control_test.go (about) 1 //go:build integration 2 // +build integration 3 4 package integration 5 6 import ( 7 "context" 8 "database/sql" 9 "testing" 10 11 "github.com/stretchr/testify/require" 12 13 "github.com/ydb-platform/ydb-go-sdk/v3" 14 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xsql" 15 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest" 16 "github.com/ydb-platform/ydb-go-sdk/v3/retry" 17 "github.com/ydb-platform/ydb-go-sdk/v3/table" 18 ) 19 20 func TestDatabaseSqlWithTxControl(t *testing.T) { 21 var ( 22 ctx = xtest.Context(t) 23 scope = newScope(t) 24 db = scope.SQLDriverWithFolder( 25 ydb.WithTablePathPrefix(scope.Folder()), 26 ydb.WithAutoDeclare(), 27 ) 28 ) 29 30 t.Run("default", func(t *testing.T) { 31 var hookCalled bool 32 require.NoError(t, retry.Do( 33 ydb.WithTxControl( 34 xsql.WithTxControlHook(ctx, func(txControl *table.TransactionControl) { 35 hookCalled = true 36 require.Equal(t, table.SerializableReadWriteTxControl().Desc(), txControl.Desc()) 37 }), 38 table.SerializableReadWriteTxControl(), 39 ), 40 db, func(ctx context.Context, cc *sql.Conn) error { 41 _, err := db.QueryContext(ctx, "SELECT 1") 42 return err 43 }, 44 )) 45 require.True(t, hookCalled) 46 }) 47 48 t.Run("SerializableReadWriteTxControl", func(t *testing.T) { 49 var hookCalled bool 50 require.NoError(t, retry.Do( 51 ydb.WithTxControl( 52 xsql.WithTxControlHook(ctx, func(txControl *table.TransactionControl) { 53 hookCalled = true 54 require.Equal(t, table.SerializableReadWriteTxControl().Desc(), txControl.Desc()) 55 }), 56 table.SerializableReadWriteTxControl(), 57 ), 58 db, func(ctx context.Context, cc *sql.Conn) error { 59 _, err := db.QueryContext(ctx, "SELECT 1") 60 return err 61 }, 62 )) 63 require.True(t, hookCalled) 64 }) 65 66 t.Run("SnapshotReadOnlyTxControl", func(t *testing.T) { 67 var hookCalled bool 68 require.NoError(t, retry.Do( 69 ydb.WithTxControl( 70 xsql.WithTxControlHook(ctx, func(txControl *table.TransactionControl) { 71 hookCalled = true 72 require.Equal(t, table.SnapshotReadOnlyTxControl().Desc(), txControl.Desc()) 73 }), 74 table.SnapshotReadOnlyTxControl(), 75 ), 76 db, func(ctx context.Context, cc *sql.Conn) error { 77 _, err := db.QueryContext(ctx, "SELECT 1") 78 return err 79 }, 80 )) 81 require.True(t, hookCalled) 82 }) 83 84 t.Run("StaleReadOnlyTxControl", func(t *testing.T) { 85 var hookCalled bool 86 require.NoError(t, retry.Do( 87 ydb.WithTxControl( 88 xsql.WithTxControlHook(ctx, func(txControl *table.TransactionControl) { 89 hookCalled = true 90 require.Equal(t, table.StaleReadOnlyTxControl().Desc(), txControl.Desc()) 91 }), 92 table.StaleReadOnlyTxControl(), 93 ), 94 db, func(ctx context.Context, cc *sql.Conn) error { 95 _, err := db.QueryContext(ctx, "SELECT 1") 96 return err 97 }, 98 )) 99 require.True(t, hookCalled) 100 }) 101 102 t.Run("OnlineReadOnlyTxControl{AllowInconsistentReads:false}", func(t *testing.T) { 103 var hookCalled bool 104 require.NoError(t, retry.Do( 105 ydb.WithTxControl( 106 xsql.WithTxControlHook(ctx, func(txControl *table.TransactionControl) { 107 hookCalled = true 108 require.Equal(t, table.OnlineReadOnlyTxControl().Desc(), txControl.Desc()) 109 }), 110 table.OnlineReadOnlyTxControl(), 111 ), 112 db, func(ctx context.Context, cc *sql.Conn) error { 113 _, err := db.QueryContext(ctx, "SELECT 1") 114 return err 115 }, 116 )) 117 require.True(t, hookCalled) 118 }) 119 120 t.Run("OnlineReadOnlyTxControl{AllowInconsistentReads:true})", func(t *testing.T) { 121 var hookCalled bool 122 require.NoError(t, retry.Do( 123 ydb.WithTxControl( 124 xsql.WithTxControlHook(ctx, func(txControl *table.TransactionControl) { 125 hookCalled = true 126 require.Equal(t, table.OnlineReadOnlyTxControl(table.WithInconsistentReads()).Desc(), txControl.Desc()) 127 }), 128 table.OnlineReadOnlyTxControl(table.WithInconsistentReads()), 129 ), 130 db, func(ctx context.Context, cc *sql.Conn) error { 131 _, err := db.QueryContext(ctx, "SELECT 1") 132 return err 133 }, 134 )) 135 require.True(t, hookCalled) 136 }) 137 }