github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/remotestorage/chunk_fetcher_test.go (about)

     1  // Copyright 2024 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package remotestorage
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"golang.org/x/sync/errgroup"
    23  
    24  	remotesapi "github.com/dolthub/dolt/go/gen/proto/dolt/services/remotesapi/v1alpha1"
    25  	"github.com/dolthub/dolt/go/store/hash"
    26  )
    27  
    28  func TestFetcherHashSetToGetDlLocsReqsThread(t *testing.T) {
    29  	t.Run("ImmediateClose", func(t *testing.T) {
    30  		reqCh := make(chan hash.HashSet)
    31  		close(reqCh)
    32  
    33  		resCh := make(chan *remotesapi.GetDownloadLocsRequest)
    34  
    35  		err := fetcherHashSetToGetDlLocsReqsThread(context.Background(), reqCh, nil, resCh, 32, "", testIdFunc)
    36  		assert.NoError(t, err)
    37  		_, ok := <-resCh
    38  		assert.False(t, ok)
    39  	})
    40  
    41  	t.Run("CanceledContext", func(t *testing.T) {
    42  		reqCh := make(chan hash.HashSet)
    43  		resCh := make(chan *remotesapi.GetDownloadLocsRequest)
    44  
    45  		ctx, cancel := context.WithCancel(context.Background())
    46  		cancel()
    47  
    48  		err := fetcherHashSetToGetDlLocsReqsThread(ctx, reqCh, nil, resCh, 32, "", testIdFunc)
    49  		assert.Error(t, err)
    50  	})
    51  
    52  	t.Run("BatchesAsExpected", func(t *testing.T) {
    53  		reqCh := make(chan hash.HashSet)
    54  		resCh := make(chan *remotesapi.GetDownloadLocsRequest)
    55  
    56  		eg, ctx := errgroup.WithContext(context.Background())
    57  		eg.Go(func() error {
    58  			return fetcherHashSetToGetDlLocsReqsThread(ctx, reqCh, nil, resCh, 8, "", testIdFunc)
    59  		})
    60  
    61  		// First send a batch of 16 hashes.
    62  		{
    63  			hs := make(hash.HashSet)
    64  			for i := 0; i < 16; i++ {
    65  				var h hash.Hash
    66  				h[0] = byte(i)
    67  				hs.Insert(h)
    68  			}
    69  			reqCh <- hs
    70  		}
    71  		// And read the two requests that get formed...
    72  		apiReq := <-resCh
    73  		assert.NotNil(t, apiReq)
    74  		assert.Len(t, apiReq.ChunkHashes, 8)
    75  		apiReq = <-resCh
    76  		assert.NotNil(t, apiReq)
    77  		assert.Len(t, apiReq.ChunkHashes, 8)
    78  
    79  		// Next send 12 batches of one...
    80  		{
    81  			for i := 0; i < 12; i++ {
    82  				hs := make(hash.HashSet)
    83  				var h hash.Hash
    84  				h[0] = byte(i)
    85  				hs.Insert(h)
    86  				reqCh <- hs
    87  			}
    88  		}
    89  		// Read one batch of 8...
    90  		apiReq = <-resCh
    91  		assert.NotNil(t, apiReq)
    92  		assert.Len(t, apiReq.ChunkHashes, 8)
    93  
    94  		// Send 8 more batches of one...
    95  		{
    96  			for i := 12; i < 20; i++ {
    97  				hs := make(hash.HashSet)
    98  				var h hash.Hash
    99  				h[0] = byte(i)
   100  				hs.Insert(h)
   101  				reqCh <- hs
   102  			}
   103  		}
   104  		// Read a batch of 8 and a batch of 4...
   105  		apiReq = <-resCh
   106  		assert.NotNil(t, apiReq)
   107  		assert.Len(t, apiReq.ChunkHashes, 8)
   108  		apiReq = <-resCh
   109  		assert.NotNil(t, apiReq)
   110  		assert.Len(t, apiReq.ChunkHashes, 4)
   111  
   112  		close(reqCh)
   113  		assert.NoError(t, eg.Wait())
   114  	})
   115  }
   116  
   117  func testIdFunc() (*remotesapi.RepoId, string) {
   118  	return new(remotesapi.RepoId), ""
   119  }