Skip to content

Commit

Permalink
new tests on HandleWork
Browse files Browse the repository at this point in the history
  • Loading branch information
chadlagore committed Jul 26, 2017
1 parent 815f38e commit 16dde33
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
16 changes: 5 additions & 11 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import (
)

var (
logFile = os.Stdout
logFile = os.Stdout
blockQueue = make(chan *blockchain.Block, blockQueueSize)
transactionQueue = make(chan *blockchain.Transaction, transactionQueueSize)
quitChan = make(chan bool)
)

const (
Expand All @@ -36,15 +39,6 @@ type App struct {
Pool *pool.Pool
}

// BlockWorkQueue is a queue of blocks to process.
var blockQueue = make(chan *blockchain.Block, blockQueueSize)

// TransactionWorkQueue is a queue of transactions to process.
var transactionQueue = make(chan *blockchain.Transaction, transactionQueueSize)

// QuitChan kills the app worker.
var quitChan = make(chan bool)

// Run sets up and starts a new Cumulus node with the
// given configuration. This should only be called once (except in tests)
func Run(cfg conf.Config) {
Expand Down Expand Up @@ -121,7 +115,7 @@ func Run(cfg conf.Config) {
}
log.Info("Redirecting logs to logfile")
log.SetOutput(logFile)
go RunConsole(a.PeerStore, &a)
go RunConsole(&a)
}

if len(config.Target) > 0 {
Expand Down
22 changes: 22 additions & 0 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,25 @@ func TestHandleBlockNotOK(t *testing.T) {
// TODO: Assert miner not restarted.
// TODO: Assert pool untouched.
}

func TestGetLocalPool(t *testing.T) {
assert.NotNil(t, getLocalPool())
}

func TestGetLocalChain(t *testing.T) {
assert.NotNil(t, getLocalChain())
}

func TestHandleBlock(t *testing.T) {
a := createNewTestApp()
go a.HandleWork()
blockQueue <- blockchain.NewTestBlock()
assert.Equal(t, len(blockQueue), 0)
}

func TestHandleTransaction(t *testing.T) {
a := createNewTestApp()
go a.HandleWork()
transactionQueue <- blockchain.NewTestTransaction()
assert.Equal(t, len(transactionQueue), 0)
}
26 changes: 12 additions & 14 deletions app/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,21 @@ import (
)

var (
shell *ishell.Shell
peerStore *peer.PeerStore
shell *ishell.Shell
)

// RunConsole starts the Cumulus console. This should be run only once as a
// goroutine, and logging should be redirected away from stdout before it is run.
// It takes a pointer to a PeerStore so we can use the PeerStore to interact
// with other peers and give the user info about the running instance.
func RunConsole(ps *peer.PeerStore, app *App) {
peerStore = ps
func RunConsole(a *App) {
shell = ishell.New()

shell.AddCmd(&ishell.Cmd{
Name: "create",
Help: "create a new wallet hash or transaction",
Func: func(ctx *ishell.Context) {
create(ctx, app)
create(ctx, a)
},
})
shell.AddCmd(&ishell.Cmd{
Expand All @@ -38,21 +36,21 @@ func RunConsole(ps *peer.PeerStore, app *App) {
Name: "address",
Help: "show the address this host is listening on",
Func: func(ctx *ishell.Context) {
listenAddr(ctx, peerStore)
listenAddr(ctx, a)
},
})
shell.AddCmd(&ishell.Cmd{
Name: "peers",
Help: "show the peers this host is connected to",
Func: func(ctx *ishell.Context) {
peers(ctx, peerStore)
peers(ctx, a)
},
})
shell.AddCmd(&ishell.Cmd{
Name: "connect",
Help: "connect to another peer",
Func: func(ctx *ishell.Context) {
connect(ctx, peerStore)
connect(ctx, a)
},
})

Expand Down Expand Up @@ -97,22 +95,22 @@ func check(ctx *ishell.Context) {
}
}

func listenAddr(ctx *ishell.Context, peerStore *peer.PeerStore) {
shell.Println("Listening on", peerStore.ListenAddr)
func listenAddr(ctx *ishell.Context, a *App) {
shell.Println("Listening on", a.PeerStore.ListenAddr)
}

func peers(tcx *ishell.Context, peerStore *peer.PeerStore) {
shell.Println("Connected to", peerStore.Addrs())
func peers(tcx *ishell.Context, a *App) {
shell.Println("Connected to", a.PeerStore.Addrs())
}

func connect(ctx *ishell.Context, peerStore *peer.PeerStore) {
func connect(ctx *ishell.Context, a *App) {
if len(ctx.Args) == 0 {
shell.Println("Usage: connect [IP address]:[TCP port]")
return
}

addr := ctx.Args[0]
_, err := peer.Connect(addr, peerStore)
_, err := peer.Connect(addr, a.PeerStore)
if err != nil {
shell.Println("Failed to extablish connection:", err)
} else {
Expand Down

0 comments on commit 16dde33

Please sign in to comment.