2017-08-28 12:17:09 +00:00
|
|
|
package ae
|
|
|
|
|
|
|
|
import (
|
2017-10-19 09:12:29 +00:00
|
|
|
"errors"
|
2017-08-28 12:17:10 +00:00
|
|
|
"fmt"
|
2017-10-19 09:57:08 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2017-10-19 09:12:29 +00:00
|
|
|
"reflect"
|
2017-10-19 09:57:08 +00:00
|
|
|
"sync"
|
2017-08-28 12:17:09 +00:00
|
|
|
"testing"
|
2017-10-19 09:57:08 +00:00
|
|
|
"time"
|
2017-08-28 12:17:09 +00:00
|
|
|
)
|
|
|
|
|
2017-08-28 12:17:10 +00:00
|
|
|
func TestAE_scaleFactor(t *testing.T) {
|
2017-08-28 12:17:09 +00:00
|
|
|
t.Parallel()
|
2017-08-28 12:17:10 +00:00
|
|
|
tests := []struct {
|
|
|
|
nodes int
|
|
|
|
scale int
|
|
|
|
}{
|
|
|
|
{100, 1},
|
|
|
|
{200, 2},
|
|
|
|
{1000, 4},
|
|
|
|
{10000, 8},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(fmt.Sprintf("%d nodes", tt.nodes), func(t *testing.T) {
|
|
|
|
if got, want := scaleFactor(tt.nodes), tt.scale; got != want {
|
|
|
|
t.Fatalf("got scale factor %d want %d", got, want)
|
|
|
|
}
|
|
|
|
})
|
2017-08-28 12:17:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-19 09:17:24 +00:00
|
|
|
func TestAE_Pause_nestedPauseResume(t *testing.T) {
|
2017-08-28 12:17:09 +00:00
|
|
|
t.Parallel()
|
2017-10-19 09:20:24 +00:00
|
|
|
l := NewStateSyncer(nil, 0, nil, nil)
|
2017-08-28 12:17:09 +00:00
|
|
|
if l.Paused() != false {
|
|
|
|
t.Fatal("syncer should be unPaused after init")
|
|
|
|
}
|
|
|
|
l.Pause()
|
|
|
|
if l.Paused() != true {
|
|
|
|
t.Fatal("syncer should be Paused after first call to Pause()")
|
|
|
|
}
|
|
|
|
l.Pause()
|
|
|
|
if l.Paused() != true {
|
|
|
|
t.Fatal("syncer should STILL be Paused after second call to Pause()")
|
|
|
|
}
|
|
|
|
l.Resume()
|
|
|
|
if l.Paused() != true {
|
|
|
|
t.Fatal("syncer should STILL be Paused after FIRST call to Resume()")
|
|
|
|
}
|
|
|
|
l.Resume()
|
|
|
|
if l.Paused() != false {
|
|
|
|
t.Fatal("syncer should NOT be Paused after SECOND call to Resume()")
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := recover()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("unbalanced Resume() should cause a panic()")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
l.Resume()
|
|
|
|
}
|
2017-10-19 09:12:29 +00:00
|
|
|
|
2017-10-19 09:17:24 +00:00
|
|
|
func TestAE_Pause_ResumeTriggersSyncChanges(t *testing.T) {
|
2017-10-19 09:20:24 +00:00
|
|
|
l := NewStateSyncer(nil, 0, nil, nil)
|
2017-10-19 09:17:24 +00:00
|
|
|
l.Pause()
|
|
|
|
l.Resume()
|
|
|
|
select {
|
|
|
|
case <-l.SyncChanges.Notif():
|
|
|
|
// expected
|
|
|
|
case <-l.SyncFull.Notif():
|
|
|
|
t.Fatal("resume triggered SyncFull instead of SyncChanges")
|
|
|
|
default:
|
|
|
|
t.Fatal("resume did not trigger SyncFull")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAE_Pause_ifNotPausedRun(t *testing.T) {
|
2017-10-19 09:20:24 +00:00
|
|
|
l := NewStateSyncer(nil, 0, nil, nil)
|
2017-10-19 09:12:29 +00:00
|
|
|
|
|
|
|
errCalled := errors.New("f called")
|
|
|
|
f := func() error { return errCalled }
|
|
|
|
|
|
|
|
l.Pause()
|
|
|
|
err := l.ifNotPausedRun(f)
|
|
|
|
if got, want := err, errPaused; !reflect.DeepEqual(got, want) {
|
|
|
|
t.Fatalf("got error %q want %q", got, want)
|
|
|
|
}
|
|
|
|
l.Resume()
|
|
|
|
|
|
|
|
err = l.ifNotPausedRun(f)
|
|
|
|
if got, want := err, errCalled; got != want {
|
|
|
|
t.Fatalf("got error %q want %q", got, want)
|
|
|
|
}
|
|
|
|
}
|
2017-10-19 09:57:08 +00:00
|
|
|
|
|
|
|
func TestAE_Run_SyncFullBeforeChanges(t *testing.T) {
|
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
state := &mock{
|
|
|
|
syncChanges: func() error {
|
|
|
|
close(shutdownCh)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// indicate that we have partial changes before starting Run
|
|
|
|
l := testSyncer(state, shutdownCh)
|
|
|
|
l.SyncChanges.Trigger()
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
l.Run()
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if got, want := state.seq, []string{"full", "changes"}; !reflect.DeepEqual(got, want) {
|
|
|
|
t.Fatalf("got call sequence %v want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type mock struct {
|
|
|
|
seq []string
|
|
|
|
syncFull, syncChanges func() error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mock) SyncFull() error {
|
|
|
|
m.seq = append(m.seq, "full")
|
|
|
|
if m.syncFull != nil {
|
|
|
|
return m.syncFull()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mock) SyncChanges() error {
|
|
|
|
m.seq = append(m.seq, "changes")
|
|
|
|
if m.syncChanges != nil {
|
|
|
|
return m.syncChanges()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSyncer(state State, shutdownCh chan struct{}) *StateSyncer {
|
|
|
|
logger := log.New(os.Stderr, "", 0)
|
|
|
|
l := NewStateSyncer(state, 0, shutdownCh, logger)
|
|
|
|
l.stagger = func(d time.Duration) time.Duration { return d }
|
|
|
|
l.ClusterSize = func() int { return 1 }
|
|
|
|
return l
|
|
|
|
}
|