forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels_test.go
91 lines (83 loc) · 2.36 KB
/
models_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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2016 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package lib
import (
"encoding/json"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"gopkg.in/guregu/null.v3"
"go.k6.io/k6/lib/types"
)
func TestStageJSON(t *testing.T) {
s := Stage{Duration: types.NullDurationFrom(10 * time.Second), Target: null.IntFrom(10)}
data, err := json.Marshal(s)
assert.NoError(t, err)
assert.Equal(t, `{"duration":"10s","target":10}`, string(data))
var s2 Stage
assert.NoError(t, json.Unmarshal(data, &s2))
assert.Equal(t, s, s2)
}
// Suggested by @nkovacs in https://github.com/k6io/k6/issues/207#issuecomment-330545467
func TestDataRaces(t *testing.T) {
t.Run("Check race", func(t *testing.T) {
group, err := NewGroup("test", nil)
assert.Nil(t, err, "NewGroup")
wg := sync.WaitGroup{}
wg.Add(2)
var check1, check2 *Check
go func() {
var err error // using the outer err would result in a data race
check1, err = group.Check("race")
assert.Nil(t, err, "Check 1")
wg.Done()
}()
go func() {
var err error
check2, err = group.Check("race")
assert.Nil(t, err, "Check 2")
wg.Done()
}()
wg.Wait()
assert.Equal(t, check1, check2, "Checks are the same")
})
t.Run("Group race", func(t *testing.T) {
group, err := NewGroup("test", nil)
assert.Nil(t, err, "NewGroup")
wg := sync.WaitGroup{}
wg.Add(2)
var group1, group2 *Group
go func() {
var err error
group1, err = group.Group("race")
assert.Nil(t, err, "Group 1")
wg.Done()
}()
go func() {
var err error
group2, err = group.Group("race")
assert.Nil(t, err, "Group 2")
wg.Done()
}()
wg.Wait()
assert.Equal(t, group1, group2, "Groups are the same")
})
}