...

Source file src/go.etcd.io/etcd/client/v3/txn_test.go

Documentation: go.etcd.io/etcd/client/v3

     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
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  	"time"
    21  
    22  	"go.etcd.io/etcd/client/pkg/v3/testutil"
    23  )
    24  
    25  func TestTxnPanics(t *testing.T) {
    26  	testutil.RegisterLeakDetection(t)
    27  
    28  	kv := &kv{}
    29  
    30  	errc := make(chan string, 1)
    31  	df := func() {
    32  		if s := recover(); s != nil {
    33  			errc <- s.(string)
    34  		}
    35  	}
    36  
    37  	cmp := Compare(CreateRevision("foo"), "=", 0)
    38  	op := OpPut("foo", "bar")
    39  
    40  	tests := []struct {
    41  		f func()
    42  
    43  		err string
    44  	}{
    45  		{
    46  			f: func() {
    47  				defer df()
    48  				kv.Txn(context.TODO()).If(cmp).If(cmp)
    49  			},
    50  
    51  			err: "cannot call If twice!",
    52  		},
    53  		{
    54  			f: func() {
    55  				defer df()
    56  				kv.Txn(context.TODO()).Then(op).If(cmp)
    57  			},
    58  
    59  			err: "cannot call If after Then!",
    60  		},
    61  		{
    62  			f: func() {
    63  				defer df()
    64  				kv.Txn(context.TODO()).Else(op).If(cmp)
    65  			},
    66  
    67  			err: "cannot call If after Else!",
    68  		},
    69  		{
    70  			f: func() {
    71  				defer df()
    72  				kv.Txn(context.TODO()).Then(op).Then(op)
    73  			},
    74  
    75  			err: "cannot call Then twice!",
    76  		},
    77  		{
    78  			f: func() {
    79  				defer df()
    80  				kv.Txn(context.TODO()).Else(op).Then(op)
    81  			},
    82  
    83  			err: "cannot call Then after Else!",
    84  		},
    85  		{
    86  			f: func() {
    87  				defer df()
    88  				kv.Txn(context.TODO()).Else(op).Else(op)
    89  			},
    90  
    91  			err: "cannot call Else twice!",
    92  		},
    93  	}
    94  
    95  	for i, tt := range tests {
    96  		go tt.f()
    97  		select {
    98  		case err := <-errc:
    99  			if err != tt.err {
   100  				t.Errorf("#%d: got %s, wanted %s", i, err, tt.err)
   101  			}
   102  		case <-time.After(time.Second):
   103  			t.Errorf("#%d: did not panic, wanted panic %s", i, tt.err)
   104  		}
   105  	}
   106  }
   107  

View as plain text