func testTxPassNotInTransaction(t *testing.T, conn *vtgateconn.VTGateConn) { ctx := context.Background() execCase := execMap["txRequestNIT"] tx, err := conn.Begin(ctx) if err != nil { t.Error(err) } _, err = tx.Execute(ctx, execCase.execQuery.Sql, execCase.execQuery.BindVariables, execCase.execQuery.TabletType, true) if err != nil { t.Error(err) } _, err = tx.ExecuteShard(ctx, execCase.shardQuery.Sql, execCase.shardQuery.Keyspace, execCase.shardQuery.Shards, execCase.shardQuery.BindVariables, execCase.shardQuery.TabletType, true) if err != nil { t.Error(err) } _, err = tx.ExecuteKeyspaceIds(ctx, execCase.keyspaceIdQuery.Sql, execCase.keyspaceIdQuery.Keyspace, execCase.keyspaceIdQuery.KeyspaceIds, execCase.keyspaceIdQuery.BindVariables, execCase.keyspaceIdQuery.TabletType, true) if err != nil { t.Error(err) } _, err = tx.ExecuteKeyRanges(ctx, execCase.keyRangeQuery.Sql, execCase.keyRangeQuery.Keyspace, execCase.keyRangeQuery.KeyRanges, execCase.keyRangeQuery.BindVariables, execCase.keyRangeQuery.TabletType, true) if err != nil { t.Error(err) } _, err = tx.ExecuteEntityIds(ctx, execCase.entityIdsQuery.Sql, execCase.entityIdsQuery.Keyspace, execCase.entityIdsQuery.EntityColumnName, execCase.entityIdsQuery.EntityKeyspaceIDs, execCase.entityIdsQuery.BindVariables, execCase.entityIdsQuery.TabletType, true) if err != nil { t.Error(err) } _, err = tx.ExecuteBatchShard(ctx, execCase.batchQueryShard.Queries, execCase.batchQueryShard.Keyspace, execCase.batchQueryShard.Shards, execCase.batchQueryShard.TabletType, true) if err != nil { t.Error(err) } _, err = tx.ExecuteBatchKeyspaceIds(ctx, execCase.keyspaceIdBatchQuery.Queries, execCase.keyspaceIdBatchQuery.Keyspace, execCase.keyspaceIdBatchQuery.KeyspaceIds, execCase.keyspaceIdBatchQuery.TabletType, true) if err != nil { t.Error(err) } // no rollback necessary }
func checkTransactionExecuteErrors(t *testing.T, conn *vtgateconn.VTGateConn, execute func(tx *vtgateconn.VTGateTx, query string) error) { ctx := context.Background() for errStr, errCode := range executeErrors { query := errorPrefix + errStr tx, err := conn.Begin(ctx) if err != nil { t.Errorf("[%v] Begin error: %v", query, err) } checkError(t, execute(tx, query), query, errStr, errCode) // Partial error where server doesn't close the session. query = partialErrorPrefix + errStr tx, err = conn.Begin(ctx) if err != nil { t.Errorf("[%v] Begin error: %v", query, err) } checkError(t, execute(tx, query), query, errStr, errCode) // The transaction should still be usable now. if err := tx.Rollback(ctx); err != nil { t.Errorf("[%v] Rollback error: %v", query, err) } // Partial error where server closes the session. tx, err = conn.Begin(ctx) if err != nil { t.Errorf("[%v] Begin error: %v", query, err) } query = partialErrorPrefix + errStr + "/close transaction" checkError(t, execute(tx, query), query, errStr, errCode) // The transaction should be unusable now. if tx.Rollback(ctx) == nil { t.Errorf("[%v] expected Rollback error, got nil", query) } } }
func testEchoTransactionExecute(t *testing.T, conn *vtgateconn.VTGateConn) { var qr *mproto.QueryResult var err error ctx := callerid.NewContext(context.Background(), callerID, nil) tx, err := conn.Begin(ctx) if err != nil { t.Fatalf("Begin error: %v", err) } qr, err = tx.Execute(ctx, echoPrefix+query, bindVars, tabletType, true) checkEcho(t, "Execute", qr, err, map[string]string{ "callerId": callerIDEcho, "query": echoPrefix + query, "bindVars": bindVarsEcho, "tabletType": tabletTypeEcho, "session": sessionEcho, "notInTransaction": "true", }) qr, err = tx.ExecuteShards(ctx, echoPrefix+query, keyspace, shards, bindVars, tabletType, true) checkEcho(t, "ExecuteShards", qr, err, map[string]string{ "callerId": callerIDEcho, "query": echoPrefix + query, "keyspace": keyspace, "shards": shardsEcho, "bindVars": bindVarsEcho, "tabletType": tabletTypeEcho, "session": sessionEcho, "notInTransaction": "true", }) qr, err = tx.ExecuteKeyspaceIds(ctx, echoPrefix+query, keyspace, keyspaceIDs, bindVars, tabletType, true) checkEcho(t, "ExecuteKeyspaceIds", qr, err, map[string]string{ "callerId": callerIDEcho, "query": echoPrefix + query, "keyspace": keyspace, "keyspaceIds": keyspaceIDsEcho, "bindVars": bindVarsEcho, "tabletType": tabletTypeEcho, "session": sessionEcho, "notInTransaction": "true", }) qr, err = tx.ExecuteKeyRanges(ctx, echoPrefix+query, keyspace, keyRanges, bindVars, tabletType, true) checkEcho(t, "ExecuteKeyRanges", qr, err, map[string]string{ "callerId": callerIDEcho, "query": echoPrefix + query, "keyspace": keyspace, "keyRanges": keyRangesEcho, "bindVars": bindVarsEcho, "tabletType": tabletTypeEcho, "session": sessionEcho, "notInTransaction": "true", }) qr, err = tx.ExecuteEntityIds(ctx, echoPrefix+query, keyspace, "column1", entityKeyspaceIDs, bindVars, tabletType, true) checkEcho(t, "ExecuteEntityIds", qr, err, map[string]string{ "callerId": callerIDEcho, "query": echoPrefix + query, "keyspace": keyspace, "entityColumnName": "column1", "entityIds": entityKeyspaceIDsEcho, "bindVars": bindVarsEcho, "tabletType": tabletTypeEcho, "session": sessionEcho, "notInTransaction": "true", }) if err := tx.Rollback(ctx); err != nil { t.Fatalf("Rollback error: %v", err) } tx, err = conn.Begin(ctx) if err != nil { t.Fatalf("Begin (again) error: %v", err) } var qrs []mproto.QueryResult qrs, err = tx.ExecuteBatchShards(ctx, []gproto.BoundShardQuery{ gproto.BoundShardQuery{ Sql: echoPrefix + query, Keyspace: keyspace, Shards: shards, BindVariables: bindVars, }, }, tabletType, true) checkEcho(t, "ExecuteBatchShards", &qrs[0], err, map[string]string{ "callerId": callerIDEcho, "query": echoPrefix + query, "keyspace": keyspace, "shards": shardsEcho, "bindVars": bindVarsEcho, "tabletType": tabletTypeEcho, "session": sessionEcho, "asTransaction": "true", }) qrs, err = tx.ExecuteBatchKeyspaceIds(ctx, []gproto.BoundKeyspaceIdQuery{ gproto.BoundKeyspaceIdQuery{ Sql: echoPrefix + query, Keyspace: keyspace, KeyspaceIds: keyspaceIDs, BindVariables: bindVars, }, }, tabletType, true) checkEcho(t, "ExecuteBatchKeyspaceIds", &qrs[0], err, map[string]string{ "callerId": callerIDEcho, "query": echoPrefix + query, "keyspace": keyspace, "keyspaceIds": keyspaceIDsEcho, "bindVars": bindVarsEcho, "tabletType": tabletTypeEcho, "session": sessionEcho, "asTransaction": "true", }) }
func testTxFail(t *testing.T, conn *vtgateconn.VTGateConn) { ctx := context.Background() tx, err := conn.Begin(ctx) if err != nil { t.Error(err) } err = tx.Commit(ctx) want := "commit: session mismatch" if err == nil || !strings.Contains(err.Error(), want) { t.Errorf("Commit: %v, want %v", err, want) } _, err = tx.Execute(ctx, "", nil, "", false) want = "execute: not in transaction" if err == nil || err.Error() != want { t.Errorf("Execute: %v, want %v", err, want) } _, err = tx.ExecuteShard(ctx, "", "", nil, nil, "", false) want = "executeShard: not in transaction" if err == nil || err.Error() != want { t.Errorf("ExecuteShard: %v, want %v", err, want) } _, err = tx.ExecuteKeyspaceIds(ctx, "", "", nil, nil, "", false) want = "executeKeyspaceIds: not in transaction" if err == nil || err.Error() != want { t.Errorf("ExecuteShard: %v, want %v", err, want) } _, err = tx.ExecuteKeyRanges(ctx, "", "", nil, nil, "", false) want = "executeKeyRanges: not in transaction" if err == nil || err.Error() != want { t.Errorf("ExecuteShard: %v, want %v", err, want) } _, err = tx.ExecuteEntityIds(ctx, "", "", "", nil, nil, "", false) want = "executeEntityIds: not in transaction" if err == nil || err.Error() != want { t.Errorf("ExecuteShard: %v, want %v", err, want) } _, err = tx.ExecuteBatchShard(ctx, nil, "", nil, "", false) want = "executeBatchShard: not in transaction" if err == nil || err.Error() != want { t.Errorf("ExecuteShard: %v, want %v", err, want) } _, err = tx.ExecuteBatchKeyspaceIds(ctx, nil, "", nil, "", false) want = "executeBatchKeyspaceIds: not in transaction" if err == nil || err.Error() != want { t.Errorf("ExecuteShard: %v, want %v", err, want) } err = tx.Commit(ctx) want = "commit: not in transaction" if err == nil || err.Error() != want { t.Errorf("Commit: %v, want %v", err, want) } err = tx.Rollback(ctx) if err != nil { t.Error(err) } tx, err = conn.Begin(ctx) if err != nil { t.Error(err) } err = tx.Rollback(ctx) want = "rollback: session mismatch" if err == nil || !strings.Contains(err.Error(), want) { t.Errorf("Rollback: %v, want %v", err, want) } }
func testBeginPanic(t *testing.T, conn *vtgateconn.VTGateConn) { ctx := context.Background() _, err := conn.Begin(ctx) expectPanic(t, err) }
func testTxPass(t *testing.T, conn *vtgateconn.VTGateConn) { ctx := context.Background() execCase := execMap["txRequest"] // Execute tx, err := conn.Begin(ctx) if err != nil { t.Error(err) } _, err = tx.Execute(ctx, execCase.execQuery.Sql, execCase.execQuery.BindVariables, execCase.execQuery.TabletType, false) if err != nil { t.Error(err) } err = tx.Commit(ctx) if err != nil { t.Error(err) } // ExecuteShard tx, err = conn.Begin(ctx) if err != nil { t.Error(err) } _, err = tx.ExecuteShard(ctx, execCase.shardQuery.Sql, execCase.shardQuery.Keyspace, execCase.shardQuery.Shards, execCase.shardQuery.BindVariables, execCase.shardQuery.TabletType, false) if err != nil { t.Error(err) } err = tx.Rollback(ctx) if err != nil { t.Error(err) } // ExecuteKeyspaceIds tx, err = conn.Begin(ctx) if err != nil { t.Error(err) } _, err = tx.ExecuteKeyspaceIds(ctx, execCase.keyspaceIdQuery.Sql, execCase.keyspaceIdQuery.Keyspace, execCase.keyspaceIdQuery.KeyspaceIds, execCase.keyspaceIdQuery.BindVariables, execCase.keyspaceIdQuery.TabletType, false) if err != nil { t.Error(err) } err = tx.Rollback(ctx) if err != nil { t.Error(err) } // ExecuteKeyRanges tx, err = conn.Begin(ctx) if err != nil { t.Error(err) } _, err = tx.ExecuteKeyRanges(ctx, execCase.keyRangeQuery.Sql, execCase.keyRangeQuery.Keyspace, execCase.keyRangeQuery.KeyRanges, execCase.keyRangeQuery.BindVariables, execCase.keyRangeQuery.TabletType, false) if err != nil { t.Error(err) } err = tx.Rollback(ctx) if err != nil { t.Error(err) } // ExecuteEntityIds tx, err = conn.Begin(ctx) if err != nil { t.Error(err) } _, err = tx.ExecuteEntityIds(ctx, execCase.entityIdsQuery.Sql, execCase.entityIdsQuery.Keyspace, execCase.entityIdsQuery.EntityColumnName, execCase.entityIdsQuery.EntityKeyspaceIDs, execCase.entityIdsQuery.BindVariables, execCase.entityIdsQuery.TabletType, false) if err != nil { t.Error(err) } err = tx.Rollback(ctx) if err != nil { t.Error(err) } // ExecuteBatchShard tx, err = conn.Begin(ctx) if err != nil { t.Error(err) } _, err = tx.ExecuteBatchShard(ctx, execCase.batchQueryShard.Queries, execCase.batchQueryShard.Keyspace, execCase.batchQueryShard.Shards, execCase.batchQueryShard.TabletType, false) if err != nil { t.Error(err) } err = tx.Rollback(ctx) if err != nil { t.Error(err) } // ExecuteBatchKeyspaceIds tx, err = conn.Begin(ctx) if err != nil { t.Error(err) } _, err = tx.ExecuteBatchKeyspaceIds(ctx, execCase.keyspaceIdBatchQuery.Queries, execCase.keyspaceIdBatchQuery.Keyspace, execCase.keyspaceIdBatchQuery.KeyspaceIds, execCase.keyspaceIdBatchQuery.TabletType, false) if err != nil { t.Error(err) } err = tx.Rollback(ctx) if err != nil { t.Error(err) } }