@@ -36,41 +36,67 @@ started with `gosim`, you need to import it in your module:
36
36
To test code with Gosim, write a small a test in a file ` simple_test.go ` and then run
37
37
it using ` go test ` :
38
38
``` go
39
- package example_test
39
+ package examples_test
40
40
41
41
import (
42
+ " math/rand"
42
43
" testing"
43
44
44
45
" github.com/jellevandenhooff/gosim"
45
46
)
46
47
47
48
func TestGosim (t *testing .T ) {
48
49
t.Logf (" Are we in the Matrix? %v " , gosim.IsSim ())
50
+ t.Logf (" Random: %d " , rand.Int ())
49
51
}
50
52
```
51
53
```
52
- > go test -v -run TestGosim
54
+ > go test -v -count=1 - run TestGosim
53
55
=== RUN TestGosim
54
- simple_test.go:10: Are we in the Matrix? false
55
- --- PASS: TestGosim (0.00s simulated)
56
+ simple_test.go:11: Are we in the Matrix? false
57
+ simple_test.go:12: Random: 1225418128470362734
58
+ --- PASS: TestGosim (0.00s)
56
59
PASS
57
60
ok example 0.216s
58
61
```
62
+ The test prints that Gosim is not enabled, and each time the test runs the random number is different.
63
+
59
64
To run this test with ` gosim ` instead, replace ` go test ` with
60
65
` go run github.com/jellevandenhooff/gosim/cmd/gosim test ` :
61
66
```
62
67
> go run github.com/jellevandenhooff/gosim/cmd/gosim test -v -run TestGosim
63
- === RUN TestGosim
64
- 1 main/4 14:10:03.000 INF example/simple_test.go:11 > Are we in the Matrix? true method=t.Logf
65
- --- PASS: TestGosim (0.00s)
68
+ === RUN TestGosim (seed 1)
69
+ 1 main/4 14:10:03.000 INF examples/simple_test.go:12 > Are we in the Matrix? true method=t.Logf
70
+ 2 main/4 14:10:03.000 INF examples/simple_test.go:13 > Random: 811966193383742320 method=t.Logf
71
+ --- PASS: TestGosim (0.00s simulated)
66
72
ok translated/example 0.204s
67
73
```
68
74
The ` gosim test ` command has flags similar to ` go test ` . The test output is
69
75
more involved than a normal ` go test ` . Every log line includes the simulated
70
76
machine and the goroutine that invoked the log to help debug tests running on
71
77
multiple machines.
72
78
73
- If running gosim fails with errors about missing ` go.sum ` entries, run
79
+ The ` === RUN ` line includes the test's seed. Each time this test runs with the
80
+ same seed it will output the same random number. To test with different seeds,
81
+ you can pass ranges of seeds to ` gosim test ` :
82
+ ```
83
+ go run github.com/jellevandenhooff/gosim/cmd/gosim test -v -seeds=1-3 -run=TestGosim .
84
+ === RUN TestGosim (seed 1)
85
+ 1 main/4 14:10:03.000 INF examples/simple_test.go:12 > Are we in the Matrix? true method=t.Logf
86
+ 2 main/4 14:10:03.000 INF examples/simple_test.go:13 > Random: 811966193383742320 method=t.Logf
87
+ --- PASS: TestGosim (0.00s simulated)
88
+ === RUN TestGosim (seed 2)
89
+ 1 main/4 14:10:03.000 INF examples/simple_test.go:12 > Are we in the Matrix? true method=t.Logf
90
+ 2 main/4 14:10:03.000 INF examples/simple_test.go:13 > Random: 5374891573232646577 method=t.Logf
91
+ --- PASS: TestGosim (0.00s simulated)
92
+ === RUN TestGosim (seed 3)
93
+ 1 main/4 14:10:03.000 INF examples/simple_test.go:12 > Are we in the Matrix? true method=t.Logf
94
+ 2 main/4 14:10:03.000 INF examples/simple_test.go:13 > Random: 3226404213937589817 method=t.Logf
95
+ --- PASS: TestGosim (0.00s simulated)
96
+ ok translated/github.com/jellevandenhooff/gosim/examples 0.254s
97
+ ```
98
+
99
+ If running ` gosim ` fails with errors about missing ` go.sum ` entries, run
74
100
` go mod tidy ` to update your ` go.sum ` file.
75
101
76
102
## Simulation
@@ -162,7 +188,7 @@ what happens this test is run:
162
188
<!-- TODO: two machines and a bug? -->
163
189
```
164
190
> go run github.com/jellevandenhooff/gosim/cmd/gosim test -v -run TestMachines
165
- === RUN TestMachines
191
+ === RUN TestMachines (seed 1)
166
192
1 server/5 14:10:03.000 INF example/machines_test.go:17 > starting server
167
193
2 main/4 14:10:04.000 INF example/machines_test.go:27 > making a request
168
194
3 server/8 14:10:04.000 INF example/machines_test.go:20 > got a request from 11.0.0.1:10000
@@ -217,7 +243,7 @@ realistic latency and timeouts without development time becoming slow.
217
243
## Debugging
218
244
Gosim's simulation is deterministic, which means that running a test twice
219
245
will result in the exact same behavior, from randomly generated numbers
220
- to goroutine concurrency interleavings. That is cool because it means that
246
+ to goroutine concurrency interleavings. That is useful because it means that
221
247
if we see an interesting log after running the program but do not fully
222
248
understand why that log printed a certain value, we can re-run the program
223
249
and see exactly what happened at the moment of printing.
0 commit comments