github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/network/simulation/http_test.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //
    10  //
    11  //
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  
    25  package simulation
    26  
    27  import (
    28  	"context"
    29  	"fmt"
    30  	"net/http"
    31  	"sync"
    32  	"testing"
    33  	"time"
    34  
    35  	"github.com/ethereum/go-ethereum/log"
    36  	"github.com/ethereum/go-ethereum/node"
    37  	"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
    38  )
    39  
    40  func TestSimulationWithHTTPServer(t *testing.T) {
    41  	log.Debug("Init simulation")
    42  
    43  	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
    44  	defer cancel()
    45  
    46  	sim := New(
    47  		map[string]ServiceFunc{
    48  			"noop": func(_ *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) {
    49  				return newNoopService(), nil, nil
    50  			},
    51  		}).WithServer(DefaultHTTPSimAddr)
    52  	defer sim.Close()
    53  	log.Debug("Done.")
    54  
    55  	_, err := sim.AddNode()
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  
    60  	log.Debug("Starting sim round and let it time out...")
    61  //
    62  //
    63  	result := sim.Run(ctx, func(ctx context.Context, sim *Simulation) error {
    64  		log.Debug("Just start the sim without any action and wait for the timeout")
    65  //
    66  		time.Sleep(2 * time.Second)
    67  		return nil
    68  	})
    69  
    70  	if result.Error != nil {
    71  		if result.Error.Error() == "context deadline exceeded" {
    72  			log.Debug("Expected timeout error received")
    73  		} else {
    74  			t.Fatal(result.Error)
    75  		}
    76  	}
    77  
    78  //
    79  //
    80  	log.Debug("Starting sim round and wait for frontend signal...")
    81  //
    82  	ctx, cancel2 := context.WithTimeout(context.Background(), 5*time.Second)
    83  	defer cancel2()
    84  	go sendRunSignal(t)
    85  	result = sim.Run(ctx, func(ctx context.Context, sim *Simulation) error {
    86  		log.Debug("This run waits for the run signal from `frontend`...")
    87  //
    88  		time.Sleep(2 * time.Second)
    89  		return nil
    90  	})
    91  	if result.Error != nil {
    92  		t.Fatal(result.Error)
    93  	}
    94  	log.Debug("Test terminated successfully")
    95  }
    96  
    97  func sendRunSignal(t *testing.T) {
    98  //
    99  	time.Sleep(2 * time.Second)
   100  //
   101  
   102  	log.Debug("Sending run signal to simulation: POST /runsim...")
   103  resp, err := http.Post(fmt.Sprintf("http://
   104  	if err != nil {
   105  		t.Fatalf("Request failed: %v", err)
   106  	}
   107  	defer func() {
   108  		err := resp.Body.Close()
   109  		if err != nil {
   110  			log.Error("Error closing response body", "err", err)
   111  		}
   112  	}()
   113  	log.Debug("Signal sent")
   114  	if resp.StatusCode != http.StatusOK {
   115  		t.Fatalf("err %s", resp.Status)
   116  	}
   117  }