Skip to content

Commit

Permalink
Runs the addition of digital.
Browse files Browse the repository at this point in the history
  • Loading branch information
brotherlogic committed Oct 17, 2020
1 parent c06b345 commit 49ebcab
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 28 deletions.
48 changes: 46 additions & 2 deletions recordadderapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ import (
//InitTestServer gets a test version of the server
func InitTestServer() *Server {
s := Init()
s.rc = &testCollection{}
s.budget = &testBudget{}
s.SkipLog = true
s.SkipIssue = true
s.testing = true
s.GoServer.KSclient = *keystoreclient.GetTestClient(".test")
s.GoServer.KSclient = *keystoreclient.GetTestClient("./testing")
s.GoServer.KSclient.Save(context.Background(), QUEUE, &pb.Queue{})
s.fanout = []string{"madeup1"}
s.testing = true

return s
}

Expand All @@ -33,6 +37,46 @@ func TestAddRequest(t *testing.T) {
}
}

func TestAddDigitalRequest(t *testing.T) {
s := InitTestServer()

_, err := s.AddRecord(context.Background(), &pb.AddRecordRequest{Id: 12, Arrived: true})
if err != nil {
t.Fatalf("Add Record failed: %v", err)
}
_, err = s.AddRecord(context.Background(), &pb.AddRecordRequest{Id: 13, Folder: 242018, Arrived: true})
if err != nil {
t.Fatalf("Add Record failed: %v", err)
}

list, err := s.ListQueue(context.Background(), &pb.ListQueueRequest{})
if err != nil {
t.Fatalf("Error listing records: %v", err)
}

if len(list.GetRequests()) != 2 {
t.Fatalf("Two requests were not added: %v", list)
}

err = s.processQueue(context.Background())
if err != nil {
t.Errorf("Bad queue process: %v", err)
}
err = s.processQueue(context.Background())
if err != nil {
t.Errorf("Bad queue process: %v", err)
}

list, err = s.ListQueue(context.Background(), &pb.ListQueueRequest{})
if err != nil {
t.Fatalf("Error listing records: %v", err)
}

if len(list.GetRequests()) != 0 {
t.Fatalf("Two requests were not added: %v", list)
}
}

