forked from linkedin/goavro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers_test.go
113 lines (101 loc) · 2.7 KB
/
helpers_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
// Copyright [2019] LinkedIn Corp. Licensed under the Apache License, Version
// 2.0 (the "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
package goavro
import (
"io"
"runtime"
"strings"
"sync"
"testing"
)
func benchmarkLowAndHigh(b *testing.B, callback func()) {
// Run test case in parallel at relative low concurrency
b.Run("Low", func(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
callback()
}
})
})
// Run test case in parallel at relative high concurrency
b.Run("High", func(b *testing.B) {
concurrency := runtime.NumCPU() * 1000
wg := new(sync.WaitGroup)
wg.Add(concurrency)
b.ResetTimer()
for c := 0; c < concurrency; c++ {
go func() {
defer wg.Done()
for n := 0; n < b.N; n++ {
callback()
}
}()
}
wg.Wait()
})
}
func ensureEqual(t *testing.T, actual, expected interface{}) {
if actual != expected {
_, file, line, ok := runtime.Caller(1)
if !ok {
t.Errorf("Actual: %#v; Expected: %#v", actual, expected)
} else {
if index := strings.LastIndex(file, "/"); index != -1 {
file = file[index+1:]
}
t.Errorf("Actual: %#v; Expected: %#v; %s:%d", actual, expected, file, line)
}
}
}
// ensure code under test returns error containing specified string
func ensureError(tb testing.TB, err error, contains ...string) {
if err == nil {
tb.Errorf("GOT: %v; WANT: %#v", err, contains)
return
}
for _, stub := range contains {
if !strings.Contains(err.Error(), stub) {
tb.Errorf("GOT: %v; WANT: %#v", err, contains)
}
}
}
// ShortWriter returns a structure that wraps an io.Writer, but returns
// io.ErrShortWrite when the number of bytes to write exceeds a preset limit.
//
// Copied with author's permission from https://github.com/karrick/gorill.
//
// bb := NopCloseBuffer()
// sw := ShortWriter(bb, 16)
//
// n, err := sw.Write([]byte("short write"))
// // n == 11, err == nil
//
// n, err := sw.Write([]byte("a somewhat longer write"))
// // n == 16, err == io.ErrShortWrite
func ShortWriter(w io.Writer, max int) io.Writer {
return shortWriter{Writer: w, max: max}
}
func (s shortWriter) Write(data []byte) (int, error) {
var short bool
index := len(data)
if index > s.max {
index = s.max
short = true
}
n, err := s.Writer.Write(data[:index])
if short {
return n, io.ErrShortWrite
}
return n, err
}
type shortWriter struct {
io.Writer
max int
}