Skip to content

Commit 5b6d5cd

Browse files
committed
initial commit
0 parents  commit 5b6d5cd

File tree

18 files changed

+519
-0
lines changed

18 files changed

+519
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/.idea
2+
/.vscode

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2020 Tobias Salzmann
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# go-test-buckets
2+
Split your go tests into buckets.
3+
4+
```golang
5+
package main_test
6+
7+
import (
8+
"testing"
9+
10+
_ "github.com/Eun/go-test-buckets"
11+
)
12+
13+
// run with go test -bucket=0 -total-buckets=2 -v
14+
// will run TestA and TestB
15+
16+
// run with go test -bucket=1 -total-buckets=2 -v
17+
// will run TestC
18+
19+
func TestA(t *testing.T) {
20+
}
21+
22+
func TestB(t *testing.T) {
23+
}
24+
25+
func TestC(t *testing.T) {
26+
}
27+
```
28+
29+
> Note that uses some nasty memory patching to make this possible. So use with care.
30+
31+
# Why?
32+
Speed up ci pipelines by parallelizing go tests without thinking about [t.Parallel](https://golang.org/pkg/testing/#T.Parallel).

example/test/some_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package test
2+
3+
import (
4+
"testing"
5+
6+
_ "github.com/Eun/go-test-buckets"
7+
)
8+
9+
// run with go test -bucket=0 -total-buckets=2 -v
10+
// will run TestA and TestB
11+
12+
// run with go test -bucket=1 -total-buckets=2 -v
13+
// will run TestC
14+
15+
func TestA(t *testing.T) {
16+
t.Run("TestA1", func(t *testing.T) {})
17+
t.Run("TestA2", func(t *testing.T) {})
18+
}
19+
20+
func TestB(t *testing.T) {
21+
t.Run("TestB1", func(t *testing.T) {})
22+
}
23+
24+
func TestC(t *testing.T) {
25+
}

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/Eun/go-test-buckets
2+
3+
go 1.12
4+
5+
require bou.ke/monkey v1.0.2

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
2+
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=

main.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package buckets
2+
3+
import (
4+
"flag"
5+
"math"
6+
"reflect"
7+
"testing"
8+
"unsafe"
9+
10+
"bou.ke/monkey"
11+
)
12+
13+
var bucketIndex = flag.Int("bucket", 0, "bucket index to use")
14+
var bucketCount = flag.Int("total-buckets", 0, "total bucket count")
15+
16+
func init() {
17+
flag.Parse()
18+
19+
if bucketCount == nil || bucketIndex == nil {
20+
return
21+
}
22+
23+
if *bucketCount <= 0 || *bucketIndex < 0 || *bucketIndex >= *bucketCount {
24+
return
25+
}
26+
27+
patchTestRun()
28+
}
29+
30+
func patchTestRun() {
31+
var guard *monkey.PatchGuard
32+
guard = monkey.PatchInstanceMethod(reflect.TypeOf(&testing.M{}), "Run", func(m *testing.M) int {
33+
guard.Unpatch()
34+
defer guard.Restore()
35+
36+
v := reflect.ValueOf(m).Elem()
37+
testsField := v.FieldByName("tests")
38+
ptr := unsafe.Pointer(testsField.UnsafeAddr())
39+
tests := (*[]testing.InternalTest)(ptr)
40+
41+
perBucket := int(math.Ceil(float64(len(*tests)) / float64(*bucketCount)))
42+
43+
from := *bucketIndex * perBucket
44+
to := from + perBucket
45+
46+
if to > len(*tests) {
47+
to = len(*tests)
48+
}
49+
50+
*tests = (*tests)[from:to]
51+
52+
return m.Run()
53+
})
54+
}

vendor/bou.ke/monkey/LICENSE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Copyright Bouke van der Bijl
2+
3+
I do not give anyone permissions to use this tool for any purpose. Don't use it.
4+
5+
I’m not interested in changing this license. Please don’t ask.

vendor/bou.ke/monkey/README.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Go monkeypatching :monkey_face: :monkey:
2+
3+
Actual arbitrary monkeypatching for Go. Yes really.
4+
5+
Read this blogpost for an explanation on how it works: https://bou.ke/blog/monkey-patching-in-go/
6+
7+
## I thought that monkeypatching in Go is impossible?
8+
9+
It's not possible through regular language constructs, but we can always bend computers to our will! Monkey implements monkeypatching by rewriting the running executable at runtime and inserting a jump to the function you want called instead. **This is as unsafe as it sounds and I don't recommend anyone do it outside of a testing environment.**
10+
11+
Make sure you read the notes at the bottom of the README if you intend to use this library.
12+
13+
## Using monkey
14+
15+
Monkey's API is very simple and straightfoward. Call `monkey.Patch(<target function>, <replacement function>)` to replace a function. For example:
16+
17+
```go
18+
package main
19+
20+
import (
21+
"fmt"
22+
"os"
23+
"strings"
24+
25+
"bou.ke/monkey"
26+
)
27+
28+
func main() {
29+
monkey.Patch(fmt.Println, func(a ...interface{}) (n int, err error) {
30+
s := make([]interface{}, len(a))
31+
for i, v := range a {
32+
s[i] = strings.Replace(fmt.Sprint(v), "hell", "*bleep*", -1)
33+
}
34+
return fmt.Fprintln(os.Stdout, s...)
35+
})
36+
fmt.Println("what the hell?") // what the *bleep*?
37+
}
38+
```
39+
40+
You can then call `monkey.Unpatch(<target function>)` to unpatch the method again. The replacement function can be any function value, whether it's anonymous, bound or otherwise.
41+
42+
If you want to patch an instance method you need to use `monkey.PatchInstanceMethod(<type>, <name>, <replacement>)`. You get the type by using `reflect.TypeOf`, and your replacement function simply takes the instance as the first argument. To disable all network connections, you can do as follows for example:
43+
44+
```go
45+
package main
46+
47+
import (
48+
"fmt"
49+
"net"
50+
"net/http"
51+
"reflect"
52+
53+
"bou.ke/monkey"
54+
)
55+
56+
func main() {
57+
var d *net.Dialer // Has to be a pointer to because `Dial` has a pointer receiver
58+
monkey.PatchInstanceMethod(reflect.TypeOf(d), "Dial", func(_ *net.Dialer, _, _ string) (net.Conn, error) {
59+
return nil, fmt.Errorf("no dialing allowed")
60+
})
61+
_, err := http.Get("http://google.com")
62+
fmt.Println(err) // Get http://google.com: no dialing allowed
63+
}
64+
65+
```
66+
67+
Note that patching the method for just one instance is currently not possible, `PatchInstanceMethod` will patch it for all instances. Don't bother trying `monkey.Patch(instance.Method, replacement)`, it won't work. `monkey.UnpatchInstanceMethod(<type>, <name>)` will undo `PatchInstanceMethod`.
68+
69+
If you want to remove all currently applied monkeypatches simply call `monkey.UnpatchAll`. This could be useful in a test teardown function.
70+
71+
If you want to call the original function from within the replacement you need to use a `monkey.PatchGuard`. A patchguard allows you to easily remove and restore the patch so you can call the original function. For example:
72+
73+
```go
74+
package main
75+
76+
import (
77+
"fmt"
78+
"net/http"
79+
"reflect"
80+
"strings"
81+
82+
"bou.ke/monkey"
83+
)
84+
85+
func main() {
86+
var guard *monkey.PatchGuard
87+
guard = monkey.PatchInstanceMethod(reflect.TypeOf(http.DefaultClient), "Get", func(c *http.Client, url string) (*http.Response, error) {
88+
guard.Unpatch()
89+
defer guard.Restore()
90+
91+
if !strings.HasPrefix(url, "https://") {
92+
return nil, fmt.Errorf("only https requests allowed")
93+
}
94+
95+
return c.Get(url)
96+
})
97+
98+
_, err := http.Get("http://google.com")
99+
fmt.Println(err) // only https requests allowed
100+
resp, err := http.Get("https://google.com")
101+
fmt.Println(resp.Status, err) // 200 OK <nil>
102+
}
103+
```
104+
105+
## Notes
106+
107+
1. Monkey sometimes fails to patch a function if inlining is enabled. Try running your tests with inlining disabled, for example: `go test -gcflags=-l`. The same command line argument can also be used for build.
108+
2. Monkey won't work on some security-oriented operating system that don't allow memory pages to be both write and execute at the same time. With the current approach there's not really a reliable fix for this.
109+
3. Monkey is not threadsafe. Or any kind of safe.
110+
4. I've tested monkey on OSX 10.10.2 and Ubuntu 14.04. It should work on any unix-based x86 or x86-64 system.
111+
112+
© Bouke van der Bijl

vendor/bou.ke/monkey/circle.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test:
2+
override:
3+
- script/test

0 commit comments

Comments
 (0)