github.com/matrixorigin/matrixone@v1.2.0/pkg/txn/service/service_event_test.go (about)

     1  // Copyright 2021 - 2022 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 service
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/pb/txn"
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestAcquireAndCloseWaiter(t *testing.T) {
    26  	checker := func(w *waiter) {
    27  		assert.False(t, w.mu.closed)
    28  		assert.False(t, w.mu.notified)
    29  	}
    30  
    31  	w := acquireWaiter()
    32  	assert.Equal(t, 1, w.mu.ref)
    33  	checker(w)
    34  
    35  	w.ref()
    36  	w.notify(txn.TxnStatus_Active)
    37  	w.close()
    38  	assert.True(t, w.mu.closed)
    39  	assert.True(t, w.mu.notified)
    40  
    41  	w2 := w
    42  	w.unref()
    43  	assert.Equal(t, 0, w2.mu.ref)
    44  	checker(w2)
    45  }
    46  
    47  func TestWaiterNotify(t *testing.T) {
    48  	w := acquireWaiter()
    49  	defer w.close()
    50  
    51  	w.notify(txn.TxnStatus_Committed)
    52  	s, err := w.wait(context.Background())
    53  	assert.NoError(t, err)
    54  	assert.Equal(t, txn.TxnStatus_Committed, s)
    55  }
    56  
    57  func TestWaiterNotifyMoreTimesWillPanic(t *testing.T) {
    58  	defer func() {
    59  		if err := recover(); err == nil {
    60  			assert.Fail(t, "must panic")
    61  		}
    62  	}()
    63  
    64  	w := acquireWaiter()
    65  	defer w.close()
    66  
    67  	w.notify(txn.TxnStatus_Committed)
    68  	w.notify(txn.TxnStatus_Aborted)
    69  }
    70  
    71  func TestWaiterTimeout(t *testing.T) {
    72  	w := acquireWaiter()
    73  	defer w.close()
    74  	ctx, cancel := context.WithCancel(context.Background())
    75  	cancel()
    76  	_, err := w.wait(ctx)
    77  	assert.Error(t, err)
    78  }
    79  
    80  func TestNotifyWithEmptyWaiters(t *testing.T) {
    81  	nt := acquireNotifier()
    82  	defer nt.close(txn.TxnStatus_Aborted)
    83  
    84  	assert.Equal(t, 0, nt.notify(txn.TxnStatus_Active))
    85  }
    86  
    87  func TestNotify(t *testing.T) {
    88  	nt := acquireNotifier()
    89  	defer nt.close(txn.TxnStatus_Aborted)
    90  
    91  	w := acquireWaiter()
    92  	defer func() {
    93  		w.close()
    94  		assert.Equal(t, 0, w.mu.ref)
    95  		assert.False(t, w.mu.closed)
    96  		assert.False(t, w.mu.notified)
    97  	}()
    98  
    99  	nt.addWaiter(w, txn.TxnStatus_Prepared)
   100  	assert.Equal(t, 0, nt.notify(txn.TxnStatus_Active))
   101  	assert.Equal(t, 1, nt.notify(txn.TxnStatus_Prepared))
   102  
   103  	s, err := w.wait(context.Background())
   104  	assert.NoError(t, err)
   105  	assert.Equal(t, txn.TxnStatus_Prepared, s)
   106  }
   107  
   108  func TestNotifyWithFinalStatus(t *testing.T) {
   109  	nt := acquireNotifier()
   110  
   111  	w := acquireWaiter()
   112  	defer w.close()
   113  
   114  	nt.addWaiter(w, txn.TxnStatus_Prepared)
   115  
   116  	nt.close(txn.TxnStatus_Aborted)
   117  
   118  	s, err := w.wait(context.Background())
   119  	assert.NoError(t, err)
   120  	assert.Equal(t, txn.TxnStatus_Aborted, s)
   121  }
   122  
   123  func TestNotifyAfterWaiterClose(t *testing.T) {
   124  	nt := acquireNotifier()
   125  
   126  	w := acquireWaiter()
   127  	nt.addWaiter(w, txn.TxnStatus_Prepared)
   128  
   129  	w.close()
   130  	assert.Equal(t, 1, w.mu.ref)
   131  	assert.True(t, w.mu.closed)
   132  	assert.False(t, w.mu.notified)
   133  
   134  	assert.Equal(t, 0, nt.notify(txn.TxnStatus_Prepared))
   135  
   136  	assert.Equal(t, 0, w.mu.ref)
   137  	assert.False(t, w.mu.closed)
   138  	assert.False(t, w.mu.notified)
   139  }