File
Blob: acme/storage_test.go
| 1 | package acme |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "go.miragespace.co/specter/spec/mocks" |
| 9 | |
| 10 | "github.com/stretchr/testify/mock" |
| 11 | "github.com/stretchr/testify/require" |
| 12 | "go.uber.org/zap/zaptest" |
| 13 | ) |
| 14 | |
| 15 | const ( |
| 16 | testLockKey = "lock-key" |
| 17 | testLockToken uint64 = 12345678 |
| 18 | ) |
| 19 | |
| 20 | func TestStorageLocker(t *testing.T) { |
| 21 | as := require.New(t) |
| 22 | logger := zaptest.NewLogger(t) |
| 23 | kv := new(mocks.VNode) |
| 24 | defer kv.AssertExpectations(t) |
| 25 | |
| 26 | s, err := NewChordStorage(logger, kv, StorageConfig{ |
| 27 | RetryInterval: time.Millisecond * 100, |
| 28 | LeaseTTL: time.Millisecond * 500, |
| 29 | }) |
| 30 | as.NoError(err) |
| 31 | |
| 32 | ctx := t.Context() |
| 33 | |
| 34 | acquireCall := kv.On("Acquire", mock.Anything, mock.MatchedBy(func(name []byte) bool { |
| 35 | return bytes.Equal(name, []byte(kvKeyName(testLockKey))) |
| 36 | }), mock.MatchedBy(func(ttl time.Duration) bool { |
| 37 | return ttl == time.Millisecond*500 |
| 38 | })).Return(testLockToken, nil).Once() |
| 39 | |
| 40 | renewCall := kv.On("Renew", mock.Anything, mock.MatchedBy(func(name []byte) bool { |
| 41 | return bytes.Equal(name, []byte(kvKeyName(testLockKey))) |
| 42 | }), mock.MatchedBy(func(ttl time.Duration) bool { |
| 43 | return ttl == time.Millisecond*500 |
| 44 | }), testLockToken).Return(testLockToken, nil).NotBefore(acquireCall) |
| 45 | |
| 46 | kv.On("Release", mock.Anything, mock.MatchedBy(func(name []byte) bool { |
| 47 | return bytes.Equal(name, []byte(kvKeyName(testLockKey))) |
| 48 | }), testLockToken).Return(nil).NotBefore(renewCall) |
| 49 | |
| 50 | err = s.Lock(ctx, testLockKey) |
| 51 | as.NoError(err) |
| 52 | |
| 53 | time.Sleep(time.Second) |
| 54 | |
| 55 | err = s.Unlock(ctx, testLockKey) |
| 56 | as.NoError(err) |
| 57 | } |
| 58 | |
| 59 | func TestStorageLockerRenewLockLease(t *testing.T) { |
| 60 | as := require.New(t) |
| 61 | logger := zaptest.NewLogger(t) |
| 62 | kv := new(mocks.VNode) |
| 63 | defer kv.AssertExpectations(t) |
| 64 | |
| 65 | s, err := NewChordStorage(logger, kv, StorageConfig{ |
| 66 | RetryInterval: time.Millisecond * 100, |
| 67 | LeaseTTL: time.Hour, |
| 68 | }) |
| 69 | as.NoError(err) |
| 70 | |
| 71 | ctx := t.Context() |
| 72 | |
| 73 | acquireCall := kv.On("Acquire", mock.Anything, mock.MatchedBy(func(name []byte) bool { |
| 74 | return bytes.Equal(name, []byte(kvKeyName(testLockKey))) |
| 75 | }), mock.MatchedBy(func(ttl time.Duration) bool { |
| 76 | return ttl == time.Hour |
| 77 | })).Return(testLockToken, nil).Once() |
| 78 | |
| 79 | renewCall := kv.On("Renew", mock.Anything, mock.MatchedBy(func(name []byte) bool { |
| 80 | return bytes.Equal(name, []byte(kvKeyName(testLockKey))) |
| 81 | }), mock.MatchedBy(func(ttl time.Duration) bool { |
| 82 | return ttl == time.Hour |
| 83 | }), testLockToken).Return(testLockToken, nil).NotBefore(acquireCall).Once() |
| 84 | |
| 85 | kv.On("Release", mock.Anything, mock.MatchedBy(func(name []byte) bool { |
| 86 | return bytes.Equal(name, []byte(kvKeyName(testLockKey))) |
| 87 | }), testLockToken).Return(nil).NotBefore(renewCall) |
| 88 | |
| 89 | err = s.Lock(ctx, testLockKey) |
| 90 | as.NoError(err) |
| 91 | |
| 92 | err = s.RenewLockLease(ctx, testLockKey, time.Hour) |
| 93 | as.NoError(err) |
| 94 | |
| 95 | err = s.Unlock(ctx, testLockKey) |
| 96 | as.NoError(err) |
| 97 | } |