From 8158cec82969a1262de793ee46e13f63cc2954e6 Mon Sep 17 00:00:00 2001 From: Frank Schroeder Date: Thu, 19 Oct 2017 11:57:08 +0200 Subject: [PATCH] ae: add test that we run a full before a partial sync --- agent/ae/ae_test.go | 59 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/agent/ae/ae_test.go b/agent/ae/ae_test.go index d544c135cf..8d0afb5cb1 100644 --- a/agent/ae/ae_test.go +++ b/agent/ae/ae_test.go @@ -3,8 +3,12 @@ package ae import ( "errors" "fmt" + "log" + "os" "reflect" + "sync" "testing" + "time" ) func TestAE_scaleFactor(t *testing.T) { @@ -91,3 +95,58 @@ func TestAE_Pause_ifNotPausedRun(t *testing.T) { t.Fatalf("got error %q want %q", got, want) } } + +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 +}