Skip to content

Commit

Permalink
job-service unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregory Russell committed Dec 16, 2019
1 parent 3cb2c07 commit 5eab484
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
5 changes: 4 additions & 1 deletion job-service/job-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ func (svc *Service) NextJob() tracker.Job {
svc.advanceDate()
}
job := svc.jobTypes[svc.nextIndex]
job.Date = svc.date
svc.nextIndex++
return job
}

// JobHandler handle requests for new jobs.
// TODO - should update tracker instance.
func (svc *Service) JobHandler(resp http.ResponseWriter, req *http.Request) {
// Must be a post because it changes state.
if req.Method != http.MethodPost {
Expand Down Expand Up @@ -78,5 +80,6 @@ func NewJobService(startDate time.Time) (*Service, error) {
tracker.Job{Bucket: "archive-measurement-lab", Experiment: "ndt", Datatype: "tcpinfo"},
}

return &Service{startDate: startDate, date: startDate, dateFilter: nil, jobTypes: types}, nil
start := startDate.UTC().Truncate(24 * time.Hour)
return &Service{startDate: start, date: start, dateFilter: nil, jobTypes: types}, nil
}
80 changes: 80 additions & 0 deletions job-service/job-service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Package job provides an http handler to serve up jobs to ETL parsers.
package job_test

import (
"net/http"
"net/http/httptest"
"testing"
"time"

"bou.ke/monkey"
"github.com/go-test/deep"
job "github.com/m-lab/etl-gardener/job-service"
"github.com/m-lab/etl-gardener/tracker"
)

func now() time.Time {
return time.Date(2011, 2, 6, 1, 2, 3, 4, time.UTC)
}

func TestService_NextJob(t *testing.T) {
monkey.Patch(time.Now, now)
defer monkey.Unpatch(time.Now)

start := time.Date(2011, 2, 3, 5, 6, 7, 8, time.UTC)
svc, _ := job.NewJobService(start)
j := svc.NextJob()
w := tracker.Job{Bucket: "archive-measurement-lab", Experiment: "ndt", Datatype: "ndt5", Date: start.Truncate(24 * time.Hour)}
diff := deep.Equal(w, j)
if diff != nil {
t.Fatal(diff)
}
j = svc.NextJob()
w = tracker.Job{Bucket: "archive-measurement-lab", Experiment: "ndt", Datatype: "tcpinfo", Date: start.Truncate(24 * time.Hour)}
diff = deep.Equal(w, j)
if diff != nil {
t.Fatal(diff)
}
j = svc.NextJob()
w = tracker.Job{Bucket: "archive-measurement-lab", Experiment: "ndt", Datatype: "ndt5", Date: start.Add(24 * time.Hour).Truncate(24 * time.Hour)}
diff = deep.Equal(w, j)
if diff != nil {
t.Fatal(diff)
}
j = svc.NextJob()
w = tracker.Job{Bucket: "archive-measurement-lab", Experiment: "ndt", Datatype: "tcpinfo", Date: start.Add(24 * time.Hour).Truncate(24 * time.Hour)}
diff = deep.Equal(w, j)
if diff != nil {
t.Fatal(diff)
}
// Wrap
j = svc.NextJob()
w = tracker.Job{Bucket: "archive-measurement-lab", Experiment: "ndt", Datatype: "ndt5", Date: start.Truncate(24 * time.Hour)}
diff = deep.Equal(w, j)
if diff != nil {
t.Fatal(diff)
}
}

func TestJobHandler(t *testing.T) {
start := time.Date(2011, 2, 3, 5, 6, 7, 8, time.UTC)
svc, _ := job.NewJobService(start)
req := httptest.NewRequest("", "/job", nil)
resp := httptest.NewRecorder()
svc.JobHandler(resp, req)
if resp.Code != http.StatusMethodNotAllowed {
t.Error("Should be MethodNotAllowed", http.StatusText(resp.Code))
}

req = httptest.NewRequest("POST", "/job", nil)
resp = httptest.NewRecorder()
svc.JobHandler(resp, req)
if resp.Code != http.StatusOK {
t.Fatal(resp.Code)
}

want := `{"Bucket":"archive-measurement-lab","Experiment":"ndt","Datatype":"ndt5","Date":"2011-02-03T00:00:00Z"}`
if want != resp.Body.String() {
t.Fatal(resp.Body.String())
}
}

0 comments on commit 5eab484

Please sign in to comment.