|
| 1 | +package dynamodb |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/aws/aws-sdk-go/aws/awserr" |
| 8 | + "github.com/aws/aws-sdk-go/service/dynamodb" |
| 9 | + "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +type MockDynamoDBClient struct { |
| 14 | + dynamodbiface.DynamoDBAPI |
| 15 | + putItemCallback func(input *dynamodb.PutItemInput) (*dynamodb.PutItemOutput, error) |
| 16 | + deleteItemCallback func(input *dynamodb.DeleteItemInput) (*dynamodb.DeleteItemOutput, error) |
| 17 | +} |
| 18 | + |
| 19 | +func (c *MockDynamoDBClient) PutItem(input *dynamodb.PutItemInput) (*dynamodb.PutItemOutput, error) { |
| 20 | + return c.putItemCallback(input) |
| 21 | +} |
| 22 | + |
| 23 | +func (c *MockDynamoDBClient) DeleteItem(input *dynamodb.DeleteItemInput) (*dynamodb.DeleteItemOutput, error) { |
| 24 | + return c.deleteItemCallback(input) |
| 25 | +} |
| 26 | + |
| 27 | +func TestLock(t *testing.T) { |
| 28 | + t.Run("lock failure", func(t *testing.T) { |
| 29 | + client := &MockDynamoDBClient{} |
| 30 | + client.putItemCallback = func(input *dynamodb.PutItemInput) (*dynamodb.PutItemOutput, error) { |
| 31 | + return nil, awserr.New(dynamodb.ErrCodeConditionalCheckFailedException, "The conditional request failed.", errors.New("fake error")) |
| 32 | + } |
| 33 | + |
| 34 | + locker := &DynamoDBLocker{client} |
| 35 | + |
| 36 | + grabbed, err := locker.GrabLock("tableName", "lockPath", "uuid", "testing") |
| 37 | + assert.NoError(t, err) |
| 38 | + assert.False(t, grabbed) |
| 39 | + }) |
| 40 | + |
| 41 | + t.Run("lock acquired successfully", func(t *testing.T) { |
| 42 | + client := &MockDynamoDBClient{} |
| 43 | + client.putItemCallback = func(input *dynamodb.PutItemInput) (*dynamodb.PutItemOutput, error) { |
| 44 | + assert.Equal(t, "tableName", *input.TableName) |
| 45 | + assert.Equal(t, "lockPath", *input.Item[columnKey].S) |
| 46 | + assert.Equal(t, "uuid", *input.Item[columnId].S) |
| 47 | + assert.Equal(t, "(attribute_not_exists (#0)) OR (#1 = :0)", *input.ConditionExpression) |
| 48 | + |
| 49 | + assert.Equal(t, "key", *input.ExpressionAttributeNames["#0"]) |
| 50 | + assert.Equal(t, "id", *input.ExpressionAttributeNames["#1"]) |
| 51 | + assert.Equal(t, "uuid", *input.ExpressionAttributeValues[":0"].S) |
| 52 | + |
| 53 | + return &dynamodb.PutItemOutput{}, nil |
| 54 | + } |
| 55 | + |
| 56 | + locker := &DynamoDBLocker{client} |
| 57 | + |
| 58 | + grabbed, err := locker.GrabLock("tableName", "lockPath", "uuid", "testing") |
| 59 | + assert.NoError(t, err) |
| 60 | + assert.True(t, grabbed) |
| 61 | + }) |
| 62 | +} |
| 63 | + |
| 64 | +func TestUnlock(t *testing.T) { |
| 65 | + t.Run("unlock failure", func(t *testing.T) { |
| 66 | + client := &MockDynamoDBClient{} |
| 67 | + client.deleteItemCallback = func(input *dynamodb.DeleteItemInput) (*dynamodb.DeleteItemOutput, error) { |
| 68 | + return nil, awserr.New(dynamodb.ErrCodeConditionalCheckFailedException, "The conditional request failed.", errors.New("fake error")) |
| 69 | + } |
| 70 | + |
| 71 | + locker := &DynamoDBLocker{client} |
| 72 | + |
| 73 | + err := locker.ReleaseLock("tableName", "lockPath", "uuid") |
| 74 | + assert.Error(t, err) |
| 75 | + }) |
| 76 | + |
| 77 | + t.Run("unlock released", func(t *testing.T) { |
| 78 | + client := &MockDynamoDBClient{} |
| 79 | + client.deleteItemCallback = func(input *dynamodb.DeleteItemInput) (*dynamodb.DeleteItemOutput, error) { |
| 80 | + assert.Equal(t, "tableName", *input.TableName) |
| 81 | + |
| 82 | + assert.Equal(t, "id", *input.ExpressionAttributeNames["#0"]) |
| 83 | + assert.Equal(t, "uuid", *input.ExpressionAttributeValues[":0"].S) |
| 84 | + assert.Equal(t, "lockPath", *input.Key[columnKey].S) |
| 85 | + |
| 86 | + return &dynamodb.DeleteItemOutput{}, nil |
| 87 | + } |
| 88 | + |
| 89 | + locker := &DynamoDBLocker{client} |
| 90 | + |
| 91 | + err := locker.ReleaseLock("tableName", "lockPath", "uuid") |
| 92 | + assert.NoError(t, err) |
| 93 | + }) |
| 94 | +} |
0 commit comments