-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_options_optiongen.go
177 lines (156 loc) · 5.16 KB
/
gen_options_optiongen.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Code generated by optiongen. DO NOT EDIT.
// optiongen: github.com/timestee/optiongen
package xtime
import (
"io"
"os"
"time"
)
// Options should use NewOptions to initialize it
type Options struct {
// annotation@Gosched(comment="Gosched让出CPU防止忙占")
Gosched func()
// annotation@TickIntervalUnderMock(comment="真实的tick时间间隔,用于驱动mock模式下的tiker、timer")
TickIntervalUnderMock time.Duration
// annotation@TickAtMockNow(comment="timer,ticker在tick的时间为mock的当前时间,而不是Next时间,如果为Next时间,在时间跳转后会导致循环执行同一个ticker,timer")
TickAtMockNow bool
// annotation@NowProvider(comment="系统时间")
NowProvider NowProvider
// annotation@SleepProviderUnderFrozen(comment="Frozen模式下sleep将再次Freeze一次,将时间向前推进")
SleepProviderUnderFrozen SleepProviderUnderFrozen
// annotation@Debug(comment="debug模式下以会向DebugWriter写日志")
Debug bool
// annotation@DebugWriter(comment="调试日志输出")
DebugWriter io.Writer
}
// NewOptions new Options
func NewOptions(opts ...Option) *Options {
cc := newDefaultOptions()
for _, opt := range opts {
opt(cc)
}
if watchDogOptions != nil {
watchDogOptions(cc)
}
return cc
}
// ApplyOption apply mutiple new option and return the old ones
// sample:
// old := cc.ApplyOption(WithTimeout(time.Second))
// defer cc.ApplyOption(old...)
func (cc *Options) ApplyOption(opts ...Option) []Option {
var previous []Option
for _, opt := range opts {
previous = append(previous, opt(cc))
}
return previous
}
// Option option func
type Option func(cc *Options) Option
// WithGosched Gosched让出CPU防止忙占
func WithGosched(v func()) Option {
return func(cc *Options) Option {
previous := cc.Gosched
cc.Gosched = v
return WithGosched(previous)
}
}
// WithTickIntervalUnderMock 真实的tick时间间隔,用于驱动mock模式下的tiker、timer
func WithTickIntervalUnderMock(v time.Duration) Option {
return func(cc *Options) Option {
previous := cc.TickIntervalUnderMock
cc.TickIntervalUnderMock = v
return WithTickIntervalUnderMock(previous)
}
}
// WithTickAtMockNow timer,ticker在tick的时间为mock的当前时间,而不是Next时间,如果为Next时间,在时间跳转后会导致循环执行同一个ticker,timer
func WithTickAtMockNow(v bool) Option {
return func(cc *Options) Option {
previous := cc.TickAtMockNow
cc.TickAtMockNow = v
return WithTickAtMockNow(previous)
}
}
// WithNowProvider 系统时间
func WithNowProvider(v NowProvider) Option {
return func(cc *Options) Option {
previous := cc.NowProvider
cc.NowProvider = v
return WithNowProvider(previous)
}
}
// WithSleepProviderUnderFrozen Frozen模式下sleep将再次Freeze一次,将时间向前推进
func WithSleepProviderUnderFrozen(v SleepProviderUnderFrozen) Option {
return func(cc *Options) Option {
previous := cc.SleepProviderUnderFrozen
cc.SleepProviderUnderFrozen = v
return WithSleepProviderUnderFrozen(previous)
}
}
// WithDebug debug模式下以会向DebugWriter写日志
func WithDebug(v bool) Option {
return func(cc *Options) Option {
previous := cc.Debug
cc.Debug = v
return WithDebug(previous)
}
}
// WithDebugWriter 调试日志输出
func WithDebugWriter(v io.Writer) Option {
return func(cc *Options) Option {
previous := cc.DebugWriter
cc.DebugWriter = v
return WithDebugWriter(previous)
}
}
// InstallOptionsWatchDog the installed func will called when NewOptions called
func InstallOptionsWatchDog(dog func(cc *Options)) { watchDogOptions = dog }
// watchDogOptions global watch dog
var watchDogOptions func(cc *Options)
// newDefaultOptions new default Options
func newDefaultOptions() *Options {
cc := &Options{}
for _, opt := range [...]Option{
WithGosched(func() {
timeSleep(1 * time.Millisecond)
}),
WithTickIntervalUnderMock(time.Millisecond),
WithTickAtMockNow(false),
WithNowProvider(func() time.Time {
return timeNow()
}),
WithSleepProviderUnderFrozen(func(m Mock, d time.Duration) {
m.Freeze(m.Now().Add(d))
}),
WithDebug(false),
WithDebugWriter(os.Stdout),
} {
opt(cc)
}
return cc
}
// all getter func
func (cc *Options) GetGosched() func() { return cc.Gosched }
func (cc *Options) GetTickIntervalUnderMock() time.Duration { return cc.TickIntervalUnderMock }
func (cc *Options) GetTickAtMockNow() bool { return cc.TickAtMockNow }
func (cc *Options) GetNowProvider() NowProvider { return cc.NowProvider }
func (cc *Options) GetSleepProviderUnderFrozen() SleepProviderUnderFrozen {
return cc.SleepProviderUnderFrozen
}
func (cc *Options) GetDebug() bool { return cc.Debug }
func (cc *Options) GetDebugWriter() io.Writer { return cc.DebugWriter }
// OptionsVisitor visitor interface for Options
type OptionsVisitor interface {
GetGosched() func()
GetTickIntervalUnderMock() time.Duration
GetTickAtMockNow() bool
GetNowProvider() NowProvider
GetSleepProviderUnderFrozen() SleepProviderUnderFrozen
GetDebug() bool
GetDebugWriter() io.Writer
}
// OptionsInterface visitor + ApplyOption interface for Options
type OptionsInterface interface {
OptionsVisitor
ApplyOption(...Option) []Option
}