|
| 1 | +package storage |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | +) |
| 9 | + |
| 10 | +func TestHeldOneLock(t *testing.T) { |
| 11 | + lockManager := NewTestingLockManager() |
| 12 | + |
| 13 | + t.Run("holds only lockA", func(t *testing.T) { |
| 14 | + lctx := lockManager.NewContext() |
| 15 | + defer lctx.Release() |
| 16 | + err := lctx.AcquireLock(LockInsertBlock) |
| 17 | + require.NoError(t, err) |
| 18 | + |
| 19 | + held, msg := HeldOneLock(lctx, LockInsertBlock, LockFinalizeBlock) |
| 20 | + assert.True(t, held) |
| 21 | + assert.Empty(t, msg) |
| 22 | + }) |
| 23 | + |
| 24 | + t.Run("holds only lockB", func(t *testing.T) { |
| 25 | + lctx := lockManager.NewContext() |
| 26 | + defer lctx.Release() |
| 27 | + err := lctx.AcquireLock(LockFinalizeBlock) |
| 28 | + require.NoError(t, err) |
| 29 | + |
| 30 | + held, msg := HeldOneLock(lctx, LockInsertBlock, LockFinalizeBlock) |
| 31 | + assert.True(t, held) |
| 32 | + assert.Empty(t, msg) |
| 33 | + }) |
| 34 | + |
| 35 | + t.Run("holds both locks", func(t *testing.T) { |
| 36 | + lctx := lockManager.NewContext() |
| 37 | + defer lctx.Release() |
| 38 | + err := lctx.AcquireLock(LockInsertBlock) |
| 39 | + require.NoError(t, err) |
| 40 | + err = lctx.AcquireLock(LockFinalizeBlock) |
| 41 | + require.NoError(t, err) |
| 42 | + |
| 43 | + held, msg := HeldOneLock(lctx, LockInsertBlock, LockFinalizeBlock) |
| 44 | + assert.False(t, held) |
| 45 | + assert.Contains(t, msg, "epxect to hold only one lock, but actually held both locks") |
| 46 | + assert.Contains(t, msg, LockInsertBlock) |
| 47 | + assert.Contains(t, msg, LockFinalizeBlock) |
| 48 | + }) |
| 49 | + |
| 50 | + t.Run("holds neither lock", func(t *testing.T) { |
| 51 | + // Create a context that doesn't hold any locks |
| 52 | + lctx := lockManager.NewContext() |
| 53 | + defer lctx.Release() |
| 54 | + |
| 55 | + held, msg := HeldOneLock(lctx, LockInsertBlock, LockFinalizeBlock) |
| 56 | + assert.False(t, held) |
| 57 | + assert.Contains(t, msg, "expect to hold one of the locks") |
| 58 | + assert.Contains(t, msg, LockInsertBlock) |
| 59 | + assert.Contains(t, msg, LockFinalizeBlock) |
| 60 | + }) |
| 61 | + |
| 62 | + t.Run("holds different lock", func(t *testing.T) { |
| 63 | + lctx := lockManager.NewContext() |
| 64 | + defer lctx.Release() |
| 65 | + err := lctx.AcquireLock(LockBootstrapping) |
| 66 | + require.NoError(t, err) |
| 67 | + |
| 68 | + held, msg := HeldOneLock(lctx, LockInsertBlock, LockFinalizeBlock) |
| 69 | + assert.False(t, held) |
| 70 | + assert.Contains(t, msg, "expect to hold one of the locks") |
| 71 | + assert.Contains(t, msg, LockInsertBlock) |
| 72 | + assert.Contains(t, msg, LockFinalizeBlock) |
| 73 | + }) |
| 74 | + |
| 75 | + t.Run("with different lock combinations", func(t *testing.T) { |
| 76 | + lctx := lockManager.NewContext() |
| 77 | + defer lctx.Release() |
| 78 | + err := lctx.AcquireLock(LockInsertOwnReceipt) |
| 79 | + require.NoError(t, err) |
| 80 | + |
| 81 | + held, msg := HeldOneLock(lctx, LockInsertOwnReceipt, LockInsertCollection) |
| 82 | + assert.True(t, held) |
| 83 | + assert.Empty(t, msg) |
| 84 | + }) |
| 85 | + |
| 86 | + t.Run("with cluster block locks", func(t *testing.T) { |
| 87 | + lctx := lockManager.NewContext() |
| 88 | + defer lctx.Release() |
| 89 | + err := lctx.AcquireLock(LockInsertOrFinalizeClusterBlock) |
| 90 | + require.NoError(t, err) |
| 91 | + |
| 92 | + held, msg := HeldOneLock(lctx, LockInsertOrFinalizeClusterBlock, LockIndexResultApproval) |
| 93 | + assert.True(t, held) |
| 94 | + assert.Empty(t, msg) |
| 95 | + }) |
| 96 | + |
| 97 | + t.Run("error message format for both locks", func(t *testing.T) { |
| 98 | + lctx := lockManager.NewContext() |
| 99 | + defer lctx.Release() |
| 100 | + err := lctx.AcquireLock(LockInsertBlock) |
| 101 | + require.NoError(t, err) |
| 102 | + err = lctx.AcquireLock(LockFinalizeBlock) |
| 103 | + require.NoError(t, err) |
| 104 | + |
| 105 | + // Check that both locks are actually held |
| 106 | + assert.True(t, lctx.HoldsLock(LockInsertBlock)) |
| 107 | + assert.True(t, lctx.HoldsLock(LockFinalizeBlock)) |
| 108 | + |
| 109 | + held, msg := HeldOneLock(lctx, LockInsertBlock, LockFinalizeBlock) |
| 110 | + assert.False(t, held) |
| 111 | + require.NotEmpty(t, msg) |
| 112 | + assert.Contains(t, msg, "epxect to hold only one lock, but actually held both locks") |
| 113 | + assert.Contains(t, msg, LockInsertBlock) |
| 114 | + assert.Contains(t, msg, LockFinalizeBlock) |
| 115 | + }) |
| 116 | + |
| 117 | + t.Run("error message format for no locks", func(t *testing.T) { |
| 118 | + lctx := lockManager.NewContext() |
| 119 | + defer lctx.Release() |
| 120 | + |
| 121 | + held, msg := HeldOneLock(lctx, "lockA", "lockB") |
| 122 | + assert.False(t, held) |
| 123 | + require.NotEmpty(t, msg) |
| 124 | + assert.Contains(t, msg, "expect to hold one of the locks") |
| 125 | + assert.Contains(t, msg, "lockA") |
| 126 | + assert.Contains(t, msg, "lockB") |
| 127 | + }) |
| 128 | +} |
0 commit comments