ae: add test that we run a full before a partial sync

This commit is contained in:
Frank Schroeder 2017-10-19 11:57:08 +02:00 committed by Frank Schröder
parent b9a8b53d52
commit 8158cec829
1 changed files with 59 additions and 0 deletions

View File

@ -3,8 +3,12 @@ package ae
import ( import (
"errors" "errors"
"fmt" "fmt"
"log"
"os"
"reflect" "reflect"
"sync"
"testing" "testing"
"time"
) )
func TestAE_scaleFactor(t *testing.T) { 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) 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
}