forked from Azure/azure-event-hubs-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch_test.go
109 lines (88 loc) · 2.25 KB
/
batch_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
package eventhub_test
import (
"strconv"
"testing"
"github.com/stretchr/testify/assert"
eventhub "github.com/Azure/azure-event-hubs-go/v3"
)
func TestNewEventBatch(t *testing.T) {
eb := eventhub.NewEventBatch("eventId", nil)
assert.Equal(t, eventhub.DefaultMaxMessageSizeInBytes, eb.MaxSize)
}
func TestEventBatch_AddOneMessage(t *testing.T) {
eb := eventhub.NewEventBatch("eventId", nil)
event := eventhub.NewEventFromString("Foo")
ok, err := eb.Add(event)
assert.True(t, ok)
assert.NoError(t, err)
}
func TestEventBatch_AddManyMessages(t *testing.T) {
eb := eventhub.NewEventBatch("eventId", nil)
wrapperSize := eb.Size()
event := eventhub.NewEventFromString("Foo")
ok, err := eb.Add(event)
assert.True(t, ok)
assert.NoError(t, err)
msgSize := eb.Size() - wrapperSize
limit := ((int(eb.MaxSize) - 100) / msgSize) - 1
for i := 0; i < limit; i++ {
ok, err := eb.Add(event)
assert.True(t, ok)
assert.NoError(t, err)
}
ok, err = eb.Add(event)
assert.False(t, ok)
assert.NoError(t, err)
}
func TestEventBatch_Clear(t *testing.T) {
eb := eventhub.NewEventBatch("eventId", nil)
ok, err := eb.Add(eventhub.NewEventFromString("Foo"))
assert.True(t, ok)
assert.NoError(t, err)
assert.Equal(t, 163, eb.Size())
eb.Clear()
assert.Equal(t, 100, eb.Size())
}
func TestHugeBatches(t *testing.T) {
data := make([]byte, 500)
events := make([]*eventhub.Event, 0)
for i := 0; i < 100; i++ {
// 100 / 4 * 50000 = 1250000 bytes per partition
partitionKey := strconv.Itoa(i % 4)
evt := &eventhub.Event{
Data: data,
PartitionKey: &partitionKey,
}
events = append(events, evt)
}
opts := &eventhub.BatchOptions{
MaxSize: 10000,
}
iter := eventhub.NewEventBatchIterator(events...)
iterCount := 0
for !iter.Done() {
_, err := iter.Next("batchId", opts)
assert.NoError(t, err)
iterCount++
if iterCount > 101 {
assert.Fail(t, "Too much iteration")
}
}
assert.Greater(t, iterCount, 5)
}
func TestOneHugeEvent(t *testing.T) {
data := make([]byte, 1100)
events := []*eventhub.Event{
{
Data: data,
},
}
opts := &eventhub.BatchOptions{
MaxSize: 1000,
}
iter := eventhub.NewEventBatchIterator(events...)
for !iter.Done() {
_, err := iter.Next("batchId", opts)
assert.Equal(t, err, eventhub.ErrMessageIsTooBig)
}
}