github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/utils/transaction_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"os"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestTxn(t *testing.T) {
    14  	err1 := errors.New("err1")
    15  	err := Txn(
    16  		context.Background(),
    17  		func(context.Context) error {
    18  			return err1
    19  		},
    20  		nil,
    21  		func(context.Context, bool) error {
    22  			return errors.New("error 2")
    23  		},
    24  		10*time.Second,
    25  	)
    26  	assert.Contains(t, err.Error(), err1.Error())
    27  	err = Txn(
    28  		context.Background(),
    29  		func(context.Context) error {
    30  			return nil
    31  		},
    32  		func(context.Context) error {
    33  			return err1
    34  		},
    35  		nil,
    36  		10*time.Second,
    37  	)
    38  	assert.Contains(t, err.Error(), err1.Error())
    39  	err = Txn(
    40  		context.Background(),
    41  		func(context.Context) error {
    42  			return nil
    43  		},
    44  		nil,
    45  		nil,
    46  		10*time.Second,
    47  	)
    48  	assert.NoError(t, err)
    49  }
    50  
    51  func TestPCR(t *testing.T) {
    52  	prepare := func(context.Context) error {
    53  		return os.ErrClosed
    54  	}
    55  	commit := func(context.Context) error {
    56  		return os.ErrClosed
    57  	}
    58  
    59  	ctx := context.Background()
    60  	assert.Error(t, PCR(ctx, prepare, commit, commit, time.Second))
    61  }