-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
163 lines (145 loc) · 3.61 KB
/
main_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
151
152
153
154
155
156
157
158
159
160
161
162
163
package diskq
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
)
func tempDir() (string, func()) {
dir := filepath.Join(os.TempDir(), UUIDv4().String())
_ = os.Mkdir(dir, 0755)
return dir, func() {
_ = os.RemoveAll(dir)
}
}
func assert_noerror(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Errorf("expected err to be unset; %v", err)
t.FailNow()
}
}
func assert_error(t *testing.T, err error) {
t.Helper()
if err == nil {
t.Errorf("expected err to set")
t.FailNow()
}
}
func assert_nil(t *testing.T, a any) {
t.Helper()
if !isNil(a) {
t.Errorf("expected value to be nil")
t.FailNow()
}
}
func assert_notnil(t *testing.T, a any) {
t.Helper()
if isNil(a) {
t.Errorf("expected value to not be nil")
t.FailNow()
}
}
func assert_equal(t *testing.T, expected, actual any, extra ...any) {
t.Helper()
if !areEqual(expected, actual) {
if len(extra) > 0 {
t.Errorf("equal expectation failed; expected value: %v, actual value: %v (%s)", expected, actual, fmt.Sprint(extra...))
} else {
t.Errorf("equal expectation failed; expected value: %v, actual value: %v", expected, actual)
}
t.FailNow()
}
}
func assert_notequal(t *testing.T, expected, actual any, extra ...any) {
t.Helper()
if areEqual(expected, actual) {
if len(extra) > 0 {
t.Errorf("not equal expectation failed; expected value: %v, actual value: %v (%s)", expected, actual, fmt.Sprint(extra...))
} else {
t.Errorf("not equal expectation failed; expected value: %v, actual value: %v", expected, actual)
}
t.FailNow()
}
}
func areEqual(expected, actual any) bool {
if isNil(expected) && isNil(actual) {
return true
}
if (isNil(expected) && !isNil(actual)) || (!isNil(expected) && isNil(actual)) {
return false
}
actualType := reflect.TypeOf(actual)
if actualType == nil {
return false
}
expectedValue := reflect.ValueOf(expected)
if expectedValue.IsValid() && expectedValue.Type().ConvertibleTo(actualType) {
return reflect.DeepEqual(expectedValue.Convert(actualType).Interface(), actual)
}
return reflect.DeepEqual(expected, actual)
}
// isNil returns if a given reference is nil, but also returning true
// if the reference is a valid typed pointer to nil, which may not strictly
// be equal to nil.
func isNil(object any) bool {
if object == nil {
return true
}
value := reflect.ValueOf(object)
kind := value.Kind()
if kind >= reflect.Chan && kind <= reflect.Slice && value.IsNil() {
return true
}
return false
}
func readIndexEntries(r io.Reader) (output []SegmentIndex) {
for {
var si SegmentIndex
err := binary.Read(r, binary.LittleEndian, &si)
if err == io.EOF {
return
}
output = append(output, si)
}
}
func messageSizeBytes(m Message) int64 {
data := new(bytes.Buffer)
_ = Encode(m, data)
return int64(data.Len())
}
func formatTestMessagePartitionKey(index int) string {
return fmt.Sprintf("data-%03d-key", index)
}
func testMessage(index int, dataSize int64) Message {
return Message{
TimestampUTC: time.Date(2024, 04, 05, 12, 11, 10, 9, time.UTC),
PartitionKey: formatTestMessagePartitionKey(index),
Data: []byte(strings.Repeat("a", int(dataSize))),
}
}
func testMessageStable3(index int, dataSize int64) Message {
return Message{
TimestampUTC: time.Date(2024, 04, 05, 12, 11, 10, 9, time.UTC),
PartitionKey: testPartitionKeys3[index%3],
Data: []byte(strings.Repeat("a", int(dataSize))),
}
}
var testPartitionKeys3 = [3]string{
"aaa", // 0
"bbb", // 1
"111", // 2
}
// var testPartitionKeys5 = [5]string{
// "aaa", // 0
// "ddd", // 1
// "bar", // 2
// "cba", // 3
// "fef", // 4
// }