Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3-delay
Commits
325e08ed
Commit
325e08ed
authored
Nov 09, 2018
by
hannahhoward
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove sleeper only used for testing
parent
f91cdb03
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
6 additions
and
47 deletions
+6
-47
delay.go
delay.go
+5
-12
delay_test.go
delay_test.go
+1
-15
sleeper.go
sleeper.go
+0
-20
No files found.
delay.go
View file @
325e08ed
...
@@ -6,8 +6,6 @@ import (
...
@@ -6,8 +6,6 @@ import (
"time"
"time"
)
)
var
sharedRealSleeper
=
NewRealSleeper
()
// D (Delay) makes it easy to add (threadsafe) configurable delays to other
// D (Delay) makes it easy to add (threadsafe) configurable delays to other
// objects.
// objects.
type
D
interface
{
type
D
interface
{
...
@@ -21,7 +19,6 @@ type delay struct {
...
@@ -21,7 +19,6 @@ type delay struct {
l
sync
.
RWMutex
l
sync
.
RWMutex
t
time
.
Duration
t
time
.
Duration
generator
Generator
generator
Generator
sleeper
Sleeper
}
}
func
(
d
*
delay
)
Set
(
t
time
.
Duration
)
time
.
Duration
{
func
(
d
*
delay
)
Set
(
t
time
.
Duration
)
time
.
Duration
{
...
@@ -35,7 +32,7 @@ func (d *delay) Set(t time.Duration) time.Duration {
...
@@ -35,7 +32,7 @@ func (d *delay) Set(t time.Duration) time.Duration {
func
(
d
*
delay
)
Wait
()
{
func
(
d
*
delay
)
Wait
()
{
d
.
l
.
RLock
()
d
.
l
.
RLock
()
defer
d
.
l
.
RUnlock
()
defer
d
.
l
.
RUnlock
()
d
.
sleeper
.
Sleep
(
d
.
generator
.
NextWaitTime
(
d
.
t
))
time
.
Sleep
(
d
.
generator
.
NextWaitTime
(
d
.
t
))
}
}
func
(
d
*
delay
)
NextWaitTime
()
time
.
Duration
{
func
(
d
*
delay
)
NextWaitTime
()
time
.
Duration
{
...
@@ -51,32 +48,28 @@ func (d *delay) Get() time.Duration {
...
@@ -51,32 +48,28 @@ func (d *delay) Get() time.Duration {
}
}
// Delay generates a generic delay form a t, a sleeper, and a generator
// Delay generates a generic delay form a t, a sleeper, and a generator
func
Delay
(
t
time
.
Duration
,
generator
Generator
,
sleeper
Sleeper
)
D
{
func
Delay
(
t
time
.
Duration
,
generator
Generator
)
D
{
if
sleeper
==
nil
{
sleeper
=
sharedRealSleeper
}
return
&
delay
{
return
&
delay
{
t
:
t
,
t
:
t
,
sleeper
:
sleeper
,
generator
:
generator
,
generator
:
generator
,
}
}
}
}
// Fixed returns a delay with fixed latency
// Fixed returns a delay with fixed latency
func
Fixed
(
t
time
.
Duration
)
D
{
func
Fixed
(
t
time
.
Duration
)
D
{
return
Delay
(
t
,
FixedGenerator
()
,
nil
)
return
Delay
(
t
,
FixedGenerator
())
}
}
// VariableUniform is a delay following a uniform distribution
// VariableUniform is a delay following a uniform distribution
// Notice that to implement the D interface Set can only change the minimum delay
// Notice that to implement the D interface Set can only change the minimum delay
// the delta is set only at initialization
// the delta is set only at initialization
func
VariableUniform
(
t
,
d
time
.
Duration
,
rng
*
rand
.
Rand
)
D
{
func
VariableUniform
(
t
,
d
time
.
Duration
,
rng
*
rand
.
Rand
)
D
{
return
Delay
(
t
,
VariableUniformGenerator
(
d
,
rng
)
,
nil
)
return
Delay
(
t
,
VariableUniformGenerator
(
d
,
rng
))
}
}
// VariableNormal is a delay following a normal distribution
// VariableNormal is a delay following a normal distribution
// Notice that to implement the D interface Set can only change the mean delay
// Notice that to implement the D interface Set can only change the mean delay
// the standard deviation is set only at initialization
// the standard deviation is set only at initialization
func
VariableNormal
(
t
,
std
time
.
Duration
,
rng
*
rand
.
Rand
)
D
{
func
VariableNormal
(
t
,
std
time
.
Duration
,
rng
*
rand
.
Rand
)
D
{
return
Delay
(
t
,
VariableNormalGenerator
(
std
,
rng
)
,
nil
)
return
Delay
(
t
,
VariableNormalGenerator
(
std
,
rng
))
}
}
delay_test.go
View file @
325e08ed
...
@@ -46,14 +46,6 @@ func TestDelaySetAndGet(t *testing.T) {
...
@@ -46,14 +46,6 @@ func TestDelaySetAndGet(t *testing.T) {
}
}
type
recordSleeper
struct
{
lastSleep
time
.
Duration
}
func
(
rs
*
recordSleeper
)
Sleep
(
t
time
.
Duration
)
{
rs
.
lastSleep
=
t
}
type
fixedAdd
struct
{
type
fixedAdd
struct
{
toAdd
time
.
Duration
toAdd
time
.
Duration
}
}
...
@@ -66,16 +58,10 @@ func TestDelaySleep(t *testing.T) {
...
@@ -66,16 +58,10 @@ func TestDelaySleep(t *testing.T) {
initialValue
:=
1000
*
time
.
Millisecond
initialValue
:=
1000
*
time
.
Millisecond
toAdd
:=
500
*
time
.
Millisecond
toAdd
:=
500
*
time
.
Millisecond
generator
:=
&
fixedAdd
{
toAdd
:
toAdd
}
generator
:=
&
fixedAdd
{
toAdd
:
toAdd
}
sleeper
:=
&
recordSleeper
{
lastSleep
:
-
1
}
delay
:=
Delay
(
initialValue
,
generator
)
delay
:=
Delay
(
initialValue
,
generator
,
sleeper
)
if
delay
.
NextWaitTime
()
!=
initialValue
+
toAdd
{
if
delay
.
NextWaitTime
()
!=
initialValue
+
toAdd
{
t
.
Fatal
(
"NextWaitTime should call the generator"
)
t
.
Fatal
(
"NextWaitTime should call the generator"
)
}
}
delay
.
Wait
()
if
sleeper
.
lastSleep
!=
initialValue
+
toAdd
{
t
.
Fatal
(
"Wait should sleep based on the next wait time generated"
)
}
}
}
sleeper.go
deleted
100644 → 0
View file @
f91cdb03
package
delay
import
"time"
// Sleeper - a generic interface for wrapping a sleep function
// So that sleeping can be mocked in tests
type
Sleeper
interface
{
Sleep
(
time
.
Duration
)
}
type
realSleeper
struct
{}
func
(
rc
*
realSleeper
)
Sleep
(
d
time
.
Duration
)
{
time
.
Sleep
(
d
)
}
// NewRealSleeper - returns a new sleeper that uses the real time.Sleep function
func
NewRealSleeper
()
Sleeper
{
return
&
realSleeper
{}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment