Refactor DB observer to depend on repository

This commit is contained in:
Eric Meyer 2017-11-06 10:52:07 -06:00
parent 26b21e8eea
commit 24a8fcbadb
7 changed files with 28 additions and 35 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/8thlight/vulcanizedb/core"
"github.com/8thlight/vulcanizedb/geth"
"github.com/8thlight/vulcanizedb/observers"
"github.com/8thlight/vulcanizedb/repositories"
"github.com/jmoiron/sqlx"
do "gopkg.in/godo.v2"
)
@ -31,7 +32,8 @@ func startBlockchainListener(cfg config.Config) {
if err != nil {
log.Fatalf("Error connecting to DB: %v\n", err)
}
dbObserver := (observers.BlockchainDBObserver{Db: db})
repository := repositories.NewPostgres(db)
dbObserver := observers.NewBlockchainDbObserver(repository)
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{
loggingObserver,
dbObserver,

View File

@ -11,7 +11,7 @@ import (
var _ = Describe("Blockchain listeners", func() {
It("starts with no blocks", func(done Done) {
observer := fakes.NewFakeBlockchainObserverTwo()
observer := fakes.NewFakeBlockchainObserver()
blockchain := &fakes.Blockchain{}
blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
@ -21,7 +21,7 @@ var _ = Describe("Blockchain listeners", func() {
}, 1)
It("sees when one block was added", func(done Done) {
observer := fakes.NewFakeBlockchainObserverTwo()
observer := fakes.NewFakeBlockchainObserver()
blockchain := &fakes.Blockchain{}
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
go listener.Start()
@ -37,7 +37,7 @@ var _ = Describe("Blockchain listeners", func() {
}, 1)
It("sees a second block", func(done Done) {
observer := fakes.NewFakeBlockchainObserverTwo()
observer := fakes.NewFakeBlockchainObserver()
blockchain := &fakes.Blockchain{}
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
go listener.Start()
@ -55,7 +55,7 @@ var _ = Describe("Blockchain listeners", func() {
}, 1)
It("stops listening", func(done Done) {
observer := fakes.NewFakeBlockchainObserverTwo()
observer := fakes.NewFakeBlockchainObserver()
blockchain := &fakes.Blockchain{}
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{observer})
go listener.Start()

View File

@ -11,7 +11,7 @@ func (observer *BlockchainObserver) LastBlock() core.Block {
return observer.CurrentBlocks[len(observer.CurrentBlocks)-1]
}
func NewFakeBlockchainObserverTwo() *BlockchainObserver {
func NewFakeBlockchainObserver() *BlockchainObserver {
return &BlockchainObserver{
WasNotified: make(chan bool),
}

View File

@ -26,7 +26,7 @@ var _ = Describe("Reading from the Geth blockchain", func() {
var observer *fakes.BlockchainObserver
BeforeEach(func() {
observer = fakes.NewFakeBlockchainObserverTwo()
observer = fakes.NewFakeBlockchainObserver()
blockchain := geth.NewGethBlockchain(RunTimePath() + "/test_data_dir/geth.ipc")
observers := []core.BlockchainObserver{observer}
listener = blockchain_listener.NewBlockchainListener(blockchain, observers)

View File

@ -3,13 +3,16 @@ package observers
import (
"github.com/8thlight/vulcanizedb/core"
"github.com/8thlight/vulcanizedb/repositories"
"github.com/jmoiron/sqlx"
)
type BlockchainDBObserver struct {
Db *sqlx.DB
type BlockchainDbObserver struct {
repository repositories.Repository
}
func (observer BlockchainDBObserver) NotifyBlockAdded(block core.Block) {
repositories.NewPostgres(observer.Db).CreateBlock(block)
func NewBlockchainDbObserver(repository repositories.Repository) BlockchainDbObserver {
return BlockchainDbObserver{repository: repository}
}
func (observer BlockchainDbObserver) NotifyBlockAdded(block core.Block) {
observer.repository.CreateBlock(block)
}

View File

@ -1,41 +1,23 @@
package observers_test
import (
"runtime"
"github.com/8thlight/vulcanizedb/config"
"github.com/8thlight/vulcanizedb/core"
"github.com/8thlight/vulcanizedb/observers"
"github.com/8thlight/vulcanizedb/repositories"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var (
_, filename, _, _ = runtime.Caller(0)
)
var _ = Describe("Saving blocks to the database", func() {
var db *sqlx.DB
var err error
var repository *repositories.InMemory
BeforeEach(func() {
cfg := config.NewConfig("private")
pgConfig := config.DbConnectionString(cfg.Database)
db, err = sqlx.Connect("postgres", pgConfig)
db.MustExec("DELETE FROM transactions")
db.MustExec("DELETE FROM blocks")
})
AfterEach(func() {
db.Close()
repository = repositories.NewInMemory()
})
It("implements the observer interface", func() {
var observer core.BlockchainObserver = observers.BlockchainDBObserver{Db: db}
var observer core.BlockchainObserver = observers.NewBlockchainDbObserver(repository)
Expect(observer).NotTo(BeNil())
})
@ -45,10 +27,9 @@ var _ = Describe("Saving blocks to the database", func() {
Transactions: []core.Transaction{{}},
}
observer := observers.BlockchainDBObserver{Db: db}
observer := observers.NewBlockchainDbObserver(repository)
observer.NotifyBlockAdded(block)
repository := repositories.NewPostgres(db)
savedBlock := repository.FindBlockByNumber(123)
Expect(savedBlock).NotTo(BeNil())
Expect(len(savedBlock.Transactions)).To(Equal(1))

View File

@ -0,0 +1,7 @@
package repositories
import "github.com/8thlight/vulcanizedb/core"
type Repository interface {
CreateBlock(block core.Block)
}