github.com/evdatsion/aphelion-dpos-bft@v0.32.1/abci/tests/test_app/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os"
     7  	"os/exec"
     8  	"time"
     9  
    10  	"github.com/evdatsion/aphelion-dpos-bft/abci/example/code"
    11  	"github.com/evdatsion/aphelion-dpos-bft/abci/types"
    12  )
    13  
    14  var abciType string
    15  
    16  func init() {
    17  	abciType = os.Getenv("ABCI")
    18  	if abciType == "" {
    19  		abciType = "socket"
    20  	}
    21  }
    22  
    23  func main() {
    24  	testCounter()
    25  }
    26  
    27  const (
    28  	maxABCIConnectTries = 10
    29  )
    30  
    31  func ensureABCIIsUp(typ string, n int) error {
    32  	var err error
    33  	cmdString := "abci-cli echo hello"
    34  	if typ == "grpc" {
    35  		cmdString = "abci-cli --abci grpc echo hello"
    36  	}
    37  
    38  	for i := 0; i < n; i++ {
    39  		cmd := exec.Command("bash", "-c", cmdString) // nolint: gas
    40  		_, err = cmd.CombinedOutput()
    41  		if err == nil {
    42  			break
    43  		}
    44  		<-time.After(500 * time.Millisecond)
    45  	}
    46  	return err
    47  }
    48  
    49  func testCounter() {
    50  	abciApp := os.Getenv("ABCI_APP")
    51  	if abciApp == "" {
    52  		panic("No ABCI_APP specified")
    53  	}
    54  
    55  	fmt.Printf("Running %s test with abci=%s\n", abciApp, abciType)
    56  	cmd := exec.Command("bash", "-c", fmt.Sprintf("abci-cli %s", abciApp)) // nolint: gas
    57  	cmd.Stdout = os.Stdout
    58  	if err := cmd.Start(); err != nil {
    59  		log.Fatalf("starting %q err: %v", abciApp, err)
    60  	}
    61  	defer cmd.Wait()
    62  	defer cmd.Process.Kill()
    63  
    64  	if err := ensureABCIIsUp(abciType, maxABCIConnectTries); err != nil {
    65  		log.Fatalf("echo failed: %v", err)
    66  	}
    67  
    68  	client := startClient(abciType)
    69  	defer client.Stop()
    70  
    71  	setOption(client, "serial", "on")
    72  	commit(client, nil)
    73  	deliverTx(client, []byte("abc"), code.CodeTypeBadNonce, nil)
    74  	commit(client, nil)
    75  	deliverTx(client, []byte{0x00}, types.CodeTypeOK, nil)
    76  	commit(client, []byte{0, 0, 0, 0, 0, 0, 0, 1})
    77  	deliverTx(client, []byte{0x00}, code.CodeTypeBadNonce, nil)
    78  	deliverTx(client, []byte{0x01}, types.CodeTypeOK, nil)
    79  	deliverTx(client, []byte{0x00, 0x02}, types.CodeTypeOK, nil)
    80  	deliverTx(client, []byte{0x00, 0x03}, types.CodeTypeOK, nil)
    81  	deliverTx(client, []byte{0x00, 0x00, 0x04}, types.CodeTypeOK, nil)
    82  	deliverTx(client, []byte{0x00, 0x00, 0x06}, code.CodeTypeBadNonce, nil)
    83  	commit(client, []byte{0, 0, 0, 0, 0, 0, 0, 5})
    84  }