forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimings_test.go
102 lines (83 loc) · 2.53 KB
/
timings_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
92
93
94
95
96
97
98
99
100
101
102
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2019-2020 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package state_test
import (
"time"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/testutil"
"github.com/snapcore/snapd/timings"
)
type timingsSuite struct {
testutil.BaseTest
st *state.State
}
var _ = Suite(&timingsSuite{})
func (s *timingsSuite) SetUpTest(c *C) {
s.BaseTest.SetUpTest(c)
s.st = state.New(nil)
s.mockDurationThreshold(0)
}
func (s *timingsSuite) mockDurationThreshold(threshold time.Duration) {
oldDurationThreshold := timings.DurationThreshold
timings.DurationThreshold = threshold
restore := func() {
timings.DurationThreshold = oldDurationThreshold
}
s.AddCleanup(restore)
}
func (s *timingsSuite) TestTagTimingsWithChange(c *C) {
s.st.Lock()
defer s.st.Unlock()
chg := s.st.NewChange("change", "...")
task := s.st.NewTask("kind", "...")
task.SetStatus(state.DoingStatus)
chg.AddTask(task)
timing := timings.New(nil)
state.TagTimingsWithChange(timing, chg)
timing.Save(s.st)
tims, err := timings.Get(s.st, 1, func(tags map[string]string) bool { return true })
c.Assert(err, IsNil)
c.Assert(tims, HasLen, 1)
c.Check(tims[0].NestedTimings, HasLen, 0)
c.Check(tims[0].Tags, DeepEquals, map[string]string{
"change-id": chg.ID(),
})
}
func (s *timingsSuite) TestTimingsForTask(c *C) {
s.st.Lock()
defer s.st.Unlock()
task := s.st.NewTask("kind", "...")
task.SetStatus(state.DoingStatus)
chg := s.st.NewChange("change", "...")
chg.AddTask(task)
troot := state.TimingsForTask(task)
span := troot.StartSpan("foo", "bar")
span.Stop()
troot.Save(s.st)
tims, err := timings.Get(s.st, 1, func(tags map[string]string) bool { return true })
c.Assert(err, IsNil)
c.Assert(tims, HasLen, 1)
c.Check(tims[0].NestedTimings, HasLen, 1)
c.Check(tims[0].Tags, DeepEquals, map[string]string{
"change-id": chg.ID(),
"task-id": task.ID(),
"task-kind": "kind",
"task-status": "Doing",
})
}