github.com/annchain/OG@v0.0.9/tests/testSendArchives/sendArchives_test.go (about)

     1  package testSendArchives
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"github.com/annchain/OG/arefactor/og/types"
     8  	"io/ioutil"
     9  	"net"
    10  	"net/http"
    11  	"testing"
    12  	"time"
    13  )
    14  
    15  func TestSend(t *testing.T) {
    16  	var ar [][]byte
    17  	for i := 0; i < 100000; i++ {
    18  		ar = append(ar, types.RandomHash().ToBytes())
    19  	}
    20  	transport := &http.Transport{
    21  		MaxIdleConnsPerHost: 15,
    22  		Proxy:               http.ProxyFromEnvironment,
    23  		DialContext: (&net.Dialer{
    24  			Timeout:   30 * time.Second,
    25  			KeepAlive: 30 * time.Second,
    26  			DualStack: true,
    27  		}).DialContext,
    28  		MaxIdleConns:          100,
    29  		IdleConnTimeout:       90 * time.Second,
    30  		TLSHandshakeTimeout:   10 * time.Second,
    31  		ExpectContinueTimeout: 1 * time.Second,
    32  	}
    33  	a := app{
    34  		client: &http.Client{
    35  			Timeout:   time.Second * 10,
    36  			Transport: transport,
    37  		},
    38  	}
    39  	for i, v := range ar {
    40  		go a.sendArchiveData(v, i)
    41  	}
    42  	time.Sleep(time.Second * 100)
    43  	return
    44  }
    45  
    46  type app struct {
    47  	client *http.Client
    48  }
    49  
    50  var url = "http://172.28.152.101:8000/new_archive"
    51  
    52  type txRequest struct {
    53  	Data []byte `json:"data"`
    54  }
    55  
    56  func (a *app) sendArchiveData(data []byte, i int) {
    57  	//req := httplib.NewBeegoRequest(url,"POST")
    58  	//req.SetTimeout(time.Second*10,time.Second*10)
    59  	tx := txRequest{
    60  		Data: data,
    61  	}
    62  	data, err := json.Marshal(&tx)
    63  	if err != nil {
    64  		panic(err)
    65  	}
    66  	r := bytes.NewReader(data)
    67  	req, err := http.NewRequest("POST", url, r)
    68  
    69  	resp, err := a.client.Do(req)
    70  	if err != nil {
    71  		fmt.Println(err)
    72  		return
    73  	}
    74  	//now := time.Now()
    75  	defer resp.Body.Close()
    76  	resDate, err := ioutil.ReadAll(resp.Body)
    77  	if err != nil {
    78  		panic(err)
    79  	}
    80  
    81  	str := string(resDate)
    82  	if err != nil {
    83  		fmt.Println(i, str, err)
    84  	}
    85  	if resp.StatusCode != 200 {
    86  		//panic( resp.StatusCode)
    87  		fmt.Println(resp.StatusCode)
    88  		return
    89  	}
    90  	//fmt.Println(i, err, time.Since(now), str)
    91  
    92  }