-
Notifications
You must be signed in to change notification settings - Fork 3
/
consumer_test.go
150 lines (124 loc) · 5.01 KB
/
consumer_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package feedx_test
import (
"context"
"io"
"time"
"github.com/bsm/bfs"
"github.com/bsm/feedx"
"github.com/bsm/feedx/internal/testdata"
. "github.com/bsm/ginkgo/v2"
. "github.com/bsm/gomega"
)
var _ = Describe("Consumer", func() {
var subject feedx.Consumer
var obj *bfs.Object
var ctx = context.Background()
consume := func(r *feedx.Reader) (interface{}, error) {
var msgs []*testdata.MockMessage
for {
var msg testdata.MockMessage
if err := r.Decode(&msg); err == io.EOF {
break
} else if err != nil {
return nil, err
}
msgs = append(msgs, &msg)
}
return msgs, nil
}
Describe("NewConsumer", func() {
BeforeEach(func() {
obj = bfs.NewInMemObject("path/to/file.jsonz")
Expect(writeMulti(obj, 2, mockTime)).To(Succeed())
var err error
subject, err = feedx.NewConsumerForRemote(ctx, obj, nil, consume)
Expect(err).NotTo(HaveOccurred())
})
AfterEach(func() {
Expect(subject.Close()).To(Succeed())
})
It("syncs/retrieves feeds from remote", func() {
Expect(subject.LastSync()).To(BeTemporally("~", time.Now(), time.Second))
Expect(subject.LastConsumed()).To(BeTemporally("==", subject.LastSync()))
Expect(subject.LastModified()).To(BeTemporally("==", mockTime.Truncate(time.Millisecond)))
Expect(subject.NumRead()).To(Equal(2))
Expect(subject.Data()).To(ConsistOf(seed(), seed()))
Expect(subject.Close()).To(Succeed())
})
It("consumes feeds only if necessary", func() {
prevSync := subject.LastSync()
time.Sleep(2 * time.Millisecond)
testable := subject.(interface{ TestSync() error })
Expect(testable.TestSync()).To(Succeed())
Expect(subject.LastSync()).To(BeTemporally(">", prevSync))
Expect(subject.LastConsumed()).To(BeTemporally("==", prevSync)) // skipped on last sync
Expect(subject.LastModified()).To(BeTemporally("==", mockTime.Truncate(time.Millisecond)))
Expect(subject.NumRead()).To(Equal(2))
})
It("always consumes if LastModified not set", func() {
noModTime := bfs.NewInMemObject("path/to/file.json")
Expect(writeMulti(noModTime, 2, time.Time{})).To(Succeed())
csmr, err := feedx.NewConsumerForRemote(ctx, noModTime, nil, consume)
Expect(err).NotTo(HaveOccurred())
prevSync := csmr.LastSync()
time.Sleep(2 * time.Millisecond)
testable := csmr.(interface{ TestSync() error })
Expect(testable.TestSync()).To(Succeed())
Expect(csmr.LastSync()).To(BeTemporally(">", prevSync))
Expect(csmr.LastConsumed()).To(BeTemporally("==", csmr.LastSync())) // consumed on last sync
Expect(csmr.LastModified()).To(BeTemporally("==", time.Unix(0, 0)))
})
})
Describe("NewIncrementalConsumer", func() {
BeforeEach(func() {
bucket := bfs.NewInMem()
dataFile := bfs.NewObjectFromBucket(bucket, "data-0-20230501-120023123.jsonz")
Expect(writeMulti(dataFile, 2, mockTime)).To(Succeed())
manifest := &feedx.Manifest{
LastModified: feedx.TimestampFromTime(mockTime),
Files: []string{dataFile.Name(), dataFile.Name()},
}
writer := feedx.NewWriter(ctx, bfs.NewObjectFromBucket(bucket, "manifest.json"), &feedx.WriterOptions{LastMod: mockTime})
defer writer.Discard()
Expect(writer.Encode(manifest)).To(Succeed())
Expect(writer.Commit()).To(Succeed())
var err error
subject, err = feedx.NewIncrementalConsumerForBucket(ctx, bucket, nil, consume)
Expect(err).NotTo(HaveOccurred())
})
AfterEach(func() {
Expect(subject.Close()).To(Succeed())
})
It("syncs/retrieves feeds from remote", func() {
Expect(subject.LastSync()).To(BeTemporally("~", time.Now(), time.Second))
Expect(subject.LastConsumed()).To(BeTemporally("==", subject.LastSync()))
Expect(subject.LastModified()).To(BeTemporally("==", mockTime.Truncate(time.Millisecond)))
Expect(subject.NumRead()).To(Equal(4))
Expect(subject.Data()).To(ConsistOf(seed(), seed(), seed(), seed()))
Expect(subject.Close()).To(Succeed())
})
It("consumes feeds only if necessary", func() {
prevSync := subject.LastSync()
time.Sleep(2 * time.Millisecond)
testable := subject.(interface{ TestSync() error })
Expect(testable.TestSync()).To(Succeed())
Expect(subject.LastSync()).To(BeTemporally(">", prevSync))
Expect(subject.LastConsumed()).To(BeTemporally("==", prevSync)) // skipped on last sync
Expect(subject.LastModified()).To(BeTemporally("==", mockTime.Truncate(time.Millisecond)))
Expect(subject.NumRead()).To(Equal(4))
})
It("always consumes if LastModified not set", func() {
noModTime := bfs.NewInMemObject("path/to/file.json")
Expect(writeMulti(noModTime, 2, time.Time{})).To(Succeed())
csmr, err := feedx.NewConsumerForRemote(ctx, noModTime, nil, consume)
Expect(err).NotTo(HaveOccurred())
prevSync := csmr.LastSync()
time.Sleep(2 * time.Millisecond)
testable := csmr.(interface{ TestSync() error })
Expect(testable.TestSync()).To(Succeed())
Expect(csmr.LastSync()).To(BeTemporally(">", prevSync))
Expect(csmr.LastConsumed()).To(BeTemporally("==", csmr.LastSync())) // consumed on last sync
Expect(csmr.LastModified()).To(BeTemporally("==", time.Unix(0, 0)))
})
})
})