github.com/matrixorigin/matrixone@v0.7.0/pkg/lockservice/waiter_queue_test.go (about)

     1  // Copyright 2023 Matrix Origin
     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 lockservice
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  func TestPut(t *testing.T) {
    24  	q := newWaiterQueue()
    25  	w := acquireWaiter([]byte("w"))
    26  	q.put(w)
    27  	assert.Equal(t, 1, len(q.waiters))
    28  }
    29  
    30  func TestLen(t *testing.T) {
    31  	q := newWaiterQueue()
    32  	q.put(acquireWaiter([]byte("w")))
    33  	q.put(acquireWaiter([]byte("w1")))
    34  	q.put(acquireWaiter([]byte("w2")))
    35  	assert.Equal(t, 3, q.len())
    36  
    37  	v, remain := q.pop()
    38  	assert.Equal(t, q.waiters[1:], remain)
    39  	assert.Equal(t, []byte("w"), v.txnID)
    40  
    41  	defer func() {
    42  		if err := recover(); err != nil {
    43  			return
    44  		}
    45  		assert.Fail(t, "must panic")
    46  	}()
    47  	q = newWaiterQueue()
    48  	q.pop()
    49  }
    50  
    51  func TestReset(t *testing.T) {
    52  	q := newWaiterQueue()
    53  	q.put(acquireWaiter([]byte("w")))
    54  	q.put(acquireWaiter([]byte("w1")))
    55  	q.put(acquireWaiter([]byte("w2")))
    56  	q.pop()
    57  
    58  	q.reset()
    59  	assert.Empty(t, q.waiters)
    60  }
    61  
    62  func TestIterTxns(t *testing.T) {
    63  	q := newWaiterQueue()
    64  	q.put(acquireWaiter([]byte("w")))
    65  	q.put(acquireWaiter([]byte("w1")))
    66  	q.put(acquireWaiter([]byte("w2")))
    67  
    68  	var values [][]byte
    69  	v := 0
    70  	q.iter(func(b []byte) bool {
    71  		values = append(values, b)
    72  		v++
    73  		return v < 2
    74  	})
    75  	assert.Equal(t, [][]byte{[]byte("w"), []byte("w1")}, values)
    76  }