Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add popTxs test #61

Merged
merged 5 commits into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8
github.com/go-kit/kit v0.10.0 // indirect
github.com/gogo/protobuf v1.3.1 // indirect
github.com/google/go-cmp v0.4.0 // indirect
github.com/golang/protobuf v1.3.3 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/gorilla/mux v1.7.3
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
Expand All @@ -33,12 +33,11 @@ require (
github.com/status-im/keycard-go v0.0.0-20191119114148-6dd40a46baa0 // indirect
github.com/stretchr/testify v1.5.1
github.com/t-tiger/gorm-bulk-insert v1.3.0
github.com/tendermint/go-amino v0.15.1 // indirect
github.com/tendermint/tendermint v0.32.7
github.com/tyler-smith/go-bip39 v1.0.2 // indirect
github.com/willf/pad v0.0.0-20200313202418-172aa767f2a4
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
google.golang.org/grpc v1.28.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
gorm.io/driver/sqlite v1.1.3
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.0.1 h1:HjfetcXq097iXP0uoPCdnM4Efp5/9MsM0/M+XOTeR3M=
github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jinzhu/now v1.1.1 h1:g39TucaRWyV3dwDO++eEc6qf8TVIQ/Da48WmqjZ3i7E=
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jmhodges/levigo v1.0.0/go.mod h1:Q6Qx+uH3RAqyK4rFQroq9RL7mdkABMcfhEI+nNuzMJQ=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
Expand Down Expand Up @@ -325,6 +327,7 @@ github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzp
github.com/mattn/go-runewidth v0.0.6 h1:V2iyH+aX9C5fsYCpK60U8BYIvmhqxuOL3JZcqc1NB7k=
github.com/mattn/go-runewidth v0.0.6/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v1.14.3/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
github.com/mattn/go-sqlite3 v2.0.1+incompatible h1:xQ15muvnzGBHpIpdrNi1DA5x0+TcBZzsIDwmw9uTHzw=
github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
Expand Down Expand Up @@ -712,6 +715,10 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gorm.io/driver/sqlite v1.1.3 h1:BYfdVuZB5He/u9dt4qDpZqiqDJ6KhPqs5QUqsr/Eeuc=
gorm.io/driver/sqlite v1.1.3/go.mod h1:AKDgRWk8lcSQSw+9kxCJnX/yySj8G3rdwYlU57cB45c=
gorm.io/gorm v1.20.1 h1:+hOwlHDqvqmBIMflemMVPLJH7tZYK4RxFDBHEfJTup0=
gorm.io/gorm v1.20.1/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
79 changes: 79 additions & 0 deletions tests/tx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package tests_test
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to a tests/ package, because Golang complains a circular dependency between migration/ and core.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, these are anyways exported functions.


import (
"io/ioutil"
"os"
"testing"

"github.com/BOPR/common"
"github.com/BOPR/config"
"github.com/BOPR/core"
"github.com/BOPR/migrations"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/stretchr/testify/assert"
)

func setupDB() (db core.DB, cleanup func(), err error) {
tmpfile, err := ioutil.TempFile("", "test.*.db")
println(tmpfile.Name())
if err != nil {
return
}

sqliteDb, err := gorm.Open("sqlite3", tmpfile.Name())
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I eventually use SQLite because the setup is much easier than installing another docker instance in CI.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very nice!

if err != nil {
return
}
logger := common.Logger.With("module", "tests")
db = core.DB{Instance: sqliteDb, Logger: logger}

allMigrations := migrations.GetMigrations()
m := migrations.NewGormigrate(db.Instance, migrations.DefaultOptions, allMigrations)
m.Migrate()
cleanup = func() {
println("Closing DB and removing file")
db.Close()
os.Remove(tmpfile.Name())
}

return db, cleanup, nil

}

func TestPopTx(t *testing.T) {
db, cleanup, err := setupDB()
if err != nil {
t.Errorf("setupDB error %s", err)
}
defer cleanup()

println("Testing start")
var txType uint64 = 1
config.GlobalCfg.TxsPerBatch = 2
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have to override this global variable to work.


tx1 := core.NewTx(1, 2, txType, []byte{00}, []byte{00})
tx2 := core.NewPendingTx(1, 2, txType, []byte{00}, []byte{01})
tx3 := core.NewPendingTx(1, 2, txType, []byte{00}, []byte{02})

if err = db.InsertTx(&tx1); err != nil {
t.Errorf("PopTxs error %s", err)
}
if err = db.InsertTx(&tx2); err != nil {
t.Errorf("PopTxs error %s", err)
}
if err = db.InsertTx(&tx3); err != nil {
t.Errorf("PopTxs error %s", err)
}

fetchedTxType, err := db.FetchTxType()
assert.Equal(t, txType, fetchedTxType)

txs, err := db.PopTxs()
if err != nil {
t.Errorf("PopTxs error %s", err)
}
for i, tx := range []core.Tx{tx2, tx3} {
assert.Equal(t, tx.TxHash, txs[i].TxHash)
}
Comment on lines +73 to +75
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really want to do assert.Equal(t, []core.Tx{tx2, tx3}, txs) here, because that allows us to have a whole view on the difference of the set of transactions.
Looping through each tx and comparing their hashes is more difficult to debug. I'm doing so because the Tx struct has a DBModel field, that makes the CreatedAt value different between []core.Tx{tx2, tx3} and txs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I think we should have something like TxList that has a .equal() and some other sorting, popping operations, similar to how geth does it.

What do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have something like TxList means we are doing pending Txs in memory, not in disk/database? Let's move the discussion here #63

}