mirror of
https://github.com/tendermint/tendermint.git
synced 2026-08-31 13:16:59 +00:00
Refactor Application to Application & AppContext
This commit is contained in:
+50
-19
@@ -1,6 +1,8 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
. "github.com/tendermint/go-common"
|
||||
"github.com/tendermint/go-merkle"
|
||||
"github.com/tendermint/go-wire"
|
||||
@@ -8,8 +10,8 @@ import (
|
||||
)
|
||||
|
||||
type DummyApplication struct {
|
||||
state merkle.Tree
|
||||
lastCommitState merkle.Tree
|
||||
mtx sync.Mutex
|
||||
state merkle.Tree
|
||||
}
|
||||
|
||||
func NewDummyApplication() *DummyApplication {
|
||||
@@ -19,48 +21,77 @@ func NewDummyApplication() *DummyApplication {
|
||||
0,
|
||||
nil,
|
||||
)
|
||||
return &DummyApplication{
|
||||
state: state,
|
||||
lastCommitState: state,
|
||||
return &DummyApplication{state: state}
|
||||
}
|
||||
|
||||
func (dapp *DummyApplication) Open() types.AppContext {
|
||||
dapp.mtx.Lock()
|
||||
defer dapp.mtx.Unlock()
|
||||
return &DummyAppContext{
|
||||
app: dapp,
|
||||
state: dapp.state.Copy(),
|
||||
}
|
||||
}
|
||||
|
||||
func (dapp *DummyApplication) Echo(message string) string {
|
||||
func (dapp *DummyApplication) commitState(state merkle.Tree) {
|
||||
dapp.mtx.Lock()
|
||||
defer dapp.mtx.Unlock()
|
||||
dapp.state = state.Copy()
|
||||
}
|
||||
|
||||
func (dapp *DummyApplication) getState() merkle.Tree {
|
||||
dapp.mtx.Lock()
|
||||
defer dapp.mtx.Unlock()
|
||||
return dapp.state.Copy()
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------
|
||||
|
||||
type DummyAppContext struct {
|
||||
app *DummyApplication
|
||||
state merkle.Tree
|
||||
}
|
||||
|
||||
func (dac *DummyAppContext) Echo(message string) string {
|
||||
return message
|
||||
}
|
||||
|
||||
func (dapp *DummyApplication) Info() []string {
|
||||
return []string{Fmt("size:%v", dapp.state.Size())}
|
||||
func (dac *DummyAppContext) Info() []string {
|
||||
return []string{Fmt("size:%v", dac.state.Size())}
|
||||
}
|
||||
|
||||
func (dapp *DummyApplication) SetOption(key string, value string) types.RetCode {
|
||||
func (dac *DummyAppContext) SetOption(key string, value string) types.RetCode {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (dapp *DummyApplication) AppendTx(tx []byte) ([]types.Event, types.RetCode) {
|
||||
dapp.state.Set(tx, tx)
|
||||
func (dac *DummyAppContext) AppendTx(tx []byte) ([]types.Event, types.RetCode) {
|
||||
dac.state.Set(tx, tx)
|
||||
return nil, 0
|
||||
}
|
||||
|
||||
func (dapp *DummyApplication) GetHash() ([]byte, types.RetCode) {
|
||||
hash := dapp.state.Hash()
|
||||
func (dac *DummyAppContext) GetHash() ([]byte, types.RetCode) {
|
||||
hash := dac.state.Hash()
|
||||
return hash, 0
|
||||
}
|
||||
|
||||
func (dapp *DummyApplication) Commit() types.RetCode {
|
||||
dapp.lastCommitState = dapp.state.Copy()
|
||||
func (dac *DummyAppContext) Commit() types.RetCode {
|
||||
dac.app.commitState(dac.state)
|
||||
return 0
|
||||
}
|
||||
|
||||
func (dapp *DummyApplication) Rollback() types.RetCode {
|
||||
dapp.state = dapp.lastCommitState.Copy()
|
||||
func (dac *DummyAppContext) Rollback() types.RetCode {
|
||||
dac.state = dac.app.getState()
|
||||
return 0
|
||||
}
|
||||
|
||||
func (dapp *DummyApplication) AddListener(key string) types.RetCode {
|
||||
func (dac *DummyAppContext) AddListener(key string) types.RetCode {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (dapp *DummyApplication) RemListener(key string) types.RetCode {
|
||||
func (dac *DummyAppContext) RemListener(key string) types.RetCode {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (dac *DummyAppContext) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
+50
-5
@@ -1,8 +1,9 @@
|
||||
package example
|
||||
|
||||
import (
|
||||
// "fmt"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "github.com/tendermint/go-common"
|
||||
"github.com/tendermint/go-wire"
|
||||
@@ -12,6 +13,8 @@ import (
|
||||
|
||||
func TestStream(t *testing.T) {
|
||||
|
||||
numAppendTxs := 200000
|
||||
|
||||
// Start the listener
|
||||
_, err := server.StartListener("tcp://127.0.0.1:8080", NewDummyApplication())
|
||||
if err != nil {
|
||||
@@ -25,7 +28,9 @@ func TestStream(t *testing.T) {
|
||||
}
|
||||
|
||||
// Read response data
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
counter := 0
|
||||
for {
|
||||
var n int
|
||||
var err error
|
||||
@@ -34,19 +39,59 @@ func TestStream(t *testing.T) {
|
||||
if err != nil {
|
||||
Exit(err.Error())
|
||||
}
|
||||
// fmt.Println("Read", n)
|
||||
|
||||
// Process response
|
||||
switch res := res.(type) {
|
||||
case types.ResponseAppendTx:
|
||||
counter += 1
|
||||
if res.RetCode != types.RetCodeOK {
|
||||
t.Error("AppendTx failed with ret_code", res.RetCode)
|
||||
}
|
||||
if counter > numAppendTxs {
|
||||
t.Fatal("Too many AppendTx responses")
|
||||
}
|
||||
t.Log("response", counter)
|
||||
if counter == numAppendTxs {
|
||||
go func() {
|
||||
time.Sleep(time.Second * 2) // Wait for a bit to allow counter overflow
|
||||
close(done)
|
||||
}()
|
||||
}
|
||||
case types.ResponseFlush:
|
||||
// ignore
|
||||
default:
|
||||
t.Error("Unexpected response type", reflect.TypeOf(res))
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Write requests
|
||||
for {
|
||||
for counter := 0; counter < numAppendTxs; counter++ {
|
||||
// Send request
|
||||
var n int
|
||||
var err error
|
||||
var req types.Request = types.RequestAppendTx{TxBytes: []byte("test")}
|
||||
wire.WriteBinary(req, conn, &n, &err)
|
||||
if err != nil {
|
||||
Exit(err.Error())
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
|
||||
// Sometimes send flush messages
|
||||
if counter%123 == 0 {
|
||||
t.Log("flush")
|
||||
wire.WriteBinary(types.RequestFlush{}, conn, &n, &err)
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
}
|
||||
// fmt.Println("Wrote", n)
|
||||
}
|
||||
|
||||
// Send final flush message
|
||||
var n int
|
||||
wire.WriteBinary(types.RequestFlush{}, conn, &n, &err)
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
|
||||
<-done
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user