vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletserver/tx_prep_pool_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package tabletserver
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestEmptyPrep(t *testing.T) {
    26  	pp := NewTxPreparedPool(0)
    27  	want := "prepared transactions exceeded limit: 0"
    28  	err := pp.Put(nil, "aa")
    29  	if err == nil || err.Error() != want {
    30  		t.Errorf("Put err: %v, want %s", err, want)
    31  	}
    32  }
    33  
    34  func TestPrepPut(t *testing.T) {
    35  	pp := NewTxPreparedPool(2)
    36  	err := pp.Put(nil, "aa")
    37  	require.NoError(t, err)
    38  	err = pp.Put(nil, "bb")
    39  	require.NoError(t, err)
    40  	want := "prepared transactions exceeded limit: 2"
    41  	err = pp.Put(nil, "cc")
    42  	if err == nil || err.Error() != want {
    43  		t.Errorf("Put err: %v, want %s", err, want)
    44  	}
    45  	err = pp.Put(nil, "aa")
    46  	want = "duplicate DTID in Prepare: aa"
    47  	if err == nil || err.Error() != want {
    48  		t.Errorf("Put err: %v, want %s", err, want)
    49  	}
    50  	_, err = pp.FetchForCommit("aa")
    51  	require.NoError(t, err)
    52  	err = pp.Put(nil, "aa")
    53  	want = "duplicate DTID in Prepare: aa"
    54  	if err == nil || err.Error() != want {
    55  		t.Errorf("Put err: %v, want %s", err, want)
    56  	}
    57  	pp.Forget("aa")
    58  	err = pp.Put(nil, "aa")
    59  	require.NoError(t, err)
    60  }
    61  
    62  func TestPrepFetchForRollback(t *testing.T) {
    63  	pp := NewTxPreparedPool(2)
    64  	conn := &StatefulConnection{}
    65  	pp.Put(conn, "aa")
    66  	got := pp.FetchForRollback("bb")
    67  	if got != nil {
    68  		t.Errorf("Get(bb): %v, want nil", got)
    69  	}
    70  	got = pp.FetchForRollback("aa")
    71  	if got != conn {
    72  		t.Errorf("pp.Get(aa): %p, want %p", got, conn)
    73  	}
    74  	got = pp.FetchForRollback("aa")
    75  	if got != nil {
    76  		t.Errorf("Get(aa): %v, want nil", got)
    77  	}
    78  }
    79  
    80  func TestPrepFetchForCommit(t *testing.T) {
    81  	pp := NewTxPreparedPool(2)
    82  	conn := &StatefulConnection{}
    83  	got, err := pp.FetchForCommit("aa")
    84  	require.NoError(t, err)
    85  	if got != nil {
    86  		t.Errorf("Get(aa): %v, want nil", got)
    87  	}
    88  	pp.Put(conn, "aa")
    89  	got, err = pp.FetchForCommit("aa")
    90  	require.NoError(t, err)
    91  	if got != conn {
    92  		t.Errorf("pp.Get(aa): %p, want %p", got, conn)
    93  	}
    94  	_, err = pp.FetchForCommit("aa")
    95  	want := "committing"
    96  	if err == nil || err.Error() != want {
    97  		t.Errorf("FetchForCommit err: %v, want %s", err, want)
    98  	}
    99  	pp.SetFailed("aa")
   100  	_, err = pp.FetchForCommit("aa")
   101  	want = "failed"
   102  	if err == nil || err.Error() != want {
   103  		t.Errorf("FetchForCommit err: %v, want %s", err, want)
   104  	}
   105  	pp.SetFailed("bb")
   106  	_, err = pp.FetchForCommit("bb")
   107  	want = "failed"
   108  	if err == nil || err.Error() != want {
   109  		t.Errorf("FetchForCommit err: %v, want %s", err, want)
   110  	}
   111  	pp.Forget("aa")
   112  	got, err = pp.FetchForCommit("aa")
   113  	require.NoError(t, err)
   114  	if got != nil {
   115  		t.Errorf("Get(aa): %v, want nil", got)
   116  	}
   117  }
   118  
   119  func TestPrepFetchAll(t *testing.T) {
   120  	pp := NewTxPreparedPool(2)
   121  	conn1 := &StatefulConnection{}
   122  	conn2 := &StatefulConnection{}
   123  	pp.Put(conn1, "aa")
   124  	pp.Put(conn2, "bb")
   125  	got := pp.FetchAll()
   126  	if len(got) != 2 {
   127  		t.Errorf("FetchAll len: %d, want 2", len(got))
   128  	}
   129  	if len(pp.conns) != 0 {
   130  		t.Errorf("len(pp.conns): %d, want 0", len(pp.conns))
   131  	}
   132  }