Skip to content

Commit 421918a

Browse files
authored
Merge pull request #1 from Roverr/feature/always_active
2 parents 0709e41 + b277f6b commit 421918a

File tree

3 files changed

+47
-26
lines changed

3 files changed

+47
-26
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ After a configurable time, if no `Hit` were made at all, it deactivates.
2121
* `Limit` - int - _Describes how many times we have to hit before a streak becomes hot_
2222
* `HotWait` - time.Duration - _Describes the amount of time we are waiting before declaring a cool down_
2323
* `ActiveWait` - time.Duration - _Describes the amount of time we are waiting to check on a streak being active_
24+
* `AlwaysActive` - boolean - _Describes if the streak can deactivate or should stay active forever_
2425

2526
### Chainability
2627
Most commands are chainable to allow easier handling.

hotstreak.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,20 @@ import (
77

88
// Hotstreak is the main structure of the library
99
type Hotstreak struct {
10-
active bool
11-
hot bool
12-
mux *sync.Mutex
13-
counter int
14-
Limit int
15-
HotWait time.Duration
16-
ActiveWait time.Duration
17-
notifier chan uint8
10+
active bool
11+
hot bool
12+
mux *sync.Mutex
13+
counter int
14+
config Config
15+
notifier chan uint8
1816
}
1917

2018
// Config is the structure of the configuration that can be injected to the lib
2119
type Config struct {
22-
Limit int // Describes how many times we have to hit before a streak becomes hot
23-
HotWait time.Duration // Describes the amount of time we are waiting before declaring a cool down
24-
ActiveWait time.Duration // Describes the amount of time we are waiting to check on a streak being active
20+
Limit int // Describes how many times we have to hit before a streak becomes hot
21+
HotWait time.Duration // Describes the amount of time we are waiting before declaring a cool down
22+
ActiveWait time.Duration // Describes the amount of time we are waiting to check on a streak being active
23+
AlwaysActive bool // Describes if the streak can deactivate or not
2524
}
2625

2726
var (
@@ -45,14 +44,22 @@ func New(config Config) *Hotstreak {
4544
if activeWait == 0 {
4645
activeWait = time.Minute * 5
4746
}
48-
return &Hotstreak{Limit: limit, HotWait: hotwait, ActiveWait: activeWait, mux: &sync.Mutex{}, notifier: make(chan uint8)}
47+
return &Hotstreak{
48+
config: Config{
49+
Limit: limit,
50+
HotWait: hotwait,
51+
ActiveWait: activeWait,
52+
AlwaysActive: config.AlwaysActive,
53+
},
54+
mux: &sync.Mutex{},
55+
notifier: make(chan uint8)}
4956
}
5057

5158
func (hs *Hotstreak) coolDown() {
5259
if hs == nil {
5360
return
5461
}
55-
time.Sleep(hs.HotWait)
62+
time.Sleep(hs.config.HotWait)
5663
hs.mux.Lock()
5764
defer hs.mux.Unlock()
5865
hs.hot = false
@@ -66,7 +73,10 @@ func (hs *Hotstreak) dieSlowly() {
6673
select {
6774
case <-hs.notifier:
6875
return
69-
case <-time.After(hs.ActiveWait):
76+
case <-time.After(hs.config.ActiveWait):
77+
if hs.config.AlwaysActive {
78+
return
79+
}
7080
hs.mux.Lock()
7181
defer hs.mux.Unlock()
7282
if hs.hot || hs.counter > 0 {
@@ -89,7 +99,7 @@ func (hs *Hotstreak) Hit() *Hotstreak {
8999
return hs
90100
}
91101
hs.counter++
92-
if hs.counter >= hs.Limit {
102+
if hs.counter >= hs.config.Limit {
93103
hs.hot = true
94104
go hs.coolDown()
95105
}

hotstreak_test.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ func TestCreate(t *testing.T) {
1212
assert.False(t, hotstreak.IsActive())
1313
assert.False(t, hotstreak.IsHot())
1414
assert.Equal(t, hotstreak.counter, 0)
15-
assert.Equal(t, hotstreak.Limit, 20)
16-
assert.Equal(t, hotstreak.ActiveWait, time.Minute*5)
17-
assert.Equal(t, hotstreak.HotWait, time.Minute*5)
15+
assert.Equal(t, hotstreak.config.Limit, 20)
16+
assert.Equal(t, hotstreak.config.ActiveWait, time.Minute*5)
17+
assert.Equal(t, hotstreak.config.HotWait, time.Minute*5)
1818
}
1919

2020
func TestActivation(t *testing.T) {
@@ -24,14 +24,6 @@ func TestActivation(t *testing.T) {
2424
assert.True(t, hotstreak.IsActive())
2525
})
2626

27-
t.Run("Should be able to end activation after wait time", func(t *testing.T) {
28-
hotstreak := New(Config{ActiveWait: time.Millisecond * 500})
29-
hotstreak.Activate()
30-
assert.True(t, hotstreak.IsActive())
31-
<-time.After(time.Second)
32-
assert.False(t, hotstreak.IsActive())
33-
})
34-
3527
t.Run("Should not deactivate if any hit has been made in the activation time", func(t *testing.T) {
3628
hotstreak := New(Config{ActiveWait: time.Millisecond * 500})
3729
hotstreak.Activate()
@@ -45,6 +37,24 @@ func TestActivation(t *testing.T) {
4537
})
4638
}
4739

40+
func TestDeactivation(t *testing.T) {
41+
t.Run("Should be able to end activation after wait time", func(t *testing.T) {
42+
hotstreak := New(Config{ActiveWait: time.Millisecond * 500})
43+
hotstreak.Activate()
44+
assert.True(t, hotstreak.IsActive())
45+
<-time.After(time.Second)
46+
assert.False(t, hotstreak.IsActive())
47+
})
48+
49+
t.Run("Should not be able to end activation after wait time if active set to always", func(t *testing.T) {
50+
hotstreak := New(Config{ActiveWait: time.Millisecond * 500, AlwaysActive: true})
51+
hotstreak.Activate()
52+
assert.True(t, hotstreak.IsActive())
53+
<-time.After(time.Second)
54+
assert.True(t, hotstreak.IsActive())
55+
})
56+
}
57+
4858
func TestHitting(t *testing.T) {
4959
t.Run("Should be able to reach being hot", func(t *testing.T) {
5060
hotstreak := New(Config{Limit: 2})

0 commit comments

Comments
 (0)