forked from Argus-Labs/world-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
128 lines (110 loc) · 3.62 KB
/
option.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
package cardinal
import (
"os"
"time"
"github.com/alicebob/miniredis/v2"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"pkg.world.dev/world-engine/cardinal/gamestate"
"pkg.world.dev/world-engine/cardinal/receipt"
"pkg.world.dev/world-engine/cardinal/router"
"pkg.world.dev/world-engine/cardinal/server"
)
// WorldOption represents an option that can be used to augment how the cardinal.World will be run.
type WorldOption struct {
serverOption server.Option
routerOption router.Option
cardinalOption Option
}
type Option func(*World)
// WithPort sets the port that the HTTP server will run on.
func WithPort(port string) WorldOption {
return WorldOption{
serverOption: server.WithPort(port),
}
}
// WithReceiptHistorySize specifies how many ticks worth of transaction receipts should be kept in memory. The default
// is 10. A smaller number uses less memory, but limits the amount of historical receipts available.
func WithReceiptHistorySize(size int) WorldOption {
return WorldOption{
cardinalOption: func(world *World) {
world.receiptHistory = receipt.NewHistory(world.CurrentTick(), size)
},
}
}
// WithDisableSignatureVerification disables signature verification for the HTTP server. This should only be
// used for local development.
func WithDisableSignatureVerification() WorldOption {
return WorldOption{
serverOption: server.DisableSignatureVerification(),
}
}
// WithTickChannel sets the channel that will be used to decide when world.doTick is executed. If unset, a loop interval
// of 1 second will be set. To set some other time, use: WithTickChannel(time.Tick(<some-duration>)). Tests can pass
// in a channel controlled by the test for fine-grained control over when ticks are executed.
func WithTickChannel(ch <-chan time.Time) WorldOption {
return WorldOption{
cardinalOption: func(world *World) {
world.tickChannel = ch
},
}
}
// WithTickDoneChannel sets a channel that will be notified each time a tick completes. The completed tick will be
// pushed to the channel. This option is useful in tests when assertions need to be performed at the end of a tick.
func WithTickDoneChannel(ch chan<- uint64) WorldOption {
return WorldOption{
cardinalOption: func(world *World) {
world.tickDoneChannel = ch
},
}
}
func WithStoreManager(s gamestate.Manager) WorldOption {
return WorldOption{
cardinalOption: func(world *World) {
world.entityStore = s
},
}
}
// WithMockRedis runs the World with an embedded miniredis instance on port 6379.
func WithMockRedis() WorldOption {
// Start a miniredis instance on port 6379.
mr := miniredis.NewMiniRedis()
err := mr.StartAddr(":6379")
if err != nil {
log.Fatal().Err(err).Msg("failed to start miniredis")
}
log.Debug().Msgf("miniredis started at %s", mr.Addr())
// Set the REDIS_ADDRESS environment variable to the miniredis address.
err = os.Setenv("REDIS_ADDRESS", mr.Addr())
if err != nil {
log.Fatal().Err(err).Msg("unable to set REDIS_ADDRESS")
}
return WorldOption{}
}
// WithMockJobQueue runs the router with an in-memory job queue instead of a persistent one that writes to disk.
func WithMockJobQueue() WorldOption {
return WorldOption{
routerOption: router.WithMockJobQueue(),
}
}
func WithCustomLogger(logger zerolog.Logger) WorldOption {
return WorldOption{
cardinalOption: func(_ *World) {
log.Logger = logger
},
}
}
func WithCustomRouter(rtr router.Router) WorldOption {
return WorldOption{
cardinalOption: func(world *World) {
world.router = rtr
},
}
}
func WithPrettyLog() WorldOption {
return WorldOption{
cardinalOption: func(_ *World) {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
},
}
}