-
Notifications
You must be signed in to change notification settings - Fork 0
/
attack_test.go
71 lines (64 loc) · 1.68 KB
/
attack_test.go
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
/*
* // Copyright 2020 Insolar Network Ltd.
* // All rights reserved.
* // This material is licensed under the Insolar License version 1.0,
* // available at https://github.com/insolar/assured-ledger/blob/master/LICENSE.md.
*/
package loaderbot
import (
"context"
"testing"
"time"
)
func DefaultRunnerCfg() *RunnerConfig {
return &RunnerConfig{
Name: "test_runner",
Attackers: 1,
AttackerTimeout: 1,
StartRPS: 20,
StepDurationSec: 2,
StepRPS: 1,
TestTimeSec: 5,
ReportOptions: &ReportOptions{
CSV: false,
PNG: false,
},
}
}
func TestCommonAttackSuccess(t *testing.T) {
r := NewRunner(DefaultRunnerCfg(), &ControlAttackerMock{}, nil)
r.controlled.Sleep = 10
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(r.Cfg.TestTimeSec)*time.Second)
r.TimeoutCtx = ctx
r.CancelFunc = cancel
// sync
go attack(r.attackers[0], r)
r.next <- attackToken{
Step: 1,
Tick: 1,
}
res := <-r.results
if got, want := res.DoResult.Error, ""; got != want {
t.Fatalf("got %v want %v", got, want)
}
if got, want := int(res.Elapsed), int(r.controlled.Sleep); got < want {
t.Fatalf("got %v want >= %v", got, want)
}
}
func TestCommonAttackTimeout(t *testing.T) {
r := NewRunner(DefaultRunnerCfg(), &ControlAttackerMock{}, nil)
r.controlled.Sleep = 2000
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(r.Cfg.TestTimeSec)*time.Second)
r.TimeoutCtx = ctx
r.CancelFunc = cancel
// sync
go attack(r.attackers[0], r)
r.next <- attackToken{
Step: 1,
Tick: 1,
}
res := <-r.results
if got, want := res.DoResult.Error, errAttackDoTimedOut; got != want {
t.Fatalf("got %v want %v", got, want)
}
}