github.com/kaydxh/golang@v0.0.131/pkg/discovery/etcd/etcd_test.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package etcd_test
    23  
    24  import (
    25  	"context"
    26  	"fmt"
    27  	"testing"
    28  	"time"
    29  
    30  	etcd_ "github.com/kaydxh/golang/pkg/discovery/etcd"
    31  	viper_ "github.com/kaydxh/golang/pkg/viper"
    32  	"github.com/sirupsen/logrus"
    33  	"github.com/stretchr/testify/assert"
    34  	clientv3 "go.etcd.io/etcd/client/v3"
    35  )
    36  
    37  func TestGetKV(t *testing.T) {
    38  	testCases := []struct {
    39  		Addresses []string
    40  	}{
    41  		{
    42  			Addresses: []string{"9.135.121.151:2379"},
    43  		},
    44  	}
    45  
    46  	ctx := context.Background()
    47  	for i, testCase := range testCases {
    48  		t.Run(fmt.Sprintf("name-%d", i), func(t *testing.T) {
    49  			kv := etcd_.NewEtcdKV(etcd_.EtcdConfig{
    50  				Addresses: testCase.Addresses,
    51  			})
    52  			etcdKV, err := kv.GetKV(ctx)
    53  			if err != nil {
    54  				t.Fatalf("failed to get kv: %v, got : %s", testCase.Addresses, err)
    55  			}
    56  			assert.NotNil(t, etcdKV)
    57  		})
    58  	}
    59  
    60  }
    61  
    62  func TestGetKVUntil(t *testing.T) {
    63  	testCases := []struct {
    64  		Addresses   []string
    65  		DailTimeout time.Duration
    66  	}{
    67  		{
    68  			Addresses:   []string{"9.135.121.151:2379"},
    69  			DailTimeout: 3 * time.Second,
    70  		},
    71  	}
    72  
    73  	for i, testCase := range testCases {
    74  		t.Run(fmt.Sprintf("name-%d", i), func(t *testing.T) {
    75  			db := etcd_.NewEtcdKV(etcd_.EtcdConfig{
    76  				Addresses: testCase.Addresses,
    77  			}, etcd_.WithDialTimeout(testCase.DailTimeout))
    78  			sqlDB, err := db.GetKVUntil(context.Background(), 5*time.Second, 20*time.Second)
    79  			if err != nil {
    80  				t.Fatalf("failed to get kv: %v, got : %s", testCase.Addresses, err)
    81  			}
    82  			assert.NotNil(t, sqlDB)
    83  		})
    84  	}
    85  
    86  }
    87  
    88  func CreateCallback(ctx context.Context, key, value string) {
    89  	logrus.Infof("create key: %v, value: %v", key, value)
    90  }
    91  
    92  func DeleteCallback(ctx context.Context, key, value string) {
    93  	logrus.Infof("delete key: %v, value: %v", key, value)
    94  }
    95  
    96  func InstallEtcd(ctx context.Context) (*etcd_.EtcdKV, error) {
    97  	cfgFile := "./etcd.yaml"
    98  	config := etcd_.NewConfig(etcd_.WithViper(viper_.GetViper(cfgFile, "discovery.etcd")))
    99  
   100  	kv, err := config.Complete().New(ctx, CreateCallback, DeleteCallback)
   101  	if err != nil {
   102  		logrus.Errorf("failed to new config err: %v", err)
   103  		return nil, err
   104  	}
   105  
   106  	return kv, nil
   107  
   108  }
   109  
   110  func TestNew(t *testing.T) {
   111  
   112  	ctx := context.Background()
   113  	kv, err := InstallEtcd(ctx)
   114  	if err != nil {
   115  		t.Errorf("failed to new config err: %v", err)
   116  		return
   117  	}
   118  	_ = kv
   119  	kv.Lock(ctx, etcd_.WithLockKey("/kay/lock"), etcd_.WithLockTTL(15*time.Second))
   120  	kv.Lock(ctx, etcd_.WithLockKey("/kay/lock1"), etcd_.WithLockTTL(15*time.Second))
   121  
   122  	time.Sleep(20 * time.Second)
   123  
   124  	// only unlock the latest lock /kay/lock1
   125  	kv.Unlock(ctx)
   126  	kv.TxPipelined(ctx, nil, []clientv3.Op{clientv3.OpPut("foo", "foo_new_value")}, nil)
   127  	/*
   128  		go func() {
   129  			t.Logf("before lock by routine 1")
   130  			kv.Lock(ctx, "kay/lock", 15*time.Second)
   131  			t.Logf("after lock by routine 1")
   132  		}()
   133  	*/
   134  
   135  	select {}
   136  }
   137  
   138  func TestTxPipelined(t *testing.T) {
   139  	ctx := context.Background()
   140  	kv, err := InstallEtcd(ctx)
   141  	if err != nil {
   142  		t.Errorf("failed to new config err: %v", err)
   143  		return
   144  	}
   145  	kv.TxPipelined(ctx, nil, []clientv3.Op{clientv3.OpPut("foo", "foo_new_value")}, nil)
   146  }