github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/swarm/network/simulation/http_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:43</date>
    10  //</624450114424934400>
    11  
    12  
    13  package simulation
    14  
    15  import (
    16  	"context"
    17  	"fmt"
    18  	"net/http"
    19  	"sync"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/ethereum/go-ethereum/log"
    24  	"github.com/ethereum/go-ethereum/node"
    25  	"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
    26  )
    27  
    28  func TestSimulationWithHTTPServer(t *testing.T) {
    29  	log.Debug("Init simulation")
    30  
    31  	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
    32  	defer cancel()
    33  
    34  	sim := New(
    35  		map[string]ServiceFunc{
    36  			"noop": func(_ *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) {
    37  				return newNoopService(), nil, nil
    38  			},
    39  		}).WithServer(DefaultHTTPSimAddr)
    40  	defer sim.Close()
    41  	log.Debug("Done.")
    42  
    43  	_, err := sim.AddNode()
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  
    48  	log.Debug("Starting sim round and let it time out...")
    49  //第一个不发送到通道的运行测试
    50  //阻止模拟,让它超时
    51  	result := sim.Run(ctx, func(ctx context.Context, sim *Simulation) error {
    52  		log.Debug("Just start the sim without any action and wait for the timeout")
    53  //使用睡眠确保模拟不会在超时之前终止。
    54  		time.Sleep(2 * time.Second)
    55  		return nil
    56  	})
    57  
    58  	if result.Error != nil {
    59  		if result.Error.Error() == "context deadline exceeded" {
    60  			log.Debug("Expected timeout error received")
    61  		} else {
    62  			t.Fatal(result.Error)
    63  		}
    64  	}
    65  
    66  //现在再次运行它并在等待通道上发送预期信号,
    67  //然后关闭模拟
    68  	log.Debug("Starting sim round and wait for frontend signal...")
    69  //这一次超时时间应该足够长,这样它就不会过早启动。
    70  	ctx, cancel2 := context.WithTimeout(context.Background(), 5*time.Second)
    71  	defer cancel2()
    72  	errC := make(chan error, 1)
    73  	go triggerSimulationRun(t, errC)
    74  	result = sim.Run(ctx, func(ctx context.Context, sim *Simulation) error {
    75  		log.Debug("This run waits for the run signal from `frontend`...")
    76  //确保睡眠状态下,模拟不会在收到信号之前终止。
    77  		time.Sleep(2 * time.Second)
    78  		return nil
    79  	})
    80  	if result.Error != nil {
    81  		t.Fatal(result.Error)
    82  	}
    83  	if err := <-errC; err != nil {
    84  		t.Fatal(err)
    85  	}
    86  	log.Debug("Test terminated successfully")
    87  }
    88  
    89  func triggerSimulationRun(t *testing.T, errC chan error) {
    90  //我们需要首先等待SIM HTTP服务器开始运行…
    91  	time.Sleep(2 * time.Second)
    92  //然后我们可以发送信号
    93  
    94  	log.Debug("Sending run signal to simulation: POST /runsim...")
    95  resp, err := http.Post(fmt.Sprintf("http://本地主机%s/runsim“,defaulthttpsimaddr”,“application/json”,nil)
    96  	if err != nil {
    97  		errC <- fmt.Errorf("Request failed: %v", err)
    98  		return
    99  	}
   100  	log.Debug("Signal sent")
   101  	if resp.StatusCode != http.StatusOK {
   102  		errC <- fmt.Errorf("err %s", resp.Status)
   103  		return
   104  	}
   105  	errC <- resp.Body.Close()
   106  }
   107