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: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-10-19 18:22:55 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/lib"
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
|
|
|
"github.com/hashicorp/go-hclog"
|
2018-09-27 14:00:51 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
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()")
|
|
|
|
}
|
2018-09-27 14:00:51 +00:00
|
|
|
gotR := l.Resume()
|
2017-08-28 12:17:09 +00:00
|
|
|
if l.Paused() != true {
|
|
|
|
t.Fatal("syncer should STILL be Paused after FIRST call to Resume()")
|
|
|
|
}
|
2018-09-27 14:00:51 +00:00
|
|
|
assert.False(t, gotR)
|
|
|
|
gotR = l.Resume()
|
2017-08-28 12:17:09 +00:00
|
|
|
if l.Paused() != false {
|
|
|
|
t.Fatal("syncer should NOT be Paused after SECOND call to Resume()")
|
|
|
|
}
|
2018-09-27 14:00:51 +00:00
|
|
|
assert.True(t, gotR)
|
2017-08-28 12:17:09 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := recover()
|
|
|
|
if err == nil {
|
2017-10-19 14:39:16 +00:00
|
|
|
t.Fatal("unbalanced Resume() should panic")
|
2017-08-28 12:17:09 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-19 18:22:55 +00:00
|
|
|
func TestAE_staggerDependsOnClusterSize(t *testing.T) {
|
|
|
|
libRandomStagger = func(d time.Duration) time.Duration { return d }
|
|
|
|
defer func() { libRandomStagger = lib.RandomStagger }()
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
l := testSyncer(t)
|
2017-10-19 18:22:55 +00:00
|
|
|
if got, want := l.staggerFn(10*time.Millisecond), 10*time.Millisecond; got != want {
|
|
|
|
t.Fatalf("got %v want %v", got, want)
|
|
|
|
}
|
|
|
|
l.ClusterSize = func() int { return 256 }
|
|
|
|
if got, want := l.staggerFn(10*time.Millisecond), 20*time.Millisecond; got != want {
|
|
|
|
t.Fatalf("got %v want %v", 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
|
2020-01-28 23:50:41 +00:00
|
|
|
l := testSyncer(t)
|
2017-10-19 14:39:16 +00:00
|
|
|
l.State = state
|
|
|
|
l.ShutdownCh = shutdownCh
|
2017-10-19 09:57:08 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-19 14:39:16 +00:00
|
|
|
func TestAE_Run_Quit(t *testing.T) {
|
2017-10-19 18:22:55 +00:00
|
|
|
t.Run("Run panics without ClusterSize", func(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
err := recover()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Run should panic")
|
|
|
|
}
|
|
|
|
}()
|
2020-01-28 23:50:41 +00:00
|
|
|
l := testSyncer(t)
|
2017-10-19 18:22:55 +00:00
|
|
|
l.ClusterSize = nil
|
|
|
|
l.Run()
|
|
|
|
})
|
|
|
|
t.Run("runFSM quits", func(t *testing.T) {
|
|
|
|
// start timer which explodes if runFSM does not quit
|
|
|
|
tm := time.AfterFunc(time.Second, func() { panic("timeout") })
|
2017-10-19 14:39:16 +00:00
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
l := testSyncer(t)
|
2017-10-19 18:22:55 +00:00
|
|
|
l.runFSM(fullSyncState, func(fsmState) fsmState { return doneState })
|
|
|
|
// should just quit
|
|
|
|
tm.Stop()
|
|
|
|
})
|
2017-10-19 14:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAE_FSM(t *testing.T) {
|
|
|
|
t.Run("fullSyncState", func(t *testing.T) {
|
|
|
|
t.Run("Paused -> retryFullSyncState", func(t *testing.T) {
|
2020-01-28 23:50:41 +00:00
|
|
|
l := testSyncer(t)
|
2017-10-19 14:39:16 +00:00
|
|
|
l.Pause()
|
|
|
|
fs := l.nextFSMState(fullSyncState)
|
|
|
|
if got, want := fs, retryFullSyncState; got != want {
|
|
|
|
t.Fatalf("got state %v want %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("SyncFull() error -> retryFullSyncState", func(t *testing.T) {
|
2020-01-28 23:50:41 +00:00
|
|
|
l := testSyncer(t)
|
2017-10-19 14:39:16 +00:00
|
|
|
l.State = &mock{syncFull: func() error { return errors.New("boom") }}
|
|
|
|
fs := l.nextFSMState(fullSyncState)
|
|
|
|
if got, want := fs, retryFullSyncState; got != want {
|
|
|
|
t.Fatalf("got state %v want %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("SyncFull() OK -> partialSyncState", func(t *testing.T) {
|
2020-01-28 23:50:41 +00:00
|
|
|
l := testSyncer(t)
|
2017-10-19 14:39:16 +00:00
|
|
|
l.State = &mock{}
|
|
|
|
fs := l.nextFSMState(fullSyncState)
|
|
|
|
if got, want := fs, partialSyncState; got != want {
|
|
|
|
t.Fatalf("got state %v want %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("retryFullSyncState", func(t *testing.T) {
|
|
|
|
// helper for testing state transitions from retrySyncFullState
|
|
|
|
test := func(ev event, to fsmState) {
|
2020-01-28 23:50:41 +00:00
|
|
|
l := testSyncer(t)
|
2017-10-19 14:39:16 +00:00
|
|
|
l.retrySyncFullEvent = func() event { return ev }
|
|
|
|
fs := l.nextFSMState(retryFullSyncState)
|
|
|
|
if got, want := fs, to; got != want {
|
|
|
|
t.Fatalf("got state %v want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Run("shutdownEvent -> doneState", func(t *testing.T) {
|
|
|
|
test(shutdownEvent, doneState)
|
|
|
|
})
|
|
|
|
t.Run("syncFullNotifEvent -> fullSyncState", func(t *testing.T) {
|
|
|
|
test(syncFullNotifEvent, fullSyncState)
|
|
|
|
})
|
|
|
|
t.Run("syncFullTimerEvent -> fullSyncState", func(t *testing.T) {
|
|
|
|
test(syncFullTimerEvent, fullSyncState)
|
|
|
|
})
|
|
|
|
t.Run("invalid event -> panic ", func(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
err := recover()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("invalid event should panic")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
test(event("invalid"), fsmState(""))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("partialSyncState", func(t *testing.T) {
|
|
|
|
// helper for testing state transitions from partialSyncState
|
|
|
|
test := func(ev event, to fsmState) {
|
2020-01-28 23:50:41 +00:00
|
|
|
l := testSyncer(t)
|
2017-10-19 14:39:16 +00:00
|
|
|
l.syncChangesEvent = func() event { return ev }
|
|
|
|
fs := l.nextFSMState(partialSyncState)
|
|
|
|
if got, want := fs, to; got != want {
|
|
|
|
t.Fatalf("got state %v want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Run("shutdownEvent -> doneState", func(t *testing.T) {
|
|
|
|
test(shutdownEvent, doneState)
|
|
|
|
})
|
|
|
|
t.Run("syncFullNotifEvent -> fullSyncState", func(t *testing.T) {
|
|
|
|
test(syncFullNotifEvent, fullSyncState)
|
|
|
|
})
|
|
|
|
t.Run("syncFullTimerEvent -> fullSyncState", func(t *testing.T) {
|
|
|
|
test(syncFullTimerEvent, fullSyncState)
|
|
|
|
})
|
|
|
|
t.Run("syncChangesEvent+Paused -> partialSyncState", func(t *testing.T) {
|
2020-01-28 23:50:41 +00:00
|
|
|
l := testSyncer(t)
|
2017-10-19 14:39:16 +00:00
|
|
|
l.Pause()
|
|
|
|
l.syncChangesEvent = func() event { return syncChangesNotifEvent }
|
|
|
|
fs := l.nextFSMState(partialSyncState)
|
|
|
|
if got, want := fs, partialSyncState; got != want {
|
|
|
|
t.Fatalf("got state %v want %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("syncChangesEvent+SyncChanges() error -> partialSyncState", func(t *testing.T) {
|
2020-01-28 23:50:41 +00:00
|
|
|
l := testSyncer(t)
|
2017-10-19 14:39:16 +00:00
|
|
|
l.State = &mock{syncChanges: func() error { return errors.New("boom") }}
|
|
|
|
l.syncChangesEvent = func() event { return syncChangesNotifEvent }
|
|
|
|
fs := l.nextFSMState(partialSyncState)
|
|
|
|
if got, want := fs, partialSyncState; got != want {
|
|
|
|
t.Fatalf("got state %v want %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("syncChangesEvent+SyncChanges() OK -> partialSyncState", func(t *testing.T) {
|
2020-01-28 23:50:41 +00:00
|
|
|
l := testSyncer(t)
|
2017-10-19 14:39:16 +00:00
|
|
|
l.State = &mock{}
|
|
|
|
l.syncChangesEvent = func() event { return syncChangesNotifEvent }
|
|
|
|
fs := l.nextFSMState(partialSyncState)
|
|
|
|
if got, want := fs, partialSyncState; got != want {
|
|
|
|
t.Fatalf("got state %v want %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
2017-10-19 18:22:55 +00:00
|
|
|
t.Run("invalid event -> panic ", func(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
err := recover()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("invalid event should panic")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
test(event("invalid"), fsmState(""))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("invalid state -> panic ", func(t *testing.T) {
|
|
|
|
defer func() {
|
|
|
|
err := recover()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("invalid state should panic")
|
|
|
|
}
|
|
|
|
}()
|
2020-01-28 23:50:41 +00:00
|
|
|
l := testSyncer(t)
|
2017-10-19 18:22:55 +00:00
|
|
|
l.nextFSMState(fsmState("invalid"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAE_RetrySyncFullEvent(t *testing.T) {
|
|
|
|
t.Run("trigger shutdownEvent", func(t *testing.T) {
|
2020-01-28 23:50:41 +00:00
|
|
|
l := testSyncer(t)
|
2017-10-19 18:22:55 +00:00
|
|
|
l.ShutdownCh = make(chan struct{})
|
|
|
|
evch := make(chan event)
|
|
|
|
go func() { evch <- l.retrySyncFullEvent() }()
|
|
|
|
close(l.ShutdownCh)
|
|
|
|
if got, want := <-evch, shutdownEvent; got != want {
|
|
|
|
t.Fatalf("got event %q want %q", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("trigger shutdownEvent during FullNotif", func(t *testing.T) {
|
2020-01-28 23:50:41 +00:00
|
|
|
l := testSyncer(t)
|
2017-10-19 18:22:55 +00:00
|
|
|
l.ShutdownCh = make(chan struct{})
|
|
|
|
evch := make(chan event)
|
|
|
|
go func() { evch <- l.retrySyncFullEvent() }()
|
|
|
|
l.SyncFull.Trigger()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
close(l.ShutdownCh)
|
|
|
|
if got, want := <-evch, shutdownEvent; got != want {
|
|
|
|
t.Fatalf("got event %q want %q", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("trigger syncFullNotifEvent", func(t *testing.T) {
|
2020-01-28 23:50:41 +00:00
|
|
|
l := testSyncer(t)
|
2017-10-19 18:22:55 +00:00
|
|
|
l.serverUpInterval = 10 * time.Millisecond
|
|
|
|
evch := make(chan event)
|
|
|
|
go func() { evch <- l.retrySyncFullEvent() }()
|
|
|
|
l.SyncFull.Trigger()
|
|
|
|
if got, want := <-evch, syncFullNotifEvent; got != want {
|
|
|
|
t.Fatalf("got event %q want %q", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("trigger syncFullTimerEvent", func(t *testing.T) {
|
2020-01-28 23:50:41 +00:00
|
|
|
l := testSyncer(t)
|
2017-10-19 18:22:55 +00:00
|
|
|
l.retryFailInterval = 10 * time.Millisecond
|
|
|
|
evch := make(chan event)
|
|
|
|
go func() { evch <- l.retrySyncFullEvent() }()
|
|
|
|
if got, want := <-evch, syncFullTimerEvent; got != want {
|
|
|
|
t.Fatalf("got event %q want %q", got, want)
|
|
|
|
}
|
2017-10-19 14:39:16 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAE_SyncChangesEvent(t *testing.T) {
|
|
|
|
t.Run("trigger shutdownEvent", func(t *testing.T) {
|
2020-01-28 23:50:41 +00:00
|
|
|
l := testSyncer(t)
|
2017-10-19 14:39:16 +00:00
|
|
|
l.ShutdownCh = make(chan struct{})
|
|
|
|
evch := make(chan event)
|
|
|
|
go func() { evch <- l.syncChangesEvent() }()
|
|
|
|
close(l.ShutdownCh)
|
|
|
|
if got, want := <-evch, shutdownEvent; got != want {
|
|
|
|
t.Fatalf("got event %q want %q", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("trigger shutdownEvent during FullNotif", func(t *testing.T) {
|
2020-01-28 23:50:41 +00:00
|
|
|
l := testSyncer(t)
|
2017-10-19 14:39:16 +00:00
|
|
|
l.ShutdownCh = make(chan struct{})
|
|
|
|
evch := make(chan event)
|
|
|
|
go func() { evch <- l.syncChangesEvent() }()
|
|
|
|
l.SyncFull.Trigger()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
close(l.ShutdownCh)
|
|
|
|
if got, want := <-evch, shutdownEvent; got != want {
|
|
|
|
t.Fatalf("got event %q want %q", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("trigger syncFullNotifEvent", func(t *testing.T) {
|
2020-01-28 23:50:41 +00:00
|
|
|
l := testSyncer(t)
|
2017-10-19 14:39:16 +00:00
|
|
|
l.serverUpInterval = 10 * time.Millisecond
|
|
|
|
evch := make(chan event)
|
|
|
|
go func() { evch <- l.syncChangesEvent() }()
|
|
|
|
l.SyncFull.Trigger()
|
|
|
|
if got, want := <-evch, syncFullNotifEvent; got != want {
|
|
|
|
t.Fatalf("got event %q want %q", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("trigger syncFullTimerEvent", func(t *testing.T) {
|
2020-01-28 23:50:41 +00:00
|
|
|
l := testSyncer(t)
|
2017-10-19 14:39:16 +00:00
|
|
|
l.Interval = 10 * time.Millisecond
|
|
|
|
evch := make(chan event)
|
|
|
|
go func() { evch <- l.syncChangesEvent() }()
|
|
|
|
if got, want := <-evch, syncFullTimerEvent; got != want {
|
|
|
|
t.Fatalf("got event %q want %q", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("trigger syncChangesNotifEvent", func(t *testing.T) {
|
2020-01-28 23:50:41 +00:00
|
|
|
l := testSyncer(t)
|
2017-10-19 14:39:16 +00:00
|
|
|
evch := make(chan event)
|
|
|
|
go func() { evch <- l.syncChangesEvent() }()
|
|
|
|
l.SyncChanges.Trigger()
|
|
|
|
if got, want := <-evch, syncChangesNotifEvent; got != want {
|
|
|
|
t.Fatalf("got event %q want %q", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-10-19 09:57:08 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
func testSyncer(t *testing.T) *StateSyncer {
|
|
|
|
logger := hclog.New(&hclog.LoggerOptions{
|
|
|
|
Level: 0,
|
|
|
|
Output: testutil.TestWriter(t),
|
|
|
|
})
|
|
|
|
|
2017-10-19 14:39:16 +00:00
|
|
|
l := NewStateSyncer(nil, time.Second, nil, logger)
|
2017-10-19 09:57:08 +00:00
|
|
|
l.stagger = func(d time.Duration) time.Duration { return d }
|
|
|
|
l.ClusterSize = func() int { return 1 }
|
2020-02-06 14:30:58 +00:00
|
|
|
l.resetNextFullSyncCh()
|
2017-10-19 09:57:08 +00:00
|
|
|
return l
|
|
|
|
}
|