github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/search/progress_test.go (about)

     1  // Copyright 2020 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package search
     6  
     7  import (
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/keybase/client/go/kbfs/test/clocktest"
    12  	"github.com/keybase/client/go/kbfs/tlf"
    13  	"github.com/keybase/client/go/protocol/keybase1"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestProgress(t *testing.T) {
    18  	clock, start := clocktest.NewTestClockAndTimeNow()
    19  	p := NewProgress(clock)
    20  
    21  	t.Log("Queue one TLF")
    22  	id1 := tlf.FakeID(1, tlf.Private)
    23  	size1 := uint64(100)
    24  	p.tlfQueue(id1, size1)
    25  
    26  	type expectedProg struct {
    27  		currTlf      tlf.ID
    28  		q            []tlf.ID
    29  		overallTotal int64
    30  		overallSoFar int64
    31  		overallEnd   keybase1.Time
    32  		currTotal    int64
    33  		currSoFar    int64
    34  		currEnd      keybase1.Time
    35  	}
    36  	checkStatus := func(e expectedProg) {
    37  		currProg, overallProg, currTlf, q := p.GetStatus()
    38  		require.Len(t, q, len(e.q))
    39  		m := make(map[tlf.ID]bool, len(q))
    40  		for _, id := range q {
    41  			m[id] = true
    42  		}
    43  		for _, id := range e.q {
    44  			delete(m, id)
    45  		}
    46  		require.Len(t, m, 0)
    47  		require.Equal(t, e.currTlf, currTlf)
    48  		require.Equal(t, e.overallTotal, overallProg.BytesTotal)
    49  		require.Equal(t, e.overallSoFar, overallProg.BytesSoFar)
    50  		require.Equal(t, e.overallEnd, overallProg.EndEstimate)
    51  		require.Equal(t, e.currTotal, currProg.BytesTotal)
    52  		require.Equal(t, e.currSoFar, currProg.BytesSoFar)
    53  		require.Equal(t, e.currEnd, currProg.EndEstimate)
    54  	}
    55  	checkStatus(expectedProg{
    56  		q:            []tlf.ID{id1},
    57  		overallTotal: int64(size1),
    58  	})
    59  
    60  	t.Log("Start the index")
    61  	err := p.startIndex(id1, size1, indexFull)
    62  	require.NoError(t, err)
    63  	checkStatus(expectedProg{
    64  		currTlf:      id1,
    65  		currTotal:    int64(size1),
    66  		overallTotal: int64(size1),
    67  	})
    68  
    69  	t.Log("Index 10 bytes in 1 second")
    70  	clock.Add(1 * time.Second)
    71  	p.indexedBytes(10)
    72  	currEndEstimate := keybase1.ToTime(start.Add(10 * time.Second))
    73  	checkStatus(expectedProg{
    74  		currTlf:      id1,
    75  		currTotal:    int64(size1),
    76  		currSoFar:    10,
    77  		currEnd:      currEndEstimate,
    78  		overallTotal: int64(size1),
    79  		overallSoFar: 10,
    80  		overallEnd:   currEndEstimate,
    81  	})
    82  
    83  	t.Log("Queue another TLF")
    84  	id2 := tlf.FakeID(2, tlf.Private)
    85  	size2 := uint64(900)
    86  	p.tlfQueue(id2, size2)
    87  	overallEndEstimate := keybase1.ToTime(start.Add(100 * time.Second))
    88  	checkStatus(expectedProg{
    89  		currTlf:      id1,
    90  		q:            []tlf.ID{id2},
    91  		currTotal:    int64(size1),
    92  		currSoFar:    10,
    93  		currEnd:      currEndEstimate,
    94  		overallTotal: int64(size1 + size2),
    95  		overallSoFar: 10,
    96  		overallEnd:   overallEndEstimate,
    97  	})
    98  
    99  	t.Log("Complete first index")
   100  	clock.Add(9 * time.Second)
   101  	p.indexedBytes(90)
   102  	checkStatus(expectedProg{
   103  		currTlf:      id1,
   104  		q:            []tlf.ID{id2},
   105  		currTotal:    int64(size1),
   106  		currSoFar:    int64(size1),
   107  		currEnd:      currEndEstimate,
   108  		overallTotal: int64(size1 + size2),
   109  		overallSoFar: int64(size1),
   110  		overallEnd:   overallEndEstimate,
   111  	})
   112  
   113  	err = p.finishIndex(id1)
   114  	require.NoError(t, err)
   115  	checkStatus(expectedProg{
   116  		q:            []tlf.ID{id2},
   117  		overallTotal: int64(size2),
   118  		overallEnd:   overallEndEstimate,
   119  	})
   120  }