forked from obsidiandynamics/goharvest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_mock_test.go
86 lines (75 loc) · 1.9 KB
/
db_mock_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
package goharvest
import (
"github.com/google/uuid"
"github.com/obsidiandynamics/libstdgo/concurrent"
)
type dbMockFuncs struct {
Mark func(m *dbMock, leaderID uuid.UUID, limit int) ([]OutboxRecord, error)
Purge func(m *dbMock, id int64) (bool, error)
Reset func(m *dbMock, id int64) (bool, error)
Dispose func(m *dbMock)
}
type dbMockCounts struct {
Mark,
Purge,
Reset,
Dispose concurrent.AtomicCounter
}
type dbMock struct {
markedRecords chan []OutboxRecord
f dbMockFuncs
c dbMockCounts
}
func (m *dbMock) Mark(leaderID uuid.UUID, limit int) ([]OutboxRecord, error) {
defer m.c.Mark.Inc()
return m.f.Mark(m, leaderID, limit)
}
func (m *dbMock) Purge(id int64) (bool, error) {
defer m.c.Purge.Inc()
return m.f.Purge(m, id)
}
func (m *dbMock) Reset(id int64) (bool, error) {
defer m.c.Reset.Inc()
return m.f.Reset(m, id)
}
func (m *dbMock) Dispose() {
defer m.c.Dispose.Inc()
m.f.Dispose(m)
}
func (m *dbMock) fillDefaults() {
if m.markedRecords == nil {
m.markedRecords = make(chan []OutboxRecord)
}
if m.f.Mark == nil {
m.f.Mark = func(m *dbMock, leaderID uuid.UUID, limit int) ([]OutboxRecord, error) {
select {
case records := <-m.markedRecords:
return records, nil
default:
return []OutboxRecord{}, nil
}
}
}
if m.f.Purge == nil {
m.f.Purge = func(m *dbMock, id int64) (bool, error) {
return true, nil
}
}
if m.f.Reset == nil {
m.f.Reset = func(m *dbMock, id int64) (bool, error) {
return true, nil
}
}
if m.f.Dispose == nil {
m.f.Dispose = func(m *dbMock) {}
}
m.c.Mark = concurrent.NewAtomicCounter()
m.c.Purge = concurrent.NewAtomicCounter()
m.c.Reset = concurrent.NewAtomicCounter()
m.c.Dispose = concurrent.NewAtomicCounter()
}
func mockDatabaseBindingProvider(m *dbMock) func(string, string) (DatabaseBinding, error) {
return func(dataSource string, table string) (DatabaseBinding, error) {
return m, nil
}
}