File: httpstress_test.go

package info (click to toggle)
syncthing 0.14.18%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 7,388 kB
  • ctags: 4,608
  • sloc: xml: 781; sh: 271; makefile: 45
file content (143 lines) | stat: -rw-r--r-- 2,707 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (C) 2014 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.

// +build integration

package integration

import (
	"bytes"
	"crypto/tls"
	"errors"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"sync"
	"testing"
	"time"
)

func TestStressHTTP(t *testing.T) {
	log.Println("Cleaning...")
	err := removeAll("s2", "h2/index*")
	if err != nil {
		t.Fatal(err)
	}

	log.Println("Starting up...")

	p := startInstance(t, 2)
	defer checkedStop(t, p)

	// Create a client with reasonable timeouts on all stages of the request.

	tc := &tls.Config{InsecureSkipVerify: true}
	tr := &http.Transport{
		TLSClientConfig:       tc,
		DisableKeepAlives:     true,
		ResponseHeaderTimeout: 10 * time.Second,
		TLSHandshakeTimeout:   10 * time.Second,
	}
	client := &http.Client{
		Transport: tr,
		Timeout:   10 * time.Second,
	}

	var (
		requestsOK    = map[string]int{}
		requestsError = map[string]int{}
		firstError    error
		lock          sync.Mutex
	)

	gotError := func(ctx string, err error) {
		lock.Lock()
		requestsError[ctx]++
		if firstError == nil {
			firstError = err
		}
		lock.Unlock()
	}

	requestOK := func(ctx string) {
		lock.Lock()
		requestsOK[ctx]++
		lock.Unlock()
	}

	log.Println("Testing...")

	var wg sync.WaitGroup
	t0 := time.Now()

	// One thread with immediately closed connections
	wg.Add(1)
	go func() {
		defer wg.Done()
		for time.Since(t0).Seconds() < 30 {
			conn, err := net.Dial("tcp", "localhost:8082")
			if err != nil {
				gotError("Dial", err)
			} else {
				requestOK("Dial")
				conn.Close()
			}

			// At most 100 connects/sec
			time.Sleep(10 * time.Millisecond)
		}
	}()

	// 50 threads doing mixed HTTP and HTTPS requests
	for i := 0; i < 50; i++ {
		i := i
		wg.Add(1)
		go func() {
			defer wg.Done()
			for time.Since(t0).Seconds() < 30 {
				proto := "http"
				if i%2 == 0 {
					proto = "https"
				}
				url := proto + "://localhost:8082/"
				resp, err := client.Get(url)

				if err != nil {
					gotError("Get "+proto, err)
					continue
				}

				bs, err := ioutil.ReadAll(resp.Body)
				resp.Body.Close()

				if err != nil {
					gotError("Read "+proto, err)
					continue
				}

				if !bytes.Contains(bs, []byte("</html>")) {
					err := errors.New("Incorrect response")
					gotError("Get "+proto, err)
					continue
				}

				requestOK(url)

				// At most 100 requests/sec
				time.Sleep(10 * time.Millisecond)
			}
		}()
	}

	wg.Wait()
	t.Logf("OK: %v reqs", requestsOK)
	t.Logf("Err: %v reqs", requestsError)

	if firstError != nil {
		t.Error(firstError)
	}
}