github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/overlord/state/timings_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019-2020 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package state_test
    21  
    22  import (
    23  	"time"
    24  
    25  	. "gopkg.in/check.v1"
    26  
    27  	"github.com/snapcore/snapd/overlord/state"
    28  	"github.com/snapcore/snapd/testutil"
    29  	"github.com/snapcore/snapd/timings"
    30  )
    31  
    32  type timingsSuite struct {
    33  	testutil.BaseTest
    34  	st *state.State
    35  }
    36  
    37  var _ = Suite(&timingsSuite{})
    38  
    39  func (s *timingsSuite) SetUpTest(c *C) {
    40  	s.BaseTest.SetUpTest(c)
    41  
    42  	s.st = state.New(nil)
    43  
    44  	s.mockDurationThreshold(0)
    45  }
    46  
    47  func (s *timingsSuite) mockDurationThreshold(threshold time.Duration) {
    48  	oldDurationThreshold := timings.DurationThreshold
    49  	timings.DurationThreshold = threshold
    50  	restore := func() {
    51  		timings.DurationThreshold = oldDurationThreshold
    52  	}
    53  	s.AddCleanup(restore)
    54  }
    55  
    56  func (s *timingsSuite) TestTagTimingsWithChange(c *C) {
    57  	s.st.Lock()
    58  	defer s.st.Unlock()
    59  
    60  	chg := s.st.NewChange("change", "...")
    61  	task := s.st.NewTask("kind", "...")
    62  	task.SetStatus(state.DoingStatus)
    63  	chg.AddTask(task)
    64  
    65  	timing := timings.New(nil)
    66  	state.TagTimingsWithChange(timing, chg)
    67  	timing.Save(s.st)
    68  
    69  	tims, err := timings.Get(s.st, 1, func(tags map[string]string) bool { return true })
    70  	c.Assert(err, IsNil)
    71  	c.Assert(tims, HasLen, 1)
    72  	c.Check(tims[0].NestedTimings, HasLen, 0)
    73  	c.Check(tims[0].Tags, DeepEquals, map[string]string{
    74  		"change-id": chg.ID(),
    75  	})
    76  }
    77  
    78  func (s *timingsSuite) TestTimingsForTask(c *C) {
    79  	s.st.Lock()
    80  	defer s.st.Unlock()
    81  
    82  	task := s.st.NewTask("kind", "...")
    83  	task.SetStatus(state.DoingStatus)
    84  	chg := s.st.NewChange("change", "...")
    85  	chg.AddTask(task)
    86  
    87  	troot := state.TimingsForTask(task)
    88  	span := troot.StartSpan("foo", "bar")
    89  	span.Stop()
    90  	troot.Save(s.st)
    91  
    92  	tims, err := timings.Get(s.st, 1, func(tags map[string]string) bool { return true })
    93  	c.Assert(err, IsNil)
    94  	c.Assert(tims, HasLen, 1)
    95  	c.Check(tims[0].NestedTimings, HasLen, 1)
    96  	c.Check(tims[0].Tags, DeepEquals, map[string]string{
    97  		"change-id":   chg.ID(),
    98  		"task-id":     task.ID(),
    99  		"task-kind":   "kind",
   100  		"task-status": "Doing",
   101  	})
   102  }