github.com/oskarth/go-ethereum@v1.6.8-0.20191013093314-dac24a9d3494/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 go sendRunSignal(t) 77 result = sim.Run(ctx, func(ctx context.Context, sim *Simulation) error { 78 log.Debug("This run waits for the run signal from `frontend`...") 79 //ensure with a Sleep that simulation doesn't terminate before the signal is received 80 time.Sleep(2 * time.Second) 81 return nil 82 }) 83 if result.Error != nil { 84 t.Fatal(result.Error) 85 } 86 log.Debug("Test terminated successfully") 87 } 88 89 func sendRunSignal(t *testing.T) { 90 //We need to first wait for the sim HTTP server to start running... 91 time.Sleep(2 * time.Second) 92 //then we can send the signal 93 94 log.Debug("Sending run signal to simulation: POST /runsim...") 95 resp, err := http.Post(fmt.Sprintf("http://localhost%s/runsim", DefaultHTTPSimAddr), "application/json", nil) 96 if err != nil { 97 t.Fatalf("Request failed: %v", err) 98 } 99 defer func() { 100 err := resp.Body.Close() 101 if err != nil { 102 log.Error("Error closing response body", "err", err) 103 } 104 }() 105 log.Debug("Signal sent") 106 if resp.StatusCode != http.StatusOK { 107 t.Fatalf("err %s", resp.Status) 108 } 109 }