Skip to content

Commit

Permalink
Merge pull request #15 from tarent/error-500-on-empty
Browse files Browse the repository at this point in the history
Error 500 on empty fetch job list
  • Loading branch information
b00lduck committed Jul 11, 2016
2 parents 69845bb + e38d6f6 commit b1079ff
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
7 changes: 7 additions & 0 deletions composition/composition_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ func (agg *CompositionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

fetcher := agg.contentFetcherFactory(r)

if fetcher.Empty() {
w.WriteHeader(500)
w.Write([]byte("Internal server error"))
logging.Application(r.Header).Error("No fetchers available for composition, throwing error 500")
return
}

// fetch all contents
results := fetcher.WaitForResults()

Expand Down
23 changes: 23 additions & 0 deletions composition/composition_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,24 @@ func Test_CompositionHandler_ErrorInFetching(t *testing.T) {
a.Equal(502, resp.Code)
}

func Test_CompositionHandler_ErrorEmptyFetchersList(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
a := assert.New(t)

contentFetcherFactory := func(r *http.Request) FetchResultSupplier {
return MockFetchResultSupplier{}
}
aggregator := NewCompositionHandler(ContentFetcherFactory(contentFetcherFactory))

resp := httptest.NewRecorder()
r, _ := http.NewRequest("GET", "http://example.com", nil)
aggregator.ServeHTTP(resp, r)

a.Equal("Internal server error", string(resp.Body.Bytes()))
a.Equal(500, resp.Code)
}

func Test_metadataForRequest(t *testing.T) {
a := assert.New(t)
r, _ := http.NewRequest("GET", "https://example.com/nothing?foo=bar", nil)
Expand Down Expand Up @@ -265,3 +283,8 @@ func (m MockFetchResultSupplier) WaitForResults() []*FetchResult {
func (m MockFetchResultSupplier) MetaJSON() map[string]interface{} {
return nil
}

func (m MockFetchResultSupplier) Empty() bool {
return len([]*FetchResult(m)) == 0
}

6 changes: 6 additions & 0 deletions composition/content_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ func (fetcher *ContentFetcher) AddFetchJob(d *FetchDefinition) {
}()
}

func (fetcher *ContentFetcher) Empty() bool {
fetcher.r.mutex.Lock()
defer fetcher.r.mutex.Unlock()
return len(fetcher.r.results) == 0
}

// isAlreadySheduled checks, if there is already a job for a FetchDefinition, or it is already fetched.
// The method has to be called in a locked mutex block.
func (fetcher *ContentFetcher) isAlreadySheduled(fetchDefinitionHash string) bool {
Expand Down
15 changes: 15 additions & 0 deletions composition/content_fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ func Test_ContentFetcher_FetchingWithDependency(t *testing.T) {
meta := fetcher.MetaJSON()
a.Equal("bar", meta["foo"])
a.Equal("bla", meta["bli"])

a.False(fetcher.Empty())
}

func Test_ContentFetcher_Empty(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
a := assert.New(t)

loader := NewMockContentLoader(ctrl)

fetcher := NewContentFetcher(nil)
fetcher.Loader = loader

a.True(fetcher.Empty())
}

func getFetchDefinitionMock(ctrl *gomock.Controller, loaderMock *MockContentLoader, url string, requiredContent []*FetchDefinition, loaderBlocking time.Duration, metaJSON map[string]interface{}) *FetchDefinition {
Expand Down
3 changes: 3 additions & 0 deletions composition/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type FetchResultSupplier interface {

// MetaJSON returns the composed meta JSON object
MetaJSON() map[string]interface{}

// True, if no fetch jobs were added
Empty() bool
}

type CacheStrategy interface {
Expand Down

0 comments on commit b1079ff

Please sign in to comment.