2022-09-14 12:04:24 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-12-29 11:36:44 +00:00
|
|
|
func TestRule_stateDisabled(t *testing.T) {
|
|
|
|
state := newRuleState(-1)
|
|
|
|
e := state.getLast()
|
|
|
|
if !e.at.IsZero() {
|
|
|
|
t.Fatalf("expected entry to be zero")
|
|
|
|
}
|
|
|
|
|
|
|
|
state.add(ruleStateEntry{at: time.Now()})
|
2023-02-08 13:34:03 +00:00
|
|
|
state.add(ruleStateEntry{at: time.Now()})
|
|
|
|
state.add(ruleStateEntry{at: time.Now()})
|
2022-12-29 11:36:44 +00:00
|
|
|
|
2023-02-08 13:34:03 +00:00
|
|
|
if len(state.getAll()) != 1 {
|
|
|
|
// state should store at least one update at any circumstances
|
2022-12-29 11:36:44 +00:00
|
|
|
t.Fatalf("expected for state to have %d entries; got %d",
|
2023-02-08 13:34:03 +00:00
|
|
|
1, len(state.getAll()),
|
2022-12-29 11:36:44 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2022-09-14 12:04:24 +00:00
|
|
|
func TestRule_state(t *testing.T) {
|
2022-12-29 11:36:44 +00:00
|
|
|
stateEntriesN := 20
|
|
|
|
state := newRuleState(stateEntriesN)
|
2022-09-14 12:04:24 +00:00
|
|
|
e := state.getLast()
|
|
|
|
if !e.at.IsZero() {
|
|
|
|
t.Fatalf("expected entry to be zero")
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
state.add(ruleStateEntry{at: now})
|
|
|
|
|
|
|
|
e = state.getLast()
|
|
|
|
if e.at != now {
|
|
|
|
t.Fatalf("expected entry at %v to be equal to %v",
|
|
|
|
e.at, now)
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
now2 := time.Now()
|
|
|
|
state.add(ruleStateEntry{at: now2})
|
|
|
|
|
|
|
|
e = state.getLast()
|
|
|
|
if e.at != now2 {
|
|
|
|
t.Fatalf("expected entry at %v to be equal to %v",
|
|
|
|
e.at, now2)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(state.getAll()) != 2 {
|
|
|
|
t.Fatalf("expected for state to have 2 entries only; got %d",
|
|
|
|
len(state.getAll()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
var last time.Time
|
2022-12-29 11:36:44 +00:00
|
|
|
for i := 0; i < stateEntriesN*2; i++ {
|
2022-09-14 12:04:24 +00:00
|
|
|
last = time.Now()
|
|
|
|
state.add(ruleStateEntry{at: last})
|
|
|
|
}
|
|
|
|
|
|
|
|
e = state.getLast()
|
|
|
|
if e.at != last {
|
|
|
|
t.Fatalf("expected entry at %v to be equal to %v",
|
|
|
|
e.at, last)
|
|
|
|
}
|
|
|
|
|
2022-12-29 11:36:44 +00:00
|
|
|
if len(state.getAll()) != stateEntriesN {
|
2022-09-14 12:04:24 +00:00
|
|
|
t.Fatalf("expected for state to have %d entries only; got %d",
|
2022-12-29 11:36:44 +00:00
|
|
|
stateEntriesN, len(state.getAll()),
|
2022-09-14 12:04:24 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestRule_stateConcurrent supposed to test concurrent
|
|
|
|
// execution of state updates.
|
|
|
|
// Should be executed with -race flag
|
2023-09-01 07:34:16 +00:00
|
|
|
func TestRule_stateConcurrent(_ *testing.T) {
|
2022-12-29 11:36:44 +00:00
|
|
|
state := newRuleState(20)
|
2022-09-14 12:04:24 +00:00
|
|
|
|
|
|
|
const workers = 50
|
|
|
|
const iterations = 100
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(workers)
|
|
|
|
for i := 0; i < workers; i++ {
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for i := 0; i < iterations; i++ {
|
|
|
|
state.add(ruleStateEntry{at: time.Now()})
|
|
|
|
state.getAll()
|
|
|
|
state.getLast()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|