From a1f33d90f21ff678f8b85df691213e1f434825d4 Mon Sep 17 00:00:00 2001 From: Annika Hannig Date: Wed, 9 Feb 2022 11:56:03 +0100 Subject: [PATCH] added tests --- pkg/store/sources_store.go | 2 + pkg/store/sources_store_test.go | 99 ++++++++++++++++++++++++++++++++- 2 files changed, 98 insertions(+), 3 deletions(-) diff --git a/pkg/store/sources_store.go b/pkg/store/sources_store.go index 83f4740..bef8572 100644 --- a/pkg/store/sources_store.go +++ b/pkg/store/sources_store.go @@ -249,6 +249,8 @@ func (s *SourcesStore) GetSourceIDsForRefresh() []string { } } + log.Println(locked) + // Sort by refresh start time ascending sort.Sort(sources) diff --git a/pkg/store/sources_store_test.go b/pkg/store/sources_store_test.go index 86434cc..f7c62f0 100644 --- a/pkg/store/sources_store_test.go +++ b/pkg/store/sources_store_test.go @@ -1,11 +1,104 @@ package store -import "testing" +import ( + "testing" + "time" +) -func TestAddSource(t *testing.T) { +func TestGetSourceIDsForRefreshSequential(t *testing.T) { + + s := &SourcesStore{ + refreshParallelism: 1, + status: map[string]*Status{ + "src1": { + SourceID: "src1", + }, + "src2": { + SourceID: "src2", + }, + }, + } + + ids := s.GetSourceIDsForRefresh() + if len(ids) != 1 { + t.Error("expected 1 id") + } + if err := s.LockSource(ids[0]); err != nil { + t.Error(err) + } + lastID := ids[0] + + ids = s.GetSourceIDsForRefresh() + if len(ids) != 0 { + t.Error("all concurrent refresh slots should be taken") + } + + if err := s.RefreshSuccess(lastID); err != nil { + t.Error(err) + } + + ids = s.GetSourceIDsForRefresh() + if len(ids) != 1 { + t.Error("expected 1 id") + } + + if ids[0] == lastID { + t.Error("the next source should have been returned") + } } -func TestGetStatus(t *testing.T) { +func TestGetSourceIDsForRefreshParallel(t *testing.T) { + s := &SourcesStore{ + refreshParallelism: 2, + status: map[string]*Status{ + "src1": { + SourceID: "src1", + }, + "src2": { + SourceID: "src2", + }, + "src3": { + SourceID: "src3", + lastRefreshStart: time.Now().UTC(), + }, + }, + } + + ids := s.GetSourceIDsForRefresh() + if len(ids) != 2 { + t.Error("expected 2 ids") + } + for _, id := range ids { + if err := s.LockSource(id); err != nil { + t.Error(err) + } + + if id == "src3" { + t.Error("unexpected src3") + } + } + + nextIds := s.GetSourceIDsForRefresh() + if len(nextIds) != 0 { + t.Error("all concurrent refresh slots should be taken") + } + + for _, id := range ids { + if err := s.RefreshSuccess(id); err != nil { + t.Error(err) + } + } + + ids = s.GetSourceIDsForRefresh() + t.Log(ids) + t.Log(s.status["src1"]) + if len(ids) != 2 { + t.Error("expected 2 id") + } + + if ids[0] != "src3" { + t.Error("expected src3 to be least refreshed") + } }