This repository has been archived by the owner on Jul 21, 2020. It is now read-only.
/
maintain_test.go
114 lines (93 loc) · 2.95 KB
/
maintain_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
package etcbeat
import (
"errors"
"testing"
"time"
"golang.org/x/net/context"
"github.com/benbjohnson/clock"
etcd "github.com/coreos/etcd/client"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
type testEtcdAPI struct {
mock.Mock
}
func (t *testEtcdAPI) Get(ctx context.Context, key string, opts *etcd.GetOptions) (*etcd.Response, error) {
args := t.Called(ctx, key, opts)
return nil, args.Error(1)
}
func (t *testEtcdAPI) Set(ctx context.Context, key, value string, opts *etcd.SetOptions) (*etcd.Response, error) {
args := t.Called(ctx, key, value, opts)
return nil, args.Error(1)
}
func (t *testEtcdAPI) Delete(ctx context.Context, key string, opts *etcd.DeleteOptions) (*etcd.Response, error) {
args := t.Called(ctx, key, opts)
return nil, args.Error(1)
}
func pause() {
time.Sleep(time.Millisecond * 500)
}
func TestEtcdKeyMaintainer(t *testing.T) {
api := new(testEtcdAPI)
clk := clock.NewMock()
m := NewKeyMaintainer(api, "/test", "asdf")
m.clock = clk
api.On("Set", mock.Anything, "/test", "asdf", mock.Anything).Return(nil, nil)
go m.Maintain()
pause()
api.AssertNumberOfCalls(t, "Set", 1)
clk.Add(m.Interval - time.Nanosecond)
api.AssertNumberOfCalls(t, "Set", 1)
clk.Add(time.Nanosecond + 100)
api.AssertNumberOfCalls(t, "Set", 2)
api.On("Delete", mock.Anything, "/test", mock.Anything).Return(nil, nil)
m.Close()
pause()
clk.Add(m.Interval)
api.AssertNumberOfCalls(t, "Set", 2)
del := api.Calls[2]
assert.Equal(t, "Delete", del.Method)
assert.Equal(t, "/test", del.Arguments.String(1))
}
func TestSendsErrorsDownChannels(t *testing.T) {
api := new(testEtcdAPI)
m := NewKeyMaintainer(api, "/test", "asdf")
err := errors.New("oh noes!")
api.On("Set", mock.Anything, "/test", "asdf", mock.Anything).Return(nil, err)
api.On("Delete", mock.Anything, "/test", mock.Anything).Return(nil, nil)
go m.Maintain()
defer m.Close()
assert.Equal(t, <-m.Errors(), err)
}
func TestEtcdDirMaintainer(t *testing.T) {
api := new(testEtcdAPI)
clk := clock.NewMock()
m := NewDirMaintainer(api, "/test", func(m *Maintainer) error {
_, err := m.API.Set(m.Context, m.Key+"/asdf", "42", nil)
return err
})
m.clock = clk
api.On("Set", mock.Anything, "/test", "", &etcd.SetOptions{
TTL: time.Second * 9,
Dir: true,
PrevExist: etcd.PrevExist,
}).Return(nil, nil).Once()
go m.Maintain()
pause()
api.AssertNumberOfCalls(t, "Set", 1)
api.On("Set", mock.Anything, "/test", "", &etcd.SetOptions{
TTL: time.Second * 9,
Dir: true,
PrevExist: etcd.PrevExist,
}).Return(nil, etcd.Error{Code: etcd.ErrorCodeKeyNotFound}).Once()
api.On("Set", mock.Anything, "/test", "", &etcd.SetOptions{
TTL: time.Second * 9,
Dir: true,
}).Return(nil, nil).Once()
var opts *etcd.SetOptions
api.On("Set", mock.Anything, "/test/asdf", "42", opts).Return(nil, nil).Once()
clk.Add(m.Interval)
api.AssertNumberOfCalls(t, "Set", 4)
api.On("Delete", mock.Anything, "/test", mock.Anything).Return(nil, nil)
m.Close()
}