(t *testing.T)
| 47 | ) |
| 48 | |
| 49 | func TestAcquireTransactionLocker(t *testing.T) { |
| 50 | t.Run("fail fast when wait timeout disabled", func(t *testing.T) { |
| 51 | db, mock := redismock.NewClientMock() |
| 52 | ledgerforge := &LedgerForge{ |
| 53 | redis: db, |
| 54 | config: &config.Configuration{ |
| 55 | Transaction: config.TransactionConfig{ |
| 56 | LockDuration: 5 * time.Second, |
| 57 | }, |
| 58 | }, |
| 59 | } |
| 60 | locker := redlock.NewMultiLocker(db, []string{"key-b", "key-a"}, "test-value") |
| 61 | |
| 62 | mock.ExpectSetNX("key-a", "test-value", 5*time.Second).SetVal(true) |
| 63 | mock.ExpectSetNX("key-b", "test-value", 5*time.Second).SetVal(true) |
| 64 | |
| 65 | err := ledgerforge.acquireTransactionLocker(context.Background(), locker) |
| 66 | require.NoError(t, err) |
| 67 | require.NoError(t, mock.ExpectationsWereMet()) |
| 68 | }) |
| 69 | |
| 70 | t.Run("waits when configured", func(t *testing.T) { |
| 71 | db, mock := redismock.NewClientMock() |
| 72 | ledgerforge := &LedgerForge{ |
| 73 | redis: db, |
| 74 | config: &config.Configuration{ |
| 75 | Transaction: config.TransactionConfig{ |
| 76 | LockDuration: 5 * time.Second, |
| 77 | LockWaitTimeout: time.Second, |
| 78 | }, |
| 79 | }, |
| 80 | } |
| 81 | locker := redlock.NewMultiLocker(db, []string{"key-b", "key-a"}, "test-value") |
| 82 | |
| 83 | mock.ExpectSetNX("key-a", "test-value", 5*time.Second).SetVal(false) |
| 84 | mock.ExpectSetNX("key-a", "test-value", 5*time.Second).SetVal(true) |
| 85 | mock.ExpectSetNX("key-b", "test-value", 5*time.Second).SetVal(true) |
| 86 | |
| 87 | err := ledgerforge.acquireTransactionLocker(context.Background(), locker) |
| 88 | require.NoError(t, err) |
| 89 | require.NoError(t, mock.ExpectationsWereMet()) |
| 90 | }) |
| 91 | } |
| 92 | |
| 93 | // pollForTransactionStatus polls for a transaction until it reaches the expected status or a timeout occurs. |
| 94 | func pollForTransactionStatus(ctx context.Context, ds database.IDataSource, transactionRef string, expectedStatus string, pollInterval time.Duration, timeoutDuration time.Duration) (*model.Transaction, error) { |
nothing calls this directly
no test coverage detected