github.com/jincm/wesharechain@v0.0.0-20210122032815-1537409ce26a/chain/swarm/network/simulation/http_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser 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 // The go-ethereum library 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 Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package simulation 18 19 import ( 20 "context" 21 "fmt" 22 "net/http" 23 "sync" 24 "testing" 25 "time" 26 27 "github.com/ethereum/go-ethereum/log" 28 "github.com/ethereum/go-ethereum/node" 29 "github.com/ethereum/go-ethereum/p2p/simulations/adapters" 30 ) 31 32 func TestSimulationWithHTTPServer(t *testing.T) { 33 log.Debug("Init simulation") 34 35 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) 36 defer cancel() 37 38 sim := New( 39 map[string]ServiceFunc{ 40 "noop": func(_ *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) { 41 return newNoopService(), nil, nil 42 }, 43 }).WithServer(DefaultHTTPSimAddr) 44 defer sim.Close() 45 log.Debug("Done.") 46 47 _, err := sim.AddNode() 48 if err != nil { 49 t.Fatal(err) 50 } 51 52 log.Debug("Starting sim round and let it time out...") 53 //first test that running without sending to the channel will actually 54 //block the simulation, so let it time out 55 result := sim.Run(ctx, func(ctx context.Context, sim *Simulation) error { 56 log.Debug("Just start the sim without any action and wait for the timeout") 57 //ensure with a Sleep that simulation doesn't terminate before the timeout 58 time.Sleep(2 * time.Second) 59 return nil 60 }) 61 62 if result.Error != nil { 63 if result.Error.Error() == "context deadline exceeded" { 64 log.Debug("Expected timeout error received") 65 } else { 66 t.Fatal(result.Error) 67 } 68 } 69 70 //now run it again and send the expected signal on the waiting channel, 71 //then close the simulation 72 log.Debug("Starting sim round and wait for frontend signal...") 73 //this time the timeout should be long enough so that it doesn't kick in too early 74 ctx, cancel2 := context.WithTimeout(context.Background(), 5*time.Second) 75 defer cancel2() 76 errC := make(chan error, 1) 77 go triggerSimulationRun(t, errC) 78 result = sim.Run(ctx, func(ctx context.Context, sim *Simulation) error { 79 log.Debug("This run waits for the run signal from `frontend`...") 80 //ensure with a Sleep that simulation doesn't terminate before the signal is received 81 time.Sleep(2 * time.Second) 82 return nil 83 }) 84 if result.Error != nil { 85 t.Fatal(result.Error) 86 } 87 if err := <-errC; err != nil { 88 t.Fatal(err) 89 } 90 log.Debug("Test terminated successfully") 91 } 92 93 func triggerSimulationRun(t *testing.T, errC chan error) { 94 //We need to first wait for the sim HTTP server to start running... 95 time.Sleep(2 * time.Second) 96 //then we can send the signal 97 98 log.Debug("Sending run signal to simulation: POST /runsim...") 99 resp, err := http.Post(fmt.Sprintf("http://localhost%s/runsim", DefaultHTTPSimAddr), "application/json", nil) 100 if err != nil { 101 errC <- fmt.Errorf("Request failed: %v", err) 102 return 103 } 104 log.Debug("Signal sent") 105 if resp.StatusCode != http.StatusOK { 106 errC <- fmt.Errorf("err %s", resp.Status) 107 return 108 } 109 errC <- resp.Body.Close() 110 }