mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 02:42:27 +00:00
swarm/network/simulation: fix T.Fatal inside a goroutine
This commit is contained in:
parent
0cde6cd4e5
commit
032e071edf
2 changed files with 15 additions and 11 deletions
|
|
@ -81,6 +81,7 @@ func TestPeerEventsTimeout(t *testing.T) {
|
||||||
events := sim.PeerEvents(ctx, sim.NodeIDs())
|
events := sim.PeerEvents(ctx, sim.NodeIDs())
|
||||||
|
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
|
errC := make(chan error)
|
||||||
go func() {
|
go func() {
|
||||||
for e := range events {
|
for e := range events {
|
||||||
if e.Error == context.Canceled {
|
if e.Error == context.Canceled {
|
||||||
|
|
@ -90,7 +91,7 @@ func TestPeerEventsTimeout(t *testing.T) {
|
||||||
close(done)
|
close(done)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
t.Fatal(e.Error)
|
errC <- e.Error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
@ -98,6 +99,8 @@ func TestPeerEventsTimeout(t *testing.T) {
|
||||||
select {
|
select {
|
||||||
case <-time.After(time.Second):
|
case <-time.After(time.Second):
|
||||||
t.Error("no context deadline received")
|
t.Error("no context deadline received")
|
||||||
|
case err := <-errC:
|
||||||
|
t.Fatal(err)
|
||||||
case <-done:
|
case <-done:
|
||||||
// all good, context deadline detected
|
// all good, context deadline detected
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,8 @@ func TestSimulationWithHTTPServer(t *testing.T) {
|
||||||
//this time the timeout should be long enough so that it doesn't kick in too early
|
//this time the timeout should be long enough so that it doesn't kick in too early
|
||||||
ctx, cancel2 := context.WithTimeout(context.Background(), 5*time.Second)
|
ctx, cancel2 := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
defer cancel2()
|
defer cancel2()
|
||||||
go sendRunSignal(t)
|
errC := make(chan error, 1)
|
||||||
|
go sendRunSignal(t, errC)
|
||||||
result = sim.Run(ctx, func(ctx context.Context, sim *Simulation) error {
|
result = sim.Run(ctx, func(ctx context.Context, sim *Simulation) error {
|
||||||
log.Debug("This run waits for the run signal from `frontend`...")
|
log.Debug("This run waits for the run signal from `frontend`...")
|
||||||
//ensure with a Sleep that simulation doesn't terminate before the signal is received
|
//ensure with a Sleep that simulation doesn't terminate before the signal is received
|
||||||
|
|
@ -83,10 +84,13 @@ func TestSimulationWithHTTPServer(t *testing.T) {
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
t.Fatal(result.Error)
|
t.Fatal(result.Error)
|
||||||
}
|
}
|
||||||
|
if err := <-errC; err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
log.Debug("Test terminated successfully")
|
log.Debug("Test terminated successfully")
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendRunSignal(t *testing.T) {
|
func sendRunSignal(t *testing.T, errC chan error) {
|
||||||
//We need to first wait for the sim HTTP server to start running...
|
//We need to first wait for the sim HTTP server to start running...
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
//then we can send the signal
|
//then we can send the signal
|
||||||
|
|
@ -94,16 +98,13 @@ func sendRunSignal(t *testing.T) {
|
||||||
log.Debug("Sending run signal to simulation: POST /runsim...")
|
log.Debug("Sending run signal to simulation: POST /runsim...")
|
||||||
resp, err := http.Post(fmt.Sprintf("http://localhost%s/runsim", DefaultHTTPSimAddr), "application/json", nil)
|
resp, err := http.Post(fmt.Sprintf("http://localhost%s/runsim", DefaultHTTPSimAddr), "application/json", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Request failed: %v", err)
|
errC <- fmt.Errorf("Request failed: %v", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
defer func() {
|
|
||||||
err := resp.Body.Close()
|
|
||||||
if err != nil {
|
|
||||||
log.Error("Error closing response body", "err", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
log.Debug("Signal sent")
|
log.Debug("Signal sent")
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
t.Fatalf("err %s", resp.Status)
|
errC <- fmt.Errorf("err %s", resp.Status)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
errC <- resp.Body.Close()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue