forked from attic-labs/noms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamo_table_reader_test.go
122 lines (98 loc) · 3.29 KB
/
dynamo_table_reader_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2017 Attic Labs, Inc. All rights reserved.
// Licensed under the Apache License, version 2.0:
// http://www.apache.org/licenses/LICENSE-2.0
package nbs
import (
"testing"
"github.com/attic-labs/noms/go/util/sizecache"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/stretchr/testify/assert"
)
func TestDynamoTableReaderAt(t *testing.T) {
ddb := makeFakeDDB(t)
chunks := [][]byte{
[]byte("hello2"),
[]byte("goodbye2"),
[]byte("badbye2"),
}
tableData, h := buildTable(chunks)
ddb.putData(fmtTableName(h), tableData)
t.Run("ddbTableStore", func(t *testing.T) {
t.Run("ReadTable", func(t *testing.T) {
test := func(dts *ddbTableStore) {
assert := assert.New(t)
data, err := dts.ReadTable(h, &Stats{})
assert.NoError(err)
assert.Equal(tableData, data)
data, err = dts.ReadTable(computeAddr([]byte{}), &Stats{})
assert.Error(err)
assert.IsType(tableNotInDynamoErr{}, err)
assert.Nil(data)
}
t.Run("EventuallyConsistentSuccess", func(t *testing.T) {
test(&ddbTableStore{ddb, "table", nil, nil})
})
t.Run("EventuallyConsistentFailure", func(t *testing.T) {
test(&ddbTableStore{&eventuallyConsistentDDB{ddb}, "table", nil, nil})
})
t.Run("WithCache", func(t *testing.T) {
tc := sizecache.New(uint64(2 * len(tableData)))
dts := &ddbTableStore{ddb, "table", nil, tc}
test(dts)
// Table should have been cached on read
baseline := ddb.numGets
_, err := dts.ReadTable(h, &Stats{})
assert.NoError(t, err)
assert.Zero(t, ddb.numGets-baseline)
})
})
t.Run("WriteTable", func(t *testing.T) {
t.Run("WithoutCache", func(t *testing.T) {
assert := assert.New(t)
dts := &ddbTableStore{makeFakeDDB(t), "table", nil, nil}
assert.NoError(dts.Write(h, tableData))
data, err := dts.ReadTable(h, &Stats{})
assert.NoError(err)
assert.Equal(tableData, data)
})
t.Run("WithCache", func(t *testing.T) {
assert := assert.New(t)
tc := sizecache.New(uint64(2 * len(tableData)))
dts := &ddbTableStore{makeFakeDDB(t), "table", nil, tc}
assert.NoError(dts.Write(h, tableData))
// Table should have been cached on write
baseline := ddb.numGets
data, err := dts.ReadTable(h, &Stats{})
assert.NoError(err)
assert.Equal(tableData, data)
assert.Zero(ddb.numGets - baseline)
})
})
})
t.Run("ReadAtWithCache", func(t *testing.T) {
assert := assert.New(t)
stats := &Stats{}
tc := sizecache.New(uint64(2 * len(tableData)))
tra := &dynamoTableReaderAt{&ddbTableStore{ddb, "table", nil, tc}, h}
// First, read when table is not yet cached
scratch := make([]byte, len(tableData)/4)
baseline := ddb.numGets
_, err := tra.ReadAtWithStats(scratch, 0, stats)
assert.NoError(err)
assert.True(ddb.numGets > baseline)
// Table should have been cached on read so read again, a different slice this time
baseline = ddb.numGets
_, err = tra.ReadAtWithStats(scratch, int64(len(scratch)), stats)
assert.NoError(err)
assert.Zero(ddb.numGets - baseline)
})
}
type eventuallyConsistentDDB struct {
ddbsvc
}
func (ec *eventuallyConsistentDDB) GetItem(input *dynamodb.GetItemInput) (*dynamodb.GetItemOutput, error) {
if input.ConsistentRead != nil && *(input.ConsistentRead) {
return ec.ddbsvc.GetItem(input)
}
return &dynamodb.GetItemOutput{}, nil
}