github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/overlord/snapstate/progress_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 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 snapstate
    21  
    22  import (
    23  	. "gopkg.in/check.v1"
    24  
    25  	"github.com/snapcore/snapd/overlord/state"
    26  )
    27  
    28  type progressAdapterTestSuite struct{}
    29  
    30  var _ = Suite(&progressAdapterTestSuite{})
    31  
    32  func (s *progressAdapterTestSuite) TestProgressAdapterWrite(c *C) {
    33  	st := state.New(nil)
    34  	st.Lock()
    35  	m := NewTaskProgressAdapterUnlocked(st.NewTask("op", "msg"))
    36  	st.Unlock()
    37  	p := m.(*taskProgressAdapter)
    38  
    39  	m.Start("msg", 161803)
    40  	c.Check(p.total, Equals, float64(161803))
    41  
    42  	m.Write([]byte("some-bytes"))
    43  	c.Check(p.current, Equals, float64(len("some-bytes")))
    44  }
    45  
    46  func (s *progressAdapterTestSuite) TestProgressAdapterWriteLocked(c *C) {
    47  	st := state.New(nil)
    48  	st.Lock()
    49  	defer st.Unlock()
    50  	m := NewTaskProgressAdapterLocked(st.NewTask("op", "msg"))
    51  	p := m.(*taskProgressAdapter)
    52  
    53  	m.Start("msg", 161803)
    54  	c.Check(p.total, Equals, float64(161803))
    55  
    56  	m.Write([]byte("some-bytes"))
    57  	c.Check(p.current, Equals, float64(len("some-bytes")))
    58  }
    59  
    60  func (s *progressAdapterTestSuite) TestProgressAdapterSetTaskProgress(c *C) {
    61  	st := state.New(nil)
    62  
    63  	st.Lock()
    64  	t := st.NewTask("op", "msg")
    65  	m := NewTaskProgressAdapterUnlocked(t)
    66  	st.Unlock()
    67  
    68  	// we expect 1000 bytes
    69  	m.Start("msg", 1000)
    70  
    71  	// write a single byte (0.1% of the toal)
    72  	m.Write([]byte("1"))
    73  
    74  	// check that the progress is not updated yet
    75  	st.Lock()
    76  	msg, done, total := t.Progress()
    77  	st.Unlock()
    78  	c.Check(msg, Equals, "msg")
    79  	c.Check(done, Equals, 0)
    80  	c.Check(total, Equals, 1000)
    81  
    82  	// write another byte (0.2% now)
    83  	m.Write([]byte("2"))
    84  	// now the progress in the task gets updated (we update every 0.2%)
    85  	st.Lock()
    86  	_, done, total = t.Progress()
    87  	st.Unlock()
    88  	c.Check(done, Equals, 2)
    89  	c.Check(total, Equals, 1000)
    90  }