vendor: revendor
This commit is contained in:
vendor
github.com
Sirupsen
logrus
cockroachdb
cockroach-go
crdb
coreos
go-oidc
ghodss
go-sql-driver
mysql
golang
protobuf
gorilla
context
mux
gtank
inconshreveable
mousetrap
kylelemons
godebug
lib
pq
mattn
go-sqlite3
pquerna
cachecontrol
spf13
golang.org
x
crypto
net
context
http2
.gitignoreDockerfileMakefileREADMEerrors_test.gofixed_buffer_test.goflow_test.goframe_test.gogotrack_test.go
hpack
http2_test.gopipe_test.gopriority_test.goserver_test.gotransport_test.goz_spec_test.gointernal
timeseries
lex
httplex
trace
oauth2
.travis.ymlAUTHORSCONTRIBUTING.mdCONTRIBUTORSREADME.mdexample_test.go
internal
oauth2_test.gotoken_test.gotransport_test.gosys
unix
.gitignorecreds_test.goexport_test.gomkall.shmkerrors.shmksyscall.plmksyscall_solaris.plmksysctl_openbsd.plmksysnum_darwin.plmksysnum_dragonfly.plmksysnum_freebsd.plmksysnum_linux.plmksysnum_netbsd.plmksysnum_openbsd.plmmap_unix_test.gosyscall_bsd_test.gosyscall_freebsd_test.gosyscall_test.gosyscall_unix_test.go
google.golang.org
appengine
appengine.goappengine_vm.goerrors.goidentity.go
internal
api_race_test.goapi_test.goapp_id_test.go
namespace.gotimeout.gobase
datastore
internal_test.golog
net_test.goregen.shremote_api
urlfetch
grpc
gopkg.in
asn1-ber.v1
ldap.v2
.gitignore.travis.ymlREADME.mdconn_test.godn_test.goerror_test.goexample_test.gofilter_test.goldap_test.gosearch_test.go
square
go-jose.v2
.gitcookies.sh.enc.gitignore.travis.ymlBUG-BOUNTY.mdCONTRIBUTING.mdREADME.mdasymmetric_test.go
cipher
crypter_test.godoc_test.goencoding_test.gojson
README.mdbench_test.godecode_test.goencode_test.gonumber_test.goscanner_test.gostream_test.gotagkey_test.gotags_test.go
jwe_test.gojwk_test.gojws_test.gosigning_test.gosymmetric_test.goutils_test.goyaml.v2
125
vendor/github.com/cockroachdb/cockroach-go/crdb/tx_test.go
generated
vendored
125
vendor/github.com/cockroachdb/cockroach-go/crdb/tx_test.go
generated
vendored
@@ -1,125 +0,0 @@
|
||||
// Copyright 2016 The Cockroach Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
// implied. See the License for the specific language governing
|
||||
// permissions and limitations under the License.
|
||||
//
|
||||
// Author: Spencer Kimball (spencer@cockroachlabs.com)
|
||||
|
||||
package crdb
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/cockroachdb/cockroach-go/testserver"
|
||||
)
|
||||
|
||||
// TestExecuteTx verifies transaction retry using the classic
|
||||
// example of write skew in bank account balance transfers.
|
||||
func TestExecuteTx(t *testing.T) {
|
||||
db, stop := testserver.NewDBForTest(t)
|
||||
defer stop()
|
||||
|
||||
initStmt := `
|
||||
CREATE DATABASE d;
|
||||
CREATE TABLE d.t (acct INT PRIMARY KEY, balance INT);
|
||||
INSERT INTO d.t (acct, balance) VALUES (1, 100), (2, 100);
|
||||
`
|
||||
if _, err := db.Exec(initStmt); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
type queryI interface {
|
||||
Query(string, ...interface{}) (*sql.Rows, error)
|
||||
}
|
||||
|
||||
getBalances := func(q queryI) (bal1, bal2 int, err error) {
|
||||
var rows *sql.Rows
|
||||
rows, err = q.Query(`SELECT balance FROM d.t WHERE acct IN (1, 2);`)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
balances := []*int{&bal1, &bal2}
|
||||
i := 0
|
||||
for ; rows.Next(); i += 1 {
|
||||
if err = rows.Scan(balances[i]); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
if i != 2 {
|
||||
err = fmt.Errorf("expected two balances; got %d", i)
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
runTxn := func(wg *sync.WaitGroup, iter *int) <-chan error {
|
||||
errCh := make(chan error, 1)
|
||||
go func() {
|
||||
*iter = 0
|
||||
errCh <- ExecuteTx(db, func(tx *sql.Tx) error {
|
||||
*iter++
|
||||
bal1, bal2, err := getBalances(tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// If this is the first iteration, wait for the other tx to also read.
|
||||
if *iter == 1 {
|
||||
wg.Done()
|
||||
wg.Wait()
|
||||
}
|
||||
// Now, subtract from one account and give to the other.
|
||||
if bal1 > bal2 {
|
||||
if _, err := tx.Exec(`
|
||||
UPDATE d.t SET balance=balance-100 WHERE acct=1;
|
||||
UPDATE d.t SET balance=balance+100 WHERE acct=2;
|
||||
`); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if _, err := tx.Exec(`
|
||||
UPDATE d.t SET balance=balance+100 WHERE acct=1;
|
||||
UPDATE d.t SET balance=balance-100 WHERE acct=2;
|
||||
`); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}()
|
||||
return errCh
|
||||
}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(2)
|
||||
var iters1, iters2 int
|
||||
txn1Err := runTxn(&wg, &iters1)
|
||||
txn2Err := runTxn(&wg, &iters2)
|
||||
if err := <-txn1Err; err != nil {
|
||||
t.Errorf("expected success in txn1; got %s", err)
|
||||
}
|
||||
if err := <-txn2Err; err != nil {
|
||||
t.Errorf("expected success in txn2; got %s", err)
|
||||
}
|
||||
if iters1+iters2 <= 2 {
|
||||
t.Errorf("expected at least one retry between the competing transactions; "+
|
||||
"got txn1=%d, txn2=%d", iters1, iters2)
|
||||
}
|
||||
bal1, bal2, err := getBalances(db)
|
||||
if err != nil || bal1 != 100 || bal2 != 100 {
|
||||
t.Errorf("expected balances to be restored without error; "+
|
||||
"got acct1=%d, acct2=%d: %s", bal1, bal2, err)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user