Skip to content

Commit

Permalink
Enforce golang conventions for SBFT tests
Browse files Browse the repository at this point in the history
Change-Id: I8bfb64dc5930e407d8c127929ff65e466fd5cf5c
Signed-off-by: Gabor Hosszu <gabor@digitalasset.com>
  • Loading branch information
gaborh-da committed Jan 5, 2017
1 parent 9981150 commit b777eb8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
34 changes: 17 additions & 17 deletions orderer/sbft/main/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ const maindir = "github.com/hyperledger/fabric/orderer/sbft/main"

var mainexe = os.TempDir() + "/" + "sbft"

type peer struct {
type Peer struct {
id uint64
config flags
cancel context.CancelFunc
cmd *exec.Cmd
}

type receiver struct {
type Receiver struct {
id uint64
retch chan []byte
signals chan bool
Expand Down Expand Up @@ -193,7 +193,7 @@ func TestTenReplicasBombedWithBroadcastsIfLedgersConsistent(t *testing.T) {
StartPeers(peers)
defer StopPeers(peers)

receivers := make([]*receiver, 0, len(peers))
receivers := make([]*Receiver, 0, len(peers))
for i := 0; i < len(peers); i++ {
r, err := Receive(peers[i], startingPort)
if err != nil {
Expand Down Expand Up @@ -223,8 +223,8 @@ func TestTenReplicasBombedWithBroadcastsIfLedgersConsistent(t *testing.T) {
}
}

func InitPeers(num uint64, startingPort int) []*peer {
peers := make([]*peer, 0, num)
func InitPeers(num uint64, startingPort int) []*Peer {
peers := make([]*Peer, 0, num)
certFiles := make([]string, 0, num)
for i := uint64(0); i < num; i++ {
certFiles = append(certFiles, generateCertificate(i, keyfile))
Expand All @@ -236,13 +236,13 @@ func InitPeers(num uint64, startingPort int) []*peer {
return peers
}

func StartPeers(peers []*peer) {
func StartPeers(peers []*Peer) {
for _, p := range peers {
p.start()
}
}

func StopPeers(peers []*peer) {
func StopPeers(peers []*Peer) {
for _, p := range peers {
p.stop()
}
Expand Down Expand Up @@ -275,7 +275,7 @@ func generateConfig(peerNum uint64, startingPort int, certFiles []string) string
return conffilepath
}

func initPeer(uid uint64, startingPort int, configFile string, certFile string) (p *peer) {
func initPeer(uid uint64, startingPort int, configFile string, certFile string) (p *Peer) {
tempDir, err := ioutil.TempDir("", "sbft_test")
panicOnError(err)
os.RemoveAll(tempDir)
Expand All @@ -286,7 +286,7 @@ func initPeer(uid uint64, startingPort int, configFile string, certFile string)
keyFile: keyfile,
dataDir: tempDir}
ctx, cancel := context.WithCancel(context.Background())
p = &peer{id: uid, cancel: cancel, config: c}
p = &Peer{id: uid, cancel: cancel, config: c}
err = initInstance(c)
panicOnError(err)
p.cmd = exec.CommandContext(ctx, mainexe, "-addr", p.config.listenAddr, "-gaddr", p.config.grpcAddr, "-cert", p.config.certFile, "-key",
Expand All @@ -296,17 +296,17 @@ func initPeer(uid uint64, startingPort int, configFile string, certFile string)
return
}

func (p *peer) start() {
func (p *Peer) start() {
err := p.cmd.Start()
panicOnError(err)
}

func (p *peer) stop() {
func (p *Peer) stop() {
p.cancel()
p.cmd.Wait()
}

func Broadcast(p *peer, startingPort int, bytes []byte) error {
func Broadcast(p *Peer, startingPort int, bytes []byte) error {
timeout := 10 * time.Second
grpcAddress := grpcAddress(p.id, startingPort)
clientconn, err := grpc.Dial(grpcAddress, grpc.WithBlock(), grpc.WithTimeout(timeout), grpc.WithInsecure())
Expand All @@ -330,7 +330,7 @@ func Broadcast(p *peer, startingPort int, bytes []byte) error {
return nil
}

func Receive(p *peer, startingPort int) (*receiver, error) {
func Receive(p *Peer, startingPort int) (*Receiver, error) {
retch := make(chan []byte, 100)
signals := make(chan bool, 100)
timeout := 4 * time.Second
Expand Down Expand Up @@ -378,14 +378,14 @@ func Receive(p *peer, startingPort int) (*receiver, error) {
}
}
}()
return &receiver{id: p.id, retch: retch, signals: signals}, nil
return &Receiver{id: p.id, retch: retch, signals: signals}, nil
}

func (r *receiver) Received() int {
func (r *Receiver) Received() int {
return len(r.retch)
}

func (r *receiver) Stop() {
func (r *Receiver) Stop() {
close(r.signals)
}

Expand All @@ -396,7 +396,7 @@ func AssertWithTimeout(assertion func() bool, timeoutSec int) bool {
return assertion()
}

func WaitForConnection(peers []*peer) {
func WaitForConnection(peers []*Peer) {
l := len(peers)
m := math.Max(float64(3), float64(l-3))
_ = <-time.After(time.Duration(m) * time.Second)
Expand Down
20 changes: 10 additions & 10 deletions orderer/sbft/main/sbft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ import (

var logger = logging.MustGetLogger("sbft_test")

var UPDATE byte = 0
var SEND byte = 1
const update byte = 0
const sent byte = 1

var NEEDED_UPDATES = 2
var NEEDED_SENT = 1
const neededUpdates = 2
const neededSent = 1

func TestSbftPeer(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -108,18 +108,18 @@ func checkResults(t *testing.T, resultch chan byte, errorch chan error) {
select {
case result := <-resultch:
switch result {
case UPDATE:
case update:
updates++
case SEND:
case sent:
sentBroadcast++
}
case <-time.After(30 * time.Second):
continue
}
}
if updates != NEEDED_UPDATES {
if updates != neededUpdates {
t.Errorf("We did not get all the ledger updates.")
} else if sentBroadcast != NEEDED_SENT {
} else if sentBroadcast != neededSent {
t.Errorf("We were unable to send all the broadcasts.")
} else {
logger.Info("Successfully sent and received everything.")
Expand Down Expand Up @@ -162,7 +162,7 @@ func updateReceiver(t *testing.T, resultch chan byte, errorch chan error, client
logger.Infof("{Update Receiver} %d - %v", i+1, pl.Data)
}
}
resultch <- UPDATE
resultch <- update
}
logger.Info("{Update Receiver} Exiting...")
}
Expand All @@ -184,5 +184,5 @@ func broadcastSender(t *testing.T, resultch chan byte, errorch chan error, clien
bstream.Send(&cb.Envelope{Payload: mpl})
logger.Infof("{Broadcast Sender} Broadcast sent: %v", bs)
logger.Info("{Broadcast Sender} Exiting...")
resultch <- SEND
resultch <- sent
}

0 comments on commit b777eb8

Please sign in to comment.