func TestAddRequestFail(t *testing.T) {
s := InitTestServer()
s.GoServer.KSclient.Fail = true
Expand Down
44 changes: 40 additions & 4 deletions recordadderutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ func (s *Server) processQueue(ctx context.Context) error {

available := budget.GetBudget() - budget.GetSpends()
s.Log(fmt.Sprintf("Found %v entries in the queue with %v in the budget (%v)", len(queue.Requests), available, time.Now().Sub(time.Unix(queue.LastAdditionDate, 0)) >= time.Hour*24))
time.Sleep(time.Second * 2)

if len(queue.Requests) > 0 && time.Now().Sub(time.Unix(queue.LastAdditionDate, 0)) >= time.Hour*24 {
for i, req := range queue.GetRequests() {
if req.GetId() <= 0 || req.GetResetFolder() > 0 {
queue.Requests = append(queue.Requests[:i], queue.Requests[i+1:]...)
s.KSclient.Save(ctx, QUEUE, queue)
return fmt.Errorf("Bad entry in the queue")
}
if req.GetCost() < available && req.GetArrived() {
if !isDigital(req) && req.GetCost() < available && req.GetArrived() {
err = s.rc.addRecord(ctx, queue.Requests[i])
s.Log(fmt.Sprintf("Adding %v -> %v", queue.Requests[i], err))
if err != nil {
Expand All @@ -54,7 +52,45 @@ func (s *Server) processQueue(ctx context.Context) error {
}
}

s.Log(fmt.Sprintf("Still %v to go!", time.Now().Sub(time.Unix(queue.LastAdditionDate, 0))))
err = s.runDigital(ctx, queue, available)
if err != nil {
return err
}

s.Log(fmt.Sprintf("Still %v to go (with digital %v) !", time.Now().Sub(time.Unix(queue.LastAdditionDate, 0)), err))

return nil
}

func isDigital(req *pb.AddRecordRequest) bool {
//Digital is CD, Bandcamp or Computer
return req.GetFolder() == 242018 ||
req.GetFolder() == 1782105 ||
req.GetFolder() == 2274270
}

func (s *Server) runDigital(ctx context.Context, queue *pb.Queue, available int32) error {
if len(queue.Requests) > 0 && time.Now().Sub(time.Unix(queue.GetLastDigitalAddition(), 0)) >= time.Hour*24 {
for i, req := range queue.GetRequests() {
if isDigital(req) && req.GetCost() < available && req.GetArrived() {
err := s.rc.addRecord(ctx, queue.Requests[i])
s.Log(fmt.Sprintf("DIGITAL Adding %v -> %v", queue.Requests[i], err))
if err != nil {
return fmt.Errorf("Error adding digital record: %v", err)
}

queue.LastDigitalAddition = time.Now().Unix()
queue.Requests = append(queue.Requests[:i], queue.Requests[i+1:]...)

// We need to refresh the context for the save since the fanout may have run out the clock
ctxinner, cancelinner := utils.ManualContext("rasave", "rasave", time.Minute, true)
err = s.KSclient.Save(ctxinner, QUEUE, queue)
cancelinner()

return err
}
time.Sleep(time.Second * 5)
}
}
return nil
}
43 changes: 21 additions & 22 deletions recordadderutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"testing"

keystoreclient "github.com/brotherlogic/keystore/client"
rbpb "github.com/brotherlogic/recordbudget/proto"
"golang.org/x/net/context"

Expand All @@ -13,19 +12,6 @@ import (
pbrc "github.com/brotherlogic/recordcollection/proto"
)

func InitTest() *Server {
s := Init()
s.rc = &testCollection{}
s.budget = &testBudget{}
s.SkipLog = true
s.SkipIssue = true
s.GoServer.KSclient = *keystoreclient.GetTestClient("./testing")
s.GoServer.KSclient.Save(context.Background(), QUEUE, &pb.Queue{})
s.fanout = []string{"madeup1"}
s.testing = true
return s
}

type testCollection struct {
addedRecord *pbrc.Record
fail bool
Expand All @@ -51,7 +37,7 @@ func (p *testCollection) addRecord(ctx context.Context, r *pb.AddRecordRequest)
}

func TestBasicRunThrough(t *testing.T) {
s := InitTest()
s := InitTestServer()
tc := &testCollection{}
s.rc = tc

Expand All @@ -68,7 +54,7 @@ func TestBasicRunThrough(t *testing.T) {
}

func TestBasicRunThroughWithFanoutFail(t *testing.T) {
s := InitTest()
s := InitTestServer()
tc := &testCollection{}
s.rc = tc
s.testingFail = true
Expand All @@ -86,7 +72,7 @@ func TestBasicRunThroughWithFanoutFail(t *testing.T) {
}

func TestBasicRunThroughWithBudgetFail(t *testing.T) {
s := InitTest()
s := InitTestServer()
tc := &testCollection{}
tb := &testBudget{fail: true}
s.budget = tb
Expand All @@ -101,7 +87,7 @@ func TestBasicRunThroughWithBudgetFail(t *testing.T) {
}

func TestEmptyRunThrough(t *testing.T) {
s := InitTest()
s := InitTestServer()

err := s.processQueue(context.Background())
if err != nil {
Expand All @@ -110,7 +96,7 @@ func TestEmptyRunThrough(t *testing.T) {
}

func TestBadReadRunThrough(t *testing.T) {
s := InitTest()
s := InitTestServer()
s.GoServer.KSclient.Fail = true

err := s.processQueue(context.Background())
Expand All @@ -121,7 +107,7 @@ func TestBadReadRunThrough(t *testing.T) {
}

func TestRunThroughWithAddFail(t *testing.T) {
s := InitTest()
s := InitTestServer()
tc := &testCollection{fail: true}
s.rc = tc

Expand All @@ -133,8 +119,21 @@ func TestRunThroughWithAddFail(t *testing.T) {
}
}

func TestRunThroughWithDigitalAddFail(t *testing.T) {
s := InitTestServer()
tc := &testCollection{fail: true}
s.rc = tc

s.AddRecord(context.Background(), &pb.AddRecordRequest{Id: 12, Folder: 242018, Cost: 12, Arrived: true})

err := s.processQueue(context.Background())
if err == nil {
t.Errorf("No error processing the queue with failing add")
}
}

func TestWithBadAdd(t *testing.T) {
s := InitTest()
s := InitTestServer()

s.AddRecord(context.Background(), &pb.AddRecordRequest{Id: -1, Folder: 12, Cost: 12, Arrived: true})

Expand All @@ -145,7 +144,7 @@ func TestWithBadAdd(t *testing.T) {
}

func TestBasicRunThroughWithNoAction(t *testing.T) {
s := InitTest()
s := InitTestServer()
tc := &testCollection{}
s.rc = tc

Expand Down

0 comments on commit 49ebcab

Please sign in to comment.