-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnemesis_test.go
122 lines (107 loc) · 2.59 KB
/
nemesis_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
//go:build sim
package nemesis_test
import (
"context"
"fmt"
"io"
"log"
"net/http"
"net/netip"
"testing"
"time"
"golang.org/x/sync/errgroup"
"github.com/unnaturalfi/gosim"
"github.com/unnaturalfi/gosim/nemesis"
)
func makeAddresses() []string {
var addresses []string
for i := 0; i < 3; i++ {
addresses = append(addresses, fmt.Sprintf("10.0.0.%d", i+1))
}
return addresses
}
// request makes a HTTP GET request with a 1 second timeout to
// http://<addr>/ping and prints the result
func request(addr string) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("http://%s/ping", addr), nil)
if err != nil {
log.Fatal(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Printf("pinging %s: got error: %v", addr, err)
return
}
defer resp.Body.Close()
bytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("pinging %s: got error: %v", addr, err)
return
}
log.Printf("pinging %s: got response %q", addr, string(bytes))
}
func pingerMain() {
log.Printf("starting pinger")
// figure out our own address
// TODO: make another API work
addr := gosim.CurrentMachine().Label()
// run http server
// TODO: make ":80" without addr work
go http.ListenAndServe(addr+":80", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello there")
}))
addrs := makeAddresses()
// ping others every 5 seconds
t := time.NewTicker(5 * time.Second)
for range t.C {
// ping in parallel
var g errgroup.Group
for _, other := range addrs {
if addr != other {
g.Go(func() error {
request(other)
return nil
})
}
}
g.Wait()
}
}
func TestPartition(t *testing.T) {
addrs := makeAddresses()
// run ping on several machines
var machines []gosim.Machine
for _, addr := range addrs {
m := gosim.NewMachine(gosim.MachineConfig{
Label: addr, // fmt.Sprintf("server-%d", i),
Addr: netip.MustParseAddr(addr),
MainFunc: pingerMain,
})
machines = append(machines, m)
}
// let machines communicate for a while, then randomly partition, then
// repair, then crash one machine, and repair again
scenario := nemesis.Sequence(
nemesis.Sleep{
Duration: 10 * time.Second,
},
nemesis.PartitionMachines{
// TODO: make this API based of machines?
Addresses: addrs,
Duration: 10 * time.Second,
},
nemesis.Sleep{
Duration: 10 * time.Second,
},
nemesis.RestartRandomly{
Machines: machines,
Downtime: 10 * time.Second,
},
nemesis.Sleep{
Duration: 10 * time.Second,
},
)
scenario.Run()
}