cli: add tests for server commands

Fixes:
  * Proper exit code should be returned on exit for server-related
commands
This commit is contained in:
Anna Shaleva 2022-01-31 16:20:14 +03:00 committed by Anna Shaleva
parent 613a23cc3f
commit 9de9bcb17c
5 changed files with 364 additions and 21 deletions

View file

@ -7,6 +7,7 @@ import (
"io/ioutil"
"math"
"strings"
"sync"
"testing"
"time"
@ -58,13 +59,66 @@ type executor struct {
// NetSrv is a network server (can be empty).
NetSrv *network.Server
// Out contains command output.
Out *bytes.Buffer
Out *ConcurrentBuffer
// Err contains command errors.
Err *bytes.Buffer
// In contains command input.
In *bytes.Buffer
}
// ConcurrentBuffer is a wrapper over Buffer with mutex.
type ConcurrentBuffer struct {
lock sync.RWMutex
buf *bytes.Buffer
}
// NewConcurrentBuffer returns new ConcurrentBuffer with underlying buffer initialized.
func NewConcurrentBuffer() *ConcurrentBuffer {
return &ConcurrentBuffer{
buf: bytes.NewBuffer(nil),
}
}
// Write is a concurrent wrapper over the corresponding method of bytes.Buffer.
func (w *ConcurrentBuffer) Write(p []byte) (int, error) {
w.lock.Lock()
defer w.lock.Unlock()
return w.buf.Write(p)
}
// ReadString is a concurrent wrapper over the corresponding method of bytes.Buffer.
func (w *ConcurrentBuffer) ReadString(delim byte) (string, error) {
w.lock.RLock()
defer w.lock.RUnlock()
return w.buf.ReadString(delim)
}
// Bytes is a concurrent wrapper over the corresponding method of bytes.Buffer.
func (w *ConcurrentBuffer) Bytes() []byte {
w.lock.RLock()
defer w.lock.RUnlock()
return w.buf.Bytes()
}
// String is a concurrent wrapper over the corresponding method of bytes.Buffer.
func (w *ConcurrentBuffer) String() string {
w.lock.RLock()
defer w.lock.RUnlock()
return w.buf.String()
}
// Reset is a concurrent wrapper over the corresponding method of bytes.Buffer.
func (w *ConcurrentBuffer) Reset() {
w.lock.Lock()
defer w.lock.Unlock()
w.buf.Reset()
}
func newTestChain(t *testing.T, f func(*config.Config), run bool) (*core.Blockchain, *server.Server, *network.Server) {
configPath := "../config/protocol.unit_testnet.single.yml"
cfg, err := config.LoadFile(configPath)
@ -115,7 +169,7 @@ func newExecutorSuspended(t *testing.T) *executor {
func newExecutorWithConfig(t *testing.T, needChain, runChain bool, f func(*config.Config)) *executor {
e := &executor{
CLI: newApp(),
Out: bytes.NewBuffer(nil),
Out: NewConcurrentBuffer(),
Err: bytes.NewBuffer(nil),
In: bytes.NewBuffer(nil),
}