github.com/wrgl/wrgl@v0.14.0/pkg/progress/progress_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright © 2022 Wrangle Ltd
     3  
     4  package progress
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/wrgl/wrgl/pkg/testutils"
    12  )
    13  
    14  func TestTracker(t *testing.T) {
    15  	tr := NewSingleTracker(10*time.Millisecond, 10)
    16  	c := tr.Start()
    17  	tr.SetCurrent(1)
    18  	testutils.Retry(t, 10*time.Millisecond, 10, func() bool {
    19  		return assert.ObjectsAreEqual(Event{
    20  			Progress: 1,
    21  			Total:    10,
    22  		}, <-c)
    23  	}, "")
    24  
    25  	tr.Add(2)
    26  	testutils.Retry(t, 10*time.Millisecond, 10, func() bool {
    27  		return assert.ObjectsAreEqual(Event{
    28  			Progress: 3,
    29  			Total:    10,
    30  		}, <-c)
    31  	}, "")
    32  
    33  	tr.Stop()
    34  	ok := true
    35  	for ok {
    36  		_, ok = <-c
    37  	}
    38  	// c is closed after a few straggler events
    39  }
    40  
    41  func TestJoinChannels(t *testing.T) {
    42  	tr1 := NewSingleTracker(3*time.Millisecond, 10)
    43  	tr2 := NewSingleTracker(3*time.Millisecond, 15)
    44  	tr3 := NewSingleTracker(3*time.Millisecond, 27)
    45  	tr := JoinTrackers(tr1, tr2, tr3)
    46  	assert.Equal(t, 3*time.Millisecond, tr.Duration())
    47  	c := tr.Start()
    48  
    49  	tr1.SetCurrent(3)
    50  	for e := range c {
    51  		assert.Equal(t, int64(52), e.Total)
    52  		if e.Progress == 3 {
    53  			break
    54  		}
    55  	}
    56  
    57  	tr2.SetCurrent(2)
    58  	for e := range c {
    59  		assert.Equal(t, int64(52), e.Total)
    60  		if e.Progress == 5 {
    61  			break
    62  		}
    63  	}
    64  
    65  	tr3.SetCurrent(12)
    66  	for e := range c {
    67  		assert.Equal(t, int64(52), e.Total)
    68  		if e.Progress == 17 {
    69  			break
    70  		}
    71  	}
    72  
    73  	tr.Stop()
    74  	ok := true
    75  	for ok {
    76  		_, ok = <-c
    77  	}
    78  }