github.com/theQRL/go-zond@v0.2.1/cmd/gzond/run_test.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // go-ethereum is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"os"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/docker/docker/pkg/reexec"
    27  	"github.com/theQRL/go-zond/internal/cmdtest"
    28  	"github.com/theQRL/go-zond/rpc"
    29  )
    30  
    31  type testgzond struct {
    32  	*cmdtest.TestCmd
    33  
    34  	// template variables for expect
    35  	Datadir string
    36  }
    37  
    38  func init() {
    39  	// Run the app if we've been exec'd as "gzond-test" in runGzond.
    40  	reexec.Register("gzond-test", func() {
    41  		if err := app.Run(os.Args); err != nil {
    42  			fmt.Fprintln(os.Stderr, err)
    43  			os.Exit(1)
    44  		}
    45  		os.Exit(0)
    46  	})
    47  }
    48  
    49  func TestMain(m *testing.M) {
    50  	// check if we have been reexec'd
    51  	if reexec.Init() {
    52  		return
    53  	}
    54  	os.Exit(m.Run())
    55  }
    56  
    57  // spawns gzond with the given command line args. If the args don't set --datadir, the
    58  // child g gets a temporary data directory.
    59  func runGzond(t *testing.T, args ...string) *testgzond {
    60  	tt := &testgzond{}
    61  	tt.TestCmd = cmdtest.NewTestCmd(t, tt)
    62  	for i, arg := range args {
    63  		switch arg {
    64  		case "--datadir":
    65  			if i < len(args)-1 {
    66  				tt.Datadir = args[i+1]
    67  			}
    68  		}
    69  	}
    70  	if tt.Datadir == "" {
    71  		// The temporary datadir will be removed automatically if something fails below.
    72  		tt.Datadir = t.TempDir()
    73  		args = append([]string{"--datadir", tt.Datadir}, args...)
    74  	}
    75  
    76  	// Boot "gzond". This actually runs the test binary but the TestMain
    77  	// function will prevent any tests from running.
    78  	tt.Run("gzond-test", args...)
    79  
    80  	return tt
    81  }
    82  
    83  // waitForEndpoint attempts to connect to an RPC endpoint until it succeeds.
    84  func waitForEndpoint(t *testing.T, endpoint string, timeout time.Duration) {
    85  	probe := func() bool {
    86  		ctx, cancel := context.WithTimeout(context.Background(), timeout)
    87  		defer cancel()
    88  		c, err := rpc.DialContext(ctx, endpoint)
    89  		if c != nil {
    90  			_, err = c.SupportedModules()
    91  			c.Close()
    92  		}
    93  		return err == nil
    94  	}
    95  
    96  	start := time.Now()
    97  	for {
    98  		if probe() {
    99  			return
   100  		}
   101  		if time.Since(start) > timeout {
   102  			t.Fatal("endpoint", endpoint, "did not open within", timeout)
   103  		}
   104  		time.Sleep(200 * time.Millisecond)
   105  	}
   106  }