github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/cmd/geth/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 "io/ioutil" 23 "os" 24 "testing" 25 "time" 26 27 "github.com/docker/docker/pkg/reexec" 28 29 "github.com/scroll-tech/go-ethereum/internal/cmdtest" 30 "github.com/scroll-tech/go-ethereum/rpc" 31 ) 32 33 func tmpdir(t *testing.T) string { 34 dir, err := ioutil.TempDir("", "geth-test") 35 if err != nil { 36 t.Fatal(err) 37 } 38 return dir 39 } 40 41 type testgeth struct { 42 *cmdtest.TestCmd 43 44 // template variables for expect 45 Datadir string 46 Etherbase string 47 } 48 49 func init() { 50 // Run the app if we've been exec'd as "geth-test" in runGeth. 51 reexec.Register("geth-test", func() { 52 if err := app.Run(os.Args); err != nil { 53 fmt.Fprintln(os.Stderr, err) 54 os.Exit(1) 55 } 56 os.Exit(0) 57 }) 58 } 59 60 func TestMain(m *testing.M) { 61 // check if we have been reexec'd 62 if reexec.Init() { 63 return 64 } 65 os.Exit(m.Run()) 66 } 67 68 // spawns geth with the given command line args. If the args don't set --datadir, the 69 // child g gets a temporary data directory. 70 func runGeth(t *testing.T, args ...string) *testgeth { 71 tt := &testgeth{} 72 tt.TestCmd = cmdtest.NewTestCmd(t, tt) 73 for i, arg := range args { 74 switch arg { 75 case "--datadir": 76 if i < len(args)-1 { 77 tt.Datadir = args[i+1] 78 } 79 case "--miner.etherbase": 80 if i < len(args)-1 { 81 tt.Etherbase = args[i+1] 82 } 83 } 84 } 85 if tt.Datadir == "" { 86 tt.Datadir = tmpdir(t) 87 tt.Cleanup = func() { os.RemoveAll(tt.Datadir) } 88 args = append([]string{"--datadir", tt.Datadir}, args...) 89 // Remove the temporary datadir if something fails below. 90 defer func() { 91 if t.Failed() { 92 tt.Cleanup() 93 } 94 }() 95 } 96 97 // Boot "geth". This actually runs the test binary but the TestMain 98 // function will prevent any tests from running. 99 tt.Run("geth-test", args...) 100 101 return tt 102 } 103 104 // waitForEndpoint attempts to connect to an RPC endpoint until it succeeds. 105 func waitForEndpoint(t *testing.T, endpoint string, timeout time.Duration) { 106 probe := func() bool { 107 ctx, cancel := context.WithTimeout(context.Background(), timeout) 108 defer cancel() 109 c, err := rpc.DialContext(ctx, endpoint) 110 if c != nil { 111 _, err = c.SupportedModules() 112 c.Close() 113 } 114 return err == nil 115 } 116 117 start := time.Now() 118 for { 119 if probe() { 120 return 121 } 122 if time.Since(start) > timeout { 123 t.Fatal("endpoint", endpoint, "did not open within", timeout) 124 } 125 time.Sleep(200 * time.Millisecond) 126 } 127 }