github.com/lfch/etcd-io/tests/v3@v3.0.0-20221004140520-eac99acd3e9d/integration/hashkv_test.go (about)

     1  // Copyright 2022 The etcd Authors
     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 integration
    16  
    17  import (
    18  	"context"
    19  	"net"
    20  	"net/http"
    21  	"testing"
    22  	"time"
    23  
    24  	clientv3 "github.com/lfch/etcd-io/client/v3"
    25  	"github.com/lfch/etcd-io/server/v3/etcdserver"
    26  	"github.com/lfch/etcd-io/server/v3/storage/mvcc/testutil"
    27  	integration2 "github.com/lfch/etcd-io/tests/v3/framework/integration"
    28  )
    29  
    30  // TODO: Change this to fuzz test
    31  func TestCompactionHash(t *testing.T) {
    32  	integration2.BeforeTest(t)
    33  
    34  	clus := integration2.NewCluster(t, &integration2.ClusterConfig{Size: 1})
    35  	defer clus.Terminate(t)
    36  
    37  	cc, err := clus.ClusterClient(t)
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	client := &http.Client{
    42  		Transport: &http.Transport{
    43  			DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
    44  				return net.Dial("unix", clus.Members[0].PeerURLs[0].Host)
    45  			},
    46  		},
    47  	}
    48  
    49  	testutil.TestCompactionHash(context.Background(), t, hashTestCase{cc, clus.Members[0].GRPCURL(), client}, 1000)
    50  }
    51  
    52  type hashTestCase struct {
    53  	*clientv3.Client
    54  	url  string
    55  	http *http.Client
    56  }
    57  
    58  func (tc hashTestCase) Put(ctx context.Context, key, value string) error {
    59  	_, err := tc.Client.Put(ctx, key, value)
    60  	return err
    61  }
    62  
    63  func (tc hashTestCase) Delete(ctx context.Context, key string) error {
    64  	_, err := tc.Client.Delete(ctx, key)
    65  	return err
    66  }
    67  
    68  func (tc hashTestCase) HashByRev(ctx context.Context, rev int64) (testutil.KeyValueHash, error) {
    69  	resp, err := etcdserver.HashByRev(ctx, tc.http, "http://unix", rev)
    70  	return testutil.KeyValueHash{Hash: resp.Hash, CompactRevision: resp.CompactRevision, Revision: resp.Header.Revision}, err
    71  }
    72  
    73  func (tc hashTestCase) Defrag(ctx context.Context) error {
    74  	_, err := tc.Client.Defragment(ctx, tc.url)
    75  	return err
    76  }
    77  
    78  func (tc hashTestCase) Compact(ctx context.Context, rev int64) error {
    79  	_, err := tc.Client.Compact(ctx, rev)
    80  	// Wait for compaction to be compacted
    81  	time.Sleep(50 * time.Millisecond)
    82  	return err
    83  }