github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/fs/mpather/worker_test.go (about)

     1  // Package mpather provides per-mountpath concepts.
     2  /*
     3   * Copyright (c) 2018-2021, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package mpather_test
     6  
     7  import (
     8  	"os"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/NVIDIA/aistore/cmn/atomic"
    13  	"github.com/NVIDIA/aistore/cmn/cos"
    14  	"github.com/NVIDIA/aistore/core"
    15  	"github.com/NVIDIA/aistore/fs"
    16  	"github.com/NVIDIA/aistore/fs/mpather"
    17  	"github.com/NVIDIA/aistore/tools"
    18  	"github.com/NVIDIA/aistore/tools/tassert"
    19  )
    20  
    21  func TestWorkerGroup(t *testing.T) {
    22  	var (
    23  		desc = tools.ObjectsDesc{
    24  			CTs: []tools.ContentTypeDesc{
    25  				{Type: fs.ObjectType, ContentCnt: 100},
    26  			},
    27  			MountpathsCnt: 10,
    28  			ObjectSize:    cos.KiB,
    29  		}
    30  		out     = tools.PrepareObjects(t, desc)
    31  		counter = atomic.NewInt32(0)
    32  	)
    33  	defer os.RemoveAll(out.Dir)
    34  
    35  	wg := mpather.NewWorkerGroup(&mpather.WorkerGroupOpts{
    36  		Callback: func(_ *core.LOM, _ []byte) {
    37  			counter.Inc()
    38  		},
    39  		QueueSize: 10,
    40  	})
    41  	defer wg.Stop()
    42  
    43  	wg.Run()
    44  
    45  	for _, fqn := range out.FQNs[fs.ObjectType] {
    46  		lom := &core.LOM{}
    47  		err := lom.InitFQN(fqn, &out.Bck)
    48  		tassert.CheckError(t, err)
    49  
    50  		_, err = wg.PostLIF(lom)
    51  		tassert.CheckError(t, err)
    52  	}
    53  
    54  	// Give some time for the workers to pick all the tasks.
    55  	time.Sleep(time.Second)
    56  
    57  	tassert.Errorf(
    58  		t, int(counter.Load()) == len(out.FQNs[fs.ObjectType]),
    59  		"invalid number of objects visited (%d vs %d)", counter.Load(), len(out.FQNs[fs.ObjectType]),
    60  	)
    61  }