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

     1  // Copyright 2016 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 clientv3_test
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"log"
    21  
    22  	"github.com/lfch/etcd-io/api/v3/v3rpc/rpctypes"
    23  )
    24  
    25  func mockKV_put() {}
    26  
    27  func ExampleKV_put() {
    28  	forUnitTestsRunInMockedContext(mockKV_put, func() {
    29  		cli, err := clientv3.New(clientv3.Config{
    30  			Endpoints:   exampleEndpoints(),
    31  			DialTimeout: dialTimeout,
    32  		})
    33  		if err != nil {
    34  			log.Fatal(err)
    35  		}
    36  		defer cli.Close()
    37  
    38  		ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
    39  		_, err = cli.Put(ctx, "sample_key", "sample_value")
    40  		cancel()
    41  		if err != nil {
    42  			log.Fatal(err)
    43  		}
    44  	})
    45  	// Output:
    46  }
    47  
    48  func mockKV_putErrorHandling() {
    49  	fmt.Println("client-side error: etcdserver: key is not provided")
    50  }
    51  
    52  func ExampleKV_putErrorHandling() {
    53  	forUnitTestsRunInMockedContext(mockKV_putErrorHandling, func() {
    54  		cli, err := clientv3.New(clientv3.Config{
    55  			Endpoints:   exampleEndpoints(),
    56  			DialTimeout: dialTimeout,
    57  		})
    58  		if err != nil {
    59  			log.Fatal(err)
    60  		}
    61  		defer cli.Close()
    62  
    63  		ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
    64  		_, err = cli.Put(ctx, "", "sample_value")
    65  		cancel()
    66  		if err != nil {
    67  			switch err {
    68  			case context.Canceled:
    69  				fmt.Printf("ctx is canceled by another routine: %v\n", err)
    70  			case context.DeadlineExceeded:
    71  				fmt.Printf("ctx is attached with a deadline is exceeded: %v\n", err)
    72  			case rpctypes.ErrEmptyKey:
    73  				fmt.Printf("client-side error: %v\n", err)
    74  			default:
    75  				fmt.Printf("bad cluster endpoints, which are not etcd servers: %v\n", err)
    76  			}
    77  		}
    78  	})
    79  	// Output: client-side error: etcdserver: key is not provided
    80  }
    81  
    82  func mockKV_get() {
    83  	fmt.Println("foo : bar")
    84  }
    85  
    86  func ExampleKV_get() {
    87  	forUnitTestsRunInMockedContext(mockKV_get, func() {
    88  		cli, err := clientv3.New(clientv3.Config{
    89  			Endpoints:   exampleEndpoints(),
    90  			DialTimeout: dialTimeout,
    91  		})
    92  		if err != nil {
    93  			log.Fatal(err)
    94  		}
    95  		defer cli.Close()
    96  
    97  		_, err = cli.Put(context.TODO(), "foo", "bar")
    98  		if err != nil {
    99  			log.Fatal(err)
   100  		}
   101  
   102  		ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
   103  		resp, err := cli.Get(ctx, "foo")
   104  		cancel()
   105  		if err != nil {
   106  			log.Fatal(err)
   107  		}
   108  		for _, ev := range resp.Kvs {
   109  			fmt.Printf("%s : %s\n", ev.Key, ev.Value)
   110  		}
   111  	})
   112  	// Output: foo : bar
   113  }
   114  
   115  func mockKV_getWithRev() {
   116  	fmt.Println("foo : bar1")
   117  }
   118  
   119  func ExampleKV_getWithRev() {
   120  	forUnitTestsRunInMockedContext(mockKV_getWithRev, func() {
   121  		cli, err := clientv3.New(clientv3.Config{
   122  			Endpoints:   exampleEndpoints(),
   123  			DialTimeout: dialTimeout,
   124  		})
   125  		if err != nil {
   126  			log.Fatal(err)
   127  		}
   128  		defer cli.Close()
   129  
   130  		presp, err := cli.Put(context.TODO(), "foo", "bar1")
   131  		if err != nil {
   132  			log.Fatal(err)
   133  		}
   134  		_, err = cli.Put(context.TODO(), "foo", "bar2")
   135  		if err != nil {
   136  			log.Fatal(err)
   137  		}
   138  
   139  		ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
   140  		resp, err := cli.Get(ctx, "foo", clientv3.WithRev(presp.Header.Revision))
   141  		cancel()
   142  		if err != nil {
   143  			log.Fatal(err)
   144  		}
   145  		for _, ev := range resp.Kvs {
   146  			fmt.Printf("%s : %s\n", ev.Key, ev.Value)
   147  		}
   148  	})
   149  	// Output: foo : bar1
   150  }
   151  
   152  func mockKV_getSortedPrefix() {
   153  	fmt.Println(`key_2 : value`)
   154  	fmt.Println(`key_1 : value`)
   155  	fmt.Println(`key_0 : value`)
   156  }
   157  
   158  func ExampleKV_getSortedPrefix() {
   159  	forUnitTestsRunInMockedContext(mockKV_getSortedPrefix, func() {
   160  		cli, err := clientv3.New(clientv3.Config{
   161  			Endpoints:   exampleEndpoints(),
   162  			DialTimeout: dialTimeout,
   163  		})
   164  		if err != nil {
   165  			log.Fatal(err)
   166  		}
   167  		defer cli.Close()
   168  
   169  		for i := range make([]int, 3) {
   170  			ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
   171  			_, err = cli.Put(ctx, fmt.Sprintf("key_%d", i), "value")
   172  			cancel()
   173  			if err != nil {
   174  				log.Fatal(err)
   175  			}
   176  		}
   177  
   178  		ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
   179  		resp, err := cli.Get(ctx, "key", clientv3.WithPrefix(), clientv3.WithSort(clientv3.SortByKey, clientv3.SortDescend))
   180  		cancel()
   181  		if err != nil {
   182  			log.Fatal(err)
   183  		}
   184  		for _, ev := range resp.Kvs {
   185  			fmt.Printf("%s : %s\n", ev.Key, ev.Value)
   186  		}
   187  	})
   188  	// Output:
   189  	// key_2 : value
   190  	// key_1 : value
   191  	// key_0 : value
   192  }
   193  
   194  func mockKV_delete() {
   195  	fmt.Println("Deleted all keys: true")
   196  }
   197  
   198  func ExampleKV_delete() {
   199  	forUnitTestsRunInMockedContext(mockKV_delete, func() {
   200  		cli, err := clientv3.New(clientv3.Config{
   201  			Endpoints:   exampleEndpoints(),
   202  			DialTimeout: dialTimeout,
   203  		})
   204  		if err != nil {
   205  			log.Fatal(err)
   206  		}
   207  		defer cli.Close()
   208  
   209  		ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
   210  		defer cancel()
   211  
   212  		// count keys about to be deleted
   213  		gresp, err := cli.Get(ctx, "key", clientv3.WithPrefix())
   214  		if err != nil {
   215  			log.Fatal(err)
   216  		}
   217  
   218  		// delete the keys
   219  		dresp, err := cli.Delete(ctx, "key", clientv3.WithPrefix())
   220  		if err != nil {
   221  			log.Fatal(err)
   222  		}
   223  
   224  		fmt.Println("Deleted all keys:", int64(len(gresp.Kvs)) == dresp.Deleted)
   225  	})
   226  	// Output:
   227  	// Deleted all keys: true
   228  }
   229  
   230  func mockKV_compact() {}
   231  
   232  func ExampleKV_compact() {
   233  	forUnitTestsRunInMockedContext(mockKV_compact, func() {
   234  		cli, err := clientv3.New(clientv3.Config{
   235  			Endpoints:   exampleEndpoints(),
   236  			DialTimeout: dialTimeout,
   237  		})
   238  		if err != nil {
   239  			log.Fatal(err)
   240  		}
   241  		defer cli.Close()
   242  
   243  		ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
   244  		resp, err := cli.Get(ctx, "foo")
   245  		cancel()
   246  		if err != nil {
   247  			log.Fatal(err)
   248  		}
   249  		compRev := resp.Header.Revision // specify compact revision of your choice
   250  
   251  		ctx, cancel = context.WithTimeout(context.Background(), requestTimeout)
   252  		_, err = cli.Compact(ctx, compRev)
   253  		cancel()
   254  		if err != nil {
   255  			log.Fatal(err)
   256  		}
   257  	})
   258  	// Output:
   259  }
   260  
   261  func mockKV_txn() {
   262  	fmt.Println("key : XYZ")
   263  }
   264  
   265  func ExampleKV_txn() {
   266  	forUnitTestsRunInMockedContext(mockKV_txn, func() {
   267  		cli, err := clientv3.New(clientv3.Config{
   268  			Endpoints:   exampleEndpoints(),
   269  			DialTimeout: dialTimeout,
   270  		})
   271  		if err != nil {
   272  			log.Fatal(err)
   273  		}
   274  		defer cli.Close()
   275  
   276  		kvc := clientv3.NewKV(cli)
   277  
   278  		_, err = kvc.Put(context.TODO(), "key", "xyz")
   279  		if err != nil {
   280  			log.Fatal(err)
   281  		}
   282  
   283  		ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
   284  		_, err = kvc.Txn(ctx).
   285  			// txn value comparisons are lexical
   286  			If(clientv3.Compare(clientv3.Value("key"), ">", "abc")).
   287  			// the "Then" runs, since "xyz" > "abc"
   288  			Then(clientv3.OpPut("key", "XYZ")).
   289  			// the "Else" does not run
   290  			Else(clientv3.OpPut("key", "ABC")).
   291  			Commit()
   292  		cancel()
   293  		if err != nil {
   294  			log.Fatal(err)
   295  		}
   296  
   297  		gresp, err := kvc.Get(context.TODO(), "key")
   298  		if err != nil {
   299  			log.Fatal(err)
   300  		}
   301  		for _, ev := range gresp.Kvs {
   302  			fmt.Printf("%s : %s\n", ev.Key, ev.Value)
   303  		}
   304  	})
   305  	// Output: key : XYZ
   306  }
   307  
   308  func mockKV_do() {}
   309  
   310  func ExampleKV_do() {
   311  	forUnitTestsRunInMockedContext(mockKV_do, func() {
   312  		cli, err := clientv3.New(clientv3.Config{
   313  			Endpoints:   exampleEndpoints(),
   314  			DialTimeout: dialTimeout,
   315  		})
   316  		if err != nil {
   317  			log.Fatal(err)
   318  		}
   319  		defer cli.Close()
   320  
   321  		ops := []clientv3.Op{
   322  			clientv3.OpPut("put-key", "123"),
   323  			clientv3.OpGet("put-key"),
   324  			clientv3.OpPut("put-key", "456")}
   325  
   326  		for _, op := range ops {
   327  			if _, err := cli.Do(context.TODO(), op); err != nil {
   328  				log.Fatal(err)
   329  			}
   330  		}
   331  	})
   332  	// Output:
   333  }