-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrouting_test.go
133 lines (117 loc) · 4.09 KB
/
routing_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
package slackscot
import (
"context"
"fmt"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/metric"
"log"
"math"
"os"
"testing"
)
func TestNewPartitioner(t *testing.T) {
tests := map[string]struct {
partitionCount int
expectedError string
}{
"InvalidZeroPartitions": {
partitionCount: 0,
expectedError: "A partition router can only work with a partitionCount that is a power of two but was [0]",
},
"ValidOnePartition": {
partitionCount: 1,
expectedError: "",
},
"ValidTwoPartitions": {
partitionCount: 2,
expectedError: "",
},
"Invalid3Partitions": {
partitionCount: 3,
expectedError: "A partition router can only work with a partitionCount that is a power of two but was [3]",
},
"Valid4Partitions": {
partitionCount: 4,
expectedError: "",
},
"Invalid5Partitions": {
partitionCount: 5,
expectedError: "A partition router can only work with a partitionCount that is a power of two but was [5]",
},
"Valid8Partitions": {
partitionCount: 8,
expectedError: "",
},
"Valid16Partitions": {
partitionCount: 16,
expectedError: "",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ins, _ := newInstrumenter("test", metric.Meter{}, func(ctx context.Context, result metric.Int64ObserverResult) {})
pr, err := newPartitionRouter(tc.partitionCount, 1, nil, ins)
if tc.expectedError == "" {
assert.NoError(t, err)
assert.NotNil(t, pr)
assert.Len(t, pr.messageQueues, tc.partitionCount)
assert.Len(t, pr.workerTerminationSignals, tc.partitionCount)
} else {
assert.EqualError(t, err, tc.expectedError)
}
})
}
}
func TestConsistentHashing(t *testing.T) {
msgID := SlackMessageID{channelID: "general", timestamp: "11298321983.23"}
for i := 0; i < 16; i++ {
partitionCount := int(math.Pow(float64(2), float64(i)))
name := fmt.Sprintf("With_%d_Partitions", partitionCount)
t.Run(name, func(t *testing.T) {
ins, _ := newInstrumenter("test", metric.Meter{}, func(ctx context.Context, result metric.Int64ObserverResult) {})
pr, _ := newPartitionRouter(partitionCount, 1, &sLogger{logger: log.New(os.Stdout, "", log.LstdFlags)}, ins)
partition := pr.partitionForMsgID(msgID)
for i := 0; i < 100; i++ {
assert.Equal(t, partition, pr.partitionForMsgID(msgID))
}
})
}
}
func TestHashDistribution(t *testing.T) {
// Generate message IDs that are all different to validate the uniform distribution across partitions
msgIDs := make([]SlackMessageID, 0)
for i := 0; i < 500000; i++ {
msgTimestamp := fmt.Sprintf("19292929%d.214", i*1000)
msgIDs = append(msgIDs, SlackMessageID{channelID: "general", timestamp: msgTimestamp})
msgIDs = append(msgIDs, SlackMessageID{channelID: "général", timestamp: msgTimestamp})
}
msgIDCount := len(msgIDs)
for i := 0; i < 10; i++ {
partitionCount := int(math.Pow(float64(2), float64(i)))
name := fmt.Sprintf("With_%d_Partitions", partitionCount)
partitionHitCount := make([]int, partitionCount)
t.Run(name, func(t *testing.T) {
ins, _ := newInstrumenter("test", metric.Meter{}, func(ctx context.Context, result metric.Int64ObserverResult) {})
pr, _ := newPartitionRouter(partitionCount, 1, &sLogger{logger: log.New(os.Stdout, "", log.LstdFlags)}, ins)
for _, msgID := range msgIDs {
partition := pr.partitionForMsgID(msgID)
partitionHitCount[partition] = partitionHitCount[partition] + 1
}
expectedHitsPerPartition := float64(msgIDCount) / float64(partitionCount)
deviationTolerance := 3.0 * expectedHitsPerPartition / 100
for partition, hitCount := range partitionHitCount {
assert.InDeltaf(t, expectedHitsPerPartition, hitCount, deviationTolerance, "All partitions should have received about [%.1f] hits but partition [%d] got [%d]", expectedHitsPerPartition, partition, hitCount)
}
})
}
}
func TestHashMask(t *testing.T) {
for i := 0; i < 16; i++ {
partitionCount := int(math.Pow(float64(2), float64(i)))
name := fmt.Sprintf("With_%d_Partitions", partitionCount)
t.Run(name, func(t *testing.T) {
mask := hashMask(partitionCount)
assert.Equal(t, partitionCount-1, mask)
})
}
}