Refactor to use listener

* This removes some duplication between the fake blockchain and
   geth blockchain.
 * This pulls the observers into the blockchain listener
This commit is contained in:
Eric Meyer
2017-11-02 12:51:46 -05:00
parent 60a8be67f4
commit 646e0fa057
9 changed files with 147 additions and 130 deletions
+13 -21
View File
@@ -1,48 +1,40 @@
package integration_test
import (
"fmt"
"path"
"path/filepath"
"runtime"
"github.com/8thlight/vulcanizedb/core"
"github.com/8thlight/vulcanizedb/fakes"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var (
_, filename, _, _ = runtime.Caller(0)
basepath = filepath.Dir(filename)
)
func RunTimePath() string {
return path.Join(path.Dir(filename), "../")
}
type ObserverWithChannel struct {
blocks chan core.Block
}
func (observer *ObserverWithChannel) NotifyBlockAdded(block core.Block) {
fmt.Println("Block: ", block.Number)
observer.blocks <- block
}
var _ = Describe("Reading from the Geth blockchain", func() {
It("reads two blocks with incrementing numbers", func(done Done) {
addedBlock := make(chan core.Block, 10)
observer := &ObserverWithChannel{addedBlock}
It("reads two block with listener", func(done Done) {
observer := fakes.NewFakeBlockchainObserverTwo()
blockchain := core.NewGethBlockchain(RunTimePath() + "/test_data_dir/geth.ipc")
observers := []core.BlockchainObserver{observer}
listener := core.NewBlockchainListener(blockchain, observers)
go listener.Start()
var blockchain core.Blockchain = core.NewGethBlockchain(RunTimePath() + "/test_data_dir/geth.ipc")
blockchain.RegisterObserver(observer)
<-observer.WasNotified
firstBlock := observer.LastBlock()
Expect(firstBlock).NotTo(BeNil())
go blockchain.SubscribeToEvents()
<-observer.WasNotified
secondBlock := observer.LastBlock()
Expect(secondBlock).NotTo(BeNil())
firstBlock := <-addedBlock
Expect(firstBlock).ShouldNot(BeNil())
secondBlock := <-addedBlock
Expect(firstBlock.Number + 1).Should(Equal(secondBlock.Number))
close(done)