github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/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 12:09:47</date>
    10  //</624342673821536256>
    11  
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  //
    25  //
    26  //
    27  
    28  package simulation
    29  
    30  import (
    31  	"context"
    32  	"fmt"
    33  	"net/http"
    34  	"sync"
    35  	"testing"
    36  	"time"
    37  
    38  	"github.com/ethereum/go-ethereum/log"
    39  	"github.com/ethereum/go-ethereum/node"
    40  	"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
    41  )
    42  
    43  func TestSimulationWithHTTPServer(t *testing.T) {
    44  	log.Debug("Init simulation")
    45  
    46  	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
    47  	defer cancel()
    48  
    49  	sim := New(
    50  		map[string]ServiceFunc{
    51  			"noop": func(_ *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) {
    52  				return newNoopService(), nil, nil
    53  			},
    54  		}).WithServer(DefaultHTTPSimAddr)
    55  	defer sim.Close()
    56  	log.Debug("Done.")
    57  
    58  	_, err := sim.AddNode()
    59  	if err != nil {
    60  		t.Fatal(err)
    61  	}
    62  
    63  	log.Debug("Starting sim round and let it time out...")
    64  //
    65  //
    66  	result := sim.Run(ctx, func(ctx context.Context, sim *Simulation) error {
    67  		log.Debug("Just start the sim without any action and wait for the timeout")
    68  //
    69  		time.Sleep(2 * time.Second)
    70  		return nil
    71  	})
    72  
    73  	if result.Error != nil {
    74  		if result.Error.Error() == "context deadline exceeded" {
    75  			log.Debug("Expected timeout error received")
    76  		} else {
    77  			t.Fatal(result.Error)
    78  		}
    79  	}
    80  
    81  //
    82  //
    83  	log.Debug("Starting sim round and wait for frontend signal...")
    84  //
    85  	ctx, cancel2 := context.WithTimeout(context.Background(), 5*time.Second)
    86  	defer cancel2()
    87  	go sendRunSignal(t)
    88  	result = sim.Run(ctx, func(ctx context.Context, sim *Simulation) error {
    89  		log.Debug("This run waits for the run signal from `frontend`...")
    90  //
    91  		time.Sleep(2 * time.Second)
    92  		return nil
    93  	})
    94  	if result.Error != nil {
    95  		t.Fatal(result.Error)
    96  	}
    97  	log.Debug("Test terminated successfully")
    98  }
    99  
   100  func sendRunSignal(t *testing.T) {
   101  //
   102  	time.Sleep(2 * time.Second)
   103  //
   104  
   105  	log.Debug("Sending run signal to simulation: POST /runsim...")
   106  resp, err := http.Post(fmt.Sprintf("http://
   107  	if err != nil {
   108  		t.Fatalf("Request failed: %v", err)
   109  	}
   110  	defer func() {
   111  		err := resp.Body.Close()
   112  		if err != nil {
   113  			log.Error("Error closing response body", "err", err)
   114  		}
   115  	}()
   116  	log.Debug("Signal sent")
   117  	if resp.StatusCode != http.StatusOK {
   118  		t.Fatalf("err %s", resp.Status)
   119  	}
   120  }
   121