Unit test scheduler

This commit is contained in:
Aloïs Micard 2020-09-26 13:42:38 +02:00
parent 73f52703f1
commit 4633cc7695
No known key found for this signature in database
GPG Key ID: 1A0EB82F071F5EFE
4 changed files with 79 additions and 8 deletions

View File

@ -49,6 +49,6 @@ jobs:
go generate ./...
go test -race --coverprofile=coverage.coverprofile --covermode=atomic -v ./...
#- name: Update go report card
# if: success() && matrix.os == 'ubuntu-latest'
# uses: creekorful/goreportcard-action@v1.0
- name: Update go report card
if: success() && matrix.os == 'ubuntu-latest'
uses: creekorful/goreportcard-action@v1.0

View File

@ -70,7 +70,6 @@ func TestHandleMessage(t *testing.T) {
subscriberMock := messaging_mock.NewMockSubscriber(mockCtrl)
msg := nats.Msg{}
subscriberMock.EXPECT().
ReadMsg(&msg, &messaging.NewResourceMsg{}).
SetArg(1, messaging.NewResourceMsg{URL: "https://example.onion", Body: "Hello, world<title>Title</title><a href=\"https://google.com\"></a>"}).

View File

@ -94,25 +94,25 @@ func handleMessage(apiClient api.Client, refreshDelay time.Duration) messaging.M
// Make sure URL is valid .onion
if !strings.Contains(u.Host, ".onion") {
log.Debug().Stringer("url", u).Msg("URL is not a valid hidden service")
return err
return fmt.Errorf("%s is not a valid .onion", u.Host)
}
// If we want to allow re-schedule of existing crawled resources we need to retrieve only resources
// that are newer than now-refreshDelay.
// that are newer than `now - refreshDelay`.
endDate := time.Time{}
if refreshDelay != -1 {
endDate = time.Now().Add(-refreshDelay)
}
b64URI := base64.URLEncoding.EncodeToString([]byte(u.String()))
urls, _, err := apiClient.SearchResources(b64URI, "", time.Time{}, endDate, 1, 1)
_, count, err := apiClient.SearchResources(b64URI, "", time.Time{}, endDate, 1, 1)
if err != nil {
log.Err(err).Msg("Error while searching URL")
return err
}
// No matches: schedule!
if len(urls) == 0 {
if count == 0 {
log.Debug().Stringer("url", u).Msg("URL should be scheduled")
if err := sub.PublishMsg(&messaging.URLTodoMsg{URL: urlMsg.URL}); err != nil {
return fmt.Errorf("error while publishing URL: %s", err)

View File

@ -1,6 +1,12 @@
package scheduler
import (
"github.com/creekorful/trandoshan/api"
"github.com/creekorful/trandoshan/api_mock"
"github.com/creekorful/trandoshan/internal/messaging"
"github.com/creekorful/trandoshan/internal/messaging_mock"
"github.com/golang/mock/gomock"
"github.com/nats-io/nats.go"
"testing"
"time"
)
@ -22,3 +28,69 @@ func TestParseRefreshDelay(t *testing.T) {
t.Fail()
}
}
func TestHandleMessageNotOnion(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
apiClientMock := api_mock.NewMockClient(mockCtrl)
subscriberMock := messaging_mock.NewMockSubscriber(mockCtrl)
msg := nats.Msg{}
subscriberMock.EXPECT().
ReadMsg(&msg, &messaging.URLFoundMsg{}).
SetArg(1, messaging.URLFoundMsg{URL: "https://example.org"}).
Return(nil)
if err := handleMessage(apiClientMock, -1)(subscriberMock, &msg); err == nil {
t.FailNow()
}
}
func TestHandleMessageNoSchedule(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
apiClientMock := api_mock.NewMockClient(mockCtrl)
subscriberMock := messaging_mock.NewMockSubscriber(mockCtrl)
msg := nats.Msg{}
subscriberMock.EXPECT().
ReadMsg(&msg, &messaging.URLFoundMsg{}).
SetArg(1, messaging.URLFoundMsg{URL: "https://example.onion"}).
Return(nil)
apiClientMock.EXPECT().
SearchResources("aHR0cHM6Ly9leGFtcGxlLm9uaW9u", "", time.Time{}, time.Time{}, 1, 1).
Return([]api.ResourceDto{}, int64(1), nil)
if err := handleMessage(apiClientMock, -1)(subscriberMock, &msg); err != nil {
t.FailNow()
}
}
func TestHandleMessage(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
apiClientMock := api_mock.NewMockClient(mockCtrl)
subscriberMock := messaging_mock.NewMockSubscriber(mockCtrl)
msg := nats.Msg{}
subscriberMock.EXPECT().
ReadMsg(&msg, &messaging.URLFoundMsg{}).
SetArg(1, messaging.URLFoundMsg{URL: "https://example.onion"}).
Return(nil)
apiClientMock.EXPECT().
SearchResources("aHR0cHM6Ly9leGFtcGxlLm9uaW9u", "", time.Time{}, time.Time{}, 1, 1).
Return([]api.ResourceDto{}, int64(0), nil)
subscriberMock.EXPECT().
PublishMsg(&messaging.URLTodoMsg{URL: "https://example.onion"}).
Return(nil)
if err := handleMessage(apiClientMock, -1)(subscriberMock, &msg); err != nil {
t.FailNow()
}
}