MCPcopy
hub / github.com/tsenart/vegeta / TestAttackSignalOnce

Function TestAttackSignalOnce

attack_test.go:58–114  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

56}
57
58func TestAttackSignalOnce(t *testing.T) {
59 t.Parallel()
60
61 const (
62 signalDelay = 300 * time.Millisecond // Delay before stopping.
63 clientTimeout = 1 * time.Second // This, plus delay, is the max time for the attack.
64 serverTimeout = 2 * time.Second // Must be more than clientTimeout.
65 attackDuration = 10 * time.Second // The attack should never take this long.
66 )
67
68 server := httptest.NewServer(
69 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
70 time.Sleep(serverTimeout) // Server.Close() will block for this long on shutdown.
71 }),
72 )
73 defer server.Close()
74
75 tr := vegeta.NewStaticTargeter(vegeta.Target{Method: "GET", URL: server.URL})
76 atk := vegeta.NewAttacker(vegeta.Timeout(clientTimeout))
77 rate := vegeta.Rate{Freq: 10, Per: time.Second} // Every 100ms.
78
79 var buf bytes.Buffer
80 writer := bufio.NewWriter(&buf)
81 enc := vegeta.NewEncoder(writer)
82 sig := make(chan os.Signal, 1)
83 res := atk.Attack(tr, rate, attackDuration, "")
84
85 var wg sync.WaitGroup
86 wg.Add(1)
87 go func() {
88 defer wg.Done()
89 processAttack(atk, res, enc, sig, nil)
90 }()
91
92 // Allow more than one request to have started before stopping.
93 time.Sleep(signalDelay)
94 sig <- os.Interrupt
95 wg.Wait()
96 writer.Flush()
97
98 metrics, err := decodeMetrics(buf)
99 if err != nil {
100 t.Error(err)
101 }
102 if got, min := metrics.Requests, uint64(2); got < min {
103 t.Errorf("not enough requests recorded. got %+v, min: %+v", got, min)
104 }
105 if got, want := metrics.Success, 0.0; got != want {
106 t.Errorf("all requests should fail. got %+v, want: %+v", got, want)
107 }
108 if got, max := metrics.Duration, clientTimeout; got > max {
109 t.Errorf("attack duration too long. got %+v, max: %+v", got, max)
110 }
111 if got, want := metrics.Wait.Round(time.Second), clientTimeout; got != want {
112 t.Errorf("attack wait doesn't match timeout. got %+v, want: %+v", got, want)
113 }
114}
115

Callers

nothing calls this directly

Calls 5

AttackMethod · 0.95
processAttackFunction · 0.85
decodeMetricsFunction · 0.85
CloseMethod · 0.65
AddMethod · 0.65

Tested by

no test coverage detected